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.

DeleteActiveMethodTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.methods;
  25. import static org.junit.Assert.assertEquals;
  26. import com.github.dcevm.MethodRedefinitionPolicy;
  27. import com.github.dcevm.RedefinitionPolicy;
  28. import com.github.dcevm.test.TestUtil;
  29. import junit.framework.Assert;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  33. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  34. /**
  35. * Test cases that delete a method that is currently active on the stack.
  36. *
  37. * @author Thomas Wuerthinger
  38. */
  39. public class DeleteActiveMethodTest {
  40. @Before
  41. public void setUp() throws Exception {
  42. __toVersion__(0);
  43. }
  44. // Version 0
  45. public static class A {
  46. boolean firstCall;
  47. public int value() {
  48. firstCall = true;
  49. return helperValue();
  50. }
  51. public int helperValue() {
  52. if (!firstCall) {
  53. return -1;
  54. }
  55. firstCall = false;
  56. Thread t = new Thread(new Runnable() {
  57. @Override
  58. public void run() {
  59. __toVersion__(1);
  60. }
  61. });
  62. t.start();
  63. try {
  64. while (t.isAlive()) {
  65. try {
  66. this.helperValue();
  67. Thread.sleep(500);
  68. } catch (InterruptedException e) {
  69. }
  70. helperValue();
  71. }
  72. Assert.fail("Exception expected!");
  73. } catch (NoSuchMethodError e) {
  74. }
  75. try {
  76. t.join();
  77. } catch (InterruptedException e) {
  78. }
  79. return 1;
  80. }
  81. }
  82. public static class B {
  83. public int fac(int x) {
  84. if (x == 0) {
  85. __toVersion__(1);
  86. }
  87. return x * fac(x - 1);
  88. }
  89. }
  90. // Version 1
  91. @MethodRedefinitionPolicy(RedefinitionPolicy.DynamicCheck)
  92. public static class A___1 {
  93. boolean firstCall;
  94. public int value() {
  95. __toVersion__(0);
  96. return 2;
  97. }
  98. }
  99. @MethodRedefinitionPolicy(RedefinitionPolicy.DynamicCheck)
  100. public static class B___1 {
  101. }
  102. @Test
  103. public void testDeleteActiveMethodSimple() {
  104. assert __version__() == 0;
  105. final B b = new B();
  106. TestUtil.assertException(NoSuchMethodError.class, new Runnable() {
  107. @Override
  108. public void run() {
  109. b.fac(5);
  110. }
  111. });
  112. assert __version__() == 1;
  113. __toVersion__(0);
  114. assert __version__() == 0;
  115. }
  116. @Test
  117. public void testDeleteActiveMethod() {
  118. assert __version__() == 0;
  119. A a = new A();
  120. assertEquals(1, a.value());
  121. assert __version__() == 1;
  122. assertEquals(2, a.value());
  123. assert __version__() == 0;
  124. assertEquals(1, a.value());
  125. assert __version__() == 1;
  126. assertEquals(2, a.value());
  127. assert __version__() == 0;
  128. assertEquals(1, a.value());
  129. assert __version__() == 1;
  130. assertEquals(2, a.value());
  131. assert __version__() == 0;
  132. }
  133. }