Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 16 | package org.onlab.junit; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | import static org.hamcrest.Matchers.containsString; |
| 22 | import static org.hamcrest.Matchers.is; |
| 23 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 24 | |
| 25 | /** |
| 26 | * Set of unit tests to check the implementation of the immutable class |
| 27 | * checker. |
| 28 | */ |
| 29 | public class ImmutableClassCheckerTest { |
| 30 | /** |
| 31 | * Test class for non final class check. |
| 32 | */ |
| 33 | // CHECKSTYLE IGNORE FinalClass FOR NEXT 1 LINES |
| 34 | static class NonFinal { |
| 35 | private NonFinal() { } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Check that a non final class correctly produces an error. |
| 40 | * @throws Exception if any of the reflection lookups fail. |
| 41 | */ |
| 42 | @Test |
| 43 | public void testNonFinalClass() throws Exception { |
| 44 | boolean gotException = false; |
| 45 | try { |
| 46 | assertThatClassIsImmutable(NonFinal.class); |
| 47 | } catch (AssertionError assertion) { |
| 48 | assertThat(assertion.getMessage(), |
| 49 | containsString("is not final")); |
| 50 | gotException = true; |
| 51 | } |
| 52 | assertThat(gotException, is(true)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Test class for non private member class check. |
| 57 | */ |
| 58 | static final class FinalProtectedMember { |
| 59 | protected final int x = 0; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Check that a final class with a non-private member is properly detected. |
| 64 | * |
| 65 | * @throws Exception if any of the reflection lookups fail. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testFinalProtectedMember() throws Exception { |
| 69 | boolean gotException = false; |
| 70 | try { |
| 71 | assertThatClassIsImmutable(FinalProtectedMember.class); |
| 72 | } catch (AssertionError assertion) { |
| 73 | assertThat(assertion.getMessage(), |
| 74 | containsString("a field named 'x' that is not private")); |
| 75 | gotException = true; |
| 76 | } |
| 77 | assertThat(gotException, is(true)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Test class for non private member class check. |
| 82 | */ |
| 83 | static final class NotFinalPrivateMember { |
| 84 | private int x = 0; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Check that a final class with a non-final private |
| 89 | * member is properly detected. |
| 90 | * |
| 91 | * @throws Exception if any of the reflection lookups fail. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testNotFinalPrivateMember() throws Exception { |
| 95 | boolean gotException = false; |
| 96 | try { |
| 97 | assertThatClassIsImmutable(NotFinalPrivateMember.class); |
| 98 | } catch (AssertionError assertion) { |
| 99 | assertThat(assertion.getMessage(), |
| 100 | containsString("a field named 'x' that is not final")); |
| 101 | gotException = true; |
| 102 | } |
| 103 | assertThat(gotException, is(true)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Test class for non private member class check. |
| 108 | */ |
| 109 | static final class ClassWithSetter { |
| 110 | private final int x = 0; |
| 111 | public void setX(int newX) { |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Check that a final class with a final private |
| 117 | * member that is modifyable by a setter is properly detected. |
| 118 | * |
| 119 | * @throws Exception if any of the reflection lookups fail. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testClassWithSetter() throws Exception { |
| 123 | boolean gotException = false; |
| 124 | try { |
| 125 | assertThatClassIsImmutable(ClassWithSetter.class); |
| 126 | } catch (AssertionError assertion) { |
| 127 | assertThat(assertion.getMessage(), |
| 128 | containsString("a class with a setter named 'setX'")); |
| 129 | gotException = true; |
| 130 | } |
| 131 | assertThat(gotException, is(true)); |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |