blob: 8c2c5532d9c240a6bc22aabeffeaf4d449180578 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Pavlin Radoslavovd26f57a2014-10-23 17:19:45 -070016package org.onlab.junit;
17
18import org.junit.Test;
19
20import static org.hamcrest.MatcherAssert.assertThat;
21import static org.hamcrest.Matchers.containsString;
22import static org.hamcrest.Matchers.is;
23import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
24
25/**
26 * Set of unit tests to check the implementation of the utility class
27 * checker.
28 */
29public class UtilityClassCheckerTest {
30
31 // CHECKSTYLE:OFF test data intentionally not final
32 /**
33 * Test class for non final class check.
34 */
35 static class NonFinal {
36 private NonFinal() { }
37 }
38 // CHECKSTYLE:ON
39
40 /**
41 * Check that a non final class correctly produces an error.
42 * @throws Exception if any of the reflection lookups fail.
43 */
44 @Test
45 public void testNonFinalClass() throws Exception {
46 boolean gotException = false;
47 try {
48 assertThatClassIsUtility(NonFinal.class);
49 } catch (AssertionError assertion) {
50 assertThat(assertion.getMessage(),
51 containsString("is not final"));
52 gotException = true;
53 }
54 assertThat(gotException, is(true));
55 }
56
57 /**
58 * Test class for final no constructor class check.
59 */
60 static final class FinalNoConstructor {
61 }
62
63 /**
64 * Check that a final class with no declared constructor correctly produces
65 * an error. In this case, the compiler generates a default constructor
66 * for you, but the constructor is 'protected' and will fail the check.
67 *
68 * @throws Exception if any of the reflection lookups fail.
69 */
70 @Test
71 public void testFinalNoConstructorClass() throws Exception {
72 boolean gotException = false;
73 try {
74 assertThatClassIsUtility(FinalNoConstructor.class);
75 } catch (AssertionError assertion) {
76 assertThat(assertion.getMessage(),
77 containsString("class with a default constructor that " +
78 "is not private"));
79 gotException = true;
80 }
81 assertThat(gotException, is(true));
82 }
83
84 /**
85 * Test class for class with more than one constructor check.
86 */
87 static final class TwoConstructors {
88 private TwoConstructors() { }
89 private TwoConstructors(int x) { }
90 }
91
92 /**
93 * Check that a non static class correctly produces an error.
94 * @throws Exception if any of the reflection lookups fail.
95 */
96 @Test
97 public void testOnlyOneConstructor() throws Exception {
98 boolean gotException = false;
99 try {
100 assertThatClassIsUtility(TwoConstructors.class);
101 } catch (AssertionError assertion) {
102 assertThat(assertion.getMessage(),
103 containsString("more than one constructor"));
104 gotException = true;
105 }
106 assertThat(gotException, is(true));
107 }
108
109 /**
110 * Test class with a non private constructor.
111 */
112 static final class NonPrivateConstructor {
113 protected NonPrivateConstructor() { }
114 }
115
116 /**
117 * Check that a class with a non private constructor correctly
118 * produces an error.
119 * @throws Exception if any of the reflection lookups fail.
120 */
121 @Test
122 public void testNonPrivateConstructor() throws Exception {
123
124 boolean gotException = false;
125 try {
126 assertThatClassIsUtility(NonPrivateConstructor.class);
127 } catch (AssertionError assertion) {
128 assertThat(assertion.getMessage(),
129 containsString("constructor that is not private"));
130 gotException = true;
131 }
132 assertThat(gotException, is(true));
133 }
134
135 /**
136 * Test class with a non static method.
137 */
138 static final class NonStaticMethod {
139 private NonStaticMethod() { }
140 public void aPublicMethod() { }
141 }
142
143 /**
144 * Check that a class with a non static method correctly produces an error.
145 * @throws Exception if any of the reflection lookups fail.
146 */
147 @Test
148 public void testNonStaticMethod() throws Exception {
149
150 boolean gotException = false;
151 try {
152 assertThatClassIsUtility(NonStaticMethod.class);
153 } catch (AssertionError assertion) {
154 assertThat(assertion.getMessage(),
155 containsString("one or more non-static methods"));
156 gotException = true;
157 }
158 assertThat(gotException, is(true));
159 }
160}