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