|
| 1 | +package rx.util; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.fail; |
| 7 | + |
| 8 | +public class PrivateConstructionCheckerTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void builderShouldThrowExceptionIfNullWasPassedAsExpectedTypeOfException() { |
| 12 | + try { |
| 13 | + PrivateConstructorChecker |
| 14 | + .forClass(Object.class) |
| 15 | + .expectedTypeOfException(null); |
| 16 | + |
| 17 | + fail(); |
| 18 | + } catch (IllegalArgumentException expected) { |
| 19 | + assertEquals("expectedTypeOfException can not be null", expected.getMessage()); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void builderShouldThrowExceptionIfNullWasPassedAsExpectedExceptionMessage() { |
| 25 | + try { |
| 26 | + PrivateConstructorChecker |
| 27 | + .forClass(Object.class) |
| 28 | + .expectedExceptionMessage(null); |
| 29 | + |
| 30 | + fail(); |
| 31 | + } catch (IllegalArgumentException expected) { |
| 32 | + assertEquals("expectedExceptionMessage can not be null", expected.getMessage()); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static class ClassWithoutDefaultConstructor { |
| 37 | + private ClassWithoutDefaultConstructor(String someParam) { |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void shouldThrowExceptionIfClassHasNonDefaultConstructor() { |
| 43 | + try { |
| 44 | + PrivateConstructorChecker |
| 45 | + .forClass(ClassWithoutDefaultConstructor.class) |
| 46 | + .check(); |
| 47 | + |
| 48 | + fail(); |
| 49 | + } catch (AssertionError expected) { |
| 50 | + assertEquals( |
| 51 | + "Class has non-default constructor with some parameters", |
| 52 | + expected.getMessage() |
| 53 | + ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + static class ClassWithPrivateConstructor { |
| 58 | + private ClassWithPrivateConstructor() { |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void shouldAssertThatConstructorIsPrivateAndDoesNotThrowExceptions() { |
| 64 | + PrivateConstructorChecker |
| 65 | + .forClass(ClassWithPrivateConstructor.class) |
| 66 | + .check(); |
| 67 | + } |
| 68 | + |
| 69 | + static class ClassWithDefaultProtectedConstructor { |
| 70 | + ClassWithDefaultProtectedConstructor() { |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void shouldThrowExceptionBecauseConstructorHasDefaultModifier() { |
| 76 | + try { |
| 77 | + PrivateConstructorChecker |
| 78 | + .forClass(ClassWithDefaultProtectedConstructor.class) |
| 79 | + .check(); |
| 80 | + |
| 81 | + fail(); |
| 82 | + } catch (AssertionError expected) { |
| 83 | + assertEquals("Constructor must be private", expected.getMessage()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + static class ClassWithProtectedConstructor { |
| 88 | + protected ClassWithProtectedConstructor() { |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void shouldThrowExceptionBecauseConstructorHasProtectedModifier() { |
| 94 | + try { |
| 95 | + PrivateConstructorChecker |
| 96 | + .forClass(ClassWithProtectedConstructor.class) |
| 97 | + .check(); |
| 98 | + |
| 99 | + fail(); |
| 100 | + } catch (AssertionError expected) { |
| 101 | + assertEquals("Constructor must be private", expected.getMessage()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static class ClassWithPublicConstructor { |
| 106 | + public ClassWithPublicConstructor() { |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void shouldThrowExceptionBecauseConstructorHasPublicModifier() { |
| 112 | + try { |
| 113 | + PrivateConstructorChecker |
| 114 | + .forClass(ClassWithPublicConstructor.class) |
| 115 | + .check(); |
| 116 | + |
| 117 | + fail(); |
| 118 | + } catch (AssertionError expected) { |
| 119 | + assertEquals("Constructor must be private", expected.getMessage()); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + static class ClassWithConstructorThatThrowsException { |
| 124 | + private ClassWithConstructorThatThrowsException() { |
| 125 | + throw new IllegalStateException("test exception"); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void shouldCheckThatConstructorThrowsExceptionWithoutCheckingMessage() { |
| 131 | + PrivateConstructorChecker |
| 132 | + .forClass(ClassWithConstructorThatThrowsException.class) |
| 133 | + .expectedTypeOfException(IllegalStateException.class) |
| 134 | + .check(); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void shouldCheckThatConstructorThrowsExceptionWithExpectedMessage() { |
| 139 | + PrivateConstructorChecker |
| 140 | + .forClass(ClassWithConstructorThatThrowsException.class) |
| 141 | + .expectedTypeOfException(IllegalStateException.class) |
| 142 | + .expectedExceptionMessage("test exception") |
| 143 | + .check(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void shouldCheckThatConstructorThrowsExceptionWithExpectedMessageButWithoutExpectedExceptionType() { |
| 148 | + PrivateConstructorChecker |
| 149 | + .forClass(ClassWithConstructorThatThrowsException.class) |
| 150 | + .expectedExceptionMessage("test exception") |
| 151 | + .check(); // without checking exception's type |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void shouldThrowExceptionBecauseTypeOfExpectedExceptionDoesNotMatch() { |
| 156 | + try { |
| 157 | + PrivateConstructorChecker |
| 158 | + .forClass(ClassWithConstructorThatThrowsException.class) |
| 159 | + .expectedTypeOfException(IllegalArgumentException.class) // Incorrect type |
| 160 | + .check(); |
| 161 | + |
| 162 | + fail(); |
| 163 | + } catch (IllegalStateException expected) { |
| 164 | + assertEquals("Expected exception of type = class java.lang.IllegalArgumentException, " + |
| 165 | + "but was exception of type = class java.lang.IllegalStateException", |
| 166 | + expected.getMessage() |
| 167 | + ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void shouldThrowExceptionBecauseExpectedMessageDoesNotMatch() { |
| 173 | + try { |
| 174 | + PrivateConstructorChecker |
| 175 | + .forClass(ClassWithConstructorThatThrowsException.class) |
| 176 | + .expectedTypeOfException(IllegalStateException.class) // Correct type |
| 177 | + .expectedExceptionMessage("lol, not something that you've expected?") // Incorrect message |
| 178 | + .check(); |
| 179 | + |
| 180 | + fail(); |
| 181 | + } catch (IllegalStateException expected) { |
| 182 | + assertEquals("Expected exception message = 'lol, not something that you've expected?', " + |
| 183 | + "but was = 'test exception'", |
| 184 | + expected.getMessage() |
| 185 | + ); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void shouldThrowExceptionBecauseConstructorThrownUnexpectedException() { |
| 191 | + try { |
| 192 | + PrivateConstructorChecker |
| 193 | + .forClass(ClassWithConstructorThatThrowsException.class) |
| 194 | + .check(); // We don't expect exception, but it will be thrown |
| 195 | + |
| 196 | + fail(); |
| 197 | + } catch (IllegalStateException expected) { |
| 198 | + assertEquals("No exception was expected", expected.getMessage()); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + static class ClassWith2Constructors { |
| 203 | + // This is good constructor for the checker |
| 204 | + private ClassWith2Constructors() { |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + // This is bad constructor |
| 209 | + private ClassWith2Constructors(String str) { |
| 210 | + |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void shouldThrowExceptionBecauseClassHasMoreThanOneConstructor() { |
| 216 | + try { |
| 217 | + PrivateConstructorChecker |
| 218 | + .forClass(ClassWith2Constructors.class) |
| 219 | + .check(); |
| 220 | + |
| 221 | + fail(); |
| 222 | + } catch (AssertionError expected) { |
| 223 | + assertEquals("Class has more than one constructor", expected.getMessage()); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + static class ClassWithoutDeclaredConstructor { |
| 228 | + |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void shouldThrowExceptionBecauseClassDoesNotHaveDeclaredConstructors() { |
| 233 | + try { |
| 234 | + PrivateConstructorChecker |
| 235 | + .forClass(ClassWithoutDeclaredConstructor.class) |
| 236 | + .check(); |
| 237 | + |
| 238 | + fail(); |
| 239 | + } catch (AssertionError expected) { |
| 240 | + assertEquals("Constructor must be private", expected.getMessage()); |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments