KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ozoneDB > tools > OPP > srcgen > builder > MockClassBuilder


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by Joakim Ohlrogge are
5
// Copyright (C) 1997-@year@ by Joakim Ohlrogge. All rights reserved.
6
//
7
// $Id$
8
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 JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.lang.reflect.Modifier JavaDoc;
22
23 /**
24  * User: Jocke
25  * Date: 2003-dec-29
26  * Time: 22:36:04
27  */

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     // class to check methods and constructors
37
private class MethodDescriptor {
38         public int lockLevel = 0;
39         public int modifier = 0;
40         public String JavaDoc name;
41         public String JavaDoc returnType;
42         public List parameters;
43         public List exceptions;
44
45         public MethodDescriptor(int modifier, String JavaDoc returnType, String JavaDoc name, ClassBuilder.Parameter parameters[], String JavaDoc 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 JavaDoc 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 JavaDoc toString() {
82             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
83             result.append(Modifier.toString(modifier)).append(" ");
84             result.append(returnType).append(" ").append(name).append(" (");
85             for (Iterator JavaDoc 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 JavaDoc iterator = exceptions.iterator(); iterator.hasNext();) {
97                 String JavaDoc exception = (String JavaDoc) 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 JavaDoc fullName;
112         public String JavaDoc superClass;
113         public List interfaces;
114
115         public ClassDescriptor(int modifier, String JavaDoc fullName, String JavaDoc superClass, String JavaDoc 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 JavaDoc 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 JavaDoc toString() {
146             return Modifier.toString(modifier) + " " + fullName + " " + (superClass.equals("") ? "" : " extends " + superClass) + " " +
147                     printInterfaces();
148         }
149
150         private String JavaDoc printInterfaces() {
151             if (interfaces.isEmpty()) {
152                 return "";
153             } else {
154                 String JavaDoc result = "interfaces ";
155                 for (Iterator JavaDoc iterator = interfaces.iterator(); iterator.hasNext();) {
156                     String JavaDoc itf = (String JavaDoc) 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 JavaDoc fullName, String JavaDoc superClass, String JavaDoc 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 JavaDoc exceptions[]) throws BuilderException {
182         makeConstructorExpectations.addExpected(new MethodDescriptor(modifier, null, null, parameters, exceptions, 0));
183     }
184
185     public void setupMakeMethod(int modifier, String JavaDoc name, ClassBuilder.Parameter parameters[], String JavaDoc returnType, String JavaDoc 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 JavaDoc fullName, String JavaDoc superClass, String JavaDoc interfaces[]) throws BuilderException {
194         beginClassExpectations.addActual(new ClassDescriptor(modifier, fullName, superClass, interfaces));
195     }
196
197     public void makeConstructor(int modifier, ClassBuilder.Parameter parameters[], String JavaDoc exceptions[]) throws BuilderException {
198         makeConstructorExpectations.addActual(new MethodDescriptor(modifier, null, null, parameters, exceptions, 0));
199     }
200
201     public void makeMethod(int modifier, String JavaDoc name, ClassBuilder.Parameter parameters[], String JavaDoc returnType, String JavaDoc 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