001: // $Id$
002: // =====================================================================
003: //
004: // (history at end)
005: //
006:
007: package ch.ethz.prose;
008:
009: // used packages
010: import junit.framework.*;
011: import java.lang.reflect.*;
012: import java.lang.ref.WeakReference;
013: import java.util.Collection;
014:
015: //import ch.ethz.jvmai.MethodWeaver;
016: //import ch.ethz.jvmai.ClassWeaver;
017: import ch.ethz.inf.iks.jvmai.jvmdi.HotSwapClassRegister; //import ch.ethz.inf.iks.jvmai.jvmdi.UniqueIdArrayManager;
018: import ch.ethz.inf.iks.jvmai.jvmdi.HotSwapAspectInterfaceImpl;
019: import ch.ethz.inf.iks.jvmai.jvmdi.HotSwapFieldWeaver;
020:
021: /**
022: * JUnit testcase for class HotSwapClassRegister (HotSwap advice weaving).
023: *
024: * @version $Revision$
025: * @author Gerald Linhofer
026: * @author Angela Nicoara
027: */
028: public class HotSwapClassRegisterTest extends TestCase {
029:
030: Object dummyField = null;
031: Object dummyField2 = null;
032:
033: public Object dummyMethod1() {
034: return dummyField;
035: }
036:
037: public void dummyMethod2(Object newValue) {
038: dummyField = newValue;
039: }
040:
041: HotSwapClassRegister classReg = null;
042:
043: /**
044: * Construct test with given name.
045: * @param name test name
046: */
047: public HotSwapClassRegisterTest(String name) {
048: super (name);
049: }
050:
051: /**
052: * Set up fixture.
053: */
054: protected void setUp() {
055: classReg = HotSwapClassRegister.getInstance();
056: }
057:
058: /**
059: * Clean up.
060: */
061: protected void tearDown() {
062: try {
063: classReg.reset();
064: } catch (Exception e) {
065: fail("HotSwapClassRegister.reset()");
066: }
067: }
068:
069: public void test_010_reset() {
070: // reset when there are no weavers
071: try {
072: classReg.reset();
073: } catch (Exception e) {
074: fail("reset");
075: }
076: }
077:
078: public void test_020_registration() {
079: // register null
080: int exceptions = 0;
081: try {
082: classReg.registrationRequest((Class) null);
083: } catch (NullPointerException e) {
084: exceptions++;
085: } catch (ClassNotFoundException e) {
086: fail("register(null) throws ClassNotFoundException");
087: }
088: try {
089: classReg.registrationRequest((Package) null);
090: } catch (NullPointerException e) {
091: exceptions++;
092: }
093: try {
094: classReg.registrationRequestSubClasses(null);
095: } catch (NullPointerException e) {
096: exceptions++;
097: }
098: assertEquals("Number of exceptions", 3, exceptions);
099:
100: // register this
101: try {
102: classReg.registrationRequest(this .getClass());
103: } catch (Exception e) {
104: fail("register " + e.getClass().getName());
105: }
106: // see if field accesses for dummyField where found
107: Method method = null;
108: Class klass = null;
109: try {
110: Collection col = (Collection) HotSwapClassRegister.knownFieldReferences
111: .get(this .getClass().getName()
112: + "#dummyField#Ljava/lang/Object;");
113: klass = (Class) ((WeakReference) col.iterator().next())
114: .get();
115: } catch (NullPointerException e) {
116: fail("field access: field not registered");
117: } catch (java.util.NoSuchElementException e) {
118: fail("field access: method not registered");
119: }
120: assertNotNull("get field accessor returned null method", klass);
121: assertEquals("get field accessor return wrong method", this
122: .getClass(), klass);
123:
124: // register sub classes
125: try {
126: classReg.registrationRequestSubClasses(Aspect.class);
127: } catch (Exception e) {
128: fail("register sub classes " + e.getClass().getName());
129: }
130:
131: // register package
132: try {
133: classReg.registrationRequest(this .getClass().getPackage());
134: } catch (Exception e) {
135: fail("register package " + e.getClass().getName());
136: }
137:
138: // registerAll
139: try {
140: classReg.registrationRequestAll();
141: } catch (Exception e) {
142: fail("registerAll " + e.getClass().getName());
143: }
144:
145: // unregister
146: try {
147: classReg.removeRequest(this .getClass());
148: } catch (Exception e) {
149: fail("unRegister class " + e.getClass().getName());
150: }
151: try {
152: classReg.removeRequest(this .getClass().getPackage());
153: } catch (Exception e) {
154: fail("unRegister package " + e.getClass().getName());
155: }
156: try {
157: classReg.removeRequestSubClass(Aspect.class);
158: } catch (Exception e) {
159: fail("unRegisterSubClass class " + e.getClass().getName());
160: }
161: try {
162: classReg.removeRequestAll();
163: } catch (Exception e) {
164: fail("unRegisterAll " + e.getClass().getName());
165: }
166:
167: // unregister when nothing is registered
168: try {
169: classReg.removeRequest(this .getClass());
170: } catch (Exception e) {
171: fail("unRegister class (nothing registered)"
172: + e.getClass().getName());
173: }
174: try {
175: classReg.removeRequest(this .getClass().getPackage());
176: } catch (Exception e) {
177: fail("unRegister package (nothing registered)"
178: + e.getClass().getName());
179: }
180: try {
181: classReg.removeRequestSubClass(Aspect.class);
182: } catch (Exception e) {
183: fail("unRegisterSubClass class " + e.getClass().getName());
184: }
185: try {
186: classReg.removeRequestAll();
187: } catch (Exception e) {
188: fail("unRegisterAll (nothing registered)"
189: + e.getClass().getName());
190: }
191: }
192:
193: /**
194: * Test suite.
195: * @return test instance
196: */
197: public static Test suite() {
198: return new TestSuite(HotSwapClassRegisterTest.class);
199: }
200: }
201:
202: //======================================================================
203: //
204: // $Log$
205: //
|