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.

EMCPTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.body;
  25. import com.github.dcevm.test.TestUtil;
  26. import org.junit.Test;
  27. import java.io.PrintStream;
  28. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  29. import static org.junit.Assert.assertEquals;
  30. /**
  31. * EMCP (Equivalent modulo Constant Pool) tests.
  32. *
  33. * @author Thomas Wuerthinger
  34. */
  35. public class EMCPTest {
  36. public static class A {
  37. public static int EMCPReturn() {
  38. change();
  39. PrintStream s = System.out;
  40. return 1;
  41. }
  42. }
  43. public static class B {
  44. public static int b() {
  45. change();
  46. throw new RuntimeException();
  47. }
  48. }
  49. public static class C {
  50. public static int c() {
  51. changeAndThrow();
  52. return 0;
  53. }
  54. }
  55. public static class D {
  56. private static int value = 1;
  57. public static int EMCPReturn() {
  58. change3();
  59. return value;
  60. }
  61. }
  62. public static class A___1 {
  63. public static int EMCPReturn() {
  64. change();
  65. PrintStream s = System.out;
  66. return 1;
  67. }
  68. }
  69. public static class B___1 {
  70. public static int b() {
  71. change();
  72. throw new RuntimeException();
  73. }
  74. }
  75. public static class C___1 {
  76. public static int c() {
  77. changeAndThrow();
  78. return 0;
  79. }
  80. }
  81. public static class D___1 {
  82. private static int value = 1;
  83. public static int EMCPReturn() {
  84. change3();
  85. return value;
  86. }
  87. }
  88. public static class D___2 {
  89. private static int value = 1;
  90. public static int EMCPReturn() {
  91. change3();
  92. return value;
  93. }
  94. }
  95. public static class D___3 {
  96. private static int value = 1;
  97. public static int EMCPReturn() {
  98. change3();
  99. return value;
  100. }
  101. }
  102. public static void change() {
  103. __toVersion__(1);
  104. }
  105. public static void change3() {
  106. __toVersion__(1);
  107. __toVersion__(2);
  108. __toVersion__(3);
  109. }
  110. public static void changeAndThrow() {
  111. __toVersion__(1);
  112. throw new RuntimeException();
  113. }
  114. @Test
  115. public void testEMCPReturn() {
  116. __toVersion__(0);
  117. assertEquals(1, A.EMCPReturn());
  118. __toVersion__(0);
  119. assertEquals(1, A.EMCPReturn());
  120. __toVersion__(0);
  121. }
  122. @Test
  123. public void testEMCPMultiChangeReturn() {
  124. __toVersion__(0);
  125. assertEquals(1, D.EMCPReturn());
  126. __toVersion__(0);
  127. assertEquals(1, D.EMCPReturn());
  128. __toVersion__(0);
  129. }
  130. @Test
  131. public void testEMCPException() {
  132. __toVersion__(0);
  133. TestUtil.assertException(RuntimeException.class, new Runnable() {
  134. @Override
  135. public void run() {
  136. B.b();
  137. }
  138. });
  139. __toVersion__(0);
  140. TestUtil.assertException(RuntimeException.class, new Runnable() {
  141. @Override
  142. public void run() {
  143. B.b();
  144. }
  145. });
  146. __toVersion__(0);
  147. }
  148. @Test
  149. public void testEMCPExceptionInCallee() {
  150. __toVersion__(0);
  151. TestUtil.assertException(RuntimeException.class, new Runnable() {
  152. @Override
  153. public void run() {
  154. C.c();
  155. }
  156. });
  157. __toVersion__(0);
  158. TestUtil.assertException(RuntimeException.class, new Runnable() {
  159. @Override
  160. public void run() {
  161. C.c();
  162. }
  163. });
  164. __toVersion__(0);
  165. }
  166. }