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