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 3.9KB

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