|
23 | 23 |
|
24 | 24 | import org.junit.Test;
|
25 | 25 |
|
26 |
| -import static org.hamcrest.CoreMatchers.containsString; |
27 |
| -import static org.hamcrest.CoreMatchers.startsWith; |
28 |
| -import static org.junit.Assert.assertEquals; |
29 |
| -import static org.junit.Assert.assertNotNull; |
30 |
| -import static org.junit.Assert.assertThrows; |
31 |
| -import static org.hamcrest.MatcherAssert.assertThat; |
32 |
| -import static org.junit.Assert.assertTrue; |
33 |
| - |
34 | 26 | import org.apache.cassandra.auth.AllowAllAuthorizer;
|
35 | 27 | import org.apache.cassandra.auth.IAuthorizer;
|
36 | 28 | import org.apache.cassandra.exceptions.ConfigurationException;
|
37 | 29 |
|
| 30 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 31 | +import static org.junit.Assert.assertNotNull; |
| 32 | +import static org.junit.Assert.assertNull; |
| 33 | + |
38 | 34 | public class ParameterizedClassTest
|
39 | 35 | {
|
40 | 36 | @Test
|
41 |
| - public void newInstance_NonExistentClass_FailsWithConfigurationException() |
| 37 | + public void testParameterizedClassEmptyConstructorHasNullParameters() |
| 38 | + { |
| 39 | + ParameterizedClass parameterizedClass = new ParameterizedClass(); |
| 40 | + assertNull(parameterizedClass.parameters); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testParameterizedClassConstructorWithClassNameHasNonNullParameters() |
42 | 45 | {
|
43 |
| - ParameterizedClass nonExistentClass = new ParameterizedClass("NonExistentClass"); |
| 46 | + ParameterizedClass parameterizedClass = new ParameterizedClass("TestClass"); |
| 47 | + assertNotNull(parameterizedClass.parameters); |
| 48 | + } |
44 | 49 |
|
45 |
| - ConfigurationException exception = assertThrows(ConfigurationException.class, () -> { |
46 |
| - ParameterizedClass.newInstance(nonExistentClass, List.of("org.apache.cassandra.config")); |
47 |
| - }); |
| 50 | + @Test |
| 51 | + public void testParameterizedClassConstructorWithClassNameAndParametersHasNullParamters() |
| 52 | + { |
| 53 | + ParameterizedClass parameterizedClass = new ParameterizedClass("TestClass", null); |
| 54 | + assertNull(parameterizedClass.parameters); |
| 55 | + } |
48 | 56 |
|
49 |
| - String expectedError = "Unable to find class NonExistentClass in packages [\"org.apache.cassandra.config\"]"; |
50 |
| - assertEquals(expectedError, exception.getMessage()); |
| 57 | + @Test |
| 58 | + public void testNewInstanceWithNonExistentClassFailsWithConfigurationException() |
| 59 | + { |
| 60 | + assertThatThrownBy(() -> ParameterizedClass.newInstance(new ParameterizedClass("NonExistentClass"), |
| 61 | + List.of("org.apache.cassandra.config"))) |
| 62 | + .hasMessage("Unable to find class NonExistentClass in packages [\"org.apache.cassandra.config\"]") |
| 63 | + .isInstanceOf(ConfigurationException.class); |
51 | 64 | }
|
52 | 65 |
|
53 | 66 | @Test
|
54 |
| - public void newInstance_WithSingleEmptyConstructor_UsesEmptyConstructor() |
| 67 | + public void testNewInstanceWithSingleEmptyConstructorUsesEmptyConstructor() |
55 | 68 | {
|
56 | 69 | ParameterizedClass parameterizedClass = new ParameterizedClass(AllowAllAuthorizer.class.getName());
|
57 | 70 | IAuthorizer instance = ParameterizedClass.newInstance(parameterizedClass, null);
|
58 | 71 | assertNotNull(instance);
|
59 | 72 | }
|
60 | 73 |
|
61 | 74 | @Test
|
62 |
| - public void newInstance_SingleEmptyConstructorWithParameters_FailsWithConfigurationException() |
| 75 | + public void testNewInstanceWithValidConstructorsFavorsMapConstructor() |
63 | 76 | {
|
64 |
| - Map<String, String> parameters = Map.of("key", "value"); |
65 |
| - ParameterizedClass parameterizedClass = new ParameterizedClass(AllowAllAuthorizer.class.getName(), parameters); |
66 |
| - |
67 |
| - ConfigurationException exception = assertThrows(ConfigurationException.class, () -> { |
68 |
| - ParameterizedClass.newInstance(parameterizedClass, null); |
69 |
| - }); |
70 |
| - |
71 |
| - assertThat(exception.getMessage(), startsWith("No valid constructor found for class")); |
| 77 | + ParameterizedClass parameterizedClass = new ParameterizedClass(ParameterizedClassExample.class.getName()); |
| 78 | + ParameterizedClassExample instance = ParameterizedClass.newInstance(parameterizedClass, null); |
| 79 | + assertNotNull(instance); |
72 | 80 | }
|
73 | 81 |
|
74 | 82 | @Test
|
75 |
| - public void newInstance_WithValidConstructors_FavorsMapConstructor() |
| 83 | + public void testNewInstanceWithValidConstructorsUsingNullParamtersFavorsMapConstructor() |
76 | 84 | {
|
77 | 85 | ParameterizedClass parameterizedClass = new ParameterizedClass(ParameterizedClassExample.class.getName());
|
78 |
| - ParameterizedClassExample instance = ParameterizedClass.newInstance(parameterizedClass, null); |
| 86 | + parameterizedClass.parameters = null; |
79 | 87 |
|
80 |
| - assertTrue(instance.calledMapConstructor); |
| 88 | + ParameterizedClassExample instance = ParameterizedClass.newInstance(parameterizedClass, null); |
| 89 | + assertNotNull(instance); |
81 | 90 | }
|
82 | 91 |
|
83 | 92 | @Test
|
84 |
| - public void newInstance_WithConstructorException_PreservesOriginalFailure() |
| 93 | + public void testNewInstanceWithConstructorExceptionPreservesOriginalFailure() |
85 | 94 | {
|
86 |
| - Map <String, String> parameters = Map.of("fail", "true"); |
87 |
| - ParameterizedClass parameterizedClass = new ParameterizedClass(ParameterizedClassExample.class.getName(), parameters); |
88 |
| - |
89 |
| - ConfigurationException exception = assertThrows(ConfigurationException.class, () -> { |
90 |
| - ParameterizedClass.newInstance(parameterizedClass, null); |
91 |
| - }); |
92 |
| - |
93 |
| - assertThat(exception.getMessage(), startsWith("Failed to instantiate class")); |
94 |
| - assertThat(exception.getMessage(), containsString("Simulated failure")); |
| 95 | + assertThatThrownBy(() -> ParameterizedClass.newInstance(new ParameterizedClass(ParameterizedClassExample.class.getName(), |
| 96 | + Map.of("fail", "true")), null)) |
| 97 | + .hasMessageStartingWith("Failed to instantiate class") |
| 98 | + .hasMessageContaining("Simulated failure") |
| 99 | + .isInstanceOf(ConfigurationException.class); |
95 | 100 | }
|
96 | 101 | }
|
0 commit comments