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

Quick Search    Search Deep

Source code: com/simscomputing/testbed/ClearBoxTest.java


1   package com.simscomputing.testbed;
2   
3   import java.lang.reflect.Method;
4   
5   /**
6   Unit test for the ClearBox class.
7   
8   @author David Sims
9   @version $Revision: 1.1.1.1 $ $Date: 2000/02/21 21:22:33 $ */
10  public class ClearBoxTest extends TestCaseCollection {
11    /**
12    A trivial test.
13    */
14    public void runTestTrivial() {
15    }
16  
17    public void runTestFailure() {
18  //    assert(false, "x is 23");
19    }
20  
21  //  public void runTestAbnormalFailure() {
22  //    throw new NullPointerException();
23  //  }
24  
25    public void runTestTrivial2() {
26    }
27  
28    public void runTestGetField() {
29      try {
30        PrivateClass privateClass = new PrivateClass(17);
31        // privateClass.aPrivateField = 17; cannot normally access 'aPrivateField'
32        Integer integer = (Integer) ClearBox.getField(privateClass, "privateInt");
33        assert(integer.intValue() == 17, "the private field should be 17");
34      } // try
35      catch (NoSuchFieldException e) {
36        assert(false, "the private field should be reached through the ClearBox class");
37      } // catch
38    } // runTestGetField()
39  
40    public void runTestSetField() {
41      try {
42        PrivateClass privateClass = new PrivateClass(17);
43        // privateClass.aPrivateField = 17; cannot normally access 'aPrivateField'
44        Integer integer = (Integer) ClearBox.getField(privateClass, "privateInt");
45        assert(integer.intValue() == 17, "the private field should be 17");
46  
47        // now change the value to something else
48        ClearBox.setField(privateClass, "privateInt", new Integer(42));
49  
50        // finally, verify that the new value has been assigned
51        integer = (Integer) ClearBox.getField(privateClass, "privateInt");
52        assert(integer.intValue() == 42, "the private field should be 42");
53      } // try
54      catch (NoSuchFieldException e) {
55        assert(false, "the private field should be reached through the ClearBox class");
56      } // catch
57    } // runTestSetField()
58  
59  //    public void runTestConstructor() {
60  //      try {
61  //        // PrivateClass privateClass = new PrivateClass(); cannot normally access this private constructor
62  //        Object newInstance = ClearBox.createNewInstance("pc", null, null);
63  
64  //        pc privateClass = (pc) newInstance;
65  //        assert(privateClass.getValue() == -1, "should be able to create the new object and access its fields");
66  //      } // try
67  //      catch (Exception e) {
68  //        e.printStackTrace();
69  //        assert(false, "could not access private constructor");
70  //      } // catch
71  //    } // runTestField()
72  
73  //    public void runTestMethod() {
74  //      pc privateClass = new pc(42);
75  //      // privateClass.getValue(); cannot access private method
76  
77  //      try {
78  //        Method privateMethod = ClearBox.getMethod(privateClass, "getClassxxxxxxxxx", null);
79  //        Class c = (Class) privateMethod.invoke(privateClass, null);
80  //        System.out.println("c is " + c);
81  //  //        assert(result.intValue() == 42, "values should match");
82  //      } // try
83  //      catch (Exception e) {
84  //        e.printStackTrace();
85  //        assert(false, "could not access private constructor");
86  //      } // catch
87  //    }
88  
89    public static class PrivateClass {
90      private int privateInt;
91  
92      public PrivateClass(int newInt) {
93        privateInt = newInt;
94      } // constructor
95  
96      public int getPrivateInt() {
97        return privateInt;
98      } // getPrivateInt()
99    } // class PrivateClass
100 } // class ClearBoxTest