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 | */ |
Yuta HIGUCHI | 5dc8664 | 2014-06-25 14:13:45 -0700 | [diff] [blame] | 18 | // CHECKSTYLE IGNORE FinalClass FOR NEXT 1 LINES |
Ray Milkey | 27bd335 | 2014-05-14 16:59:25 -0700 | [diff] [blame] | 19 | static class NonFinal { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 20 | private NonFinal() { } |
Ray Milkey | 27bd335 | 2014-05-14 16:59:25 -0700 | [diff] [blame] | 21 | } |
Ray Milkey | 27bd335 | 2014-05-14 16:59:25 -0700 | [diff] [blame] | 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 | |