001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 package org.activemq.openwire.tool; 019 020 import org.codehaus.gram.GramSupport; 021 import org.codehaus.jam.JClass; 022 import org.codehaus.jam.JMethod; 023 import org.codehaus.jam.JProperty; 024 025 import java.util.HashMap; 026 import java.util.Map; 027 028 /** 029 * @version $Revision: 1.1 $ 030 */ 031 public abstract class OpenWireScript extends GramSupport { 032 033 private Map enums = new HashMap(); 034 private int enumCounter; 035 private Map testCaseValues = new HashMap(); 036 private int counter; 037 038 039 /** 040 * Returns true if the given type is a valid message type 041 */ 042 public boolean isMessageType(JClass it) { 043 // lets filter out silly non-qualified classes 044 if (it.getSimpleName().equals(it.getQualifiedName())) { 045 return false; 046 } 047 String name = it.getSimpleName(); 048 049 return it.isInterface() == false && it.isAbstract() == false && !name.startsWith("Abstract") && 050 name.indexOf("Queue") < 0 && name.indexOf("Topic") < 0 051 && !name.startsWith("ActiveMQDestination") 052 && !name.startsWith("ActiveMQXid") 053 && !name.startsWith("MessageAcknowledge") 054 && !name.startsWith("Packet") 055 && !name.startsWith("ReceiptHolder") 056 && !name.startsWith("TransactionType"); 057 } 058 059 public boolean isBodyType(JClass type, JProperty property) { 060 String name = property.getType().getQualifiedName(); 061 String typeName = type.getQualifiedName(); 062 if (name.equals("java.lang.Object") || name.equals("java.io.Serializable")) { 063 return true; 064 } 065 if (name.equals("java.lang.String") && typeName.equals("org.activemq.message.ActiveMQTextMessage")) { 066 return true; 067 } 068 return false; 069 } 070 071 /** 072 * Creates a new test case value for the given named property. 073 * 074 * @return 075 */ 076 public Object createTestValue(JClass packetType, JProperty property) { 077 JClass type = property.getType(); 078 String name = type.getQualifiedName(); 079 ++counter; 080 if (type.getQualifiedName().equals("java.lang.Object")) { 081 return "\"DummyString" + counter + "\""; 082 } 083 else if (property.getGetter().getSimpleName().equals("getConsumerNosAsString")) { 084 return "\"1," + counter + "\""; 085 } 086 else if (name.equals("java.lang.String")) { 087 return "\"TestString" + counter + "\""; 088 } 089 else if (isDestinationType(name)) { 090 return "new ActiveMQQueue(\"TEST.FOO.BAR." + counter + "\")"; 091 } 092 else if (isPropertiesType(name)) { 093 return null; 094 } 095 else if (name.equals("boolean")) { 096 return counter % 2 == 1 ? Boolean.TRUE : Boolean.FALSE; 097 } 098 else if (name.equals("byte")) { 099 return "(byte) " + counter; 100 } 101 else if (name.equals("char")) { 102 return "(char) " + counter; 103 } 104 else if (name.equals("short")) { 105 return "(short) " + counter; 106 } 107 else if (name.equals("int")) { 108 return new Integer(counter); 109 } 110 else if (name.equals("long")) { 111 return "(long) " + counter; 112 } 113 else if (name.equals("float")) { 114 return "(float) " + counter; 115 } 116 else if (name.equals("double")) { 117 return "(double) " + counter; 118 } 119 return "null"; 120 } 121 122 public boolean isValidProperty(JProperty it) { 123 JMethod getter = it.getGetter(); 124 return getter != null && it.getSetter() != null && getter.isPublic() && getter.isStatic() == false && getter.getAnnotation("Transient") == null; 125 } 126 127 public boolean isPropertiesType(String type) { 128 return "java.util.Properties".equals(type) || "java.util.Map".equals(type); 129 } 130 131 public boolean isDestinationType(String type) { 132 return "org.activemq.message.ActiveMQDestination".equals(type) || "javax.jms.Destination".equals(type); 133 } 134 135 /** 136 * Returns the enum counter for the given type 137 */ 138 public int getEnum(JClass type) { 139 Integer value = (Integer) enums.get(type); 140 if (value == null) { 141 value = new Integer(++enumCounter); 142 enums.put(type, value); 143 } 144 return value.intValue(); 145 } 146 147 }