blob: b4a6ff5b87667116312b6f85aa5ffa5e3ca6509b [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.ImmutableClassChecker.assertThatClassIsImmutable;
9
10/**
11 * Set of unit tests to check the implementation of the immutable class
12 * checker.
13 */
14public class ImmutableClassCheckerTest {
15 /**
16 * Test class for non final class check.
17 */
18 // CHECKSTYLE IGNORE FinalClass FOR NEXT 1 LINES
19 static class NonFinal {
20 private NonFinal() { }
21 }
22
23 /**
24 * Check that a non final class correctly produces an error.
25 * @throws Exception if any of the reflection lookups fail.
26 */
27 @Test
28 public void testNonFinalClass() throws Exception {
29 boolean gotException = false;
30 try {
31 assertThatClassIsImmutable(NonFinal.class);
32 } catch (AssertionError assertion) {
33 assertThat(assertion.getMessage(),
34 containsString("is not final"));
35 gotException = true;
36 }
37 assertThat(gotException, is(true));
38 }
39
40 /**
41 * Test class for non private member class check.
42 */
43 static final class FinalProtectedMember {
44 protected final int x = 0;
45 }
46
47 /**
48 * Check that a final class with a non-private member is properly detected.
49 *
50 * @throws Exception if any of the reflection lookups fail.
51 */
52 @Test
53 public void testFinalProtectedMember() throws Exception {
54 boolean gotException = false;
55 try {
56 assertThatClassIsImmutable(FinalProtectedMember.class);
57 } catch (AssertionError assertion) {
58 assertThat(assertion.getMessage(),
59 containsString("a field named 'x' that is not private"));
60 gotException = true;
61 }
62 assertThat(gotException, is(true));
63 }
64
65 /**
66 * Test class for non private member class check.
67 */
68 static final class NotFinalPrivateMember {
69 private int x = 0;
70 }
71
72 /**
73 * Check that a final class with a non-final private
74 * member is properly detected.
75 *
76 * @throws Exception if any of the reflection lookups fail.
77 */
78 @Test
79 public void testNotFinalPrivateMember() throws Exception {
80 boolean gotException = false;
81 try {
82 assertThatClassIsImmutable(NotFinalPrivateMember.class);
83 } catch (AssertionError assertion) {
84 assertThat(assertion.getMessage(),
85 containsString("a field named 'x' that is not final"));
86 gotException = true;
87 }
88 assertThat(gotException, is(true));
89 }
90
91 /**
92 * Test class for non private member class check.
93 */
94 static final class ClassWithSetter {
95 private final int x = 0;
96 public void setX(int newX) {
97 }
98 }
99
100 /**
101 * Check that a final class with a final private
102 * member that is modifyable by a setter is properly detected.
103 *
104 * @throws Exception if any of the reflection lookups fail.
105 */
106 @Test
107 public void testClassWithSetter() throws Exception {
108 boolean gotException = false;
109 try {
110 assertThatClassIsImmutable(ClassWithSetter.class);
111 } catch (AssertionError assertion) {
112 assertThat(assertion.getMessage(),
113 containsString("a class with a setter named 'setX'"));
114 gotException = true;
115 }
116 assertThat(gotException, is(true));
117 }
118
119}
120