Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jreversepro/tester/TestBase.java


1   package jreversepro.tester;
2   
3   import java.io.File;
4   
5   import java.lang.reflect.*;
6   
7   /**
8    *  A base class to run tests around.  Modelled slightly after junit.  I'd like to
9    *  rewrite with junit later.  Maybe...
10   */
11  public abstract class TestBase
12  {
13      private Class       originalClass = null;
14      private Class       newClass      = null;
15  
16      private TestContext context       = null;
17  
18  
19  
20      /**
21       *  the name of the class we are testing
22       */
23  
24      public abstract String getClassName();
25  
26  
27      /**
28       *  perform the actual tests on either to original
29       *  class or the new class
30       */
31  
32      public abstract void test(Class classtotest);
33  
34  
35      /**
36       * Describe <code>getContext</code> method here.
37       *
38       * @return a <code>TestContext</code> value
39       */
40  
41      public TestContext getContext() {
42          return context;
43      }
44  
45  
46      /**
47       * Describe <code>setContext</code> method here.
48       *
49       * @param context a <code>TestContext</code> value
50       */
51      
52      public void setContext(TestContext context) {
53          this.context = context;
54      }
55  
56  
57      /**
58       * Describe <code>getOriginalClass</code> method here.
59       *
60       * @return a <code>Class</code> value
61       */
62  
63      public Class getOriginalClass() {
64          return originalClass;
65      }
66  
67      
68      /**
69       * Describe <code>setNewClass</code> method here.
70       *
71       * @return a <code>Class</code> value
72       */
73  
74      public Class getNewClass() {
75          return newClass;
76      }
77  
78  
79      /**
80       *  initialize for the tests to run
81       *  1 - load original class
82       *  2 - decompile / recompile class and load
83       */
84      public void init() {
85          File tmpdir = new File(getContext().getCompileDir());
86          tmpdir.mkdir();
87          
88          String origclassfile = getContext().getTestClassDir() + File.separatorChar + getClassName() + ".class";
89          String javafile      = getContext().getCompileDir()   + File.separatorChar + getClassName() + ".java";
90  
91          log("DECOMPILE " + origclassfile + " to " + javafile);
92          TestHelper.decompile(origclassfile, javafile);
93      
94          log("COMPILE " + getClassName() + " : " +
95              TestHelper.compile(getContext().getCompileDir(), javafile));
96          
97          originalClass = TestHelper.loadClass(getContext().getTestClassDir(), getClassName());
98          newClass      = TestHelper.loadClass(getContext().getCompileDir(),   getClassName());
99          
100         log("LOADED: orig=" + originalClass);
101         log("LOADED: new="  + newClass);
102         
103     }
104    
105 
106     /**
107      *  kill the classes.  maybe delete the intermediate files?
108      */
109     public void teardown() {
110         originalClass = null;
111         newClass      = null;
112     }
113 
114     /**
115      *  log a message to the test log
116      *
117      *  @param message the message to log
118      */
119     public void log(String message) {
120         System.out.println("[" + getClassName() + "] " + message);
121     }
122 
123 
124 
125     public int invokeInt(Object o, String method) {
126         Method found = null;
127 
128         try {
129             Method[] m = o.getClass().getMethods();
130 
131             for (int i=0; i<m.length; i++) {
132                 if (!m[i].getName().equals(method))         continue;
133                 if (Modifier.isStatic(m[i].getModifiers())) continue;
134                 if (m[i].getReturnType() != Integer.TYPE)   continue;
135                 if (m[i].getParameterTypes().length > 0)    continue;
136                 found = m[i];
137                 break;
138             }                              
139         } catch (Exception e) {
140             log("invokeInt -- " + e.getMessage());
141         }
142 
143         if (found == null) {
144             log("couldn't find int method " + method);
145             return -1;  // need better control flow!
146         }
147 
148         try {
149             return ((Integer) found.invoke(o, null)).intValue();
150         } catch (Exception e) {
151             log("Error invoking " + method + ": " + e.getMessage());
152             return -1;  // again - flow here is pathetic.  junit will help
153         }
154     }
155 
156 }