KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > tool > JavaTestsGenerator


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.openwire.tool;
19
20 import java.io.File JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.codehaus.jam.JAnnotation;
26 import org.codehaus.jam.JClass;
27 import org.codehaus.jam.JPackage;
28 import org.codehaus.jam.JProperty;
29
30
31 /**
32  *
33  * @version $Revision: 384826 $
34  */

35 public class JavaTestsGenerator extends MultiSourceGenerator {
36
37     protected String JavaDoc targetDir="src/test/java";
38
39     public Object JavaDoc run() {
40         if (destDir == null) {
41             destDir = new File JavaDoc(targetDir+"/org/apache/activemq/openwire/v" + getOpenwireVersion());
42         }
43         return super.run();
44     }
45     
46     protected String JavaDoc getClassName(JClass jclass) {
47         if( isAbstract(jclass) ) {
48             return super.getClassName(jclass) + "TestSupport";
49         } else {
50             return super.getClassName(jclass) + "Test";
51         }
52     }
53
54     protected String JavaDoc getBaseClassName(JClass jclass) {
55         String JavaDoc answer = "DataFileGeneratorTestSupport";
56         if (superclass != null) {
57             String JavaDoc name = superclass.getSimpleName();
58             if (name!=null
59                     && !name.equals("JNDIBaseStorable")
60                     && !name.equals("DataStructureSupport")
61                     && !name.equals("Object")) {
62                answer = name + "Test";
63                if (isAbstract(getJclass().getSuperclass()))
64                    answer += "Support";
65             }
66         }
67         return answer;
68     }
69
70     private void generateLicence(PrintWriter JavaDoc out) {
71 out.println("/**");
72 out.println(" *");
73 out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
74 out.println(" * contributor license agreements. See the NOTICE file distributed with");
75 out.println(" * this work for additional information regarding copyright ownership.");
76 out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
77 out.println(" * (the \"License\"); you may not use this file except in compliance with");
78 out.println(" * the License. You may obtain a copy of the License at");
79 out.println(" *");
80 out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
81 out.println(" *");
82 out.println(" * Unless required by applicable law or agreed to in writing, software");
83 out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
84 out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
85 out.println(" * See the License for the specific language governing permissions and");
86 out.println(" * limitations under the License.");
87 out.println(" */");
88     }
89     
90     protected void generateFile(PrintWriter JavaDoc out) {
91
92         generateLicence(out);
93
94 out.println("package org.apache.activemq.openwire.v"+openwireVersion+";");
95 out.println("");
96 out.println("import java.io.DataInputStream;");
97 out.println("import java.io.DataOutputStream;");
98 out.println("import java.io.IOException;");
99 out.println("");
100 out.println("import org.apache.activemq.openwire.*;");
101 out.println("import org.apache.activemq.command.*;");
102 out.println("");
103         for (int i = 0; i < getJclass().getImportedPackages().length; i++) {
104             JPackage pkg = getJclass().getImportedPackages()[i];
105             for (int j = 0; j < pkg.getClasses().length; j++) {
106                 JClass clazz = pkg.getClasses()[j];
107 out.println("import " + clazz.getQualifiedName() + ";");
108             }
109         }
110
111         boolean marshallerAware = isMarshallAware(jclass);
112
113 out.println("");
114 out.println("/**");
115 out.println(" * Test case for the OpenWire marshalling for "+jclass.getSimpleName()+"");
116 out.println(" *");
117 out.println(" *");
118 out.println(" * NOTE!: This file is auto generated - do not modify!");
119 out.println(" * if you need to make a change, please see the modify the groovy scripts in the");
120 out.println(" * under src/gram/script and then use maven openwire:generate to regenerate ");
121 out.println(" * this file.");
122 out.println(" *");
123 out.println(" * @version $Revision: $");
124 out.println(" */");
125 out.println("public "+getAbstractClassText()+"class "+className+" extends "+baseClass+" {");
126 out.println("");
127         if (!isAbstractClass()) {
128 out.println("");
129 out.println(" public static "+jclass.getSimpleName()+"Test SINGLETON = new "+jclass.getSimpleName()+"Test();");
130 out.println("");
131 out.println(" public Object createObject() throws Exception {");
132 out.println(" "+jclass.getSimpleName()+" info = new "+jclass.getSimpleName()+"();");
133 out.println(" populateObject(info);");
134 out.println(" return info;");
135 out.println(" }");
136         }
137 out.println("");
138 out.println(" protected void populateObject(Object object) throws Exception {");
139 out.println(" super.populateObject(object);");
140 out.println(" "+getJclass().getSimpleName()+" info = ("+getJclass().getSimpleName()+") object;");
141 out.println("");
142
143         TestDataGenerator generator = new TestDataGenerator();
144
145         List JavaDoc properties = getProperties();
146         for (Iterator JavaDoc iter = properties.iterator(); iter.hasNext();) {
147             JProperty property = (JProperty) iter.next();
148
149             JAnnotation annotation = property.getAnnotation("openwire:property");
150             String JavaDoc size = stringValue(annotation, "size");
151             String JavaDoc testSize = stringValue(annotation, "testSize");
152             String JavaDoc type = property.getType().getSimpleName();
153             boolean cached = isCachedProperty(property);
154             String JavaDoc propertyName = property.getSimpleName();
155             if ("-1".equals(testSize))
156                 continue;
157                     
158             
159             String JavaDoc setterName = property.getSetter().getSimpleName();
160             
161             if( type.equals("boolean")) {
162 out.println(" info."+setterName+"("+generator.createBool()+");");
163             } else if( type.equals("byte")) {
164 out.println(" info."+setterName+"("+generator.createByte()+");");
165             } else if( type.equals("char")) {
166 out.println(" info."+setterName+"("+generator.createChar()+");");
167             } else if( type.equals("short")) {
168 out.println(" info."+setterName+"("+generator.createShort()+");");
169             } else if( type.equals("int")) {
170 out.println(" info."+setterName+"("+generator.createInt()+");");
171             } else if( type.equals("long")) {
172 out.println(" info."+setterName+"("+generator.createLong()+");");
173             } else if( type.equals("byte[]")) {
174 out.println(" info."+setterName+"("+generator.createByteArray(propertyName)+");");
175             } else if( type.equals("String")) {
176 out.println(" info."+setterName+"(\""+generator.createString(propertyName)+"\");");
177             } else if( type.equals("ByteSequence")) {
178 out.println(" {");
179 out.println(" byte data[] = "+generator.createByteArray(propertyName)+";");
180 out.println(" info."+setterName+"(new org.apache.activemq.util.ByteSequence(data,0,data.length));");
181 out.println( "}");
182             } else if( type.equals("Throwable")) {
183 out.println(" info."+setterName+"(createThrowable(\""+generator.createString(propertyName)+"\"));");
184             } else {
185                 if( property.getType().isArrayType() ) {
186                     String JavaDoc arrayType = property.getType().getArrayComponentType().getSimpleName();
187                     if (size == null)
188                       size = "2";
189                     if (arrayType == jclass.getSimpleName())
190                       size = "0";
191 out.println(" {");
192 out.println(" "+arrayType+" value[] = new "+arrayType+"["+size+"];");
193 out.println(" for( int i=0; i < "+size+"; i++ ) {");
194 out.println(" value[i] = create"+arrayType+"(\""+generator.createString(propertyName)+"\");");
195 out.println(" }");
196 out.println(" info."+setterName+"(value);");
197 out.println(" }");
198                 } else {
199 out.println(" info."+setterName+"(create"+type+"(\""+generator.createString(propertyName)+"\"));");
200                 }
201             }
202         }
203             
204 out.println(" }");
205 out.println("}");
206     }
207
208     public String JavaDoc getTargetDir() {
209         return targetDir;
210     }
211
212     public void setTargetDir(String JavaDoc targetDir) {
213         this.targetDir = targetDir;
214     }
215 }
216
217
Popular Tags