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.

AnnotationUtilsTest.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.api.utils;
  21. import static org.hamcrest.Matchers.is;
  22. import static org.hamcrest.core.IsNull.nullValue;
  23. import static org.junit.Assert.assertThat;
  24. import org.junit.Test;
  25. import java.lang.annotation.ElementType;
  26. import java.lang.annotation.Retention;
  27. import java.lang.annotation.RetentionPolicy;
  28. import java.lang.annotation.Target;
  29. public class AnnotationUtilsTest {
  30. @Test
  31. public void getClassAnnotation() {
  32. FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(new SuperClass(), FakeAnnotation.class);
  33. assertThat(annotation.value(), is("foo"));
  34. }
  35. @Test
  36. public void searchClassAnnotationInSuperClass() {
  37. FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(new ChildClass(), FakeAnnotation.class);
  38. assertThat(annotation.value(), is("foo"));
  39. }
  40. @Test
  41. public void noClassAnnotation() {
  42. FakeAnnotation annotation = AnnotationUtils.getClassAnnotation("a string", FakeAnnotation.class);
  43. assertThat(annotation, nullValue());
  44. }
  45. @Test
  46. public void shouldAcceptClasses() {
  47. FakeAnnotation annotation = AnnotationUtils.getClassAnnotation(SuperClass.class, FakeAnnotation.class);
  48. assertThat(annotation.value(), is("foo"));
  49. annotation = AnnotationUtils.getClassAnnotation(ChildClass.class, FakeAnnotation.class);
  50. assertThat(annotation.value(), is("foo"));
  51. }
  52. }
  53. @Retention(RetentionPolicy.RUNTIME)
  54. @Target(ElementType.TYPE)
  55. @interface FakeAnnotation {
  56. String value();
  57. }
  58. @FakeAnnotation("foo")
  59. class SuperClass {
  60. }
  61. class ChildClass extends SuperClass {
  62. }