summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/description/AnnotatedDescriptionTest.java
blob: 370293b3959bffe0e80ed44ab3b654c990ddab7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.junit.tests.description;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.Request;

public class AnnotatedDescriptionTest {
	@Retention(RetentionPolicy.RUNTIME)
	public @interface MyOwnAnnotation {

	}

	@MyOwnAnnotation
	public static class AnnotatedClass {
		@Test
		public void a() {
		}
	}

	@Test
	public void annotationsExistOnDescriptionsOfClasses() {
		assertTrue((describe(AnnotatedClass.class).getAnnotation(
				MyOwnAnnotation.class) != null));
	}

	@Test
	public void getAnnotationsReturnsAllAnnotations() {
		assertEquals(1, describe(ValueAnnotatedClass.class).getAnnotations()
				.size());
	}

	@Ignore
	public static class IgnoredClass {
		@Test
		public void a() {
		}
	}

	@Test
	public void annotationsExistOnDescriptionsOfIgnoredClass() {
		assertTrue((describe(IgnoredClass.class).getAnnotation(Ignore.class) != null));
	}

	@Retention(RetentionPolicy.RUNTIME)
	public @interface ValuedAnnotation {
		String value();
	}

	@ValuedAnnotation("hello")
	public static class ValueAnnotatedClass {
		@Test
		public void a() {
		}
	}

	@Test
	public void descriptionOfTestClassHasValuedAnnotation() {
		Description description= describe(ValueAnnotatedClass.class);
		assertEquals("hello", description.getAnnotation(ValuedAnnotation.class)
				.value());
	}

	@Test
	public void childlessCopyOfDescriptionStillHasAnnotations() {
		Description description= describe(ValueAnnotatedClass.class);
		assertEquals("hello", description.childlessCopy().getAnnotation(ValuedAnnotation.class)
				.value());
	}

	@Test
	public void characterizeCreatingMyOwnAnnotation() {
		Annotation annotation= new Ignore() {
			public String value() {
				return "message";
			}

			public Class<? extends Annotation> annotationType() {
				return Ignore.class;
			}
		};

		assertEquals(Ignore.class, annotation.annotationType());
	}

	private Description describe(Class<?> testClass) {
		return Request.aClass(testClass).getRunner().getDescription();
	}
}