Skip to content

Commit 96ff289

Browse files
committed
suggestions
1 parent aab2205 commit 96ff289

File tree

3 files changed

+32
-49
lines changed

3 files changed

+32
-49
lines changed

src/java/org/apache/cassandra/auth/AuthConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static void applyAuth()
8585
IRoleManager roleManager = authInstantiate(conf.role_manager, CassandraRoleManager.class);
8686

8787
if (authenticator instanceof PasswordAuthenticator && !(roleManager instanceof CassandraRoleManager))
88-
throw new ConfigurationException(authenticator.getClass().getName() + " requires CassandraRoleManager", false);
88+
throw new ConfigurationException(authenticator.getClass().getName() + " requires " + CassandraRoleManager.class.getName(), false);
8989

9090
DatabaseDescriptor.setRoleManager(roleManager);
9191

src/java/org/apache/cassandra/config/ParameterizedClass.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
import java.lang.reflect.Constructor;
2121
import java.lang.reflect.InvocationTargetException;
22-
import java.util.Arrays;
2322
import java.util.Collections;
2423
import java.util.List;
2524
import java.util.Map;
25+
import java.util.function.Predicate;
2626
import java.util.stream.Collectors;
2727

2828
import com.google.common.base.Objects;
@@ -96,23 +96,18 @@ static public <K> K newInstance(ParameterizedClass parameterizedClass, List<Stri
9696
{
9797
Constructor<?>[] declaredConstructors = providerClass.getDeclaredConstructors();
9898

99-
Constructor mapConstructor = Arrays.stream(declaredConstructors)
100-
.filter(c ->
101-
c.getParameterTypes().length == 1 && c.getParameterTypes()[0].equals(Map.class))
102-
.findFirst()
103-
.orElse(null);
99+
Constructor<?> mapConstructor = filterConstructor(declaredConstructors, c -> c.getParameterTypes().length == 1 && c.getParameterTypes()[0].equals(Map.class));
100+
104101
if (mapConstructor != null)
105102
return (K) mapConstructor.newInstance(parameterizedClass.parameters);
106103

107104
// Falls-back to no-arg constructor if no parameters are present
108105
if (parameterizedClass.parameters == null || parameterizedClass.parameters.isEmpty())
109106
{
110-
Constructor emptyConstructor = Arrays.stream(declaredConstructors)
111-
.filter(c -> c.getParameterTypes().length == 0)
112-
.findFirst()
113-
.orElse(null);
114-
if (emptyConstructor != null)
115-
return (K) emptyConstructor.newInstance();
107+
Constructor<?> noArgsConstructor = filterConstructor(declaredConstructors, c -> c.getParameterTypes().length == 0);
108+
109+
if (noArgsConstructor != null)
110+
return (K) noArgsConstructor.newInstance();
116111
}
117112

118113
throw new ConfigurationException("No valid constructor found for class " + parameterizedClass.class_name);
@@ -129,6 +124,15 @@ static public <K> K newInstance(ParameterizedClass parameterizedClass, List<Stri
129124
}
130125
}
131126

127+
static private Constructor<?> filterConstructor(Constructor<?>[] declaredConstructors, Predicate<Constructor<?>> filter)
128+
{
129+
for (Constructor<?> constructor : declaredConstructors)
130+
if (filter.test(constructor))
131+
return constructor;
132+
133+
return null;
134+
}
135+
132136
@Override
133137
public boolean equals(Object that)
134138
{

test/unit/org/apache/cassandra/config/ParameterizedClassTest.java

+15-36
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,24 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24-
import org.junit.Rule;
2524
import org.junit.Test;
2625

27-
import static org.hamcrest.CoreMatchers.containsString;
28-
import static org.hamcrest.CoreMatchers.startsWith;
29-
import static org.junit.Assert.assertNotNull;
30-
import static org.junit.Assert.assertTrue;
31-
import static org.junit.Assert.fail;
32-
3326
import org.apache.cassandra.auth.AllowAllAuthorizer;
3427
import org.apache.cassandra.auth.IAuthorizer;
3528
import org.apache.cassandra.exceptions.ConfigurationException;
36-
import org.junit.rules.ExpectedException;
29+
30+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertTrue;
3733

3834
public class ParameterizedClassTest
3935
{
40-
@Rule
41-
public ExpectedException exceptionRule = ExpectedException.none();
42-
4336
@Test
4437
public void newInstance_NonExistentClass_FailsWithConfigurationException()
4538
{
46-
exceptionRule.expect(ConfigurationException.class);
47-
48-
String expectedError = "Unable to find class NonExistentClass in packages [\"org.apache.cassandra.config\"]";
49-
exceptionRule.expectMessage(expectedError);
50-
51-
ParameterizedClass parameterizedClass = new ParameterizedClass("NonExistentClass");
52-
ParameterizedClass.newInstance(parameterizedClass, List.of("org.apache.cassandra.config"));
53-
fail();
39+
assertThatThrownBy(() -> ParameterizedClass.newInstance(new ParameterizedClass("NonExistentClass"), List.of("org.apache.cassandra.config")))
40+
.hasMessage("Unable to find class NonExistentClass in packages [\"org.apache.cassandra.config\"]")
41+
.isInstanceOf(ConfigurationException.class);
5442
}
5543

5644
@Test
@@ -64,14 +52,9 @@ public void newInstance_WithSingleEmptyConstructor_UsesEmptyConstructor()
6452
@Test
6553
public void newInstance_SingleEmptyConstructorWithParameters_FailsWithConfigurationException()
6654
{
67-
exceptionRule.expect(ConfigurationException.class);
68-
exceptionRule.expectMessage(startsWith("No valid constructor found for class"));
69-
70-
Map<String, String> parameters = Map.of("key", "value");
71-
ParameterizedClass parameterizedClass = new ParameterizedClass(AllowAllAuthorizer.class.getName(), parameters);
72-
73-
ParameterizedClass.newInstance(parameterizedClass, null);
74-
fail();
55+
assertThatThrownBy(() -> ParameterizedClass.newInstance(new ParameterizedClass(AllowAllAuthorizer.class.getName(), Map.of("key", "value")), null))
56+
.hasMessageStartingWith("No valid constructor found for class")
57+
.isInstanceOf(ConfigurationException.class);
7558
}
7659

7760
@Test
@@ -86,14 +69,10 @@ public void newInstance_WithValidConstructors_FavorsMapConstructor()
8669
@Test
8770
public void newInstance_WithConstructorException_PreservesOriginalFailure()
8871
{
89-
exceptionRule.expect(ConfigurationException.class);
90-
exceptionRule.expectMessage(startsWith("Failed to instantiate class"));
91-
exceptionRule.expectMessage(containsString("Simulated failure"));
92-
93-
Map <String, String> parameters = Map.of("fail", "true");
94-
ParameterizedClass parameterizedClass = new ParameterizedClass(ParameterizedClassExample.class.getName(), parameters);
95-
96-
ParameterizedClass.newInstance(parameterizedClass, null);
97-
fail();
72+
assertThatThrownBy(() -> ParameterizedClass.newInstance(new ParameterizedClass(ParameterizedClassExample.class.getName(),
73+
Map.of("fail", "true")), null))
74+
.hasMessageContaining("Simulated failure")
75+
.hasMessageStartingWith("Failed to instantiate class")
76+
.isInstanceOf(ConfigurationException.class);
9877
}
9978
}

0 commit comments

Comments
 (0)