Skip to content

Commit 601ece4

Browse files
awalter17ctrueden
authored andcommitted
Add unit tests for primitive number converters
1 parent 28a2ab4 commit 601ece4

31 files changed

+1946
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.convert;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertTrue;
36+
37+
import java.math.BigDecimal;
38+
import java.math.BigInteger;
39+
40+
import org.junit.Rule;
41+
import org.junit.Test;
42+
import org.junit.rules.ExpectedException;
43+
44+
/**
45+
* Tests converter plugins that convert from primitive numeric types to other
46+
* primitive numeric types.
47+
*
48+
* @author Alison Walter
49+
*/
50+
public abstract class AbstractNumberConverterTests {
51+
52+
protected NumberToNumberConverter<?, ?> converter = getConverter();
53+
protected Class<?> srcType = converter.getInputType();
54+
protected Class<?> destType = converter.getOutputType();
55+
56+
public abstract Number getSrc();
57+
58+
public abstract NumberToNumberConverter<?, ?> getConverter();
59+
60+
public abstract Number getExpectedValue();
61+
62+
public abstract Number getInvalidInput();
63+
64+
public abstract Class<?> getInvalidOutput();
65+
66+
/**
67+
* Test case for the wrapper classes
68+
*/
69+
@Test
70+
public void testWrapper() {
71+
final Number src = getSrc();
72+
final Number expect = getExpectedValue();
73+
assertTrue(destType.isInstance(converter.convert(src, destType)));
74+
assertEquals(expect, converter.convert(src, destType));
75+
}
76+
77+
/**
78+
* Test case for primitive values
79+
*/
80+
@Test
81+
public void testPrimitive() {
82+
final Number src = getSrc();
83+
final Number expect = getExpectedValue();
84+
if (!destType.equals(BigInteger.class) &&
85+
!destType.equals(BigDecimal.class))
86+
{
87+
// byte to number converters
88+
if (srcType.equals(Byte.class)) {
89+
final byte b = src.byteValue();
90+
if (destType.equals(Short.class)) {
91+
final short s = expect.shortValue();
92+
assertTrue(s == converter.convert(b, short.class));
93+
}
94+
else if (destType.equals(Integer.class)) {
95+
final int i = expect.intValue();
96+
assertTrue(i == converter.convert(b, int.class));
97+
}
98+
else if (destType.equals(Long.class)) {
99+
final long l = expect.longValue();
100+
assertTrue(l == converter.convert(b, long.class));
101+
}
102+
else if (destType.equals(Float.class)) {
103+
final float f = expect.floatValue();
104+
assertTrue(f == converter.convert(b, float.class));
105+
}
106+
else {
107+
final double d = expect.doubleValue();
108+
assertTrue(d == converter.convert(b, double.class));
109+
}
110+
}
111+
// int to number converters
112+
else if (srcType.equals(Integer.class)) {
113+
final int i = src.intValue();
114+
if (destType.equals(Long.class)) {
115+
final long l = expect.longValue();
116+
assertTrue(l == converter.convert(i, long.class));
117+
}
118+
else {
119+
final double d = expect.doubleValue();
120+
assertTrue(d == converter.convert(i, double.class));
121+
}
122+
}
123+
// short to number converters
124+
else if (srcType.equals(Short.class)) {
125+
final short s = src.shortValue();
126+
if (destType.equals(Integer.class)) {
127+
final int i = expect.intValue();
128+
assertTrue(i == converter.convert(s, int.class));
129+
}
130+
else if (destType.equals(Long.class)) {
131+
final long l = expect.longValue();
132+
assertTrue(l == converter.convert(s, long.class));
133+
}
134+
else if (destType.equals(Float.class)) {
135+
final float f = expect.floatValue();
136+
assertTrue(f == converter.convert(s, float.class));
137+
}
138+
else {
139+
final double d = expect.doubleValue();
140+
assertTrue(d == converter.convert(s, double.class));
141+
}
142+
}
143+
// float to number converters
144+
else if (srcType.equals(Float.class)) {
145+
final float f = expect.floatValue();
146+
final double d = expect.doubleValue();
147+
assertTrue(d == converter.convert(f, double.class));
148+
}
149+
else {
150+
// longs and doubles can't be converted to anything beside bigInteger
151+
// and big decimal
152+
}
153+
}
154+
else {
155+
// no prim equivalents for BigInteger and BigDecimal
156+
}
157+
}
158+
159+
/**
160+
* Test case for null input
161+
*/
162+
@Test
163+
public void nullInput() {
164+
iae("Null input", null, null);
165+
}
166+
167+
/**
168+
* Test case for invalid input class
169+
*/
170+
@Test
171+
public void incorrectInputType() {
172+
final Number input = getInvalidInput();
173+
final String message =
174+
"Expected input of type " + srcType.getSimpleName() + ", but got " +
175+
input.getClass().getSimpleName();
176+
iae(message, input, destType);
177+
}
178+
179+
/**
180+
* Test case for invalid output class
181+
*/
182+
@Test
183+
public void incorrectOutputType() {
184+
final Class<?> output = getInvalidOutput();
185+
final Number src = getSrc();
186+
final String message =
187+
"Expected output class of " + destType.getSimpleName() + ", but got " +
188+
output.getSimpleName();
189+
iae(message, src, output);
190+
}
191+
192+
@Rule
193+
public ExpectedException exception = ExpectedException.none();
194+
195+
// helper methods
196+
protected void
197+
iae(final String message, final Number src, final Class<?> dest)
198+
{
199+
exception(IllegalArgumentException.class, message, src, dest);
200+
}
201+
202+
protected void exception(final Class<? extends Throwable> excType,
203+
final String message, final Number src, final Class<?> dest)
204+
{
205+
exception.expect(excType);
206+
exception.expectMessage(message);
207+
converter.convert(src, dest);
208+
}
209+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.convert;
33+
34+
import java.math.BigInteger;
35+
36+
import org.scijava.convert.NumberConverters.BigIntegerToBigDecimalConverter;
37+
38+
/**
39+
* Tests {@link BigIntegerToBigDecimalConverter}.
40+
*
41+
* @author Alison Walter
42+
*/
43+
public class BigIntegerToBigDecimalConverterTest extends NumberToBigDecimalTest
44+
{
45+
46+
@Override
47+
public Number getSrc() {
48+
return BigInteger.valueOf(7l);
49+
}
50+
51+
@Override
52+
public NumberToNumberConverter<?, ?> getConverter() {
53+
return new NumberConverters.BigIntegerToBigDecimalConverter();
54+
}
55+
56+
@Override
57+
public Number getInvalidInput() {
58+
return new Long(46l);
59+
}
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.convert;
33+
34+
import org.scijava.convert.NumberConverters.ByteToBigDecimalConverter;
35+
36+
/**
37+
* Tests {@link ByteToBigDecimalConverter}.
38+
*
39+
* @author Alison Walter
40+
*/
41+
public class ByteToBigDecimalConverterTest extends NumberToBigDecimalTest {
42+
43+
@Override
44+
public Number getSrc() {
45+
return new Byte((byte) 7);
46+
}
47+
48+
@Override
49+
public NumberToNumberConverter<?, ?> getConverter() {
50+
return new NumberConverters.ByteToBigDecimalConverter();
51+
}
52+
53+
@Override
54+
public Number getInvalidInput() {
55+
return new Short((short) 101);
56+
}
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.convert;
33+
34+
import org.scijava.convert.NumberConverters.ByteToBigIntegerConverter;
35+
36+
/**
37+
* Tests {@link ByteToBigIntegerConverter}.
38+
*
39+
* @author Alison Walter
40+
*/
41+
public class ByteToBigIntegerConverterTest extends NumberToBigIntegerTest {
42+
43+
@Override
44+
public Number getSrc() {
45+
return new Byte((byte) 7);
46+
}
47+
48+
@Override
49+
public NumberToNumberConverter<?, ?> getConverter() {
50+
return new NumberConverters.ByteToBigIntegerConverter();
51+
}
52+
53+
@Override
54+
public Number getInvalidInput() {
55+
return new Short((short) 101);
56+
}
57+
}
58+

0 commit comments

Comments
 (0)