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.

AnnotationTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 org.junit.Before;
  26. import org.junit.Test;
  27. import java.lang.annotation.*;
  28. import java.lang.reflect.Field;
  29. import java.lang.reflect.Method;
  30. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  31. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  32. import static org.junit.Assert.assertEquals;
  33. /**
  34. * Tests for adding / removing annotations on methods, fields and classes.
  35. *
  36. * @author Thomas Wuerthinger
  37. * @author Jiri Bubnik
  38. */
  39. public class AnnotationTest {
  40. @Retention(RetentionPolicy.RUNTIME)
  41. @Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
  42. public @interface TestAnnotation {
  43. }
  44. @Retention(RetentionPolicy.RUNTIME)
  45. @Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
  46. public @interface TestAnnotation2 {
  47. }
  48. // Version 0
  49. public static class A {
  50. public int testField;
  51. public void testMethod() {
  52. }
  53. }
  54. // Version 1
  55. @TestAnnotation
  56. public static class A___1 {
  57. @TestAnnotation
  58. public int testField;
  59. @TestAnnotation
  60. public void testMethod() {
  61. }
  62. }
  63. // Version 2
  64. @TestAnnotation2
  65. public static class A___2 {
  66. @TestAnnotation2
  67. public int testField;
  68. @TestAnnotation2
  69. public void testMethod() {
  70. }
  71. }
  72. @Before
  73. public void setUp() throws Exception {
  74. __toVersion__(0);
  75. }
  76. private void checkAnnotation(Class<?> c, Class<? extends Annotation> expectedAnnotation) throws NoSuchMethodException, NoSuchFieldException {
  77. Class<? extends Annotation> annotation = c.getAnnotation(expectedAnnotation).annotationType();
  78. assertEquals(expectedAnnotation, annotation);
  79. Method m = c.getMethod("testMethod");
  80. annotation = m.getAnnotation(expectedAnnotation).annotationType();
  81. assertEquals(expectedAnnotation, annotation);
  82. Field f = c.getField("testField");
  83. annotation = f.getAnnotation(expectedAnnotation).annotationType();
  84. assertEquals(expectedAnnotation, annotation);
  85. }
  86. private void checkAnnotationMissing(Class<A> c) throws NoSuchMethodException, NoSuchFieldException {
  87. assertEquals(0, c.getAnnotations().length);
  88. assertEquals(0, c.getField("testField").getAnnotations().length);
  89. assertEquals(0, c.getMethod("testMethod").getAnnotations().length);
  90. }
  91. @Test
  92. public void testAddMethodToKlassWithEMCPExceptionMethod() throws NoSuchMethodException, NoSuchFieldException {
  93. assert __version__() == 0;
  94. checkAnnotationMissing(A.class);
  95. __toVersion__(1);
  96. checkAnnotation(A.class, TestAnnotation.class);
  97. __toVersion__(2);
  98. checkAnnotation(A.class, TestAnnotation2.class);
  99. __toVersion__(0);
  100. checkAnnotationMissing(A.class);
  101. }
  102. }