001: package com.mockrunner.test.gen;
002:
003: import java.beans.IntrospectionException;
004: import java.net.URL;
005: import java.util.List;
006: import java.util.Map;
007:
008: import javax.servlet.http.HttpServlet;
009:
010: import junit.framework.TestCase;
011:
012: import com.mockrunner.gen.proc.JavaClassGenerator;
013: import com.mockrunner.gen.proc.JavaClassGenerator.ConstructorDeclaration;
014: import com.mockrunner.gen.proc.JavaClassGenerator.MethodDeclaration;
015: import com.mockrunner.util.common.ClassUtil;
016: import com.mockrunner.util.common.CollectionUtil;
017: import com.mockrunner.util.web.XmlUtil;
018:
019: public class JavaClassGeneratorTest extends TestCase {
020: private final static String NL = System
021: .getProperty("line.separator");
022:
023: private JavaClassGenerator generator;
024:
025: protected void setUp() throws Exception {
026: super .setUp();
027: generator = new JavaClassGenerator();
028: }
029:
030: public void testGenerate() {
031: generator.setClassName("MyClass");
032: generator.setAbstract(true);
033: String[] commentLines = new String[] { "This is", "a comment" };
034: generator.setClassComment(commentLines);
035: generator.setPackage(this .getClass().getPackage());
036: generator.setSuperClass(ClassUtil.class);
037: generator.addInterfaceImplementation(Map.class);
038: generator.addInterfaceImplementation(List.class);
039: generator.addImport(XmlUtil.class);
040: generator.addImport(HttpServlet.class);
041: generator.addImport(TestCase.class);
042: generator.addMemberDeclaration(XmlUtil.class, "util");
043: generator.addMemberDeclaration(String.class, "name");
044: generator.addConstructorDeclaration();
045: ConstructorDeclaration constructor = new ConstructorDeclaration();
046: constructor.setCommentLines(new String[] { "A", "comment" });
047: constructor.setArguments(new Class[] { String.class,
048: JavaClassGeneratorTest.class });
049: constructor.setArgumentNames(new String[] { "string1" });
050: constructor.setExceptions(new Class[] { Exception.class });
051: constructor.setCodeLines(new String[] { "Line1", "Line2" });
052: generator.addConstructorDeclaration(constructor);
053: MethodDeclaration method1 = new MethodDeclaration();
054: method1.setName("aMethod");
055: method1.setCommentLines(new String[] { "Another", "comment" });
056: method1.setCodeLines(new String[] { "Line1" });
057: generator.addMethodDeclaration(method1);
058: MethodDeclaration method2 = new MethodDeclaration();
059: method2.setName("anotherMethod");
060: method2.setReturnType(CollectionUtil.class);
061: method2.setArguments(new Class[] { String.class, Double.TYPE });
062: method2.setArgumentNames(new String[] { "string1" });
063: generator.addMethodDeclaration(method2);
064: MethodDeclaration method3 = new MethodDeclaration();
065: method3.setName("thirdMethod");
066: method3.setReturnType(Integer.TYPE);
067: method3.setCommentLines(new String[] { "Comment" });
068: method3.setExceptions(new Class[] {
069: IntrospectionException.class, RuntimeException.class });
070: method3.setCodeLines(new String[] { "Line1", "Line2" });
071: generator.addMethodDeclaration(method3);
072: MethodDeclaration method4 = new MethodDeclaration();
073: method4.setName("fourthMethod");
074: method4.setReturnType(int[].class);
075: method4.setArguments(new Class[] { String.class, URL[][].class,
076: Double[].class, boolean[].class });
077: generator.addMethodDeclaration(method4);
078: assertEquals(getExpected(), generator.generate());
079: }
080:
081: private String getExpected() {
082: return "package com.mockrunner.test.gen;"
083: + NL
084: + NL
085: + "import java.beans.IntrospectionException;"
086: + NL
087: + "import java.net.URL;"
088: + NL
089: + "import java.util.List;"
090: + NL
091: + "import java.util.Map;"
092: + NL
093: + NL
094: + "import javax.servlet.http.HttpServlet;"
095: + NL
096: + NL
097: + "import junit.framework.TestCase;"
098: + NL
099: + NL
100: + "import com.mockrunner.util.common.ClassUtil;"
101: + NL
102: + "import com.mockrunner.util.common.CollectionUtil;"
103: + NL
104: + "import com.mockrunner.util.web.XmlUtil;"
105: + NL
106: + NL
107: + "/**"
108: + NL
109: + " * This is"
110: + NL
111: + " * a comment"
112: + NL
113: + " */"
114: + NL
115: + "public abstract class MyClass extends ClassUtil implements Map, List"
116: + NL
117: + "{"
118: + NL
119: + " private XmlUtil util;"
120: + NL
121: + " private String name;"
122: + NL
123: + NL
124: + " public MyClass()"
125: + NL
126: + " {"
127: + NL
128: + NL
129: + " }"
130: + NL
131: + NL
132: + " /**"
133: + NL
134: + " * A"
135: + NL
136: + " * comment"
137: + NL
138: + " */"
139: + NL
140: + " public MyClass(String string1, JavaClassGeneratorTest javaClassGeneratorTest) throws Exception"
141: + NL
142: + " {"
143: + NL
144: + " Line1"
145: + NL
146: + " Line2"
147: + NL
148: + " }"
149: + NL
150: + NL
151: + " /**"
152: + NL
153: + " * Another"
154: + NL
155: + " * comment"
156: + NL
157: + " */"
158: + NL
159: + " public void aMethod()"
160: + NL
161: + " {"
162: + NL
163: + " Line1"
164: + NL
165: + " }"
166: + NL
167: + NL
168: + " public CollectionUtil anotherMethod(String string1, double doubleValue)"
169: + NL
170: + " {"
171: + NL
172: + NL
173: + " }"
174: + NL
175: + NL
176: + " /**"
177: + NL
178: + " * Comment"
179: + NL
180: + " */"
181: + NL
182: + " public int thirdMethod() throws IntrospectionException, RuntimeException"
183: + NL
184: + " {"
185: + NL
186: + " Line1"
187: + NL
188: + " Line2"
189: + NL
190: + " }"
191: + NL
192: + NL
193: + " public int[] fourthMethod(String string, URL[][] urls, Double[] doubleValues, boolean[] booleanValues)"
194: + NL + " {" + NL + NL + " }" + NL + "}";
195: }
196: }
|