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