Ray Milkey | 27bd335 | 2014-05-14 16:59:25 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.util; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import static net.onrc.onos.core.util.ImmutableClassChecker.assertThatClassIsImmutable; |
| 6 | import static org.hamcrest.MatcherAssert.assertThat; |
| 7 | import static org.hamcrest.Matchers.containsString; |
| 8 | import static org.hamcrest.Matchers.is; |
| 9 | |
| 10 | /** |
| 11 | * Set of unit tests to check the implementation of the immutable class |
| 12 | * checker. |
| 13 | */ |
| 14 | public class ImmutableClassCheckerTest { |
| 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 | assertThatClassIsImmutable(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 non private member class check. |
| 41 | */ |
| 42 | static final class FinalProtectedMember { |
| 43 | protected final int x = 0; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Check that a final class with a non-private member is properly detected. |
| 48 | * |
| 49 | * @throws Exception if any of the reflection lookups fail. |
| 50 | */ |
| 51 | @Test |
| 52 | public void testFinalProtectedMember() throws Exception { |
| 53 | boolean gotException = false; |
| 54 | try { |
| 55 | assertThatClassIsImmutable(FinalProtectedMember.class); |
| 56 | } catch (AssertionError assertion) { |
| 57 | assertThat(assertion.getMessage(), |
| 58 | containsString("a field named 'x' that is not private")); |
| 59 | gotException = true; |
| 60 | } |
| 61 | assertThat(gotException, is(true)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Test class for non private member class check. |
| 66 | */ |
| 67 | static final class NotFinalPrivateMember { |
| 68 | private int x = 0; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check that a final class with a non-final private |
| 73 | * member is properly detected. |
| 74 | * |
| 75 | * @throws Exception if any of the reflection lookups fail. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testNotFinalPrivateMember() throws Exception { |
| 79 | boolean gotException = false; |
| 80 | try { |
| 81 | assertThatClassIsImmutable(NotFinalPrivateMember.class); |
| 82 | } catch (AssertionError assertion) { |
| 83 | assertThat(assertion.getMessage(), |
| 84 | containsString("a field named 'x' that is not final")); |
| 85 | gotException = true; |
| 86 | } |
| 87 | assertThat(gotException, is(true)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Test class for non private member class check. |
| 92 | */ |
| 93 | static final class ClassWithSetter { |
| 94 | private final int x = 0; |
| 95 | public void setX(int newX) { |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Check that a final class with a final private |
| 101 | * member that is modifyable by a setter is properly detected. |
| 102 | * |
| 103 | * @throws Exception if any of the reflection lookups fail. |
| 104 | */ |
| 105 | @Test |
| 106 | public void testClassWithSetter() throws Exception { |
| 107 | boolean gotException = false; |
| 108 | try { |
| 109 | assertThatClassIsImmutable(ClassWithSetter.class); |
| 110 | } catch (AssertionError assertion) { |
| 111 | assertThat(assertion.getMessage(), |
| 112 | containsString("a class with a setter named 'setX'")); |
| 113 | gotException = true; |
| 114 | } |
| 115 | assertThat(gotException, is(true)); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |