]> source.dussan.org Git - aspectj.git/commitdiff
349398
authoraclement <aclement>
Wed, 15 Jun 2011 16:14:41 +0000 (16:14 +0000)
committeraclement <aclement>
Wed, 15 Jun 2011 16:14:41 +0000 (16:14 +0000)
tests/bugs1612/pr349398/CacheAspect.java [new file with mode: 0644]
tests/bugs1612/pr349398/DataGenerator.java [new file with mode: 0644]
tests/bugs1612/pr349398/DataGeneratorCacheAspect.java [new file with mode: 0644]
tests/bugs1612/pr349398/DataGeneratorTest.java [new file with mode: 0644]
tests/src/org/aspectj/systemtest/ajc1612/Ajc1612Tests.java
tests/src/org/aspectj/systemtest/ajc1612/ajc1612.xml

diff --git a/tests/bugs1612/pr349398/CacheAspect.java b/tests/bugs1612/pr349398/CacheAspect.java
new file mode 100644 (file)
index 0000000..3240327
--- /dev/null
@@ -0,0 +1,35 @@
+//package info.unterstein.hagen.moderne.ea6.a3;
+
+import java.util.HashMap;
+
+/**
+ * Enables a more complex and generic caching aspect which can be extended to
+be
+ * used in several use cases.
+ * 
+ * @author <a href="mailto:unterstein@me.com">Johannes Unterstein</a>
+ * @param <k>
+ *            the class of the keys
+ * @param <V>
+ *            the class of the cached values
+ */
+public abstract aspect CacheAspect<V> {
+    private HashMap<Object, V> cache;
+
+    public abstract pointcut cachePoint(Object key);
+
+    V around(Object key) : cachePoint(key) {
+        if (this.cache == null) {
+            this.cache = new HashMap<Object, V>();
+        }
+        V result;
+        if (this.cache.containsKey(key)) {
+            result = this.cache.get(key);
+        } else {
+            result = proceed(key);
+            this.cache.put(key, result);
+        }
+Object o = this.cache;
+        return result;
+    }
+}
diff --git a/tests/bugs1612/pr349398/DataGenerator.java b/tests/bugs1612/pr349398/DataGenerator.java
new file mode 100644 (file)
index 0000000..6561954
--- /dev/null
@@ -0,0 +1,14 @@
+//package info.unterstein.hagen.moderne.ea6.a3;
+
+public class DataGenerator {
+
+    private static final int MAGIC_NUMBER = 23;
+
+    public Integer getData(Integer i) {
+        try {
+            Thread.sleep(100);
+        } catch (InterruptedException ex) {
+        }
+        return new Integer(i * MAGIC_NUMBER);
+    }
+}
diff --git a/tests/bugs1612/pr349398/DataGeneratorCacheAspect.java b/tests/bugs1612/pr349398/DataGeneratorCacheAspect.java
new file mode 100644 (file)
index 0000000..53c941b
--- /dev/null
@@ -0,0 +1,13 @@
+//package info.unterstein.hagen.moderne.ea6.a3;
+
+/**
+ * An extension of the generic cache for the concrete use case of caching the
+ * {@link DataGenerator}.
+ * 
+ * @author <a href="mailto:unterstein@me.com">Johannes Unterstein</a>
+ */
+public aspect DataGeneratorCacheAspect extends CacheAspect<Integer> {
+
+    public pointcut cachePoint(Object key) : call(Integer
+DataGenerator.getData(Integer)) && args(key);
+}
diff --git a/tests/bugs1612/pr349398/DataGeneratorTest.java b/tests/bugs1612/pr349398/DataGeneratorTest.java
new file mode 100644 (file)
index 0000000..9a2c0ec
--- /dev/null
@@ -0,0 +1,41 @@
+//package info.unterstein.hagen.moderne.ea6.a3;
+
+public class DataGeneratorTest  {
+  public static void main(String []argv) {
+    new DataGeneratorTest().testGetData();
+    new DataGeneratorTest().testGetDataSpeedUp();
+  }
+
+    public void testGetData() {
+        DataGenerator generator = new DataGenerator();
+        assertEquals(new Integer(0), generator.getData(0));
+        assertEquals(new Integer(23), generator.getData(1));
+        assertEquals(new Integer(2 * 23), generator.getData(2));
+    }
+
+    public void assertEquals(Object o, Object p) {
+      if (!o.equals(p)) {
+        throw new IllegalStateException();
+      }
+    }
+
+    public void assertTrue(boolean b) {
+      if (!b) {
+        throw new IllegalStateException();
+      }
+    }
+
+    public void testGetDataSpeedUp() {
+        DataGenerator generator = new DataGenerator();
+        long before = System.currentTimeMillis();
+        for (int i = 0; i < 5; i++) {
+            generator.getData(i);
+        }
+        for (int i = 0; i < 5; i++) {
+            generator.getData(0);
+        }
+        long after = System.currentTimeMillis();
+        assertTrue((after - before) < 600);
+    }
+}
+
index 74f32e479e23de8ac2c35bd3fad66546c30e320f..b23bce8917550cc64e6030ad1377efc8fb2a6ba5 100644 (file)
@@ -25,6 +25,10 @@ public class Ajc1612Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
        // runTest("anno copying");
        // }
 
+       public void testDuplicateMethods_349398() {
+               runTest("duplicate methods");
+       }
+
        public void testBindingInts_347684() {
                runTest("binding ints");
        }
index 25b0cfff7a0693fb192431c3abae0456dd39dae1..eddffe1072daddf74928ed92eb1b1897facf8bce 100644 (file)
@@ -2,6 +2,13 @@
 
 <suite>
 
+<ajc-test dir="bugs1612/pr349398" title="duplicate methods">
+<compile files="DataGenerator.java CacheAspect.java DataGeneratorCacheAspect.java DataGeneratorTest.java" options="-1.5"/>
+<run class="DataGeneratorTest">
+</run>
+</ajc-test>
+
+
 <ajc-test dir="bugs1612/pr347684" title="binding ints">
 <compile files="BindingInts.java" options="-1.5"/>
 <run class="BindingInts">