Skip to content

Commit

Permalink
Rely on auto-boxing in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jul 12, 2023
1 parent 1edc0d8 commit 68f2b0c
Show file tree
Hide file tree
Showing 32 changed files with 185 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -667,22 +667,22 @@ void setPrimitiveProperties() {
accessor.setPropertyValue("myDouble", doubleValue);

assertThat(target.getMyPrimitiveByte()).isEqualTo(Byte.MAX_VALUE);
assertThat(target.getMyByte().byteValue()).isEqualTo(Byte.MAX_VALUE);
assertThat(target.getMyByte()).isEqualTo(Byte.MAX_VALUE);

assertThat(target.getMyPrimitiveShort()).isEqualTo(Short.MAX_VALUE);
assertThat(target.getMyShort().shortValue()).isEqualTo(Short.MAX_VALUE);
assertThat(target.getMyShort()).isEqualTo(Short.MAX_VALUE);

assertThat(target.getMyPrimitiveInt()).isEqualTo(Integer.MAX_VALUE);
assertThat(target.getMyInteger().intValue()).isEqualTo(Integer.MAX_VALUE);
assertThat(target.getMyInteger()).isEqualTo(Integer.MAX_VALUE);

assertThat(target.getMyPrimitiveLong()).isEqualTo(Long.MAX_VALUE);
assertThat(target.getMyLong().longValue()).isEqualTo(Long.MAX_VALUE);
assertThat(target.getMyLong()).isEqualTo(Long.MAX_VALUE);

assertThat((double) target.getMyPrimitiveFloat()).isCloseTo(Float.MAX_VALUE, within(0.001));
assertThat((double) target.getMyFloat()).isCloseTo(Float.MAX_VALUE, within(0.001));

assertThat(target.getMyPrimitiveDouble()).isCloseTo(Double.MAX_VALUE, within(0.001));
assertThat(target.getMyDouble().doubleValue()).isCloseTo(Double.MAX_VALUE, within(0.001));
assertThat(target.getMyDouble()).isCloseTo(Double.MAX_VALUE, within(0.001));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void testGenericTypeNestingMapOfListOfInteger() {

Object obj = gb.getMapOfListOfInteger().get("testKey").get(0);
assertThat(obj).isInstanceOf(Integer.class);
assertThat(((Integer) obj).intValue()).isEqualTo(1);
assertThat(obj).isEqualTo(1);
}

@Test
Expand All @@ -351,7 +351,7 @@ void testGenericTypeNestingListOfMapOfInteger() {

Object obj = gb.getListOfMapOfInteger().get(0).get("testKey");
assertThat(obj).isInstanceOf(Integer.class);
assertThat(((Integer) obj).intValue()).isEqualTo(5);
assertThat(obj).isEqualTo(5);
}

@Test
Expand All @@ -366,7 +366,7 @@ void testGenericTypeNestingMapOfListOfListOfInteger() {

Object obj = gb.getMapOfListOfListOfInteger().get("testKey").get(0).get(0);
assertThat(obj).isInstanceOf(Integer.class);
assertThat(((Integer) obj).intValue()).isEqualTo(1);
assertThat(obj).isEqualTo(1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3201,9 +3201,9 @@ public CustomTypeConverter(NumberFormat numberFormat) {

@Override
public Object convertIfNecessary(Object value, @Nullable Class requiredType) {
if (value instanceof String && Float.class.isAssignableFrom(requiredType)) {
if (value instanceof String text && Float.class.isAssignableFrom(requiredType)) {
try {
return this.numberFormat.parse((String) value).floatValue();
return this.numberFormat.parse(text).floatValue();
}
catch (ParseException ex) {
throw new TypeMismatchException(value, requiredType, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public void testGetObject() throws Exception {
mcfb.setTargetMethod("method1");
mcfb.afterPropertiesSet();
Integer i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);
i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);

// non-singleton, non-static
tc1 = new TestClass1();
Expand All @@ -147,9 +147,9 @@ public void testGetObject() throws Exception {
mcfb.setSingleton(false);
mcfb.afterPropertiesSet();
i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);
i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(2);
assertThat(i).isEqualTo(2);

// singleton, static
TestClass1._staticField1 = 0;
Expand All @@ -158,9 +158,9 @@ public void testGetObject() throws Exception {
mcfb.setTargetMethod("staticMethod1");
mcfb.afterPropertiesSet();
i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);
i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);

// non-singleton, static
TestClass1._staticField1 = 0;
Expand All @@ -169,9 +169,9 @@ public void testGetObject() throws Exception {
mcfb.setSingleton(false);
mcfb.afterPropertiesSet();
i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);
i = (Integer) mcfb.getObject();
assertThat(i.intValue()).isEqualTo(2);
assertThat(i).isEqualTo(2);

// void return value
mcfb = new MethodInvokingFactoryBean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ void setUp() {
@Test
void testConstant() {
Integer min = (Integer) this.beanFactory.getBean("min");
assertThat(min.intValue()).isEqualTo(Integer.MIN_VALUE);
assertThat(min).isEqualTo(Integer.MIN_VALUE);
}

@Test
void testConstantWithDefaultName() {
Integer max = (Integer) this.beanFactory.getBean("java.lang.Integer.MAX_VALUE");
assertThat(max.intValue()).isEqualTo(Integer.MAX_VALUE);
assertThat(max).isEqualTo(Integer.MAX_VALUE);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public String getConversationId() {
ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
assertThat(tb3.name).isEqualTo("XXXmyNameYYY42ZZZ");
assertThat(tb3.age).isEqualTo(42);
assertThat(tb3.ageFactory.getObject().intValue()).isEqualTo(42);
assertThat(tb3.ageFactory.getObject()).isEqualTo(42);
assertThat(tb3.country).isEqualTo("123 UK");
assertThat(tb3.countryFactory.getObject()).isEqualTo("123 UK");
System.getProperties().put("country", "US");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ void propertiesCorrectInJmx() throws Exception {
Integer age = (Integer) getServer().getAttribute(oname, "Age");

assertThat(name).as("Name is incorrect in JMX").isEqualTo("Rob Harrop");
assertThat(age.intValue()).as("Age is incorrect in JMX").isEqualTo(100);
assertThat(age).as("Age is incorrect in JMX").isEqualTo(100);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,18 @@ void setUp() {

@Test
void scalarList() throws Exception {
List<String> list = new ArrayList<>();
list.add("9");
list.add("37");
List<String> list = List.of("9", "37");
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
try {
conversionService.convert(list, sourceType, targetType);
}
catch (ConversionFailedException ex) {
boolean condition = ex.getCause() instanceof ConverterNotFoundException;
assertThat(condition).isTrue();
}
assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> conversionService.convert(list, sourceType, targetType))
.withCauseInstanceOf(ConverterNotFoundException.class);
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
@SuppressWarnings("unchecked")
List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
assertThat(list.equals(result)).isFalse();
assertThat(result.get(0).intValue()).isEqualTo(9);
assertThat(result.get(1).intValue()).isEqualTo(37);
assertThat(result).isNotEqualTo(list).containsExactly(9, 37);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void plainMethodInvoker() throws Exception {
mi.setTargetMethod("method1");
mi.prepare();
Integer i = (Integer) mi.invoke();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);

// defensive check: singleton, non-static should work with null array
tc1 = new TestClass1();
Expand All @@ -52,7 +52,7 @@ void plainMethodInvoker() throws Exception {
mi.setArguments((Object[]) null);
mi.prepare();
i = (Integer) mi.invoke();
assertThat(i.intValue()).isEqualTo(1);
assertThat(i).isEqualTo(1);

// sanity check: check that argument count matching works
mi = new MethodInvoker();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ void parseNumberRequiringTrimUsingNumberFormat() {

@Test
void parseNumberAsHex() {
String aByte = "0x" + Integer.toHexString(Byte.valueOf(Byte.MAX_VALUE).intValue());
String aShort = "0x" + Integer.toHexString(Short.valueOf(Short.MAX_VALUE).intValue());
String aByte = "0x" + Integer.toHexString(Byte.valueOf(Byte.MAX_VALUE));
String aShort = "0x" + Integer.toHexString(Short.valueOf(Short.MAX_VALUE));
String anInteger = "0x" + Integer.toHexString(Integer.MAX_VALUE);
String aLong = "0x" + Long.toHexString(Long.MAX_VALUE);
String aReallyBigInt = "FEBD4E677898DFEBFFEE44";
Expand Down Expand Up @@ -368,35 +368,35 @@ void convertToLong() {


private void assertLongEquals(String aLong) {
assertThat(NumberUtils.parseNumber(aLong, Long.class).longValue()).as("Long did not parse").isEqualTo(Long.MAX_VALUE);
assertThat(NumberUtils.parseNumber(aLong, Long.class)).as("Long did not parse").isEqualTo(Long.MAX_VALUE);
}

private void assertIntegerEquals(String anInteger) {
assertThat(NumberUtils.parseNumber(anInteger, Integer.class).intValue()).as("Integer did not parse").isEqualTo(Integer.MAX_VALUE);
assertThat(NumberUtils.parseNumber(anInteger, Integer.class)).as("Integer did not parse").isEqualTo(Integer.MAX_VALUE);
}

private void assertShortEquals(String aShort) {
assertThat(NumberUtils.parseNumber(aShort, Short.class).shortValue()).as("Short did not parse").isEqualTo(Short.MAX_VALUE);
assertThat(NumberUtils.parseNumber(aShort, Short.class)).as("Short did not parse").isEqualTo(Short.MAX_VALUE);
}

private void assertByteEquals(String aByte) {
assertThat(NumberUtils.parseNumber(aByte, Byte.class).byteValue()).as("Byte did not parse").isEqualTo(Byte.MAX_VALUE);
assertThat(NumberUtils.parseNumber(aByte, Byte.class)).as("Byte did not parse").isEqualTo(Byte.MAX_VALUE);
}

private void assertNegativeLongEquals(String aLong) {
assertThat(NumberUtils.parseNumber(aLong, Long.class).longValue()).as("Long did not parse").isEqualTo(Long.MIN_VALUE);
assertThat(NumberUtils.parseNumber(aLong, Long.class)).as("Long did not parse").isEqualTo(Long.MIN_VALUE);
}

private void assertNegativeIntegerEquals(String anInteger) {
assertThat(NumberUtils.parseNumber(anInteger, Integer.class).intValue()).as("Integer did not parse").isEqualTo(Integer.MIN_VALUE);
assertThat(NumberUtils.parseNumber(anInteger, Integer.class)).as("Integer did not parse").isEqualTo(Integer.MIN_VALUE);
}

private void assertNegativeShortEquals(String aShort) {
assertThat(NumberUtils.parseNumber(aShort, Short.class).shortValue()).as("Short did not parse").isEqualTo(Short.MIN_VALUE);
assertThat(NumberUtils.parseNumber(aShort, Short.class)).as("Short did not parse").isEqualTo(Short.MIN_VALUE);
}

private void assertNegativeByteEquals(String aByte) {
assertThat(NumberUtils.parseNumber(aByte, Byte.class).byteValue()).as("Byte did not parse").isEqualTo(Byte.MIN_VALUE);
assertThat(NumberUtils.parseNumber(aByte, Byte.class)).as("Byte did not parse").isEqualTo(Byte.MIN_VALUE);
}

private void assertToNumberOverflow(Number number, Class<? extends Number> targetClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ void isEmptyUnsupportedObjectType() {
void toObjectArray() {
int[] a = new int[] {1, 2, 3, 4, 5};
Integer[] wrapper = (Integer[]) ObjectUtils.toObjectArray(a);
assertThat(wrapper.length).isEqualTo(5);
assertThat(wrapper).hasSize(5);
for (int i = 0; i < wrapper.length; i++) {
assertThat(wrapper[i].intValue()).isEqualTo(a[i]);
assertThat(wrapper[i]).isEqualTo(a[i]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ void increment01root() {
StandardEvaluationContext ctx = new StandardEvaluationContext(i);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("#this++");
assertThat(i.intValue()).isEqualTo(42);
assertThat(i).isEqualTo(42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
e.getValue(ctx, Integer.class))
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.NOT_ASSIGNABLE));
Expand Down Expand Up @@ -1009,7 +1009,7 @@ void decrement01root() {
StandardEvaluationContext ctx = new StandardEvaluationContext(i);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("#this--");
assertThat(i.intValue()).isEqualTo(42);
assertThat(i).isEqualTo(42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
e.getValue(ctx, Integer.class))
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.NOT_ASSIGNABLE));
Expand Down Expand Up @@ -1170,13 +1170,13 @@ void incrementAndDecrementTogether() {

// index1 is 3 intArray[3] is 4
e = parser.parseExpression("intArray[#root.index1++]--");
assertThat(e.getValue(ctx, Integer.class).intValue()).isEqualTo(4);
assertThat(e.getValue(ctx, Integer.class)).isEqualTo(4);
assertThat(helper.index1).isEqualTo(4);
assertThat(helper.intArray[3]).isEqualTo(3);

// index1 is 4, intArray[3] is 3
e = parser.parseExpression("intArray[--#root.index1]++");
assertThat(e.getValue(ctx, Integer.class).intValue()).isEqualTo(3);
assertThat(e.getValue(ctx, Integer.class)).isEqualTo(3);
assertThat(helper.index1).isEqualTo(3);
assertThat(helper.intArray[3]).isEqualTo(4);
}
Expand Down Expand Up @@ -1427,22 +1427,22 @@ void incrementAllNodeTypes() throws SecurityException, NoSuchMethodException {

ctx.setVariable("wobble", 3);
e = parser.parseExpression("#wobble++");
assertThat(((Integer) ctx.lookupVariable("wobble")).intValue()).isEqualTo(3);
assertThat(((Integer) ctx.lookupVariable("wobble"))).isEqualTo(3);
int r = e.getValue(ctx, Integer.TYPE);
assertThat(r).isEqualTo(3);
assertThat(((Integer) ctx.lookupVariable("wobble")).intValue()).isEqualTo(4);
assertThat(((Integer) ctx.lookupVariable("wobble"))).isEqualTo(4);

e = parser.parseExpression("--#wobble");
assertThat(((Integer) ctx.lookupVariable("wobble")).intValue()).isEqualTo(4);
assertThat(((Integer) ctx.lookupVariable("wobble"))).isEqualTo(4);
r = e.getValue(ctx, Integer.TYPE);
assertThat(r).isEqualTo(3);
assertThat(((Integer) ctx.lookupVariable("wobble")).intValue()).isEqualTo(3);
assertThat(((Integer) ctx.lookupVariable("wobble"))).isEqualTo(3);

e = parser.parseExpression("#wobble=34");
assertThat(((Integer) ctx.lookupVariable("wobble")).intValue()).isEqualTo(3);
assertThat(((Integer) ctx.lookupVariable("wobble"))).isEqualTo(3);
r = e.getValue(ctx, Integer.TYPE);
assertThat(r).isEqualTo(34);
assertThat(((Integer) ctx.lookupVariable("wobble")).intValue()).isEqualTo(34);
assertThat(((Integer) ctx.lookupVariable("wobble"))).isEqualTo(34);

// Projection
expectFailNotIncrementable(parser, ctx, "({1,2,3}.![#isEven(#this)])++"); // projection would be {false,true,false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public void testConversionsAvailable() throws Exception {
public void testSetParameterizedList() throws Exception {
StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
Expression e = parser.parseExpression("listOfInteger.size()");
assertThat(e.getValue(context, Integer.class).intValue()).isEqualTo(0);
assertThat(e.getValue(context, Integer.class)).isZero();
context.setTypeConverter(new TypeConvertorUsingConversionService());
// Assign a List<String> to the List<Integer> field - the component elements should be converted
parser.parseExpression("listOfInteger").setValue(context,listOfString);
// size now 3
assertThat(e.getValue(context, Integer.class).intValue()).isEqualTo(3);
assertThat(e.getValue(context, Integer.class)).isEqualTo(3);
Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer
assertThat(clazz).isEqualTo(Integer.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testCacheInitialization() throws Exception {

private void assertCorrectSetup(DataSource dataSource) {
JdbcTemplate jt = new JdbcTemplate(dataSource);
assertThat(jt.queryForObject("select count(*) from T_TEST", Integer.class).intValue()).isEqualTo(1);
assertThat(jt.queryForObject("select count(*) from T_TEST", Integer.class)).isEqualTo(1);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void assertBeanPropertyValueOf(String propertyName, String expected, Def
}

private void assertNumRowsInTestTable(JdbcTemplate template, int count) {
assertThat(template.queryForObject("select count(*) from T_TEST", Integer.class).intValue()).isEqualTo(count);
assertThat(template.queryForObject("select count(*) from T_TEST", Integer.class)).isEqualTo(count);
}

private void assertCorrectSetup(String file, String... dataSources) {
Expand Down
Loading

0 comments on commit 68f2b0c

Please sign in to comment.