Method: isTestClass(SClassDefinition)

1: import java.util.List;
2: import java.util.Vector;
3:
4: import org.overture.ast.definitions.SClassDefinition;
5: import org.overture.interpreter.runtime.*;
6: import org.overture.interpreter.values.ObjectValue;
7: import org.overture.interpreter.values.SetValue;
8: import org.overture.interpreter.values.Value;
9: import org.overture.interpreter.values.ValueSet;
10:
11: public class TestRunner
12: {
13:         public static Value collectTests(Value obj)
14:         {
15:                 List<String> tests = new Vector<String>();
16:                 ObjectValue instance = (ObjectValue) obj;
17:
18:                 if (ClassInterpreter.getInstance() instanceof ClassInterpreter)
19:                 {
20:                         for (SClassDefinition def : ((ClassInterpreter) ClassInterpreter.getInstance()).getClasses())
21:                         {
22:                                 if (def.getIsAbstract() || !isTestClass(def))
23:                                 {
24:                                         continue;
25:                                 }
26:                                 tests.add(def.getName().getName());
27:                         }
28:                 }
29:
30:                 Context mainContext = new StateContext(Interpreter.getInstance().getAssistantFactory(), instance.type.getLocation(), "reflection scope");
31:
32:                 mainContext.putAll(ClassInterpreter.getInstance().initialContext);
33:                 mainContext.setThreadState(ClassInterpreter.getInstance().initialContext.threadState.dbgp, ClassInterpreter.getInstance().initialContext.threadState.CPU);
34:
35:                 ValueSet vals = new ValueSet();
36:                 for (String value : tests)
37:                 {
38:                         try
39:                         {
40:                                 vals.add(ClassInterpreter.getInstance().evaluate("new " + value
41:                                                 + "()", mainContext));
42:                         } catch (Exception e)
43:                         {
44:                                 // TODO Auto-generated catch block
45:                                 e.printStackTrace();
46:                         }
47:                 }
48:
49:                 try
50:                 {
51:                         return new SetValue(vals);
52:                 }
53:                 catch (ValueException e)
54:                 {
55:                         return null;        // Not reached
56:                 }
57:         }
58:
59:         private static boolean isTestClass(SClassDefinition def)
60:         {
61:•                if (def.getIsAbstract() || def.getName().getName().equals("Test")
62:•                                || def.getName().getName().equals("TestCase")
63:•                                || def.getName().getName().equals("TestSuite"))
64:                 {
65:                         return false;
66:                 }
67:
68:•                if (checkForSuper(def, "TestSuite"))
69:                 {
70:                         // the implementation must be upgrade before this work.
71:                         // The upgrade should handle the static method for creatint the suire
72:                         return false;
73:                 }
74:
75:                 return checkForSuper(def, "Test");
76:         }
77:
78:         private static boolean checkForSuper(SClassDefinition def, String superName)
79:         {
80:                 for (SClassDefinition superDef : def.getSuperDefs())
81:                 {
82:                         if (superDef.getName().getName().equals(superName)
83:                                         || checkForSuper(superDef, superName))
84:                         {
85:                                 return true;
86:                         }
87:                 }
88:                 return false;
89:         }
90: }