1 package test.ozoneDB.tools.OPP.srcgen.builder; 9 10 import com.mockobjects.Verifiable; 11 import com.mockobjects.ExpectationCounter; 12 import com.mockobjects.ExpectationList; 13 import com.mockobjects.util.Verifier; 14 import org.ozoneDB.tools.OPP.srcgen.ClassBuilder; 15 import org.ozoneDB.tools.OPP.srcgen.BuilderException; 16 import org.ozoneDB.tools.OPP.message.MessageWriter; 17 18 import java.util.List ; 19 import java.util.Arrays ; 20 import java.util.Iterator ; 21 import java.lang.reflect.Modifier ; 22 23 28 public class MockClassBuilder implements Verifiable, ClassBuilder { 29 private ExpectationCounter initCount = new ExpectationCounter("init calls"); 30 private ExpectationList beginClassExpectations = new ExpectationList("beginClass"); 31 private ExpectationCounter endClassCount = new ExpectationCounter("endClass calls"); 32 private ExpectationList makeMethodExpectations = new ExpectationList("makeMethod"); 33 private ExpectationList makeConstructorExpectations = new ExpectationList("makeConstructor"); 34 private int expectedEndClassCount; 35 36 private class MethodDescriptor { 38 public int lockLevel = 0; 39 public int modifier = 0; 40 public String name; 41 public String returnType; 42 public List parameters; 43 public List exceptions; 44 45 public MethodDescriptor(int modifier, String returnType, String name, ClassBuilder.Parameter parameters[], String exceptions[], int lockLevel) { 46 this.modifier = modifier; 47 this.name = name; 48 this.returnType = returnType; 49 this.lockLevel = lockLevel; 50 this.parameters = Arrays.asList(parameters); 51 this.exceptions = Arrays.asList(exceptions); 52 } 53 54 public boolean equals(Object o) { 55 if (this == o) return true; 56 if (!(o instanceof MethodDescriptor)) return false; 57 58 final MethodDescriptor methodDescriptor = (MethodDescriptor) o; 59 60 if (lockLevel != methodDescriptor.lockLevel) return false; 61 if (modifier != methodDescriptor.modifier) return false; 62 if (!exceptions.equals(methodDescriptor.exceptions)) return false; 63 if (name != null ? !name.equals(methodDescriptor.name) : methodDescriptor.name != null) return false; 64 if (!parameters.equals(methodDescriptor.parameters)) return false; 65 if (returnType != null ? !returnType.equals(methodDescriptor.returnType) : methodDescriptor.returnType != null) return false; 66 67 return true; 68 } 69 70 public int hashCode() { 71 int result; 72 result = lockLevel; 73 result = 29 * result + modifier; 74 result = 29 * result + (name != null ? name.hashCode() : 0); 75 result = 29 * result + (returnType != null ? returnType.hashCode() : 0); 76 result = 29 * result + parameters.hashCode(); 77 result = 29 * result + exceptions.hashCode(); 78 return result; 79 } 80 81 public String toString() { 82 StringBuffer result = new StringBuffer (); 83 result.append(Modifier.toString(modifier)).append(" "); 84 result.append(returnType).append(" ").append(name).append(" ("); 85 for (Iterator iterator = parameters.iterator(); iterator.hasNext();) { 86 ClassBuilder.Parameter parameter = (ClassBuilder.Parameter) iterator.next(); 87 result.append(parameter); 88 if (iterator.hasNext()) { 89 result.append(", "); 90 } 91 } 92 result.append(")"); 93 if (!exceptions.isEmpty()) { 94 result.append(" throws "); 95 } 96 for (Iterator iterator = exceptions.iterator(); iterator.hasNext();) { 97 String exception = (String ) iterator.next(); 98 result.append(exception); 99 if (iterator.hasNext()) { 100 result.append(", "); 101 } 102 } 103 result.append("\n"); 104 result.append("LockLevel: " + lockLevel); 105 return result.toString(); 106 } 107 } 108 109 private class ClassDescriptor { 110 public int modifier = 0; 111 public String fullName; 112 public String superClass; 113 public List interfaces; 114 115 public ClassDescriptor(int modifier, String fullName, String superClass, String interfaces[]) { 116 this.modifier = modifier; 117 this.fullName = fullName; 118 this.superClass = superClass; 119 this.interfaces = Arrays.asList(interfaces); 120 } 121 122 public boolean equals(Object o) { 123 if (this == o) return true; 124 if (!(o instanceof ClassDescriptor)) return false; 125 126 final ClassDescriptor classDescriptor = (ClassDescriptor) o; 127 128 if (modifier != classDescriptor.modifier) return false; 129 if (!fullName.equals(classDescriptor.fullName)) return false; 130 if (!interfaces.equals(classDescriptor.interfaces)) return false; 131 if (!superClass.equals(classDescriptor.superClass)) return false; 132 133 return true; 134 } 135 136 public int hashCode() { 137 int result; 138 result = modifier; 139 result = 29 * result + fullName.hashCode(); 140 result = 29 * result + superClass.hashCode(); 141 result = 29 * result + interfaces.hashCode(); 142 return result; 143 } 144 145 public String toString() { 146 return Modifier.toString(modifier) + " " + fullName + " " + (superClass.equals("") ? "" : " extends " + superClass) + " " + 147 printInterfaces(); 148 } 149 150 private String printInterfaces() { 151 if (interfaces.isEmpty()) { 152 return ""; 153 } else { 154 String result = "interfaces "; 155 for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) { 156 String itf = (String ) iterator.next(); 157 result += itf; 158 if (iterator.hasNext()) { 159 result += ", "; 160 } 161 } 162 return result; 163 } 164 } 165 } 166 167 168 public void setupInit(boolean recieve) { 169 if (recieve) { 170 initCount.setExpected(1); 171 } else { 172 initCount.setExpectNothing(); 173 } 174 } 175 176 public void setupBeginClass(int modifier, String fullName, String superClass, String interfaces[]) { 177 beginClassExpectations.addExpected(new ClassDescriptor(modifier, fullName, superClass, interfaces)); 178 endClassCount.setExpected(++expectedEndClassCount); 179 } 180 181 public void setupMakeConstructor(int modifier, ClassBuilder.Parameter parameters[], String exceptions[]) throws BuilderException { 182 makeConstructorExpectations.addExpected(new MethodDescriptor(modifier, null, null, parameters, exceptions, 0)); 183 } 184 185 public void setupMakeMethod(int modifier, String name, ClassBuilder.Parameter parameters[], String returnType, String exceptions[], int lockLevel) throws BuilderException { 186 makeMethodExpectations.addExpected(new MethodDescriptor(modifier, returnType, name, parameters, exceptions, lockLevel)); 187 } 188 189 public void init(MessageWriter msgWriter) { 190 initCount.inc(); 191 } 192 193 public void beginClass(int modifier, String fullName, String superClass, String interfaces[]) throws BuilderException { 194 beginClassExpectations.addActual(new ClassDescriptor(modifier, fullName, superClass, interfaces)); 195 } 196 197 public void makeConstructor(int modifier, ClassBuilder.Parameter parameters[], String exceptions[]) throws BuilderException { 198 makeConstructorExpectations.addActual(new MethodDescriptor(modifier, null, null, parameters, exceptions, 0)); 199 } 200 201 public void makeMethod(int modifier, String name, ClassBuilder.Parameter parameters[], String returnType, String exceptions[], int lockLevel) throws BuilderException { 202 makeMethodExpectations.addActual(new MethodDescriptor(modifier, returnType, name, parameters, exceptions, lockLevel)); 203 } 204 205 public void endClass() throws BuilderException { 206 endClassCount.inc(); 207 } 208 209 public void verify() { 210 Verifier.verifyObject(this); 211 } 212 } 213 | Popular Tags |