001: package com.mockrunner.gen.proc;
002:
003: import junit.framework.TestCase;
004:
005: import com.mockrunner.base.BaseTestCase;
006: import com.mockrunner.base.BasicHTMLOutputTestCase;
007: import com.mockrunner.base.HTMLOutputModule;
008: import com.mockrunner.gen.proc.JavaClassGenerator.MethodDeclaration;
009: import com.mockrunner.mock.web.WebMockObjectFactory;
010: import com.mockrunner.util.common.ClassUtil;
011:
012: public class BasicAdapterProcessor extends StandardAdapterProcessor {
013: protected void addMemberDeclarations(
014: JavaClassGenerator classGenerator, MemberInfo memberInfo) {
015: super .addMemberDeclarations(classGenerator, memberInfo);
016: String factoryName = ClassUtil.getArgumentName(memberInfo
017: .getFactory());
018: memberInfo.setFactoryMember(factoryName);
019: classGenerator.addMemberDeclaration(memberInfo.getFactory(),
020: factoryName);
021: }
022:
023: protected String[] getSetUpMethodCodeLines(MemberInfo memberInfo) {
024: String[] codeLines = new String[2];
025: codeLines[0] = "super.setUp();";
026: String getFactoryCall = "get"
027: + ClassUtil.getClassName(memberInfo.getFactory());
028: codeLines[1] = memberInfo.getModuleMember() + " = create"
029: + ClassUtil.getClassName(memberInfo.getModule()) + "("
030: + getFactoryCall + "());";
031: return codeLines;
032: }
033:
034: protected String[] getTearDownMethodCodeLines(MemberInfo memberInfo) {
035: String[] codeLines = new String[3];
036: codeLines[0] = "super.tearDown();";
037: codeLines[1] = memberInfo.getModuleMember() + " = null;";
038: codeLines[2] = memberInfo.getFactoryMember() + " = null;";
039: return codeLines;
040: }
041:
042: protected void addAdditionalControlMethods(
043: JavaClassGenerator classGenerator, MemberInfo memberInfo) {
044: addCreateFactoryMethods(classGenerator, memberInfo);
045: addGetFactoryLazyMethod(classGenerator, memberInfo);
046: addSetMethod(classGenerator, memberInfo.getFactory(),
047: memberInfo.getFactoryMember());
048: addCreateModuleMethods(classGenerator, memberInfo);
049: super .addAdditionalControlMethods(classGenerator, memberInfo);
050: }
051:
052: private void addGetFactoryLazyMethod(
053: JavaClassGenerator classGenerator, MemberInfo memberInfo) {
054: String[] codeLines = new String[8];
055: codeLines[0] = "synchronized("
056: + ClassUtil.getClassName(memberInfo.getFactory())
057: + ".class)";
058: codeLines[1] = "{";
059: codeLines[2] = " if(" + memberInfo.getFactoryMember()
060: + " == null)";
061: codeLines[3] = " {";
062: codeLines[4] = " " + memberInfo.getFactoryMember()
063: + " = create"
064: + ClassUtil.getClassName(memberInfo.getFactory())
065: + "();";
066: codeLines[5] = " }";
067: codeLines[6] = "}";
068: codeLines[7] = "return " + memberInfo.getFactoryMember() + ";";
069: addGetMethod(classGenerator, memberInfo.getFactory(), codeLines);
070: }
071:
072: protected String[] getClassComment(Class module) {
073: String link = getJavaDocLink(module);
074: String[] comment = new String[9];
075: String standardClassName = ClassUtil.getPackageName(module)
076: + "."
077: + super .getClassNameFromBaseName(getBaseName(module));
078: comment[0] = "Delegator for " + link + ". You can";
079: comment[1] = "subclass this adapter or use " + link;
080: comment[2] = "directly (so your test case can use another base class).";
081: comment[3] = "This basic adapter can be used if you don't need any other modules. It";
082: comment[4] = "does not extend "
083: + getJavaDocLink(BaseTestCase.class) + ". If you want";
084: comment[5] = "to use several modules in conjunction, consider subclassing";
085: comment[6] = "{@link " + standardClassName + "}.";
086: comment[7] = "<b>This class is generated from the " + link;
087: comment[8] = "and should not be edited directly</b>.";
088: return comment;
089: }
090:
091: protected Class getSuperClass(Class module) {
092: if (HTMLOutputModule.class.isAssignableFrom(module)) {
093: return BasicHTMLOutputTestCase.class;
094: }
095: return TestCase.class;
096: }
097:
098: protected String getClassNameFromBaseName(String className) {
099: return "Basic" + className + "CaseAdapter";
100: }
101:
102: private void addCreateFactoryMethods(
103: JavaClassGenerator classGenerator, MemberInfo memberInfo) {
104: Class factory = memberInfo.getFactory();
105: MethodDeclaration createMethod = prepareCreateMethod(factory);
106: String[] comment = new String[2];
107: comment[0] = "Creates a " + getJavaDocLink(factory) + ".";
108: comment[1] = "@return the created " + getJavaDocLink(factory);
109: createMethod.setCommentLines(comment);
110: String[] codeLines = new String[] { "return new "
111: + ClassUtil.getClassName(factory) + "();" };
112: createMethod.setCodeLines(codeLines);
113: classGenerator.addMethodDeclaration(createMethod);
114: if (HTMLOutputModule.class.isAssignableFrom(memberInfo
115: .getModule())) {
116: createMethod = prepareCreateMethod(factory);
117: createMethod
118: .setArguments(new Class[] { WebMockObjectFactory.class });
119: createMethod
120: .setArgumentNames(new String[] { "otherFactory" });
121: codeLines = new String[] { "return new "
122: + ClassUtil.getClassName(factory)
123: + "(otherFactory);" };
124: createMethod.setCodeLines(codeLines);
125: comment = new String[] { "Same as <code>"
126: + createMethod.getName()
127: + "(otherFactory, true)</code>." };
128: createMethod.setCommentLines(comment);
129: classGenerator.addMethodDeclaration(createMethod);
130: createMethod = prepareCreateMethod(factory);
131: createMethod.setArguments(new Class[] {
132: WebMockObjectFactory.class, Boolean.TYPE });
133: createMethod.setArgumentNames(new String[] {
134: "otherFactory", "createNewSession" });
135: codeLines = new String[] { "return new "
136: + ClassUtil.getClassName(factory)
137: + "(otherFactory, createNewSession);" };
138: createMethod.setCodeLines(codeLines);
139: createMethod
140: .setCommentLines(getCreateMethodComment(factory));
141: classGenerator.addMethodDeclaration(createMethod);
142: }
143: }
144:
145: private void addCreateModuleMethods(
146: JavaClassGenerator classGenerator, MemberInfo memberInfo) {
147: Class module = memberInfo.getModule();
148: Class factory = memberInfo.getFactory();
149: MethodDeclaration createMethod = prepareCreateMethod(module);
150: String[] comment = new String[4];
151: comment[0] = "Creates a " + getJavaDocLink(module)
152: + " based on the current";
153: comment[1] = getJavaDocLink(factory) + ".";
154: comment[2] = "Same as <code>" + createMethod.getName() + "("
155: + createGetFactoryMethodCall(factory) + ")</code>.";
156: comment[3] = "@return the created " + getJavaDocLink(module);
157: createMethod.setCommentLines(comment);
158: String[] codeLines = new String[] { "return new "
159: + ClassUtil.getClassName(module) + "("
160: + createGetFactoryMethodCall(factory) + ");" };
161: createMethod.setCodeLines(codeLines);
162: classGenerator.addMethodDeclaration(createMethod);
163: createMethod = prepareCreateMethod(module);
164: comment = new String[3];
165: comment[0] = "Creates a " + getJavaDocLink(module)
166: + " with the specified";
167: comment[1] = getJavaDocLink(factory) + ".";
168: comment[2] = "@return the created " + getJavaDocLink(module);
169: createMethod.setCommentLines(comment);
170: createMethod.setArguments(new Class[] { factory });
171: createMethod.setArgumentNames(new String[] { "mockFactory" });
172: codeLines = new String[] { "return new "
173: + ClassUtil.getClassName(module) + "(mockFactory);" };
174: createMethod.setCodeLines(codeLines);
175: classGenerator.addMethodDeclaration(createMethod);
176: }
177:
178: private String createGetFactoryMethodCall(Class factory) {
179: return "get" + ClassUtil.getClassName(factory) + "()";
180: }
181:
182: private MethodDeclaration prepareCreateMethod(Class clazz) {
183: String methodName = "create" + ClassUtil.getClassName(clazz);
184: MethodDeclaration createMethod = createProtectedMethod();
185: createMethod.setName(methodName);
186: createMethod.setReturnType(clazz);
187: return createMethod;
188: }
189:
190: private String[] getCreateMethodComment(Class factory) {
191: String link = getJavaDocLink(factory);
192: String[] comment = new String[13];
193: comment[0] = "Creates a " + link + " based on another ";
194: comment[1] = getJavaDocLink(WebMockObjectFactory.class) + ".";
195: comment[2] = "The created " + link + " will have its own";
196: comment[3] = "request and response objects. If you set <i>createNewSession</i>";
197: comment[4] = "to <code>true</code> it will also have its own session object.";
198: comment[5] = "The two factories will share one <code>ServletContext</code>.";
199: comment[6] = "Especially important for multithreading tests.";
200: comment[7] = "If you set <i>createNewSession</i> to false, the two factories";
201: comment[8] = "will share one session. This setting simulates multiple requests";
202: comment[9] = "from the same client.";
203: comment[10] = "@param otherFactory the other factory";
204: comment[11] = "@param createNewSession create a new session for the new factory";
205: comment[12] = "@return the created " + link;
206: return comment;
207: }
208: }
|