Skip to content

Commit 3d18d7b

Browse files
author
Gabriel Einsdorf
committed
DataHandles: Add tests for the copy methods
1 parent 17ca441 commit 3d18d7b

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*-
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.handle;
34+
35+
import static org.junit.Assert.assertEquals;
36+
37+
import java.io.IOException;
38+
39+
import org.junit.After;
40+
import org.junit.Before;
41+
import org.junit.Test;
42+
import org.scijava.Context;
43+
import org.scijava.event.EventService;
44+
import org.scijava.io.location.BytesLocation;
45+
import org.scijava.io.location.Location;
46+
import org.scijava.task.DefaultTask;
47+
import org.scijava.task.Task;
48+
import org.scijava.thread.ThreadService;
49+
import org.scijava.util.MersenneTwisterFast;
50+
51+
/**
52+
* Tests for utility methods in the {@link DataHandles} class.
53+
*
54+
* @author Gabriel Einsdorf
55+
*/
56+
public class DataHandlesTest {
57+
58+
private static final int TEST_SIZE = 2_938_740;
59+
private DataHandleService handles;
60+
private Location inFile;
61+
private BytesLocation outFile;
62+
private byte[] data;
63+
private ThreadService threadService;
64+
private EventService eventService;
65+
66+
@Before
67+
public void classSetup() {
68+
final Context ctx = new Context(DataHandleService.class,
69+
ThreadService.class, EventService.class);
70+
handles = ctx.service(DataHandleService.class);
71+
threadService = ctx.service(ThreadService.class);
72+
eventService = ctx.service(EventService.class);
73+
74+
data = randomBytes(0xbabebabe);
75+
inFile = new BytesLocation(data);
76+
outFile = new BytesLocation(TEST_SIZE);
77+
}
78+
79+
@After
80+
public void cleanup() {
81+
handles.context().dispose();
82+
}
83+
84+
@Test
85+
public void testDefaultCopy() throws IOException {
86+
try (DataHandle<Location> src = handles.create(inFile);
87+
final DataHandle<Location> dest = handles.create(outFile))
88+
{
89+
DataHandles.copy(src, dest);
90+
assertHandleEquals(data, dest);
91+
}
92+
}
93+
94+
@Test
95+
public void testCopyTask() throws IOException {
96+
try (DataHandle<Location> src = handles.create(inFile);
97+
final DataHandle<Location> dest = handles.create(outFile))
98+
{
99+
final Task t = new DefaultTask(threadService, eventService);
100+
DataHandles.copy(src, dest, t);
101+
assertEquals(t.getProgressValue(), src.length());
102+
assertHandleEquals(data, dest);
103+
}
104+
}
105+
106+
@Test
107+
public void testCopyLength() throws IOException {
108+
final int sliceSize = 50_000;
109+
try (DataHandle<Location> src = handles.create(inFile);
110+
final DataHandle<Location> dest = handles.create(outFile))
111+
{
112+
DataHandles.copy(src, dest, sliceSize);
113+
final byte[] expected = new byte[sliceSize];
114+
System.arraycopy(data, 0, expected, 0, sliceSize);
115+
assertHandleEquals(expected, dest);
116+
}
117+
}
118+
119+
@Test
120+
public void testCopyLengthTask() throws IOException {
121+
final int sliceSize = 50_000;
122+
try (DataHandle<Location> src = handles.create(inFile);
123+
final DataHandle<Location> dest = handles.create(outFile))
124+
{
125+
final Task t = new DefaultTask(threadService, eventService);
126+
DataHandles.copy(src, dest, sliceSize, t);
127+
assertEquals(t.getProgressValue(), sliceSize);
128+
129+
final byte[] expected = new byte[sliceSize];
130+
System.arraycopy(data, 0, expected, 0, sliceSize);
131+
assertHandleEquals(expected, dest);
132+
}
133+
}
134+
135+
private void assertHandleEquals(final byte[] expected,
136+
final DataHandle<Location> handle) throws IOException
137+
{
138+
handle.seek(0);
139+
for (int i = 0; i < expected.length; i++) {
140+
assertEquals(expected[i], handle.readByte());
141+
}
142+
}
143+
144+
private byte[] randomBytes(final long seed) {
145+
final MersenneTwisterFast r = new MersenneTwisterFast(seed);
146+
final byte[] ldata = new byte[TEST_SIZE];
147+
for (int i = 0; i < ldata.length; i++) {
148+
ldata[i] = r.nextByte();
149+
}
150+
return ldata;
151+
}
152+
}

0 commit comments

Comments
 (0)