aboutsummaryrefslogtreecommitdiffstats
path: root/dcevm/src/test
diff options
context:
space:
mode:
authorPrzemysław Rumik <przemelek@gmail.com>2018-03-24 16:36:29 +0100
committerPrzemysław Rumik <przemelek@gmail.com>2018-03-24 16:36:29 +0100
commit57086f31f524ecfe3bfed0341d7a0289ce2a3e86 (patch)
tree04429cf96a89260a0a01ea047c3de6b2984dc1e0 /dcevm/src/test
parent10ef78858177953adc80ba6a495e597a4ef99281 (diff)
downloaddcevm-57086f31f524ecfe3bfed0341d7a0289ce2a3e86.tar.gz
dcevm-57086f31f524ecfe3bfed0341d7a0289ce2a3e86.zip
fix for proper incrementation of classRedefinedCount, till now we were updating value in old class Class file
Diffstat (limited to 'dcevm/src/test')
-rw-r--r--dcevm/src/test/java7/com/github/dcevm/test/TestUtil.java14
-rw-r--r--dcevm/src/test/java7/com/github/dcevm/test/fields/FieldOperationsUpdateClassRedefinedCount.java86
-rw-r--r--dcevm/src/test/java7/com/github/dcevm/test/methods/OperationsOnMethodsUpdateClassRedefinedCount.java111
3 files changed, 211 insertions, 0 deletions
diff --git a/dcevm/src/test/java7/com/github/dcevm/test/TestUtil.java b/dcevm/src/test/java7/com/github/dcevm/test/TestUtil.java
index 7208af20..41d9b0bc 100644
--- a/dcevm/src/test/java7/com/github/dcevm/test/TestUtil.java
+++ b/dcevm/src/test/java7/com/github/dcevm/test/TestUtil.java
@@ -26,6 +26,7 @@ package com.github.dcevm.test;
import com.github.dcevm.HotSwapTool;
import org.junit.Assert;
+import java.lang.reflect.Field;
/**
* Utility methods for unit testing.
@@ -71,4 +72,17 @@ public class TestUtil {
});
}
+ public static int getClassRedefinedCount(Class type) {
+ try {
+ Field field = Class.class.getDeclaredField("classRedefinedCount");
+ boolean accessibility = field.isAccessible();
+ field.setAccessible(true);
+ int classRedefinedCount = (Integer) field.get(type);
+ field.setAccessible(accessibility);
+ return classRedefinedCount;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
}
diff --git a/dcevm/src/test/java7/com/github/dcevm/test/fields/FieldOperationsUpdateClassRedefinedCount.java b/dcevm/src/test/java7/com/github/dcevm/test/fields/FieldOperationsUpdateClassRedefinedCount.java
new file mode 100644
index 00000000..ff7ddaa1
--- /dev/null
+++ b/dcevm/src/test/java7/com/github/dcevm/test/fields/FieldOperationsUpdateClassRedefinedCount.java
@@ -0,0 +1,86 @@
+package com.github.dcevm.test.fields;
+
+import com.github.dcevm.HotSwapTool;
+import com.github.dcevm.test.TestUtil;
+import com.github.dcevm.test.category.Light;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
+import static org.junit.Assert.assertEquals;
+
+
+@Category(Light.class)
+public class FieldOperationsUpdateClassRedefinedCount {
+
+ // Version 0
+ public static class A {
+
+ public int x;
+
+ int getFieldInOldCode() {
+
+ __toVersion__(1);
+
+ // This field does no longer exist
+ return x;
+ }
+ int getVer() {
+ return 0;
+ }
+ }
+
+ // Version 1
+ public static class A___1 {
+ public int x;
+ public int y;
+ int getVer() {
+ return 1;
+ }
+ }
+
+ public static class A___2 {
+ int getVer() {
+ return 2;
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ __toVersion__(0);
+ }
+
+ @Test
+ public void addingFieldUpdatesClassRedifinedCount() throws NoSuchFieldException, IllegalAccessException {
+ // setup
+ A a = new A();
+ __toVersion__(0);
+ int prevVersion = TestUtil.getClassRedefinedCount(A.class);
+ // examine
+ __toVersion__(1);
+
+ Object y = A.class.getDeclaredField("y").get(a);
+ // verify
+ assertEquals(0,y);
+ assertEquals(1, a.getVer());
+ assertEquals(prevVersion+1, TestUtil.getClassRedefinedCount(A.class));
+ }
+
+ @Test
+ public void deletingFieldUpdatesClassRedifinedCount() {
+ // setup
+ A a= new A();
+ __toVersion__(0);
+ int prevVersion = TestUtil.getClassRedefinedCount(A.class);
+ // examine
+ __toVersion__(2);
+
+ // verify
+ assertEquals(2, a.getVer());
+ assertEquals(prevVersion+1, TestUtil.getClassRedefinedCount(A.class));
+ }
+
+
+
+}
diff --git a/dcevm/src/test/java7/com/github/dcevm/test/methods/OperationsOnMethodsUpdateClassRedefinedCount.java b/dcevm/src/test/java7/com/github/dcevm/test/methods/OperationsOnMethodsUpdateClassRedefinedCount.java
new file mode 100644
index 00000000..96522914
--- /dev/null
+++ b/dcevm/src/test/java7/com/github/dcevm/test/methods/OperationsOnMethodsUpdateClassRedefinedCount.java
@@ -0,0 +1,111 @@
+package com.github.dcevm.test.methods;
+
+import com.github.dcevm.test.TestUtil;
+import org.junit.Before;
+import org.junit.Test;
+
+import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
+import static org.junit.Assert.assertEquals;
+
+public class OperationsOnMethodsUpdateClassRedefinedCount {
+ // Version 0
+ public static class A {
+ public int value(int newVersion) {
+ return newVersion;
+ }
+ }
+
+ // Version 1
+ public static class A___1 {
+
+ public int value(int newVersion) {
+
+ int x = 1;
+ try {
+ x = 2;
+ } catch (NumberFormatException e) {
+ x = 3;
+ } catch (Exception e) {
+ x = 4;
+ } finally {
+ x = x * 2;
+ }
+ __toVersion__(newVersion);
+ throw new IllegalArgumentException();
+ }
+ }
+
+ // Version 2
+ public static class A___2 {
+
+ public int value2() {
+ return 2;
+ }
+
+ public int value(int newVersion) {
+
+ int x = 1;
+ try {
+ x = 2;
+ } catch (NumberFormatException e) {
+ x = 3;
+ } catch (Exception e) {
+ x = 4;
+ } finally {
+ x = x * 2;
+ }
+ __toVersion__(newVersion);
+ throw new IllegalArgumentException();
+ }
+
+ public int value3() {
+ return 3;
+ }
+
+ public int value4() {
+ return 4;
+ }
+
+ public int value5() {
+ return 5;
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ __toVersion__(0);
+ }
+
+ @Test
+ public void changingMethodUpdatesClassRedefinedCount() {
+ // setup
+ __toVersion__(0);
+ int prevVersion = TestUtil.getClassRedefinedCount(A.class);
+ // examine
+ __toVersion__(1);
+ // verify
+ assertEquals(prevVersion+1, TestUtil.getClassRedefinedCount(A.class));
+ }
+
+ @Test
+ public void addingMethodUpdatesClassRedefinedCount() {
+ // setup
+ __toVersion__(0);
+ int prevVersion = TestUtil.getClassRedefinedCount(A.class);
+ // examine
+ __toVersion__(2);
+ // verify
+ assertEquals(prevVersion+1, TestUtil.getClassRedefinedCount(A.class));
+ }
+
+ @Test
+ public void deletingMethodUpdatesClassRedefinedCount() {
+ // setup
+ __toVersion__(2);
+ int prevVersion = TestUtil.getClassRedefinedCount(A.class);
+ // examine
+ __toVersion__(0);
+ // verify
+ assertEquals(prevVersion+1, TestUtil.getClassRedefinedCount(A.class));
+ }
+}