Skip to content

Commit 664bda4

Browse files
committed
Adds Tests to MultiImageFileSource
1 parent 4edbb0f commit 664bda4

File tree

6 files changed

+124
-10
lines changed

6 files changed

+124
-10
lines changed

core/src/main/java/edu/wpi/grip/core/sources/MultiImageFileSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public MultiImageFileSource(){ /* no op */ }
6262
@SuppressWarnings("unchecked")
6363
private void initialize(final EventBus eventBus, final List<String> paths, int index) throws IOException {
6464
this.eventBus = checkNotNull(eventBus, "Event bus can not be null");
65-
checkElementIndex(index, paths.size(), "File List Index");
65+
this.index = checkElementIndex(index, paths.size(), "File List Index");
6666
this.paths = paths;
6767
this.outputSocket = new OutputSocket(eventBus, imageOutputHint);
6868
for (String path : paths) {

core/src/test/java/edu/wpi/grip/core/sources/ImageFileSourceTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.eventbus.EventBus;
44
import edu.wpi.grip.core.OutputSocket;
5+
import edu.wpi.grip.util.Files;
6+
import edu.wpi.grip.util.ImageWithData;
57
import org.bytedeco.javacpp.opencv_core.Mat;
68
import org.junit.Before;
79
import org.junit.Test;
@@ -17,30 +19,26 @@
1719
*
1820
*/
1921
public class ImageFileSourceTest {
20-
private File imageFile;
21-
private File textFile;
22+
private final ImageWithData imageFile = Files.imageFile;
23+
private final File textFile = Files.textFile;
2224
private static EventBus eventBus;
2325

2426
@Before
2527
public void setUp() throws URISyntaxException {
2628
this.eventBus = new EventBus();
27-
textFile = new File(ImageFileSourceTest.class.getResource("/edu/wpi/grip/images/NotAnImage.txt").toURI());
28-
imageFile = new File(ImageFileSourceTest.class.getResource("/edu/wpi/grip/images/GRIP_Logo.png").toURI());
2929
}
3030

3131
@Test
3232
public void testLoadImageToMat() throws IOException {
3333
// Given above setup
3434
// When
35-
final ImageFileSource fileSource = new ImageFileSource(eventBus, this.imageFile);
35+
final ImageFileSource fileSource = new ImageFileSource(eventBus, this.imageFile.file);
3636
OutputSocket<Mat> outputSocket = fileSource.getOutputSockets()[0];
3737

3838
// Then
39-
assertNotNull("The output socket's value was null.", outputSocket.getValue());
39+
assertTrue("The output socket's value was empty.", outputSocket.getValue().isPresent());
4040

41-
// Check that the image that is read in is 2 dimentional
42-
assertEquals("Matrix from loaded image did not have expected number of rows.", 183, outputSocket.getValue().get().rows());
43-
assertEquals("Matrix from loaded image did not have expected number of cols.", 480, outputSocket.getValue().get().cols());
41+
imageFile.assertSameImage(outputSocket.getValue().get());
4442
}
4543

4644
@Test(expected = IOException.class)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package edu.wpi.grip.core.sources;
2+
3+
import com.google.common.eventbus.EventBus;
4+
import edu.wpi.grip.core.OutputSocket;
5+
import edu.wpi.grip.util.Files;
6+
import edu.wpi.grip.util.ImageWithData;
7+
import org.bytedeco.javacpp.opencv_core.Mat;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.util.Arrays;
14+
import java.util.Properties;
15+
16+
17+
public class MultiImageFileSourceTest {
18+
private static final ImageWithData imageFile = Files.imageFile, gompeiJpegFile = Files.gompeiJpegFile;
19+
private static final File textFile = Files.textFile;
20+
private MultiImageFileSource source;
21+
private MultiImageFileSource sourceWithIndexSet;
22+
23+
@Before
24+
public void setUp() throws IOException {
25+
source = new MultiImageFileSource(
26+
new EventBus(),
27+
Arrays.asList(imageFile.file, gompeiJpegFile.file));
28+
sourceWithIndexSet = new MultiImageFileSource(
29+
new EventBus(),
30+
Arrays.asList(imageFile.file, gompeiJpegFile.file), 1);
31+
}
32+
33+
@Test(expected = IOException.class)
34+
public void createMultiImageFileSourceWithTextFile() throws IOException {
35+
new MultiImageFileSource(new EventBus(), Arrays.asList(imageFile.file, gompeiJpegFile.file, textFile));
36+
}
37+
38+
@Test
39+
public void testNextValue() throws Exception {
40+
source.nextValue();
41+
OutputSocket<Mat> outputSocket = source.getOutputSockets()[0];
42+
gompeiJpegFile.assertSameImage(outputSocket.getValue().get());
43+
}
44+
45+
@Test
46+
public void testPreviousValue() throws Exception {
47+
source.previousValue();
48+
OutputSocket<Mat> outputSocket = source.getOutputSockets()[0];
49+
gompeiJpegFile.assertSameImage(outputSocket.getValue().get());
50+
}
51+
52+
@Test
53+
public void testConstructedWithIndex() {
54+
OutputSocket<Mat> outputSocket = sourceWithIndexSet.getOutputSockets()[0];
55+
gompeiJpegFile.assertSameImage(outputSocket.getValue().get());
56+
}
57+
58+
@Test
59+
public void testLoadFromProperties() throws Exception {
60+
final Properties properties = sourceWithIndexSet.getProperties();
61+
final MultiImageFileSource newSource = new MultiImageFileSource();
62+
newSource.createFromProperties(new EventBus(), properties);
63+
OutputSocket<Mat> outputSocket = newSource.getOutputSockets()[0];
64+
gompeiJpegFile.assertSameImage(outputSocket.getValue().get());
65+
}
66+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package edu.wpi.grip.util;
2+
3+
import java.io.File;
4+
import java.net.URISyntaxException;
5+
6+
/**
7+
* Utility class for files that may be used in tests
8+
*/
9+
public class Files {
10+
public static final ImageWithData imageFile, gompeiJpegFile;
11+
public static final File textFile;
12+
static {
13+
try {
14+
textFile = new File(Files.class.getResource("/edu/wpi/grip/images/NotAnImage.txt").toURI());
15+
imageFile = new ImageWithData(new File(
16+
Files.class.getResource("/edu/wpi/grip/images/GRIP_Logo.png").toURI()), 183, 480);
17+
gompeiJpegFile = new ImageWithData(new File(
18+
Files.class.getResource("/edu/wpi/grip/images/gompei.jpeg").toURI()), 220, 225);
19+
} catch (URISyntaxException e) {
20+
throw new IllegalStateException("Could not load file from system", e);
21+
}
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package edu.wpi.grip.util;
2+
3+
4+
import org.bytedeco.javacpp.opencv_core.Mat;
5+
6+
import java.io.File;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class ImageWithData {
11+
private final int rows;
12+
private final int cols;
13+
public final File file;
14+
15+
protected ImageWithData(File file, int rows, int cols) {
16+
this.file = file;
17+
this.rows = rows;
18+
this.cols = cols;
19+
}
20+
21+
public void assertSameImage(final Mat image) {
22+
// Check that the image that is read in is 2 dimensional
23+
assertEquals("Matrix from loaded image did not have expected number of rows.", this.rows , image.rows());
24+
assertEquals("Matrix from loaded image did not have expected number of cols.", this.cols, image.cols());
25+
}
26+
27+
}
36.7 KB
Loading

0 commit comments

Comments
 (0)