001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005:
006: package org.terracotta.modules.surefire_2_3;
007:
008: import com.tc.asm.ClassAdapter;
009: import com.tc.asm.ClassVisitor;
010: import com.tc.asm.MethodAdapter;
011: import com.tc.asm.MethodVisitor;
012: import com.tc.asm.Opcodes;
013: import com.tc.object.bytecode.ClassAdapterFactory;
014:
015: public class JUnitTestSuiteAdapter extends ClassAdapter implements
016: ClassAdapterFactory, Opcodes {
017:
018: public static final String CLUSTERED_JUNIT_BARRIER_CLASS = "org.terracotta.modules.surefire_2_3.JUnitBarrier";
019:
020: public JUnitTestSuiteAdapter() {
021: super (null);
022: }
023:
024: public JUnitTestSuiteAdapter(ClassVisitor cv) {
025: super (cv);
026: }
027:
028: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
029: return new JUnitTestSuiteAdapter(visitor);
030: }
031:
032: public MethodVisitor visitMethod(int access, String name,
033: String desc, String signature, String[] exceptions) {
034: MethodVisitor mv = super .visitMethod(access, name, desc,
035: signature, exceptions);
036: if (!name.equals("runTest")) {
037: return mv;
038: }
039:
040: return new MethodAdapter(mv) {
041: public void visitCode() {
042: super .visitCode();
043: mv.visitVarInsn(ALOAD, 1);
044: mv
045: .visitMethodInsn(INVOKESTATIC,
046: CLUSTERED_JUNIT_BARRIER_CLASS.replace(
047: '.', '/'),
048: "createBarrierAndWait",
049: "(Ljunit/framework/Test;)V");
050: }
051: };
052: }
053:
054: // private void addRunTest() {
055: // MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "runTest",
056: // "(Ljunit/framework/Test;Ljunit/framework/TestResult;)V",
057: // null, null);
058: // mv.visitCode();
059: // Label l0 = new Label();
060: // mv.visitLabel(l0);
061: // mv.visitVarInsn(ALOAD, 1);
062: // mv.visitMethodInsn(INVOKESTATIC,
063: // "org/terracotta/modules/surefire_2_3/JUnitBarrier", "createBarrierAndWait",
064: // "(Ljunit/framework/Test;)V");
065: //
066: // Label l1 = new Label();
067: // mv.visitLabel(l1);
068: // mv.visitVarInsn(ALOAD, 1);
069: // mv.visitVarInsn(ALOAD, 2);
070: // mv.visitMethodInsn(INVOKEINTERFACE, "junit/framework/Test", "run",
071: // "(Ljunit/framework/TestResult;)V");
072: // Label l2 = new Label();
073: // mv.visitLabel(l2);
074: // mv.visitInsn(RETURN);
075: // Label l3 = new Label();
076: // mv.visitLabel(l3);
077: // mv.visitLocalVariable("this", "Ljunit/framework/TestSuite;", null, l0, l3,
078: // 0);
079: // mv.visitLocalVariable("test", "Ljunit/framework/Test;", null, l0, l3, 1);
080: // mv.visitLocalVariable("result", "Ljunit/framework/TestResult;", null, l0,
081: // l3, 2);
082: // mv.visitMaxs(0, 0);
083: // mv.visitEnd();
084: // }
085:
086: /**
087: * <pre>
088: * private static void createBarrierAndWait(Test t) {
089: * int numberOfNodes;
090: * try {
091: * numberOfNodes = Integer.parseInt(System
092: * .getProperty("tc.numberOfNodes", "0"));
093: * } catch (Exception ex) {
094: * numberOfNodes = 0;
095: * }
096: * if (numberOfNodes == 0) {
097: * return;
098: * }
099: *
100: * String testName = t.getClass().getName();
101: * if (t instanceof TestCase) {
102: * testName = testName + ((TestCase) t).getName();
103: * }
104: *
105: * String globalLock = "@junit_test_suite_lock";
106: *
107: * CyclicBarrier barrier;
108: *
109: * ManagerUtil.beginLock(globalLock, Manager.LOCK_TYPE_WRITE);
110: * try {
111: * barrier = (CyclicBarrier) ManagerUtil.lookupOrCreateRoot("barrier:"
112: * + testName, new CyclicBarrier(numberOfNodes));
113: * } finally {
114: * ManagerUtil.commitLock(globalLock);
115: * }
116: *
117: * try {
118: * barrier.barrier();
119: * } catch (Exception e) {
120: * e.printStackTrace();
121: * }
122: * }
123: * </pre>
124: */
125: // private void addCreateBarrierAndWait() {
126: // MethodVisitor mv = cv.visitMethod(ACC_PRIVATE + ACC_STATIC,
127: // "createBarrierAndWait", "(Ljunit/framework/Test;)V", null, null);
128: // mv.visitCode();
129: // Label l0 = new Label();
130: // Label l1 = new Label();
131: // Label l2 = new Label();
132: // mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
133: // Label l3 = new Label();
134: // Label l4 = new Label();
135: // mv.visitTryCatchBlock(l3, l4, l4, null);
136: // Label l5 = new Label();
137: // Label l6 = new Label();
138: // mv.visitTryCatchBlock(l5, l6, l4, null);
139: // Label l7 = new Label();
140: // Label l8 = new Label();
141: // mv.visitTryCatchBlock(l6, l7, l8, "java/lang/Exception");
142: // mv.visitLabel(l0);
143: // mv.visitLdcInsn("tc.numberOfNodes");
144: // mv.visitLdcInsn("0");
145: // mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "getProperty",
146: // "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
147: // mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt",
148: // "(Ljava/lang/String;)I");
149: // mv.visitVarInsn(ISTORE, 1);
150: // mv.visitLabel(l1);
151: // Label l9 = new Label();
152: // mv.visitJumpInsn(GOTO, l9);
153: // mv.visitLabel(l2);
154: // mv.visitVarInsn(ASTORE, 2);
155: // Label l10 = new Label();
156: // mv.visitLabel(l10);
157: // mv.visitInsn(ICONST_0);
158: // mv.visitVarInsn(ISTORE, 1);
159: // mv.visitLabel(l9);
160: // mv.visitVarInsn(ILOAD, 1);
161: // Label l11 = new Label();
162: // mv.visitJumpInsn(IFNE, l11);
163: // Label l12 = new Label();
164: // mv.visitLabel(l12);
165: // mv.visitInsn(RETURN);
166: // mv.visitLabel(l11);
167: // mv.visitVarInsn(ALOAD, 0);
168: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass",
169: // "()Ljava/lang/Class;");
170: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName",
171: // "()Ljava/lang/String;");
172: // mv.visitVarInsn(ASTORE, 2);
173: // Label l13 = new Label();
174: // mv.visitLabel(l13);
175: // mv.visitVarInsn(ALOAD, 0);
176: // mv.visitTypeInsn(INSTANCEOF, "junit/framework/TestCase");
177: // Label l14 = new Label();
178: // mv.visitJumpInsn(IFEQ, l14);
179: // Label l15 = new Label();
180: // mv.visitLabel(l15);
181: // mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
182: // mv.visitInsn(DUP);
183: // mv.visitVarInsn(ALOAD, 2);
184: // mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf",
185: // "(Ljava/lang/Object;)Ljava/lang/String;");
186: // mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>",
187: // "(Ljava/lang/String;)V");
188: // mv.visitVarInsn(ALOAD, 0);
189: // mv.visitTypeInsn(CHECKCAST, "junit/framework/TestCase");
190: // mv.visitMethodInsn(INVOKEVIRTUAL, "junit/framework/TestCase", "getName",
191: // "()Ljava/lang/String;");
192: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
193: // "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
194: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString",
195: // "()Ljava/lang/String;");
196: // mv.visitVarInsn(ASTORE, 2);
197: // mv.visitLabel(l14);
198: // mv.visitLdcInsn("@junit_test_suite_lock");
199: // mv.visitVarInsn(ASTORE, 3);
200: // Label l16 = new Label();
201: // mv.visitLabel(l16);
202: // mv.visitVarInsn(ALOAD, 3);
203: // mv.visitInsn(ICONST_2);
204: // mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil",
205: // "beginLock", "(Ljava/lang/String;I)V");
206: // mv.visitLabel(l3);
207: // mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
208: // mv.visitInsn(DUP);
209: // mv.visitLdcInsn("barrier:");
210: // mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>",
211: // "(Ljava/lang/String;)V");
212: // mv.visitVarInsn(ALOAD, 2);
213: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
214: // "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
215: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString",
216: // "()Ljava/lang/String;");
217: // mv.visitTypeInsn(NEW, "EDU/oswego/cs/dl/util/concurrent/CyclicBarrier");
218: // mv.visitInsn(DUP);
219: // mv.visitVarInsn(ILOAD, 1);
220: // mv.visitMethodInsn(INVOKESPECIAL,
221: // "EDU/oswego/cs/dl/util/concurrent/CyclicBarrier", "<init>", "(I)V");
222: // mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil",
223: // "lookupOrCreateRoot",
224: // "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;");
225: // mv.visitTypeInsn(CHECKCAST,
226: // "EDU/oswego/cs/dl/util/concurrent/CyclicBarrier");
227: // mv.visitVarInsn(ASTORE, 4);
228: // Label l17 = new Label();
229: // mv.visitLabel(l17);
230: // mv.visitJumpInsn(GOTO, l5);
231: // mv.visitLabel(l4);
232: // mv.visitVarInsn(ASTORE, 6);
233: // Label l18 = new Label();
234: // mv.visitJumpInsn(JSR, l18);
235: // Label l19 = new Label();
236: // mv.visitLabel(l19);
237: // mv.visitVarInsn(ALOAD, 6);
238: // mv.visitInsn(ATHROW);
239: // mv.visitLabel(l18);
240: // mv.visitVarInsn(ASTORE, 5);
241: // Label l20 = new Label();
242: // mv.visitLabel(l20);
243: // mv.visitVarInsn(ALOAD, 3);
244: // mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil",
245: // "commitLock", "(Ljava/lang/String;)V");
246: // Label l21 = new Label();
247: // mv.visitLabel(l21);
248: // mv.visitVarInsn(RET, 5);
249: // mv.visitLabel(l5);
250: // mv.visitJumpInsn(JSR, l18);
251: // mv.visitLabel(l6);
252: // mv.visitVarInsn(ALOAD, 4);
253: // mv.visitMethodInsn(INVOKEVIRTUAL,
254: // "EDU/oswego/cs/dl/util/concurrent/CyclicBarrier", "barrier", "()I");
255: // mv.visitInsn(POP);
256: // mv.visitLabel(l7);
257: // Label l22 = new Label();
258: // mv.visitJumpInsn(GOTO, l22);
259: // mv.visitLabel(l8);
260: // mv.visitVarInsn(ASTORE, 5);
261: // Label l23 = new Label();
262: // mv.visitLabel(l23);
263: // mv.visitVarInsn(ALOAD, 5);
264: // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace",
265: // "()V");
266: // mv.visitLabel(l22);
267: // mv.visitInsn(RETURN);
268: // Label l24 = new Label();
269: // mv.visitLabel(l24);
270: // mv.visitLocalVariable("t", "Ljunit/framework/Test;", null, l0, l24, 0);
271: // mv.visitLocalVariable("numberOfNodes", "I", null, l1, l2, 1);
272: // mv.visitLocalVariable("numberOfNodes", "I", null, l9, l24, 1);
273: // mv.visitLocalVariable("ex", "Ljava/lang/Exception;", null, l10, l9, 2);
274: // mv.visitLocalVariable("testName", "Ljava/lang/String;", null, l13, l24, 2);
275: // mv.visitLocalVariable("globalLock", "Ljava/lang/String;", null, l16, l24,
276: // 3);
277: // mv.visitLocalVariable("barrier",
278: // "LEDU/oswego/cs/dl/util/concurrent/CyclicBarrier;", null, l17, l4, 4);
279: // mv.visitLocalVariable("barrier",
280: // "LEDU/oswego/cs/dl/util/concurrent/CyclicBarrier;", null, l6, l24, 4);
281: // mv.visitLocalVariable("e", "Ljava/lang/Exception;", null, l23, l22, 5);
282: // mv.visitMaxs(4, 7);
283: // mv.visitEnd();
284: // }
285: }
|