You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FieldChangedOrderTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. package com.github.dcevm.test.fields;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  28. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  29. import static org.junit.Assert.assertEquals;
  30. /**
  31. * Test that changes the order of two int fields.
  32. *
  33. * @author Thomas Wuerthinger
  34. */
  35. public class FieldChangedOrderTest {
  36. // Version 0
  37. public static class A {
  38. public int value1;
  39. public int value2;
  40. public A() {
  41. value1 = 1;
  42. value2 = 2;
  43. }
  44. public int getValue1() {
  45. return value1;
  46. }
  47. public int getValue2() {
  48. return value2;
  49. }
  50. }
  51. public static class B {
  52. public static int getStaticValue1(A a) {
  53. return a.value1;
  54. }
  55. public static int getStaticValue2(A a) {
  56. return a.value2;
  57. }
  58. }
  59. // Version 1
  60. public static class A___1 {
  61. public int value2;
  62. public int value1;
  63. public int getValue1() {
  64. return value1;
  65. }
  66. public int getValue2() {
  67. return value2;
  68. }
  69. }
  70. public static class B___1 {
  71. public static int getStaticValue1(A a) {
  72. return a.value1;
  73. }
  74. public static int getStaticValue2(A a) {
  75. return a.value2;
  76. }
  77. }
  78. // Version 2
  79. public static class A___2 {
  80. public int tmp1;
  81. public int value2;
  82. public int tmp2;
  83. public int value1;
  84. public int tmp3;
  85. public int getValue1() {
  86. return value1;
  87. }
  88. public int getValue2() {
  89. return value2;
  90. }
  91. }
  92. // Version 3
  93. public static class A___3 {
  94. public int tmp1;
  95. public int value2;
  96. public int getValue1() {
  97. return tmp1;
  98. }
  99. public int getValue2() {
  100. return value2;
  101. }
  102. }
  103. @Before
  104. public void setUp() throws Exception {
  105. __toVersion__(0);
  106. }
  107. @Test
  108. public void testRenameField() {
  109. assert __version__() == 0;
  110. A a = new A();
  111. assertObjectOK(a);
  112. __toVersion__(3);
  113. assertEquals(0, a.getValue1());
  114. assertEquals(2, a.getValue2());
  115. __toVersion__(0);
  116. assertEquals(0, a.getValue1());
  117. assertEquals(2, a.getValue2());
  118. }
  119. @Test
  120. public void testSimpleOrderChange() {
  121. assert __version__() == 0;
  122. A a = new A();
  123. assertObjectOK(a);
  124. __toVersion__(1);
  125. assertObjectOK(a);
  126. __toVersion__(0);
  127. assertObjectOK(a);
  128. }
  129. /**
  130. * Checks that the given object is unmodified (i.e. the values of the fields are correct)
  131. *
  132. * @param a the object to be checked
  133. */
  134. private void assertObjectOK(A a) {
  135. assertEquals(1, a.getValue1());
  136. assertEquals(2, a.getValue2());
  137. assertEquals(1, B.getStaticValue1(a));
  138. assertEquals(2, B.getStaticValue2(a));
  139. assertEquals(1, a.value1);
  140. assertEquals(2, a.value2);
  141. }
  142. @Test
  143. public void testSimpleOrderChangeWithNewTempFields() {
  144. assert __version__() == 0;
  145. A a = new A();
  146. assertObjectOK(a);
  147. __toVersion__(2);
  148. assertObjectOK(a);
  149. __toVersion__(0);
  150. assertObjectOK(a);
  151. }
  152. }