001 package serp.bytecode; 002 003 import junit.framework.*; 004 import junit.textui.*; 005 006 /** 007 * Base class for testing the handling of the {@link PrimitiveState} and 008 * {@link ArrayState}. Subclasses should set the {@link #_bc} member in 009 * their {@link TestCase#setUp} method. 010 * 011 * @author Abe White 012 */ 013 public abstract class AbstractStateTest extends TestCase { 014 protected Project _project = new Project(); 015 protected BCClass _bc = null; 016 017 public AbstractStateTest(String test) { 018 super(test); 019 } 020 021 /** 022 * Test the name and type operations. 023 */ 024 public abstract void testType(); 025 026 /** 027 * Test operations on the superclass. 028 */ 029 public abstract void testSuperclass(); 030 031 /** 032 * Test operations on the component type. 033 */ 034 public abstract void testComponent(); 035 036 /** 037 * Test the basics -- magic number, etc. 038 */ 039 public void testBasics() { 040 assertEquals(Constants.VALID_MAGIC, _bc.getMagic()); 041 try { 042 _bc.setMagic(1); 043 fail("Allowed set magic"); 044 } catch (UnsupportedOperationException uoe) { 045 } 046 047 assertEquals(Constants.MAJOR_VERSION, _bc.getMajorVersion()); 048 try { 049 _bc.setMajorVersion(1); 050 fail("Allowed set major version"); 051 } catch (UnsupportedOperationException uoe) { 052 } 053 054 assertEquals(Constants.MINOR_VERSION, _bc.getMinorVersion()); 055 try { 056 _bc.setMinorVersion(1); 057 fail("Allowed set minor version"); 058 } catch (UnsupportedOperationException uoe) { 059 } 060 061 assertEquals(Constants.ACCESS_PUBLIC | Constants.ACCESS_FINAL, 062 _bc.getAccessFlags()); 063 try { 064 _bc.setAccessFlags(1); 065 fail("Allowed set access flags"); 066 } catch (UnsupportedOperationException uoe) { 067 } 068 069 try { 070 _bc.getPool(); 071 fail("Allowed access constant pool"); 072 } catch (UnsupportedOperationException uoe) { 073 } 074 } 075 076 /** 077 * Test operations on interfaces. 078 */ 079 public void testInterfaces() { 080 assertEquals(0, _bc.getDeclaredInterfaceNames().length); 081 assertEquals(0, _bc.getInterfaceNames().length); 082 try { 083 _bc.declareInterface("foo"); 084 fail("Allowed declare interface"); 085 } catch (UnsupportedOperationException uoe) { 086 } 087 088 _bc.clearDeclaredInterfaces(); 089 assertTrue(!_bc.removeDeclaredInterface((String) null)); 090 assertTrue(!_bc.removeDeclaredInterface("foo")); 091 assertTrue(_bc.isInstanceOf(_bc.getName())); 092 assertTrue(!_bc.isInstanceOf("foo")); 093 } 094 095 /** 096 * Test operations on fields. 097 */ 098 public void testFields() { 099 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 }