blob: 57ae6c5c845cfd00cb18cd6b53ca98069f36221b [file] [log] [blame]
Ray Milkey22553b92014-05-06 11:06:21 -07001package net.onrc.onos.core.util;
2
3import org.junit.Test;
4
5import static net.onrc.onos.core.util.UtilityClassChecker.assertThatClassIsUtility;
6import static org.hamcrest.MatcherAssert.assertThat;
7import static org.hamcrest.Matchers.containsString;
8import static org.hamcrest.Matchers.is;
9
10/**
11 * Set of unit tests to check the implementation of the utility class
12 * checker.
13 */
14public class UtilityClassCheckerTest {
15 /**
16 * Test class for non final class check.
17 */
18 static class NonFinal {
19 private NonFinal() {}
20 }
21
22 /**
23 * Check that a non final class correctly produces an error.
24 * @throws Exception if any of the reflection lookups fail.
25 */
26 @Test
27 public void testNonFinalClass() throws Exception {
28 boolean gotException = false;
29 try {
30 assertThatClassIsUtility(NonFinal.class);
31 } catch (AssertionError assertion) {
32 assertThat(assertion.getMessage(),
33 containsString("is not final"));
34 gotException = true;
35 }
36 assertThat(gotException, is(true));
37 }
38
39 /**
40 * Test class for final no constructor class check.
41 */
42 static final class FinalNoConstructor {
43 }
44
45 /**
46 * Check that a final class with no declared constructor correctly produces
47 * an error. In this case, the compiler generates a default constructor
48 * for you, but the constructor is 'protected' and will fail the check.
49 *
50 * @throws Exception if any of the reflection lookups fail.
51 */
52 @Test
53 public void testFinalNoConstructorClass() throws Exception {
54 boolean gotException = false;
55 try {
56 assertThatClassIsUtility(FinalNoConstructor.class);
57 } catch (AssertionError assertion) {
58 assertThat(assertion.getMessage(),
59 containsString("class with a default constructor that " +
60 "is not private"));
61 gotException = true;
62 }
63 assertThat(gotException, is(true));
64 }
65
66 /**
67 * Test class for class with more than one constructor check.
68 */
69 final static class TwoConstructors {
70 private TwoConstructors() {}
71 private TwoConstructors(int x) {}
72 }
73
74 /**
75 * Check that a non static class correctly produces an error.
76 * @throws Exception if any of the reflection lookups fail.
77 */
78 @Test
79 public void testOnlyOneConstructor() throws Exception {
80 boolean gotException = false;
81 try {
82 assertThatClassIsUtility(TwoConstructors.class);
83 } catch (AssertionError assertion) {
84 assertThat(assertion.getMessage(),
85 containsString("more than one constructor"));
86 gotException = true;
87 }
88 assertThat(gotException, is(true));
89 }
90
91 /**
92 * Test class with a non private constructor.
93 */
94 final static class NonPrivateConstructor {
95 protected NonPrivateConstructor() {}
96 }
97
98 /**
99 * Check that a class with a non private constructor correctly
100 * produces an error.
101 * @throws Exception if any of the reflection lookups fail.
102 */
103 @Test
104 public void testNonPrivateConstructor() throws Exception {
105
106 boolean gotException = false;
107 try {
108 assertThatClassIsUtility(NonPrivateConstructor.class);
109 } catch (AssertionError assertion) {
110 assertThat(assertion.getMessage(),
111 containsString("constructor that is not private"));
112 gotException = true;
113 }
114 assertThat(gotException, is(true));
115 }
116
117 /**
118 * Test class with a non static method.
119 */
120 final static class NonStaticMethod {
121 private NonStaticMethod() {}
122 public void aPublicMethod() {}
123 }
124
125 /**
126 * Check that a class with a non static method correctly produces an error.
127 * @throws Exception if any of the reflection lookups fail.
128 */
129 @Test
130 public void testNonStaticMethod() throws Exception {
131
132 boolean gotException = false;
133 try {
134 assertThatClassIsUtility(NonStaticMethod.class);
135 } catch (AssertionError assertion) {
136 assertThat(assertion.getMessage(),
137 containsString("one or more non-static methods"));
138 gotException = true;
139 }
140 assertThat(gotException, is(true));
141 }
142}