View Javadoc

1   package serp.bytecode;
2   
3   import junit.framework.*;
4   import junit.textui.*;
5   
6   /**
7    * Base class for testing the handling of the {@link PrimitiveState} and
8    * {@link ArrayState}. Subclasses should set the {@link #_bc} member in
9    * their {@link TestCase#setUp} method.
10   *
11   * @author Abe White
12   */
13  public abstract class AbstractStateTest extends TestCase {
14      protected Project _project = new Project();
15      protected BCClass _bc = null;
16  
17      public AbstractStateTest(String test) {
18          super(test);
19      }
20  
21      /**
22       * Test the name and type operations.
23       */
24      public abstract void testType();
25  
26      /**
27       * Test operations on the superclass.
28       */
29      public abstract void testSuperclass();
30  
31      /**
32       * Test operations on the component type.
33       */
34      public abstract void testComponent();
35  
36      /**
37       * Test the basics -- magic number, etc.
38       */
39      public void testBasics() {
40          assertEquals(Constants.VALID_MAGIC, _bc.getMagic());
41          try {
42              _bc.setMagic(1);
43              fail("Allowed set magic");
44          } catch (UnsupportedOperationException uoe) {
45          }
46  
47          assertEquals(Constants.MAJOR_VERSION, _bc.getMajorVersion());
48          try {
49              _bc.setMajorVersion(1);
50              fail("Allowed set major version");
51          } catch (UnsupportedOperationException uoe) {
52          }
53  
54          assertEquals(Constants.MINOR_VERSION, _bc.getMinorVersion());
55          try {
56              _bc.setMinorVersion(1);
57              fail("Allowed set minor version");
58          } catch (UnsupportedOperationException uoe) {
59          }
60  
61          assertEquals(Constants.ACCESS_PUBLIC | Constants.ACCESS_FINAL,
62              _bc.getAccessFlags());
63          try {
64              _bc.setAccessFlags(1);
65              fail("Allowed set access flags");
66          } catch (UnsupportedOperationException uoe) {
67          }
68  
69          try {
70              _bc.getPool();
71              fail("Allowed access constant pool");
72          } catch (UnsupportedOperationException uoe) {
73          }
74      }
75  
76      /**
77       * Test operations on interfaces.
78       */
79      public void testInterfaces() {
80          assertEquals(0, _bc.getDeclaredInterfaceNames().length);
81          assertEquals(0, _bc.getInterfaceNames().length);
82          try {
83              _bc.declareInterface("foo");
84              fail("Allowed declare interface");
85          } catch (UnsupportedOperationException uoe) {
86          }
87  
88          _bc.clearDeclaredInterfaces();
89          assertTrue(!_bc.removeDeclaredInterface((String) null));
90          assertTrue(!_bc.removeDeclaredInterface("foo"));
91          assertTrue(_bc.isInstanceOf(_bc.getName()));
92          assertTrue(!_bc.isInstanceOf("foo"));
93      }
94  
95      /**
96       * Test operations on fields.
97       */
98      public void testFields() {
99          assertEquals(0, _bc.getDeclaredFields().length);
100         assertEquals(0, _bc.getFields().length);
101         try {
102             _bc.declareField("foo", int.class);
103             fail("Allowed declare field");
104         } catch (UnsupportedOperationException uoe) {
105         }
106 
107         _bc.clearDeclaredFields();
108         assertTrue(!_bc.removeDeclaredField((String) null));
109         assertTrue(!_bc.removeDeclaredField("foo"));
110     }
111 
112     /**
113      * Test operations on methods.
114      */
115     public void testMethods() {
116         assertEquals(0, _bc.getDeclaredMethods().length);
117         try {
118             _bc.declareMethod("foo", int.class, null);
119             fail("Allowed declare method");
120         } catch (UnsupportedOperationException uoe) {
121         }
122 
123         _bc.clearDeclaredMethods();
124         assertTrue(!_bc.removeDeclaredMethod((String) null));
125         assertTrue(!_bc.removeDeclaredMethod("foo"));
126         try {
127             _bc.addDefaultConstructor();
128             fail("Allowed add default constructor");
129         } catch (UnsupportedOperationException uoe) {
130         }
131     }
132 
133     /**
134      * Test operations on attributes.
135      */
136     public void testAttributes() {
137         assertNull(_bc.getSourceFile(false));
138         try {
139             _bc.getSourceFile(true);
140             fail("Allowed add source file");
141         } catch (UnsupportedOperationException uoe) {
142         }
143 
144         assertNull(_bc.getInnerClasses(false));
145         try {
146             _bc.getInnerClasses(true);
147             fail("Allowed add inner classes");
148         } catch (UnsupportedOperationException uoe) {
149         }
150 
151         assertTrue(!_bc.isDeprecated());
152         try {
153             _bc.setDeprecated(true);
154             fail("Allowed set deprecated");
155         } catch (UnsupportedOperationException uoe) {
156         }
157 
158         assertEquals(0, _bc.getAttributes().length);
159         _bc.clearAttributes();
160         assertTrue(!_bc.removeAttribute(Constants.ATTR_SYNTHETIC));
161         try {
162             _bc.addAttribute(Constants.ATTR_SYNTHETIC);
163             fail("Allowed add attribute");
164         } catch (UnsupportedOperationException uoe) {
165         }
166     }
167 
168     /**
169      * Tests that these types cannot be written.
170      */
171     public void testWrite() {
172         try {
173             _bc.toByteArray();
174         } catch (UnsupportedOperationException uoe) {
175         }
176     }
177 }