1 package org.junit.tests.experimental.max;
2
3 import static org.hamcrest.CoreMatchers.equalTo;
4 import static org.hamcrest.CoreMatchers.not;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertNull;
7 import static org.junit.Assert.assertThat;
8
9 import java.lang.annotation.Annotation;
10 import java.net.URL;
11 import java.net.URLClassLoader;
12
13 import org.junit.Ignore;
14 import org.junit.Test;
15 import org.junit.runner.Description;
16
17 public class DescriptionTest {
18
19 @Test
20 public void parseClass_whenCantParse() {
21 assertNull(Description.TEST_MECHANISM.getTestClass());
22 }
23
24 @Test
25 public void parseMethod_whenCantParse() {
26 assertNull(Description.TEST_MECHANISM.getMethodName());
27 }
28
29 @Test(expected = IllegalArgumentException.class)
30 public void createSuiteDescription_whenZeroLength() {
31 Description.createSuiteDescription("");
32 }
33
34 @Test(expected = IllegalArgumentException.class)
35 public void createSuiteDescription_whenNull() {
36 Description.createSuiteDescription((String) null);
37 }
38
39 @Test
40 public void parseClassAndMethodNoAnnotations() throws Exception {
41 Description description = Description.createTestDescription(Description.class, "aTestMethod");
42
43 assertThat(description.getClassName(), equalTo("org.junit.runner.Description"));
44 assertThat(description.getMethodName(), equalTo("aTestMethod"));
45 assertThat(description.getAnnotations().size(), equalTo(0));
46 }
47
48 @Test
49 public void parseClassAndMethodWithAnnotations() throws Exception {
50 Annotation[] annotations =
51 DescriptionTest.class.getMethod("parseClassAndMethodWithAnnotations").getDeclaredAnnotations();
52
53 Description description = Description.createTestDescription(Description.class, "aTestMethod", annotations);
54
55 assertThat(description.getClassName(), equalTo("org.junit.runner.Description"));
56 assertThat(description.getMethodName(), equalTo("aTestMethod"));
57 assertThat(description.getAnnotations().size(), equalTo(1));
58 }
59
60 @Test
61 public void parseClassNameAndMethodUniqueId() throws Exception {
62 Description description = Description.createTestDescription("not a class name", "aTestMethod", 123);
63
64 assertThat(description.getClassName(), equalTo("not a class name"));
65 assertThat(description.getMethodName(), equalTo("aTestMethod"));
66 assertThat(description.getAnnotations().size(), equalTo(0));
67 }
68
69 @Test
70 public void sameNamesButDifferentUniqueIdAreNotEqual() throws Exception {
71 assertThat(Description.createTestDescription("not a class name", "aTestMethod", 1),
72 not(equalTo(Description.createTestDescription("not a class name", "aTestMethod", 2))));
73 }
74
75 @Test
76 public void usesPassedInClassObject() throws Exception {
77 class URLClassLoader2 extends URLClassLoader {
78 URLClassLoader2(URL... urls) {
79 super(urls);
80 }
81
82 @Override
83 public Class<?> findClass(String name) throws ClassNotFoundException {
84 return super.findClass(name);
85 }
86 }
87 URL classpath = Sweet.class.getProtectionDomain().getCodeSource().getLocation();
88 URLClassLoader2 loader = new URLClassLoader2(classpath);
89 Class<?> clazz = loader.findClass(Sweet.class.getName());
90 assertEquals(loader, clazz.getClassLoader());
91
92 Description d = Description.createSuiteDescription(clazz);
93 assertEquals(clazz, d.getTestClass());
94 assertNull(d.getMethodName());
95 assertEquals(1, d.getAnnotations().size());
96 assertEquals(Ignore.class, d.getAnnotations().iterator().next().annotationType());
97
98 d = Description.createTestDescription(clazz, "tessed");
99 assertEquals(clazz, d.getTestClass());
100 assertEquals("tessed", d.getMethodName());
101 assertEquals(0, d.getAnnotations().size());
102
103 d = Description.createTestDescription(clazz, "tessed", clazz.getMethod("tessed").getAnnotations());
104 assertEquals(clazz, d.getTestClass());
105 assertEquals("tessed", d.getMethodName());
106 assertEquals(1, d.getAnnotations().size());
107 assertEquals(Test.class, d.getAnnotations().iterator().next().annotationType());
108
109 d = d.childlessCopy();
110 assertEquals(clazz, d.getTestClass());
111 assertEquals("tessed", d.getMethodName());
112 assertEquals(1, d.getAnnotations().size());
113 assertEquals(Test.class, d.getAnnotations().iterator().next().annotationType());
114 }
115
116 @Ignore
117 private static class Sweet {
118 @Test
119 public void tessed() {
120 }
121 }
122
123 }