aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/src/test
diff options
context:
space:
mode:
authorAndy Clement <aclement@pivotal.io>2019-01-24 12:02:42 -0800
committerAndy Clement <aclement@pivotal.io>2019-01-24 12:02:42 -0800
commite01e4369b4c60775d679d8de678f54097fdc3120 (patch)
tree9fec1dc58d902e4850f45def0f6beb44254df3da /runtime/src/test
parent38a5e6c8f6ac969a71b91de3a572fdcd4fb22b1b (diff)
downloadaspectj-e01e4369b4c60775d679d8de678f54097fdc3120.tar.gz
aspectj-e01e4369b4c60775d679d8de678f54097fdc3120.zip
mavenizing runtime module - complete
Diffstat (limited to 'runtime/src/test')
-rw-r--r--runtime/src/test/java/org/aspectj/runtime/RuntimeModuleTest.java77
-rw-r--r--runtime/src/test/java/org/aspectj/runtime/reflect/JoinPointImplTest.java42
-rw-r--r--runtime/src/test/java/org/aspectj/runtime/reflect/RuntimePerformanceTest.java111
-rw-r--r--runtime/src/test/java/org/aspectj/runtime/reflect/SignatureTest.java57
4 files changed, 287 insertions, 0 deletions
diff --git a/runtime/src/test/java/org/aspectj/runtime/RuntimeModuleTest.java b/runtime/src/test/java/org/aspectj/runtime/RuntimeModuleTest.java
new file mode 100644
index 000000000..97e1750d8
--- /dev/null
+++ b/runtime/src/test/java/org/aspectj/runtime/RuntimeModuleTest.java
@@ -0,0 +1,77 @@
+package org.aspectj.runtime;
+/* *******************************************************************
+ * Copyright (c) 1999-2001 Xerox Corporation,
+ * 2002 Palo Alto Research Center, Incorporated (PARC).
+ * All rights reserved.
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Xerox/PARC initial implementation
+ * ******************************************************************/
+
+import java.io.*;
+
+import org.aspectj.lang.*;
+import org.aspectj.runtime.reflect.JoinPointImplTest;
+import org.aspectj.runtime.reflect.RuntimePerformanceTest;
+import org.aspectj.runtime.reflect.SignatureTest;
+
+import junit.framework.*;
+
+public class RuntimeModuleTest extends TestCase {
+
+ public RuntimeModuleTest(String name) { super(name); }
+
+ public void testNoAspectBoundException() {
+ RuntimeException fun = new RuntimeException("fun");
+ NoAspectBoundException nab = new NoAspectBoundException("Foo", fun);
+ assertEquals(fun,nab.getCause());
+ }
+
+ public void testSoftExceptionPrintStackTrace() {
+ // let's see
+// Throwable t = new Error("xyz");
+// new SoftException(t).printStackTrace();
+
+ // save to specified PrintStream
+ ByteArrayOutputStream sink = new ByteArrayOutputStream();
+ PrintStream out = new PrintStream(sink);
+ new SoftException(new Error("xyz")).printStackTrace(out);
+ String s = new String(sink.toByteArray());
+ out.flush();
+ checkSoftExceptionString(s);
+
+ // save to specified PrintWriter
+ sink = new ByteArrayOutputStream();
+ PrintWriter pout = new PrintWriter(sink);
+ new SoftException(new Error("xyz")).printStackTrace(pout);
+ pout.flush();
+ s = new String(sink.toByteArray());
+ checkSoftExceptionString(s);
+
+ // check System.err redirect
+ PrintStream systemErr = System.err;
+ try {
+ sink = new ByteArrayOutputStream();
+ out = new PrintStream(sink);
+ System.setErr(out);
+ new SoftException(new Error("xyz")).printStackTrace();
+ out.flush();
+ s = new String(sink.toByteArray());
+ checkSoftExceptionString(s);
+ } finally {
+ System.setErr(systemErr);
+ }
+ }
+
+
+ static void checkSoftExceptionString(String s) {
+ assertTrue(-1 != s.indexOf("SoftException"));
+ assertTrue(-1 != s.indexOf("Caused by: java.lang.Error"));
+ assertTrue(-1 != s.indexOf("xyz"));
+ assertTrue(-1 != s.indexOf("testSoftExceptionPrintStackTrace"));
+ }
+}
diff --git a/runtime/src/test/java/org/aspectj/runtime/reflect/JoinPointImplTest.java b/runtime/src/test/java/org/aspectj/runtime/reflect/JoinPointImplTest.java
new file mode 100644
index 000000000..2a17ad998
--- /dev/null
+++ b/runtime/src/test/java/org/aspectj/runtime/reflect/JoinPointImplTest.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.aspectj.runtime.reflect;
+
+import junit.framework.TestCase;
+
+/**
+ * @author colyer
+ *
+ */
+public class JoinPointImplTest extends TestCase {
+
+ public void testGetArgs() {
+ String arg1 = "abc";
+ StringBuffer arg2 = new StringBuffer("def");
+ Object arg3 = new Object();
+ Object[] args = new Object[] { arg1, arg2, arg3 };
+ JoinPointImpl jpi = new JoinPointImpl(null,null,null,args);
+
+ Object[] retrievedArgs = jpi.getArgs();
+ assertEquals("First arg unchanged",arg1,retrievedArgs[0]);
+ assertEquals("Second arg unchanged",arg2,retrievedArgs[1]);
+ assertEquals("Third arg unchanged",arg3,retrievedArgs[2]);
+ retrievedArgs[0] = "xyz";
+ ((StringBuffer)retrievedArgs[1]).append("ghi");
+ retrievedArgs[2] = "jkl";
+ Object[] afterUpdateArgs = jpi.getArgs();
+ assertEquals("Object reference not changed",arg1,afterUpdateArgs[0]);
+ assertEquals("Object reference unchanged",arg2,afterUpdateArgs[1]);
+ assertEquals("state of referenced object updated","defghi",afterUpdateArgs[1].toString());
+ assertEquals("Object reference not changed",arg3,afterUpdateArgs[2]);
+ }
+
+}
diff --git a/runtime/src/test/java/org/aspectj/runtime/reflect/RuntimePerformanceTest.java b/runtime/src/test/java/org/aspectj/runtime/reflect/RuntimePerformanceTest.java
new file mode 100644
index 000000000..25959aebc
--- /dev/null
+++ b/runtime/src/test/java/org/aspectj/runtime/reflect/RuntimePerformanceTest.java
@@ -0,0 +1,111 @@
+/*******************************************************************************
+ * Copyright (c) 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Matthew Webster - initial implementation
+ *******************************************************************************/
+package org.aspectj.runtime.reflect;
+
+import java.lang.reflect.Method;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.aspectj.lang.Signature;
+
+import junit.framework.TestCase;
+
+public class RuntimePerformanceTest extends TestCase {
+
+ private static final Timer timer = new Timer(true);
+ private static final long TIMEOUT = 10000;
+ private static final long ITERATIONS = 1000000;
+ private static final long WARMUP_ITERATIONS = 10000;
+ private static final long EXPECTED_RATIO = 8;
+ private static final Factory factory = new Factory("RutimePerformanceTest.java",RuntimePerformanceTest.class);
+
+ private boolean savedUseCaches;
+ private Method method;
+ private Signature signature;
+
+ private TimerTask task;
+ private boolean abort;
+
+ public RuntimePerformanceTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ /* Save default state */
+ savedUseCaches = SignatureImpl.getUseCache();
+
+ /* If a test takes too long we can kill it and fail */
+ abort = false;
+ task = new TimerTask() {
+ public void run () {
+ abort = true;
+ }
+ };
+ timer.schedule(task,TIMEOUT);
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ /* Restore default state */
+ SignatureImpl.setUseCache(savedUseCaches);
+
+ task.cancel();
+ }
+
+ public void testToString () {
+ Signature signature = makeMethodSig("test");
+
+ SignatureImpl.setUseCache(false);
+ warmUp(signature);
+ long noCache = invokeSignatureToString(signature,ITERATIONS/EXPECTED_RATIO);
+ System.out.println("noCache=" + noCache);
+
+ SignatureImpl.setUseCache(true);
+ warmUp(signature);
+ long cache = invokeSignatureToString(signature,ITERATIONS);
+ System.out.println("cache=" + cache);
+
+ long ratio = (EXPECTED_RATIO*noCache/cache);
+ System.out.println("ratio=" + ratio);
+ assertTrue("Using cache should be " + EXPECTED_RATIO + " times faster: " + ratio,(ratio >= EXPECTED_RATIO));
+ }
+
+ private long invokeSignatureToString (Signature sig, long iterations) {
+ long start = System.currentTimeMillis();
+ String s;
+
+ for (long l = 0; !abort && (l < iterations); l++) {
+ s = sig.toShortString();
+ s = sig.toString();
+ s = sig.toLongString();
+ }
+ if (abort) throw new RuntimeException("invokeSignatureToString aborted after " + (TIMEOUT/1000) + " seconds");
+
+ long finish = System.currentTimeMillis();
+ return (finish-start);
+ }
+
+ private void warmUp (Signature sig) {
+ invokeSignatureToString(sig,WARMUP_ITERATIONS);
+ }
+
+ private Signature makeMethodSig (String methodName) {
+ Class clazz = getClass();
+ Class[] parameterTypes = new Class[] { String.class };
+ String[] parameterNames = new String[] { "s" };
+ Class[] exceptionTypes = new Class[] {};
+ Class returnType = Void.TYPE;
+ return factory.makeMethodSig(1,methodName,clazz,parameterTypes,parameterNames,exceptionTypes,returnType);
+ }
+} \ No newline at end of file
diff --git a/runtime/src/test/java/org/aspectj/runtime/reflect/SignatureTest.java b/runtime/src/test/java/org/aspectj/runtime/reflect/SignatureTest.java
new file mode 100644
index 000000000..7a66a5b39
--- /dev/null
+++ b/runtime/src/test/java/org/aspectj/runtime/reflect/SignatureTest.java
@@ -0,0 +1,57 @@
+/*******************************************************************************
+ * Copyright (c) 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.aspectj.runtime.reflect;
+
+import java.lang.ref.Reference;
+import java.lang.reflect.Field;
+
+import junit.framework.TestCase;
+
+/**
+ */
+public class SignatureTest extends TestCase {
+ public void testGetDeclaringTypeName() {
+ FieldSignatureImpl fsi = new FieldSignatureImpl(0,"x",SignatureTest.class,String.class);
+ assertEquals(SignatureTest.class.getName(),fsi.getDeclaringTypeName());
+ assertSame(fsi.getDeclaringTypeName(),fsi.getDeclaringTypeName()); // should be cached.
+ }
+
+ public void testToShortMiddleLongString () {
+ MethodSignatureImpl msi = new MethodSignatureImpl(0,"test",SignatureTest.class,new Class[] { String.class, Integer.TYPE }, new String[] { "s", "i" }, new Class[] {}, Runnable.class);
+ String shortString = msi.toShortString();
+ assertSame(shortString,msi.toShortString()); // should be cached.
+ String middleString = msi.toString();
+ assertSame(middleString,msi.toString()); // should be cached.
+ String longString = msi.toLongString();
+ assertSame(longString,msi.toLongString()); // should be cached.
+ assertTrue("String representations should be different",!(shortString.equals(middleString) || middleString.equals(longString) || longString.equals(shortString)));
+ }
+
+ public void testClearCache() throws Exception {
+ MethodSignatureImpl msi = new MethodSignatureImpl(0,"test",SignatureTest.class,new Class[] { String.class, Integer.TYPE }, new String[] { "s", "i" }, new Class[] {}, Runnable.class);
+ String shortString = msi.toShortString();
+ assertSame(shortString,msi.toShortString());
+
+ Field field = SignatureImpl.class.getDeclaredField("stringCache");
+ field.setAccessible(true);
+ Object res = field.get(msi);
+
+ field = res.getClass().getDeclaredField("toStringCacheRef");
+ field.setAccessible(true);
+ Reference ref = (Reference)field.get(res);
+
+ ref.clear();
+ assertEquals(shortString,msi.toShortString());
+
+ String longString = msi.toLongString();
+ assertSame(longString,msi.toLongString()); // should be cached.
+ }
+}