Skip to content

Commit a922e15

Browse files
committed
Add test for edge cases in the DataHandle default methods
1 parent 4a365aa commit a922e15

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*-
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2018 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.assertArrayEquals;
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertNull;
38+
39+
import java.io.IOException;
40+
import java.util.Arrays;
41+
42+
import org.junit.Test;
43+
import org.scijava.Context;
44+
import org.scijava.io.location.BytesLocation;
45+
import org.scijava.io.location.Location;
46+
47+
/**
48+
* Additional Tests for edge case behavior of {@link DataHandle}.
49+
*
50+
* @author Gabriel Einsdorf
51+
*/
52+
public class DataHandleEdgeCaseTests {
53+
54+
private static final byte[] BYTES = { //
55+
'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '\n', //
56+
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, -128, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, //
57+
125, 127, -127, -125, -3, 'h', 'e' };
58+
59+
/**
60+
* Test to ensure {@link DataHandle#findString(String...)} and
61+
* {@link DataHandle#readCString()} work with {@link DataHandle}
62+
* implementations that have unknown length.
63+
*
64+
* @throws IOException
65+
*/
66+
@Test
67+
public void testFindStringsOnUnknonwLengthHandle() throws IOException {
68+
final Context ctx = new Context();
69+
final DataHandleService dhs = ctx.getService(DataHandleService.class);
70+
final DummyHandle dummy = new DummyHandle(dhs.create(new BytesLocation(
71+
BYTES)));
72+
73+
assertEquals("Hello,", dummy.findString(","));
74+
assertEquals(" world\n", dummy.findString("\n"));
75+
76+
dummy.seek(41);
77+
assertEquals("he", dummy.findString("\n"));
78+
79+
dummy.seek(16);
80+
assertArrayEquals(Arrays.copyOfRange(BYTES, 16, 23), dummy.readCString()
81+
.getBytes());
82+
dummy.seek(42);
83+
assertNull(dummy.readCString());
84+
}
85+
86+
private class DummyHandle extends AbstractHigherOrderHandle<Location> {
87+
88+
public DummyHandle(final DataHandle<Location> handle) {
89+
super(handle);
90+
}
91+
92+
@Override
93+
public long length() throws IOException {
94+
return -1;
95+
}
96+
97+
@Override
98+
public long offset() throws IOException {
99+
return handle().offset();
100+
}
101+
102+
@Override
103+
public void seek(final long pos) throws IOException {
104+
handle().seek(pos);
105+
}
106+
107+
@Override
108+
public void setLength(final long length) throws IOException {
109+
handle().setLength(length);
110+
}
111+
112+
@Override
113+
public int read(final byte[] b, final int off, final int len)
114+
throws IOException
115+
{
116+
return handle().read(b, off, len);
117+
}
118+
119+
@Override
120+
public byte readByte() throws IOException {
121+
return handle().readByte();
122+
}
123+
124+
@Override
125+
public void write(final int b) throws IOException {
126+
handle().write(b);
127+
}
128+
129+
@Override
130+
public void write(final byte[] b, final int off, final int len)
131+
throws IOException
132+
{
133+
handle().write(b, off, len);
134+
}
135+
136+
@Override
137+
protected void cleanup() throws IOException {
138+
//
139+
}
140+
}
141+
142+
}

0 commit comments

Comments
 (0)