Skip to content

Commit 0ef009d

Browse files
committed
add tests
1 parent 7a5d8ed commit 0ef009d

File tree

8 files changed

+114
-11
lines changed

8 files changed

+114
-11
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/application/libraries/classpath/DefaultLibrariesManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import net.lecousin.framework.application.ApplicationClassLoader;
2020
import net.lecousin.framework.application.libraries.LibrariesManager;
2121
import net.lecousin.framework.application.libraries.LibraryManagementException;
22-
import net.lecousin.framework.concurrent.CancelException;
2322
import net.lecousin.framework.concurrent.Executable;
2423
import net.lecousin.framework.concurrent.async.Async;
2524
import net.lecousin.framework.concurrent.async.IAsync;
@@ -232,7 +231,7 @@ public CustomExtensionPointLoader(CustomExtensionPoint ep, String filePath) {
232231
private String filePath;
233232

234233
@Override
235-
public Void execute(Task<Void, Exception> taskContext) throws Exception, CancelException {
234+
public Void execute(Task<Void, Exception> taskContext) throws Exception {
236235
app.getDefaultLogger().info("Loading plugin files for custom extension point " + ep.getClass().getName() + ": " + filePath);
237236
Enumeration<URL> urls = acl.getResources(filePath);
238237
while (urls.hasMoreElements()) {

net.lecousin.core/src/main/java/net/lecousin/framework/io/IOUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static <T extends IO.Readable & IO.KnownSize> void readFullyKnownSize(T i
114114
/** Read fully into a byte[], and unblock the given result upon completion. */
115115
public static void readFullyKnownSize(IO.Readable io, int size, AsyncSupplier<byte[], IOException> result) {
116116
byte[] bytes;
117-
try { bytes = new byte[size]; }
117+
try { bytes = ByteArrayCache.getInstance().get(size, false); }
118118
catch (Exception t) {
119119
result.error(IO.error(t));
120120
return;

net.lecousin.core/src/main/java/net/lecousin/framework/io/bit/SimpleReadableBitIO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public long readBits(int n) throws IOException {
7575
return getNextBits(n);
7676
if (n == currentNbBits) {
7777
long val = getRemainingBits();
78-
currentNbBits = 0;
7978
currentValue = 0;
79+
currentNbBits = 0;
8080
return val;
8181
}
8282
if (eof) throw new EOFException();

net.lecousin.core/src/main/java/net/lecousin/framework/io/buffering/BufferedReverseIOReading.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ public long getSize() {
8787
@Override
8888
public void setPriority(Priority priority) { io.setPriority(priority); }
8989

90-
@Override
91-
public IO getWrappedIO() { return io; }
92-
9390
@Override
9491
public String getSourceDescription() { return io.getSourceDescription(); }
9592

9693
@Override
9794
public TaskManager getTaskManager() { return Threading.getCPUTaskManager(); }
9895

96+
@Override
97+
public IO getWrappedIO() { return io; }
98+
9999
@Override
100100
protected IAsync<IOException> closeUnderlyingResources() {
101101
synchronized (this) {

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/text/TestArrayStringBuffer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void testModifications() {
4444
Assert.assertEquals(0, s.substring(1).length());
4545
Assert.assertEquals(0, s.substring(1, 2).length());
4646
Assert.assertTrue(s == s.removeEndChars(10));
47+
Assert.assertTrue(s.isEmpty());
4748
try {
4849
s.setCharAt(0, ' ');
4950
throw new AssertionError("must throw IllegalArgumentException");
@@ -65,6 +66,20 @@ public void testModifications() {
6566
cs = createString("wxcvbn");
6667
check("wxcvbn7654321g", s.replace(0, 3, cs));
6768
check("wxwxcvbnn7654321g", s.replace(2, 4, cs));
69+
70+
s = createString("");
71+
for (int i = 0; i < 100; ++i)
72+
s.append("Hello");
73+
Assert.assertEquals(500, s.length());
74+
for (int i = 0; i < 100; ++i)
75+
Assert.assertEquals("Hello", s.substring(i * 5, (i + 1) * 5).toString());
76+
77+
s = createString("");
78+
for (int i = 0; i < 100; ++i)
79+
s.append("Hello".toCharArray(), 0, 5);
80+
Assert.assertEquals(500, s.length());
81+
for (int i = 0; i < 100; ++i)
82+
Assert.assertEquals("Hello", s.substring(i * 5, (i + 1) * 5).toString());
6883
}
6984

7085
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/io/TestIOUtil.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.lecousin.framework.io.buffering.ByteArrayIO;
2626
import net.lecousin.framework.io.buffering.ByteBuffersIO;
2727
import net.lecousin.framework.io.buffering.SimpleBufferedReadable;
28+
import net.lecousin.framework.mutable.MutableBoolean;
2829
import net.lecousin.framework.progress.FakeWorkProgress;
2930

3031
import org.junit.Assert;
@@ -187,6 +188,21 @@ public void testReadFullyUnknownSize() throws Exception {
187188
Assert.assertNotNull(result.getError());
188189
}
189190

191+
@Test
192+
public void testReadFullyKnownSize() throws Exception {
193+
ByteArrayIO io = new ByteArrayIO(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}, "test");
194+
AsyncSupplier<byte[], IOException> result = new AsyncSupplier<>();
195+
IOUtil.readFully(io, result);
196+
byte[] res = result.blockResult(15000);
197+
Assert.assertEquals(8, res.length);
198+
199+
// error
200+
result = new AsyncSupplier<>();
201+
IOUtil.readFully(new TestIOError.ReadableAlwaysError.KnownSizeAlwaysError(), result);
202+
result.block(15000);
203+
Assert.assertNotNull(result.getError());
204+
}
205+
190206
@Test
191207
public void testReadFullyAsync() throws Exception {
192208
ByteArrayInputStream in = new ByteArrayInputStream(new byte[] {0, 1, 2, 3, 4, 5, 6, 7});
@@ -195,6 +211,21 @@ public void testReadFullyAsync() throws Exception {
195211
ByteBuffersIO bbio = result.blockResult(30000);
196212
Assert.assertEquals(8, bbio.getSizeSync());
197213
bbio.close();
214+
215+
// error
216+
AsyncSupplier<Integer, IOException> err = IOUtil.readFullyAsync(new TestIOError.ReadableAlwaysError(), ByteBuffer.allocate(16), null);
217+
err.block(5000);
218+
Assert.assertTrue(err.hasError());
219+
MutableBoolean ondoneCalled = new MutableBoolean(false);
220+
err = IOUtil.readFullyAsync(new TestIOError.ReadableAlwaysError(), ByteBuffer.allocate(16), r -> ondoneCalled.set(true));
221+
err.block(5000);
222+
Assert.assertTrue(err.hasError());
223+
Assert.assertTrue(ondoneCalled.get());
224+
}
225+
226+
@Test
227+
public void testReadFullyAsStringSync() throws Exception {
228+
Assert.assertEquals("Hello World", IOUtil.readFullyAsStringSync(new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
198229
}
199230

200231
@Test
@@ -206,5 +237,5 @@ public void testErrors() throws Exception {
206237
IOUtil.readFullyKnownSize(io, 4096, resultB);
207238
resultB.block(15000);
208239
Assert.assertNotNull(resultB.getError());
209-
}
240+
}
210241
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package net.lecousin.framework.core.tests.io;
2+
3+
import java.io.IOException;
4+
import java.util.Collection;
5+
6+
import net.lecousin.framework.core.test.io.TestFragmented;
7+
import net.lecousin.framework.core.test.io.TestFragmented.FragmentedFile;
8+
import net.lecousin.framework.core.test.io.TestReadableSeekable;
9+
import net.lecousin.framework.core.test.runners.LCConcurrentRunner;
10+
import net.lecousin.framework.io.FileIO;
11+
import net.lecousin.framework.io.IO;
12+
import net.lecousin.framework.io.LinkedIO;
13+
import net.lecousin.framework.io.SubIO;
14+
import net.lecousin.framework.io.buffering.BufferedIO;
15+
import net.lecousin.framework.math.RangeLong;
16+
17+
import org.junit.runner.RunWith;
18+
import org.junit.runners.Parameterized.Parameters;
19+
20+
@RunWith(LCConcurrentRunner.Parameterized.class) @org.junit.runners.Parameterized.UseParametersRunnerFactory(LCConcurrentRunner.ConcurrentParameterizedRunnedFactory.class)
21+
public class TestLinkedIOWithSubIOReadableSeekable3 extends TestReadableSeekable {
22+
23+
@Parameters
24+
public static Collection<Object[]> parameters() throws IOException {
25+
return TestFragmented.generateTestCases();
26+
}
27+
28+
public TestLinkedIOWithSubIOReadableSeekable3(FragmentedFile f) {
29+
super(f.file, f.testBuf, f.nbBuf);
30+
this.f = f;
31+
}
32+
33+
private FragmentedFile f;
34+
35+
@Override
36+
protected IO.Readable.Seekable createReadableSeekableFromFile(FileIO.ReadOnly file, long fileSize) throws Exception {
37+
// this test may be very slow, let's add a buffered layer
38+
BufferedIO buffered = new BufferedIO(file, f.realSize, 32768, 32768, false);
39+
IO.Readable.Seekable[] ios = new IO.Readable.Seekable[f.fragments.size()];
40+
int i = 0;
41+
for (RangeLong fragment : f.fragments)
42+
ios[i++] = new SubIO.Readable.Seekable.Buffered(buffered, fragment.min, fragment.getLength(), "fragment " + i, false);
43+
LinkedIO.Readable.Seekable.Buffered.DeterminedSize res = new LinkedIO.Readable.Seekable.Buffered.DeterminedSize("linked IO", ios);
44+
res.addCloseListener(() -> { try { buffered.close(); } catch (Exception e) {}});
45+
return res;
46+
}
47+
48+
@Override
49+
protected boolean canSetPriority() {
50+
return !f.fragments.isEmpty() && f.nbBuf > 0;
51+
}
52+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/math/TestUnits.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package net.lecousin.framework.core.tests.math;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
5-
63
import net.lecousin.framework.core.test.LCCoreAbstractTest;
74
import net.lecousin.framework.math.IntegerUnit;
5+
import net.lecousin.framework.math.IntegerUnit.ParserRegistry;
86
import net.lecousin.framework.math.IntegerUnit.UnitConversionException;
97
import net.lecousin.framework.math.TimeUnit;
108
import net.lecousin.framework.math.TimeUnit.Day;
@@ -13,6 +11,9 @@
1311
import net.lecousin.framework.math.TimeUnit.Minute;
1412
import net.lecousin.framework.math.TimeUnit.Second;
1513

14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
1617
public class TestUnits extends LCCoreAbstractTest {
1718

1819
@Test
@@ -106,5 +107,10 @@ public void testFakeUnit() {
106107
throw new AssertionError("conversion with FakeUnit should not be possible");
107108
} catch (UnitConversionException e) {}
108109
}
110+
111+
@Test
112+
public void testParsers() {
113+
Assert.assertEquals(TimeUnit.Millisecond.class, ParserRegistry.get("ms"));
114+
}
109115

110116
}

0 commit comments

Comments
 (0)