Skip to content

Commit 143a8c5

Browse files
committed
add tests
1 parent c15e1ea commit 143a8c5

File tree

10 files changed

+182
-26
lines changed

10 files changed

+182
-26
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/annotations/Instantiate.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
77

8-
import net.lecousin.framework.application.LCCore;
98
import net.lecousin.framework.io.serialization.SerializationClass.Attribute;
109
import net.lecousin.framework.io.serialization.rules.AttributeInstantiation;
1110
import net.lecousin.framework.io.serialization.rules.SerializationRule;
@@ -25,15 +24,9 @@ public static class ToRule implements AttributeAnnotationToRuleOnType<Instantiat
2524

2625
@Override
2726
public SerializationRule createRule(Instantiate annotation, Attribute attribute) {
28-
try {
29-
return new AttributeInstantiation(
30-
attribute.getDeclaringClass(), attribute.getOriginalName(), annotation.factory()
31-
);
32-
} catch (Throwable t) {
33-
LCCore.get().getApplication().getDefaultLogger()
34-
.error("Error creating AttributeInstantiation from annotation Instantiate", t);
35-
return null;
36-
}
27+
return new AttributeInstantiation(
28+
attribute.getDeclaringClass(), attribute.getOriginalName(), annotation.factory()
29+
);
3730
}
3831

3932
}

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/annotations/Instantiation.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
77

8-
import net.lecousin.framework.application.LCCore;
98
import net.lecousin.framework.io.serialization.SerializationClass.Attribute;
109
import net.lecousin.framework.io.serialization.rules.AbstractAttributeInstantiation;
1110
import net.lecousin.framework.io.serialization.rules.SerializationRule;
@@ -30,15 +29,9 @@ public static class ToRule implements AttributeAnnotationToRuleOnType<Instantiat
3029

3130
@Override
3231
public SerializationRule createRule(Instantiation annotation, Attribute attribute) {
33-
try {
34-
return new AbstractAttributeInstantiation(
35-
attribute.getDeclaringClass(), attribute.getOriginalName(), annotation.discriminator(), annotation.factory()
36-
);
37-
} catch (Throwable t) {
38-
LCCore.get().getApplication().getDefaultLogger()
39-
.error("Error creating AbstractAttributeInstantiation from annotation Instantiation", t);
40-
return null;
41-
}
32+
return new AbstractAttributeInstantiation(
33+
attribute.getDeclaringClass(), attribute.getOriginalName(), annotation.discriminator(), annotation.factory()
34+
);
4235
}
4336
}
4437

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/annotations/TypeAnnotationToRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static List<TypeAnnotationToRule<?>> getAnnotationToRules(Annotation a) {
103103
}
104104

105105
/** Registry of converters between annotations and serialization rules. */
106-
public static class Registry {
106+
public static final class Registry {
107107

108108
private static List<Pair<Class<? extends Annotation>, TypeAnnotationToRule<?>>> registry = new ArrayList<>();
109109

net.lecousin.core/src/main/java/net/lecousin/framework/io/text/BufferedReadableCharacterStreamLocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ else if (read.isCancelled())
146146
@Override
147147
public AsyncWork<UnprotectedString, IOException> readNextBufferAsync() {
148148
AsyncWork<UnprotectedString, IOException> result = new AsyncWork<>();
149-
AsyncWork<UnprotectedString, IOException> read = readNextBufferAsync();
149+
AsyncWork<UnprotectedString, IOException> read = stream.readNextBufferAsync();
150150
read.listenAsync(
151151
new Task.Cpu.FromRunnable("Calculate new location of BufferedReadableCharacterStreamLocation", stream.getPriority(), () -> {
152152
UnprotectedString str = read.getResult();

net.lecousin.core/src/main/java/net/lecousin/framework/io/text/ProgressiveBufferedReadableCharStream.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public char read() throws EOFException, IOException {
139139

140140
@Override
141141
public int readSync(char[] buf, int offset, int length) throws IOException {
142+
if (length <= 0) return 0;
142143
int done;
143144
if (back != -1) {
144145
buf[offset++] = (char)back;
@@ -444,7 +445,12 @@ public Void run() {
444445
taskFillBuffer.start();
445446
}
446447
} else if (eofReached) {
447-
result.unblockSuccess(null);
448+
if (back != -1) {
449+
char c = (char)back;
450+
back = -1;
451+
result.unblockSuccess(new UnprotectedString(c));
452+
} else
453+
result.unblockSuccess(null);
448454
return null;
449455
} else {
450456
iNeedABuffer = new SynchronizationPoint<>();
@@ -516,6 +522,8 @@ public Void run() throws CancelException {
516522
}
517523
return null;
518524
}
525+
if (isCancelling())
526+
return null;
519527
if (nb == -2) {
520528
synchronized (buffers) {
521529
if (buffer.length > 0) {

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestCharacterStreamReadable.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ protected void basicTests(IO io) throws Exception {
3737
stream.close();
3838
}
3939

40+
@Test(timeout=30000)
41+
public void basicStreamTests() throws Exception {
42+
ICharacterStream.Readable s = openStream(openFile());
43+
s.setPriority(Task.PRIORITY_IMPORTANT);
44+
s.close();
45+
}
46+
4047
@SuppressWarnings({ "resource" })
4148
@Test(timeout=120000)
4249
public void testBufferByBufferFully() throws Exception {
@@ -75,6 +82,7 @@ public void testBufferByBuffer() throws Exception {
7582
Assert.assertFalse(s.endReached());
7683
}
7784
Assert.assertTrue(s.readFullySync(buf, 0, buf.length) <= 0);
85+
Assert.assertTrue(s.readSync(buf, 0, buf.length) <= 0);
7886
Assert.assertTrue(s.endReached());
7987
s.close();
8088
}
@@ -98,6 +106,34 @@ public void testBufferByBufferAsync() throws Exception {
98106
Assert.assertFalse(s.endReached());
99107
}
100108
Assert.assertTrue(s.readFullySync(buf, 0, buf.length) <= 0);
109+
Assert.assertTrue(s.readAsync(buf, 0, buf.length).blockResult(0).intValue() <= 0);
110+
Assert.assertTrue(s.endReached());
111+
s.close();
112+
}
113+
114+
@SuppressWarnings("resource")
115+
@Test(timeout=120000)
116+
public void testBufferByBufferFullyAsync() throws Exception {
117+
ICharacterStream.Readable s = openStream(openFile());
118+
char[] buf = new char[testBuf.length * 3 - testBuf.length / 10];
119+
int pos = 0;
120+
while (pos < nbBuf * testBuf.length) {
121+
AsyncWork<Integer, IOException> read = s.readFullyAsync(buf, 0, buf.length);
122+
int nb = read.blockResult(0).intValue();
123+
if (nb <= 0)
124+
throw new AssertionError("End of stream reached after " + pos + " characters, expected was "
125+
+ (nbBuf * testBuf.length));
126+
for (int i = 0; i < nb; ++i)
127+
Assert.assertEquals("Invalid character at " + (pos + i), (char)testBuf[(pos + i) % testBuf.length], buf[i]);
128+
pos += nb;
129+
if (pos < nbBuf * testBuf.length) {
130+
Assert.assertFalse(s.endReached());
131+
if (nb != buf.length)
132+
throw new Exception("readFullyAsync returned " + nb + " on " + buf.length);
133+
}
134+
}
135+
Assert.assertTrue(s.readFullySync(buf, 0, buf.length) <= 0);
136+
Assert.assertTrue(s.readAsync(buf, 0, buf.length).blockResult(0).intValue() <= 0);
101137
Assert.assertTrue(s.endReached());
102138
s.close();
103139
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestCharacterStreamReadableBuffered.java

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import java.io.File;
55
import java.io.IOException;
66

7-
import net.lecousin.framework.io.IO;
8-
import net.lecousin.framework.io.text.ICharacterStream;
9-
107
import org.junit.Assert;
118
import org.junit.Test;
129

10+
import net.lecousin.framework.concurrent.Task;
11+
import net.lecousin.framework.concurrent.synch.SynchronizationPoint;
12+
import net.lecousin.framework.io.IO;
13+
import net.lecousin.framework.io.text.ICharacterStream;
14+
import net.lecousin.framework.util.UnprotectedString;
15+
import net.lecousin.framework.xml.XMLStreamEventsRecorder.Sync;
16+
1317
public abstract class TestCharacterStreamReadableBuffered extends TestIO.UsingGeneratedTestFiles {
1418

1519
protected TestCharacterStreamReadableBuffered(File testFile, byte[] testBuf, int nbBuf) {
@@ -46,9 +50,87 @@ public void testCharByChar() throws Exception {
4650
Assert.assertEquals('w', s.read());
4751
s.back('z');
4852
char[] buf = new char[20];
53+
Assert.assertTrue(s.readSync(buf, 0, 0) <= 0);
4954
Assert.assertEquals(1, s.readSync(buf, 0, 10));
5055
Assert.assertEquals('z', buf[0]);
56+
Assert.assertTrue(s.readSync(buf, 0, 0) <= 0);
57+
s.back('a');
58+
Assert.assertEquals(1, s.readSync(buf, 0, 1));
59+
Assert.assertEquals('a', buf[0]);
60+
s.close();
61+
}
62+
63+
@Test(timeout=120000)
64+
public void testCharByCharAsync() throws Exception {
65+
ICharacterStream.Readable.Buffered s = openStream(openFile());
66+
SynchronizationPoint<Exception> sp = new SynchronizationPoint<>();
67+
continueReadAsync(s, 0, 0, sp);
68+
sp.blockThrow(0);
69+
s.back('z');
70+
char[] buf = new char[20];
71+
Assert.assertEquals(1, s.readAsync(buf, 0, 20).blockResult(0).intValue());
72+
Assert.assertEquals('z', buf[0]);
5173
s.close();
5274
}
75+
76+
private void continueReadAsync(ICharacterStream.Readable.Buffered s, int iBuf, int iChar, SynchronizationPoint<Exception> sp) throws Exception {
77+
while (iBuf < nbBuf) {
78+
if ((iBuf + iChar) % 13 == 3) {
79+
s.back('b');
80+
Assert.assertEquals('b', s.readAsync());
81+
}
82+
int c = s.readAsync();
83+
if (c == -1)
84+
throw new Exception("Unexpected end at " + (iBuf * testBuf.length + iChar));
85+
if (c == -2) {
86+
int i = iBuf;
87+
int j = iChar;
88+
s.canStartReading().listenAsync(new Task.Cpu.FromRunnable("readAsync", Task.PRIORITY_NORMAL, () -> {
89+
try {
90+
continueReadAsync(s, i, j, sp);
91+
} catch (Exception e) {
92+
sp.error(e);
93+
}
94+
}), true);
95+
return;
96+
}
97+
if (c != (char)(testBuf[iChar]&0xFF))
98+
throw new Exception("Invalid character " + c + " at "+ (iBuf * testBuf.length + iChar));
99+
if (++iChar >= testBuf.length) {
100+
iBuf++;
101+
iChar = 0;
102+
}
103+
}
104+
sp.unblock();
105+
}
53106

107+
@Test(timeout=120000)
108+
public void testNextBufferAsync() throws Exception {
109+
ICharacterStream.Readable.Buffered s = openStream(openFile());
110+
int iBuf = 0;
111+
int iChar = 0;
112+
while (iBuf < nbBuf) {
113+
UnprotectedString str = s.readNextBufferAsync().blockResult(0);
114+
Assert.assertNotNull(str);
115+
char[] chars = str.charArray();
116+
int len = str.length();
117+
for (int i = str.charArrayStart(), nb = 0; nb < len; ++i, ++nb) {
118+
Assert.assertEquals(testBuf[iChar] & 0xFF, chars[i]);
119+
if (++iChar == testBuf.length) {
120+
iChar = 0;
121+
iBuf++;
122+
}
123+
}
124+
}
125+
Assert.assertEquals(nbBuf, iBuf);
126+
Assert.assertEquals(0, iChar);
127+
Assert.assertNull(s.readNextBufferAsync().blockResult(0));
128+
s.back('z');
129+
UnprotectedString str = s.readNextBufferAsync().blockResult(0);
130+
Assert.assertNotNull(str);
131+
Assert.assertEquals(1, str.length());
132+
Assert.assertEquals('z', str.charAt(0));
133+
Assert.assertNull(s.readNextBufferAsync().blockResult(0));
134+
s.close();
135+
}
54136
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestCharacterStreamWritable.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public void test() throws Exception {
3232
private void testWrite(String s, Charset charset) throws Exception {
3333
File tmp = TemporaryFiles.get().createFileSync("test", "writablecs");
3434
ICharacterStream.Writable cs = open(new FileIO.WriteOnly(tmp, Task.PRIORITY_NORMAL), charset);
35+
// basic tests
36+
cs.setPriority(Task.PRIORITY_IMPORTANT);
37+
Assert.assertEquals(Task.PRIORITY_IMPORTANT, cs.getPriority());
38+
cs.getDescription();
39+
Assert.assertEquals(charset, cs.getEncoding());
40+
// write
3541
cs.writeSync(s);
3642
flush(cs);
3743
cs.close();

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/serialization/TestSerialization.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,4 +1102,28 @@ protected void printSpec(IO.Readable.Seekable io, Class<?> type) throws Exceptio
11021102
@SuppressWarnings("unused")
11031103
protected void checkSpec(IO.Readable.Seekable spec, Class<?> type, IO.Readable.Seekable serialization) throws Exception {
11041104
}
1105+
1106+
// --- Error cases ---
1107+
1108+
@TypeInstantiation(factory=InvalidTypeInstantiationFactory.class)
1109+
public static abstract class InvalidTypeInstantiation {
1110+
}
1111+
1112+
public static abstract class InvalidTypeInstantiationFactory implements Provider<InvalidTypeInstantiation> {
1113+
}
1114+
1115+
public static class InvalidTypeInstantiationContainer {
1116+
public InvalidTypeInstantiation invalid;
1117+
}
1118+
1119+
@Test(timeout=30000)
1120+
public void testInvalidTypeInstantiation() throws Exception {
1121+
InvalidTypeInstantiationContainer container = new InvalidTypeInstantiationContainer();
1122+
container.invalid = new InvalidTypeInstantiation() {};
1123+
try {
1124+
testInMemory(container, InvalidTypeInstantiationContainer.class);
1125+
} catch (InstantiationException e) {
1126+
// OK
1127+
}
1128+
}
11051129
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/application/TestApplication.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.lecousin.framework.application.Application;
1010
import net.lecousin.framework.application.Artifact;
1111
import net.lecousin.framework.application.LCCore;
12+
import net.lecousin.framework.application.StandaloneLCCore;
1213
import net.lecousin.framework.application.Version;
1314
import net.lecousin.framework.application.libraries.artifacts.LoadedLibrary;
1415
import net.lecousin.framework.concurrent.Task;
@@ -117,7 +118,20 @@ public ISynchronizationPoint<Exception> closeAsync() {
117118
};
118119
LCCore.get().closed(ac);
119120
LCCore.get().isStopping();
121+
LCCore.isStopping();
120122
LCCore.get().getSystemLibraries();
123+
try {
124+
LCCore.set(new StandaloneLCCore());
125+
throw new AssertionError();
126+
} catch (IllegalStateException e) {
127+
// ok
128+
}
129+
try {
130+
LCCore.start();
131+
throw new AssertionError();
132+
} catch (IllegalStateException e) {
133+
// ok
134+
}
121135
}
122136

123137
}

0 commit comments

Comments
 (0)