Ray Milkey | 22553b9 | 2014-05-06 11:06:21 -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.UtilityClassChecker.assertThatClassIsUtility; |
| 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 utility class |
| 12 | * checker. |
| 13 | */ |
| 14 | public class UtilityClassCheckerTest { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 15 | |
| 16 | // CHECKSTYLE:OFF test data intentionally not final |
Ray Milkey | 22553b9 | 2014-05-06 11:06:21 -0700 | [diff] [blame] | 17 | /** |
| 18 | * Test class for non final class check. |
| 19 | */ |
| 20 | static class NonFinal { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 21 | private NonFinal() { } |
Ray Milkey | 22553b9 | 2014-05-06 11:06:21 -0700 | [diff] [blame] | 22 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 23 | // CHECKSTYLE:ON |
Ray Milkey | 22553b9 | 2014-05-06 11:06:21 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Check that a non final class correctly produces an error. |
| 27 | * @throws Exception if any of the reflection lookups fail. |
| 28 | */ |
| 29 | @Test |
| 30 | public void testNonFinalClass() throws Exception { |
| 31 | boolean gotException = false; |
| 32 | try { |
| 33 | assertThatClassIsUtility(NonFinal.class); |
| 34 | } catch (AssertionError assertion) { |
| 35 | assertThat(assertion.getMessage(), |
| 36 | containsString("is not final")); |
| 37 | gotException = true; |
| 38 | } |
| 39 | assertThat(gotException, is(true)); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Test class for final no constructor class check. |
| 44 | */ |
| 45 | static final class FinalNoConstructor { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Check that a final class with no declared constructor correctly produces |
| 50 | * an error. In this case, the compiler generates a default constructor |
| 51 | * for you, but the constructor is 'protected' and will fail the check. |
| 52 | * |
| 53 | * @throws Exception if any of the reflection lookups fail. |
| 54 | */ |
| 55 | @Test |
| 56 | public void testFinalNoConstructorClass() throws Exception { |
| 57 | boolean gotException = false; |
| 58 | try { |
| 59 | assertThatClassIsUtility(FinalNoConstructor.class); |
| 60 | } catch (AssertionError assertion) { |
| 61 | assertThat(assertion.getMessage(), |
| 62 | containsString("class with a default constructor that " + |
| 63 | "is not private")); |
| 64 | gotException = true; |
| 65 | } |
| 66 | assertThat(gotException, is(true)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Test class for class with more than one constructor check. |
| 71 | */ |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 72 | static final class TwoConstructors { |
| 73 | private TwoConstructors() { } |
| 74 | private TwoConstructors(int x) { } |
Ray Milkey | 22553b9 | 2014-05-06 11:06:21 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check that a non static class correctly produces an error. |
| 79 | * @throws Exception if any of the reflection lookups fail. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testOnlyOneConstructor() throws Exception { |
| 83 | boolean gotException = false; |
| 84 | try { |
| 85 | assertThatClassIsUtility(TwoConstructors.class); |
| 86 | } catch (AssertionError assertion) { |
| 87 | assertThat(assertion.getMessage(), |
| 88 | containsString("more than one constructor")); |
| 89 | gotException = true; |
| 90 | } |
| 91 | assertThat(gotException, is(true)); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Test class with a non private constructor. |
| 96 | */ |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 97 | static final class NonPrivateConstructor { |
| 98 | protected NonPrivateConstructor() { } |
Ray Milkey | 22553b9 | 2014-05-06 11:06:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Check that a class with a non private constructor correctly |
| 103 | * produces an error. |
| 104 | * @throws Exception if any of the reflection lookups fail. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testNonPrivateConstructor() throws Exception { |
| 108 | |
| 109 | boolean gotException = false; |
| 110 | try { |
| 111 | assertThatClassIsUtility(NonPrivateConstructor.class); |
| 112 | } catch (AssertionError assertion) { |
| 113 | assertThat(assertion.getMessage(), |
| 114 | containsString("constructor that is not private")); |
| 115 | gotException = true; |
| 116 | } |
| 117 | assertThat(gotException, is(true)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Test class with a non static method. |
| 122 | */ |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 123 | static final class NonStaticMethod { |
| 124 | private NonStaticMethod() { } |
| 125 | public void aPublicMethod() { } |
Ray Milkey | 22553b9 | 2014-05-06 11:06:21 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Check that a class with a non static method correctly produces an error. |
| 130 | * @throws Exception if any of the reflection lookups fail. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testNonStaticMethod() throws Exception { |
| 134 | |
| 135 | boolean gotException = false; |
| 136 | try { |
| 137 | assertThatClassIsUtility(NonStaticMethod.class); |
| 138 | } catch (AssertionError assertion) { |
| 139 | assertThat(assertion.getMessage(), |
| 140 | containsString("one or more non-static methods")); |
| 141 | gotException = true; |
| 142 | } |
| 143 | assertThat(gotException, is(true)); |
| 144 | } |
| 145 | } |