Migrate tests to AssertJ
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -45,6 +45,12 @@
|
|||||||
<version>1.48</version>
|
<version>1.48</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
package org.springframework.shell2;
|
package org.springframework.shell2;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.springframework.util.ReflectionUtils.findMethod;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.core.convert.support.DefaultConversionService;
|
import org.springframework.core.convert.support.DefaultConversionService;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.util.ReflectionUtils.findMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for DefaultParameterResolver.
|
* Unit tests for DefaultParameterResolver.
|
||||||
*
|
*
|
||||||
@@ -31,19 +29,19 @@ public class DefaultParameterResolverTest {
|
|||||||
assertThat(resolver.resolve(
|
assertThat(resolver.resolve(
|
||||||
makeMethodParameter(method, 0),
|
makeMethodParameter(method, 0),
|
||||||
asList("--force --name --foo y".split(" "))
|
asList("--force --name --foo y".split(" "))
|
||||||
), is(true));
|
)).isEqualTo(true);
|
||||||
assertThat(resolver.resolve(
|
assertThat(resolver.resolve(
|
||||||
makeMethodParameter(method, 1),
|
makeMethodParameter(method, 1),
|
||||||
asList("--force --name --foo y".split(" "))
|
asList("--force --name --foo y".split(" "))
|
||||||
), is("--foo"));
|
)).isEqualTo("--foo");
|
||||||
assertThat(resolver.resolve(
|
assertThat(resolver.resolve(
|
||||||
makeMethodParameter(method, 2),
|
makeMethodParameter(method, 2),
|
||||||
asList("--force --name --foo y".split(" "))
|
asList("--force --name --foo y".split(" "))
|
||||||
), is("y"));
|
)).isEqualTo("y");
|
||||||
assertThat(resolver.resolve(
|
assertThat(resolver.resolve(
|
||||||
makeMethodParameter(method, 3),
|
makeMethodParameter(method, 3),
|
||||||
asList("--force --name --foo y".split(" "))
|
asList("--force --name --foo y".split(" "))
|
||||||
), is("last"));
|
)).isEqualTo("last");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,45 +1,43 @@
|
|||||||
package org.springframework.shell2.jcommander;
|
package org.springframework.shell2.jcommander;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import org.junit.Test;
|
||||||
import static org.hamcrest.Matchers.contains;
|
import org.springframework.core.MethodParameter;
|
||||||
import static org.hamcrest.Matchers.is;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import org.springframework.core.MethodParameter;
|
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ericbottard on 15/12/15.
|
* Created by ericbottard on 15/12/15.
|
||||||
*/
|
*/
|
||||||
public class JCommanderParameterResolverTest {
|
public class JCommanderParameterResolverTest {
|
||||||
|
|
||||||
public static final Method COMMAND_METHOD = ReflectionUtils.findMethod(MyLordCommands.class, "genesis", FieldCollins.class);
|
private static final Method COMMAND_METHOD = ReflectionUtils.findMethod(MyLordCommands.class, "genesis", FieldCollins.class);
|
||||||
|
|
||||||
private JCommanderParameterResolver resolver = new JCommanderParameterResolver();
|
private JCommanderParameterResolver resolver = new JCommanderParameterResolver();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSupportsJCommanderPojos() throws Exception {
|
public void testSupportsJCommanderPojos() throws Exception {
|
||||||
|
assertThat(resolver.supports(new MethodParameter(COMMAND_METHOD, 0))).isEqualTo(true);
|
||||||
assertThat(resolver.supports(new MethodParameter(COMMAND_METHOD, 0)), is(true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDoesNotSupportsNonJCommanderPojos() throws Exception {
|
public void testDoesNotSupportsNonJCommanderPojos() throws Exception {
|
||||||
Method method = ReflectionUtils.findMethod(MyLordCommands.class, "apocalypse", String.class);
|
Method method = ReflectionUtils.findMethod(MyLordCommands.class, "apocalypse", String.class);
|
||||||
|
|
||||||
assertThat(resolver.supports(new MethodParameter(method, 0)), is(false));
|
assertThat(resolver.supports(new MethodParameter(method, 0))).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPojoValuesAreCorrectlySet() {
|
public void testPojoValuesAreCorrectlySet() {
|
||||||
MethodParameter methodParameter = new MethodParameter(COMMAND_METHOD, 0);
|
MethodParameter methodParameter = new MethodParameter(COMMAND_METHOD, 0);
|
||||||
|
|
||||||
FieldCollins resolved = (FieldCollins) resolver.resolve(methodParameter, asList("--name foo -level 2 something-else yet-something-else".split(" ")));
|
FieldCollins resolved = (FieldCollins) resolver.resolve(methodParameter, asList("--name foo -level 2 something-else yet-something-else".split(" ")));
|
||||||
assertThat(resolved.getName(), is("foo"));
|
|
||||||
assertThat(resolved.getLevel(), is(2));
|
assertThat(resolved.getName()).isEqualTo("foo");
|
||||||
assertThat(resolved.getRest(), contains("something-else", "yet-something-else"));
|
assertThat(resolved.getLevel()).isEqualTo(2);
|
||||||
|
assertThat(resolved.getRest()).containsOnlyOnce("something-else", "yet-something-else");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,8 @@
|
|||||||
package org.springframework.shell2.legacy;
|
package org.springframework.shell2.legacy;
|
||||||
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.hamcrest.collection.IsMapContaining;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -17,6 +12,11 @@ import org.springframework.shell2.MethodTargetResolver;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.data.MapEntry.entry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ericbottard on 09/12/15.
|
* Created by ericbottard on 09/12/15.
|
||||||
*/
|
*/
|
||||||
@@ -37,7 +37,7 @@ public class LegacyMethodTargetResolverTest {
|
|||||||
public void findsMethodsAnnotatedWithCliCommand() throws Exception {
|
public void findsMethodsAnnotatedWithCliCommand() throws Exception {
|
||||||
Map<String, MethodTarget> targets = resolver.resolve(applicationContext);
|
Map<String, MethodTarget> targets = resolver.resolve(applicationContext);
|
||||||
|
|
||||||
assertThat(targets, IsMapContaining.hasEntry(
|
assertThat(targets).contains(entry(
|
||||||
"register module",
|
"register module",
|
||||||
new MethodTarget(LegacyCommands.REGISTER_METHOD, legacyCommands, "Register a new module" )
|
new MethodTarget(LegacyCommands.REGISTER_METHOD, legacyCommands, "Register a new module" )
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -1,17 +1,9 @@
|
|||||||
package org.springframework.shell2.legacy;
|
package org.springframework.shell2.legacy;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
import org.hamcrest.Matchers;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -25,6 +17,12 @@ import org.springframework.shell2.ParameterResolver;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.shell2.legacy.LegacyCommands.REGISTER_METHOD;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = LegacyParameterResolverTest.Config.class)
|
@ContextConfiguration(classes = LegacyParameterResolverTest.Config.class)
|
||||||
public class LegacyParameterResolverTest {
|
public class LegacyParameterResolverTest {
|
||||||
@@ -42,85 +40,76 @@ public class LegacyParameterResolverTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void supportsParameterAnnotatedWithCliOption() throws Exception {
|
public void supportsParameterAnnotatedWithCliOption() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, NAME_OR_ANONYMOUS);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, NAME_OR_ANONYMOUS);
|
||||||
|
|
||||||
boolean supports = parameterResolver.supports(methodParameter);
|
boolean result = parameterResolver.supports(methodParameter);
|
||||||
|
|
||||||
assertThat(supports, is(true));
|
assertThat(result).isTrue();
|
||||||
}
|
|
||||||
|
|
||||||
private MethodParameter buildMethodParameter(Method method, int index) {
|
|
||||||
MethodParameter methodParameter = new MethodParameter(method, index);
|
|
||||||
methodParameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
|
||||||
return methodParameter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolvesParameterAnnotatedWithCliOption() throws Exception {
|
public void resolvesParameterAnnotatedWithCliOption() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, NAME_OR_ANONYMOUS);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, NAME_OR_ANONYMOUS);
|
||||||
|
|
||||||
Object result = parameterResolver.resolve(methodParameter, asList("--foo bar --name baz --qix bux".split(" ")));
|
Object result = resolve(methodParameter, "--foo bar --name baz --qix bux");
|
||||||
|
|
||||||
assertThat(result, is("baz"));
|
assertThat(result).isEqualTo("baz");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolvesAnonymousParameterAnnotatedWithCliOption() throws Exception {
|
public void resolvesAnonymousParameterAnnotatedWithCliOption() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, NAME_OR_ANONYMOUS);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, NAME_OR_ANONYMOUS);
|
||||||
|
|
||||||
Object result = parameterResolver.resolve(methodParameter, asList("--foo bar baz --qix bux".split(" ")));
|
Object result = resolve(methodParameter, "--foo bar baz --qix bux");
|
||||||
assertThat(result, is("baz"));
|
assertThat(result).isEqualTo("baz");
|
||||||
|
|
||||||
// As first param
|
// As first param
|
||||||
result = parameterResolver.resolve(methodParameter, asList("baz --foo bar --qix bux".split(" ")));
|
result = resolve(methodParameter, "baz --foo bar --qix bux");
|
||||||
assertThat(result, is("baz"));
|
assertThat(result).isEqualTo("baz");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void usesLegacyConverters() throws Exception {
|
public void usesLegacyConverters() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, TYPE);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, TYPE);
|
||||||
|
|
||||||
Object result = parameterResolver.resolve(methodParameter, asList("--foo bar --name baz --qix bux --type processor".split(" ")));
|
Object result = resolve(methodParameter, "--foo bar --name baz --qix bux --type processor");
|
||||||
|
|
||||||
assertThat(result, Matchers.is(ArtifactType.processor));
|
assertThat(result).isSameAs(ArtifactType.processor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnspecifiedDefaultValue() throws Exception {
|
public void testUnspecifiedDefaultValue() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, FORCE);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, FORCE);
|
||||||
|
|
||||||
Object result = parameterResolver.resolve(methodParameter, asList("--foo bar --name baz --qix bux".split(" ")));
|
Object result = resolve(methodParameter, "--foo bar --name baz --qix bux");
|
||||||
|
|
||||||
assertThat(result, is(false));
|
assertThat(result).isEqualTo(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSpecifiedDefaultValue() throws Exception {
|
public void testSpecifiedDefaultValue() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, FORCE);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, FORCE);
|
||||||
|
|
||||||
assertThat(parameterResolver.resolve(methodParameter, asList("--force --foo bar --name baz --qix bux".split(" "))), is(true));
|
assertThat(resolve(methodParameter, "--force --foo bar --name baz --qix bux")).isEqualTo(true);
|
||||||
assertThat(parameterResolver.resolve(methodParameter, asList("--foo bar --name baz --qix bux --force".split(" "))), is(true));
|
assertThat(resolve(methodParameter, "--foo bar --name baz --qix bux --force")).isEqualTo(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParameterNotFound() throws Exception {
|
public void testParameterNotFound() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, COORDINATES);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, COORDINATES);
|
||||||
|
|
||||||
thrown.expect(IllegalArgumentException.class);
|
thrown.expect(IllegalArgumentException.class);
|
||||||
thrown.expectMessage("Could not find parameter values for [--coordinates, --coords] in [--force, --foo, bar, --name, baz, --qix, bux]");
|
thrown.expectMessage("Could not find parameter values for [--coordinates, --coords] in [--force, --foo, bar, --name, baz, --qix, bux]");
|
||||||
parameterResolver.resolve(methodParameter, asList("--force --foo bar --name baz --qix bux".split(" ")));
|
resolve(methodParameter, "--force --foo bar --name baz --qix bux");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParameterFoundTooManyTimes() throws Exception {
|
public void testParameterFoundWithSameNameTooManyTimes() throws Exception {
|
||||||
MethodParameter methodParameter = buildMethodParameter(LegacyCommands.REGISTER_METHOD, COORDINATES);
|
MethodParameter methodParameter = buildMethodParameter(REGISTER_METHOD, COORDINATES);
|
||||||
|
|
||||||
thrown.expect(IllegalArgumentException.class);
|
thrown.expect(IllegalArgumentException.class);
|
||||||
thrown.expectMessage("Option --coordinates has already been set");
|
thrown.expectMessage("Option --coordinates has already been set");
|
||||||
// with the exact same name
|
resolve(methodParameter, "--force --coordinates bar --coordinates baz --qix bux");
|
||||||
parameterResolver.resolve(methodParameter, asList("--force --coordinates bar --coordinates baz --qix bux".split(" ")));
|
|
||||||
// or even with aliases
|
|
||||||
parameterResolver.resolve(methodParameter, asList("--force --coordinates bar --coords baz --qix bux".split(" ")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -129,9 +118,7 @@ public class LegacyParameterResolverTest {
|
|||||||
|
|
||||||
thrown.expect(IllegalStateException.class);
|
thrown.expect(IllegalStateException.class);
|
||||||
thrown.expectMessage("No converter found for --v1 from '1' to type int");
|
thrown.expectMessage("No converter found for --v1 from '1' to type int");
|
||||||
parameterResolver.resolve(methodParameter, asList("--v1 1 --v2 2".split(" ")));
|
resolve(methodParameter, "--v1 1 --v2 2");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -140,7 +127,7 @@ public class LegacyParameterResolverTest {
|
|||||||
|
|
||||||
thrown.expect(IllegalStateException.class);
|
thrown.expect(IllegalStateException.class);
|
||||||
thrown.expectMessage("No converter found for --v1 from '38' to type int");
|
thrown.expectMessage("No converter found for --v1 from '38' to type int");
|
||||||
parameterResolver.resolve(methodParameter, asList("--v2 2".split(" ")));
|
resolve(methodParameter, "--v2 2");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -149,7 +136,17 @@ public class LegacyParameterResolverTest {
|
|||||||
|
|
||||||
thrown.expect(IllegalStateException.class);
|
thrown.expect(IllegalStateException.class);
|
||||||
thrown.expectMessage("No converter found for --v2 from '42' to type int");
|
thrown.expectMessage("No converter found for --v2 from '42' to type int");
|
||||||
parameterResolver.resolve(methodParameter, asList("--v1 1 --v2".split(" ")));
|
resolve(methodParameter, "--v1 1 --v2");
|
||||||
|
}
|
||||||
|
|
||||||
|
private MethodParameter buildMethodParameter(Method method, int index) {
|
||||||
|
MethodParameter methodParameter = new MethodParameter(method, index);
|
||||||
|
methodParameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||||
|
return methodParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object resolve(MethodParameter methodParameter, String command) {
|
||||||
|
return parameterResolver.resolve(methodParameter, asList(command.split(" ")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|||||||
Reference in New Issue
Block a user