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.

AddMethodTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 com.github.dcevm.test.TestUtil;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  29. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  30. import static org.junit.Assert.assertEquals;
  31. import static org.junit.Assert.assertTrue;
  32. /**
  33. * Tests for adding / removing methods in a single class.
  34. *
  35. * @author Thomas Wuerthinger
  36. */
  37. public class AddMethodTest {
  38. // Version 0
  39. public static class A {
  40. public int value(int newVersion) {
  41. return newVersion;
  42. }
  43. }
  44. // Version 1
  45. public static class A___1 {
  46. public int value(int newVersion) {
  47. int x = 1;
  48. try {
  49. x = 2;
  50. } catch (NumberFormatException e) {
  51. x = 3;
  52. } catch (Exception e) {
  53. x = 4;
  54. } finally {
  55. x = x * 2;
  56. }
  57. __toVersion__(newVersion);
  58. throw new IllegalArgumentException();
  59. }
  60. }
  61. // Version 2
  62. public static class A___2 {
  63. public int value2() {
  64. return 2;
  65. }
  66. public int value(int newVersion) {
  67. int x = 1;
  68. try {
  69. x = 2;
  70. } catch (NumberFormatException e) {
  71. x = 3;
  72. } catch (Exception e) {
  73. x = 4;
  74. } finally {
  75. x = x * 2;
  76. }
  77. __toVersion__(newVersion);
  78. throw new IllegalArgumentException();
  79. }
  80. public int value3() {
  81. return 3;
  82. }
  83. public int value4() {
  84. return 4;
  85. }
  86. public int value5() {
  87. return 5;
  88. }
  89. }
  90. @Before
  91. public void setUp() throws Exception {
  92. __toVersion__(0);
  93. }
  94. private void checkLineNumbers(int first, int second) {
  95. assertTrue("Must have different line numbers (A.value is an EMCP method and therefore execution has to be transferred). Exception line numbers: " + first + " and " + second, first != second);
  96. }
  97. @Test
  98. public void testAddMethodToKlassWithEMCPExceptionMethod() {
  99. assert __version__() == 0;
  100. final A a = new A();
  101. assertEquals(1, a.value(1));
  102. __toVersion__(1);
  103. int firstLineNumber = TestUtil.assertException(IllegalArgumentException.class, new Runnable() {
  104. @Override
  105. public void run() {
  106. assertEquals(4, a.value(1));
  107. }
  108. });
  109. int secondLineNumber = TestUtil.assertException(IllegalArgumentException.class, new Runnable() {
  110. @Override
  111. public void run() {
  112. assertEquals(4, a.value(2));
  113. }
  114. });
  115. checkLineNumbers(firstLineNumber, secondLineNumber);
  116. assert __version__() == 2;
  117. int newFirstLineNumber = TestUtil.assertException(IllegalArgumentException.class, new Runnable() {
  118. @Override
  119. public void run() {
  120. assertEquals(4, a.value(2));
  121. }
  122. });
  123. assertEquals(secondLineNumber, newFirstLineNumber);
  124. int newSecondLineNumber = TestUtil.assertException(IllegalArgumentException.class, new Runnable() {
  125. @Override
  126. public void run() {
  127. assertEquals(4, a.value(1));
  128. }
  129. });
  130. assertEquals(newSecondLineNumber, firstLineNumber);
  131. checkLineNumbers(firstLineNumber, secondLineNumber);
  132. __toVersion__(0);
  133. assertEquals(1, a.value(1));
  134. assert __version__() == 0;
  135. }
  136. }