Skip to content

Commit 1aa05b7

Browse files
committed
Introduce unit tests for ArgumentsAccessor
Issue: #1370
1 parent 46cb665 commit 1aa05b7

File tree

2 files changed

+158
-3
lines changed

2 files changed

+158
-3
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apiguardian.api.API;
2121
import org.junit.jupiter.params.converter.DefaultArgumentConverter;
22+
import org.junit.platform.commons.util.ClassUtils;
2223
import org.junit.platform.commons.util.Preconditions;
2324

2425
/**
@@ -36,7 +37,7 @@ public class DefaultArgumentsAccessor implements ArgumentsAccessor {
3637

3738
private final Object[] arguments;
3839

39-
public DefaultArgumentsAccessor(Object[] arguments) {
40+
public DefaultArgumentsAccessor(Object... arguments) {
4041
Preconditions.notNull(arguments, "Arguments array must not be null");
4142
this.arguments = arguments;
4243
}
@@ -56,8 +57,10 @@ public <T> T get(int index, Class<T> requiredType) {
5657
Object convertedValue = DefaultArgumentConverter.INSTANCE.convert(value, requiredType);
5758
return requiredType.cast(convertedValue);
5859
}
59-
catch (ClassCastException ex) {
60-
String message = format("Argument [%s] at index [%d] could not be converted or cast to [%s]", value, index,
60+
catch (Exception ex) {
61+
String message = format(
62+
"Argument at index [%d] with value [%s] and type [%s] could not be converted or cast to type [%s].",
63+
index, value, ClassUtils.nullSafeToString(value == null ? null : value.getClass()),
6164
requiredType.getName());
6265
throw new ArgumentsAccessorException(message, ex);
6366
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright 2015-2018 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* http://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.jupiter.params.aggregator;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNull;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
20+
import java.util.Arrays;
21+
import java.util.List;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.platform.commons.util.PreconditionViolationException;
25+
26+
/**
27+
* Unit tests for {@link ArgumentsAccessor}.
28+
*
29+
* @since 5.2
30+
*/
31+
class ArgumentsAccessorTests {
32+
33+
@Test
34+
void preconditions() {
35+
assertThrows(PreconditionViolationException.class, () -> new DefaultArgumentsAccessor((Object[]) null));
36+
37+
ArgumentsAccessor arguments = new DefaultArgumentsAccessor(1, 2);
38+
Exception exception = assertThrows(PreconditionViolationException.class, () -> arguments.get(-1));
39+
assertThat(exception.getMessage()).isEqualTo("index must be >= 0 and < 2");
40+
}
41+
42+
@Test
43+
void getNull() {
44+
assertNull(new DefaultArgumentsAccessor(new Object[] { null }).get(0));
45+
}
46+
47+
@Test
48+
void getWithNullCastToWrapperType() {
49+
assertNull(new DefaultArgumentsAccessor((Object[]) new Integer[] { null }).get(0, Integer.class));
50+
}
51+
52+
@Test
53+
void get() {
54+
assertEquals(1, new DefaultArgumentsAccessor(1).get(0));
55+
}
56+
57+
@Test
58+
void getWithCast() {
59+
assertEquals(Integer.valueOf(1), new DefaultArgumentsAccessor(1).get(0, Integer.class));
60+
assertEquals(Character.valueOf('A'), new DefaultArgumentsAccessor('A').get(0, Character.class));
61+
}
62+
63+
@Test
64+
void getWithCastToPrimitiveType() {
65+
Exception exception = assertThrows(ArgumentsAccessorException.class,
66+
() -> new DefaultArgumentsAccessor(1).get(0, int.class));
67+
assertThat(exception.getMessage()).isEqualTo(
68+
"Argument at index [0] with value [1] and type [java.lang.Integer] could not be converted or cast to type [int].");
69+
}
70+
71+
@Test
72+
void getWithCastToIncompatibleType() {
73+
Exception exception = assertThrows(ArgumentsAccessorException.class,
74+
() -> new DefaultArgumentsAccessor(1).get(0, Character.class));
75+
assertThat(exception.getMessage()).isEqualTo(
76+
"Argument at index [0] with value [1] and type [java.lang.Integer] could not be converted or cast to type [java.lang.Character].");
77+
}
78+
79+
@Test
80+
void getCharacter() {
81+
assertEquals(Character.valueOf('A'), new DefaultArgumentsAccessor('A', 'B').getCharacter(0));
82+
}
83+
84+
@Test
85+
void getBoolean() {
86+
assertEquals(Boolean.TRUE, new DefaultArgumentsAccessor(true, false).getBoolean(0));
87+
}
88+
89+
@Test
90+
void getByte() {
91+
assertEquals(Byte.valueOf((byte) 42), new DefaultArgumentsAccessor((byte) 42).getByte(0));
92+
}
93+
94+
@Test
95+
void getShort() {
96+
assertEquals(Short.valueOf((short) 42), new DefaultArgumentsAccessor((short) 42).getShort(0));
97+
}
98+
99+
@Test
100+
void getInteger() {
101+
assertEquals(Integer.valueOf(42), new DefaultArgumentsAccessor(42).getInteger(0));
102+
}
103+
104+
@Test
105+
void getLong() {
106+
assertEquals(Long.valueOf(42L), new DefaultArgumentsAccessor(42L).getLong(0));
107+
}
108+
109+
@Test
110+
void getFloat() {
111+
assertEquals(Float.valueOf(42.0f), new DefaultArgumentsAccessor(42.0f).getFloat(0));
112+
}
113+
114+
@Test
115+
void getDouble() {
116+
assertEquals(Double.valueOf(42.0), new DefaultArgumentsAccessor(42.0).getDouble(0));
117+
}
118+
119+
@Test
120+
void getString() {
121+
assertEquals("foo", new DefaultArgumentsAccessor("foo", "bar").getString(0));
122+
}
123+
124+
@Test
125+
void toArray() {
126+
DefaultArgumentsAccessor arguments = new DefaultArgumentsAccessor("foo", "bar");
127+
Object[] copy = arguments.toArray();
128+
assertArrayEquals(new String[] { "foo", "bar" }, copy);
129+
130+
// Modify local copy:
131+
copy[0] = "Boom!";
132+
assertEquals("foo", arguments.toArray()[0]);
133+
}
134+
135+
@Test
136+
void toList() {
137+
DefaultArgumentsAccessor arguments = new DefaultArgumentsAccessor("foo", "bar");
138+
List<Object> copy = arguments.toList();
139+
assertIterableEquals(Arrays.asList("foo", "bar"), copy);
140+
141+
// Modify local copy:
142+
assertThrows(UnsupportedOperationException.class, () -> copy.set(0, "Boom!"));
143+
}
144+
145+
@Test
146+
void size() {
147+
assertEquals(0, new DefaultArgumentsAccessor().size());
148+
assertEquals(1, new DefaultArgumentsAccessor(42).size());
149+
assertEquals(5, new DefaultArgumentsAccessor('a', 'b', 'c', 'd', 'e').size());
150+
}
151+
152+
}

0 commit comments

Comments
 (0)