001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.activemq.openwire.tool;
018    
019    import java.io.File;
020    import java.io.PrintWriter;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import org.codehaus.jam.JAnnotation;
025    import org.codehaus.jam.JClass;
026    import org.codehaus.jam.JPackage;
027    import org.codehaus.jam.JProperty;
028    
029    /**
030     * 
031     */
032    public class JavaTestsGenerator extends MultiSourceGenerator {
033    
034        protected String targetDir = "src/test/java";
035    
036        public Object run() {
037            if (destDir == null) {
038                destDir = new File(targetDir + "/org/apache/activemq/openwire/v" + getOpenwireVersion());
039            }
040            return super.run();
041        }
042    
043        protected String getClassName(JClass jclass) {
044            if (isAbstract(jclass)) {
045                return super.getClassName(jclass) + "TestSupport";
046            } else {
047                return super.getClassName(jclass) + "Test";
048            }
049        }
050    
051        protected String getBaseClassName(JClass jclass) {
052            String answer = "DataFileGeneratorTestSupport";
053            if (superclass != null) {
054                String name = superclass.getSimpleName();
055                if (name != null && !name.equals("JNDIBaseStorable") && !name.equals("DataStructureSupport") && !name.equals("Object")) {
056                    answer = name + "Test";
057                    if (isAbstract(getJclass().getSuperclass())) {
058                        answer += "Support";
059                    }
060                }
061            }
062            return answer;
063        }
064    
065        private void generateLicence(PrintWriter out) {
066            out.println("/**");
067            out.println(" *");
068            out.println(" * Licensed to the Apache Software Foundation (ASF) under one or more");
069            out.println(" * contributor license agreements.  See the NOTICE file distributed with");
070            out.println(" * this work for additional information regarding copyright ownership.");
071            out.println(" * The ASF licenses this file to You under the Apache License, Version 2.0");
072            out.println(" * (the \"License\"); you may not use this file except in compliance with");
073            out.println(" * the License.  You may obtain a copy of the License at");
074            out.println(" *");
075            out.println(" * http://www.apache.org/licenses/LICENSE-2.0");
076            out.println(" *");
077            out.println(" * Unless required by applicable law or agreed to in writing, software");
078            out.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
079            out.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
080            out.println(" * See the License for the specific language governing permissions and");
081            out.println(" * limitations under the License.");
082            out.println(" */");
083        }
084    
085        protected void generateFile(PrintWriter out) {
086    
087            generateLicence(out);
088    
089            out.println("package org.apache.activemq.openwire.v" + openwireVersion + ";");
090            out.println("");
091            out.println("import java.io.DataInputStream;");
092            out.println("import java.io.DataOutputStream;");
093            out.println("import java.io.IOException;");
094            out.println("");
095            out.println("import org.apache.activemq.openwire.*;");
096            out.println("import org.apache.activemq.command.*;");
097            out.println("");
098            for (int i = 0; i < getJclass().getImportedPackages().length; i++) {
099                JPackage pkg = getJclass().getImportedPackages()[i];
100                for (int j = 0; j < pkg.getClasses().length; j++) {
101                    JClass clazz = pkg.getClasses()[j];
102                    out.println("import " + clazz.getQualifiedName() + ";");
103                }
104            }
105    
106            out.println("");
107            out.println("/**");
108            out.println(" * Test case for the OpenWire marshalling for " + jclass.getSimpleName() + "");
109            out.println(" *");
110            out.println(" *");
111            out.println(" * NOTE!: This file is auto generated - do not modify!");
112            out.println(" *        if you need to make a change, please see the modify the groovy scripts in the");
113            out.println(" *        under src/gram/script and then use maven openwire:generate to regenerate ");
114            out.println(" *        this file.");
115            out.println(" *");
116            out.println(" * ");
117            out.println(" */");
118            out.println("public " + getAbstractClassText() + "class " + className + " extends " + baseClass + " {");
119            out.println("");
120            if (!isAbstractClass()) {
121                out.println("");
122                out.println("    public static " + jclass.getSimpleName() + "Test SINGLETON = new " + jclass.getSimpleName() + "Test();");
123                out.println("");
124                out.println("    public Object createObject() throws Exception {");
125                out.println("        " + jclass.getSimpleName() + " info = new " + jclass.getSimpleName() + "();");
126                out.println("        populateObject(info);");
127                out.println("        return info;");
128                out.println("    }");
129            }
130            out.println("");
131            out.println("    protected void populateObject(Object object) throws Exception {");
132            out.println("        super.populateObject(object);");
133            out.println("        " + getJclass().getSimpleName() + " info = (" + getJclass().getSimpleName() + ") object;");
134            out.println("");
135    
136            TestDataGenerator generator = new TestDataGenerator();
137    
138            List properties = getProperties();
139            for (Iterator iter = properties.iterator(); iter.hasNext();) {
140                JProperty property = (JProperty)iter.next();
141    
142                JAnnotation annotation = property.getAnnotation("openwire:property");
143                String size = stringValue(annotation, "size");
144                String testSize = stringValue(annotation, "testSize");
145                String type = property.getType().getSimpleName();
146    //            boolean cached = isCachedProperty(property);
147                String propertyName = property.getSimpleName();
148                if ("-1".equals(testSize)) {
149                    continue;
150                }
151    
152                String setterName = property.getSetter().getSimpleName();
153    
154                if (type.equals("boolean")) {
155                    out.println("        info." + setterName + "(" + generator.createBool() + ");");
156                } else if (type.equals("byte")) {
157                    out.println("        info." + setterName + "(" + generator.createByte() + ");");
158                } else if (type.equals("char")) {
159                    out.println("        info." + setterName + "(" + generator.createChar() + ");");
160                } else if (type.equals("short")) {
161                    out.println("        info." + setterName + "(" + generator.createShort() + ");");
162                } else if (type.equals("int")) {
163                    out.println("        info." + setterName + "(" + generator.createInt() + ");");
164                } else if (type.equals("long")) {
165                    out.println("        info." + setterName + "(" + generator.createLong() + ");");
166                } else if (type.equals("byte[]")) {
167                    out.println("        info." + setterName + "(" + generator.createByteArray(propertyName) + ");");
168                } else if (type.equals("String")) {
169                    out.println("        info." + setterName + "(\"" + generator.createString(propertyName) + "\");");
170                } else if (type.equals("ByteSequence")) {
171                    out.println("        {");
172                    out.println("            byte data[] = " + generator.createByteArray(propertyName) + ";");
173                    out.println("            info." + setterName + "(new org.apache.activemq.util.ByteSequence(data,0,data.length));");
174                    out.println("}");
175                } else if (type.equals("Throwable")) {
176                    out.println("        info." + setterName + "(createThrowable(\"" + generator.createString(propertyName) + "\"));");
177                } else {
178                    if (property.getType().isArrayType()) {
179                        String arrayType = property.getType().getArrayComponentType().getSimpleName();
180                        if (size == null) {
181                            size = "2";
182                        }
183                        if (arrayType == jclass.getSimpleName()) {
184                            size = "0";
185                        }
186                        out.println("        {");
187                        out.println("            " + arrayType + " value[] = new " + arrayType + "[" + size + "];");
188                        out.println("            for( int i=0; i < " + size + "; i++ ) {");
189                        out.println("                value[i] = create" + arrayType + "(\"" + generator.createString(propertyName) + "\");");
190                        out.println("            }");
191                        out.println("            info." + setterName + "(value);");
192                        out.println("        }");
193                    } else {
194                        out.println("        info." + setterName + "(create" + type + "(\"" + generator.createString(propertyName) + "\"));");
195                    }
196                }
197            }
198    
199            out.println("    }");
200            out.println("}");
201        }
202    
203        public String getTargetDir() {
204            return targetDir;
205        }
206    
207        public void setTargetDir(String targetDir) {
208            this.targetDir = targetDir;
209        }
210    }