001: package U2.T2;
002:
003: //import U2.T2.Obj.* ;
004: import java.lang.reflect.*;
005: import java.util.*;
006: import java.io.*;
007: import U2.T2.Reflection.*;
008:
009: /**
010: * Abstract class representing the general structure and parameters of
011: * a test engine; furthermore the class contains a bunch of utility
012: * static methods.
013: */
014: abstract public class Engine {
015:
016: /**
017: * The class under the test.
018: */
019: protected Class CUT;
020:
021: /**
022: * The object pool used by the engine.
023: */
024: protected Pool pool;
025:
026: /*
027: * Saved execution traces. Typically it contains traces of
028: * violating executions.
029: */
030: // protected LinkedList<Trace> savedTraces ;
031: /**
032: * Specify a maximum on the numbers of total execution steps
033: * generated by the engine. Default is 500.
034: */
035: protected int maxNumOfSteps = 500;
036:
037: /**
038: * Specify a maximum on the length of each execution. Default is
039: * 4.
040: */
041: protected int maxExecLength = 4;
042:
043: /**
044: * The engine should stop if the number of violations found
045: * reaches maxNumViolations. Default is 1.
046: */
047: protected int maxNumViolations = 1;
048:
049: // some variables to keep track progress:
050:
051: /**
052: * Total number of execution steps generated.
053: */
054: protected int numOfSteps = 0;
055:
056: /**
057: * Total number of executions generated.
058: */
059: protected int numOfExecs = 0;
060:
061: /**
062: * Total number of violations found.
063: */
064: protected int numOfViolations = 0;
065:
066: /**
067: * Total number of relevant checks.
068: */
069: protected int numOfRelevantChecks = 0;
070:
071: /*
072: * Construct an engine with a default setting. The CUT is set to
073: * null.
074: */
075: // public Engine() { } // :)
076: /**
077: * Run the test engine. It does nothing here, and should be
078: * overiden by subclass.
079: */
080: abstract public void runMe();
081:
082: /**
083: * To report violating traces found by the engine. It does nothing
084: * here, and should be overiden by subclass.
085: */
086: abstract public void report(PrintStream out);
087:
088: // Some utilities methods
089:
090: private static final String CLASSINV_STR = "classinv";
091: private static final String MAIN_STR = "main";
092: private static final String SPEC_STR = "_spec";
093:
094: /**
095: * Get the constructors of a class that act as interface
096: * points. Currently these are the declared, non-private
097: * constructors of the class.
098: */
099: public static List<Constructor> getConsIPs(Class C) {
100:
101: List<Constructor> result = new LinkedList<Constructor>();
102: Constructor[] cons = C.getDeclaredConstructors();
103: for (int i = 0; i < cons.length; i++)
104: if (!Modifier.isPrivate(cons[i].getModifiers()))
105: result.add(cons[i]);
106:
107: return result;
108:
109: }
110:
111: /**
112: * Get the methods of a class that act as interface
113: * points. Currently this is all public methods of its
114: * superclasses + its own declared non-private methods.
115: */
116: public static List<Method> getMethsIPs(Class C) {
117:
118: Class CLASS_OBJECT = (new Object()).getClass();
119:
120: List<Method> result = new LinkedList<Method>();
121: // first get C's own declared methods:
122: Method[] methods = C.getDeclaredMethods();
123: for (int i = 0; i < methods.length; i++)
124: if ((!Modifier.isPrivate(methods[i].getModifiers()))
125: && (!Modifier.isStatic(methods[i].getModifiers()) || ReflUtil
126: .canAcceptAsParameter(C, methods[i]))
127: && !methods[i].getName().equals(CLASSINV_STR)
128: && !methods[i].getName().endsWith(SPEC_STR) &&
129: // exclude main for now ...
130: !methods[i].getName().equals(MAIN_STR))
131: result.add(methods[i]);
132:
133: // now get public methods from C + superclasses:
134: methods = C.getMethods();
135: for (int i = 0; i < methods.length; i++)
136: if ((!result.contains(methods[i]))
137: && (!Modifier.isStatic(methods[i].getModifiers()) || ReflUtil
138: .canAcceptAsParameter(C, methods[i]))
139: &&
140: // exclude methods of "Object" ... they should be safe:
141: (methods[i].getDeclaringClass() != CLASS_OBJECT)
142: && !methods[i].getName().equals(CLASSINV_STR)
143: && !methods[i].getName().endsWith(SPEC_STR) &&
144: // exclude main for now ...
145: !methods[i].getName().equals(MAIN_STR))
146: result.add(methods[i]);
147:
148: return result;
149:
150: }
151:
152: /**
153: * Get the fields of a class that act as interface
154: * points. Currently this is all public fields of its
155: * superclasses + its own declared non-private fields.
156: */
157: public static List<Field> getFieldsIPs(Class C) {
158:
159: Class CLASS_OBJECT = (new Object()).getClass();
160:
161: List<Field> result = new LinkedList<Field>();
162: // first get C's own declared fields:
163: Field[] fields = C.getDeclaredFields();
164: for (int i = 0; i < fields.length; i++)
165: if (!Modifier.isPrivate(fields[i].getModifiers())
166: &&
167: // Ehm... for now excluding static fields:
168: !Modifier.isStatic(fields[i].getModifiers())
169: && !Modifier.isFinal(fields[i].getModifiers()))
170: result.add(fields[i]);
171:
172: // now get public fields from C + superclasses:
173: fields = C.getFields();
174: for (int i = 0; i < fields.length; i++)
175: if (!result.contains(fields[i])
176: && !Modifier.isStatic(fields[i].getModifiers())
177: && !Modifier.isFinal(fields[i].getModifiers()))
178: result.add(fields[i]);
179:
180: return result;
181:
182: }
183:
184: /**
185: * Construct a mapping such that for any method m of the class C
186: * that has m_spec as its specification, we add the pair
187: * (m,m_spec) to the mapping.
188: */
189: public static HashMap<Method, Method> mk_specMap(Class C,
190: List<Method> methods) {
191:
192: HashMap<Method, Method> map = new HashMap<Method, Method>();
193: for (Method m : methods) {
194: Method spec = null;
195: Class[] paramTypes = m.getParameterTypes();
196:
197: try {
198: spec = C.getMethod(m.getName() + SPEC_STR, paramTypes);
199: } catch (Exception e) {
200: continue;
201: }
202: ;
203:
204: map.put(m, spec);
205: }
206: ;
207:
208: return map;
209: }
210:
211: }
|