Skip to content

Commit 8ab5820

Browse files
committed
add tests
1 parent 90d6d69 commit 8ab5820

File tree

4 files changed

+210
-106
lines changed

4 files changed

+210
-106
lines changed
Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
1-
package net.lecousin.framework.adapter;
2-
3-
import java.io.File;
4-
5-
import net.lecousin.framework.concurrent.Task;
6-
import net.lecousin.framework.io.FileIO;
7-
import net.lecousin.framework.io.IO;
8-
9-
/**
10-
* Contains classes to convert a file into an IO.
11-
*/
12-
public final class FileToIO {
13-
14-
/** Convert a File into an IO.Readable by opening it. */
15-
public static class Readable implements Adapter<File,IO.Readable.Seekable> {
16-
@Override
17-
public Class<File> getInputType() { return File.class; }
18-
19-
@Override
20-
public Class<IO.Readable.Seekable> getOutputType() { return IO.Readable.Seekable.class; }
21-
22-
@Override
23-
public boolean canAdapt(File input) {
24-
return input.isFile();
25-
}
26-
27-
@Override
28-
public IO.Readable.Seekable adapt(File input) {
29-
return new FileIO.ReadOnly(input, Task.PRIORITY_NORMAL);
30-
}
31-
}
32-
33-
/** Convert a File into an IO.Writable by opening it. */
34-
public static class Writable implements Adapter<File,IO.Writable.Seekable> {
35-
@Override
36-
public Class<File> getInputType() { return File.class; }
37-
38-
@Override
39-
public Class<IO.Writable.Seekable> getOutputType() { return IO.Writable.Seekable.class; }
40-
41-
@Override
42-
public boolean canAdapt(File input) {
43-
return input.isFile();
44-
}
45-
46-
@Override
47-
public IO.Writable.Seekable adapt(File input) {
48-
return new FileIO.ReadWrite(input, Task.PRIORITY_NORMAL);
49-
}
50-
}
51-
52-
}
1+
package net.lecousin.framework.adapter;
2+
3+
import java.io.File;
4+
5+
import net.lecousin.framework.concurrent.Task;
6+
import net.lecousin.framework.io.FileIO;
7+
import net.lecousin.framework.io.IO;
8+
9+
/**
10+
* Contains classes to convert a file into an IO.
11+
*/
12+
public final class FileToIO {
13+
14+
private FileToIO() { /* no instance */ }
15+
16+
/** Convert a File into an IO.Readable by opening it. */
17+
public static class Readable implements Adapter<File,IO.Readable.Seekable> {
18+
@Override
19+
public Class<File> getInputType() { return File.class; }
20+
21+
@Override
22+
public Class<IO.Readable.Seekable> getOutputType() { return IO.Readable.Seekable.class; }
23+
24+
@Override
25+
public boolean canAdapt(File input) {
26+
return input.isFile();
27+
}
28+
29+
@Override
30+
public IO.Readable.Seekable adapt(File input) {
31+
return new FileIO.ReadOnly(input, Task.PRIORITY_NORMAL);
32+
}
33+
}
34+
35+
/** Convert a File into an IO.Writable by opening it. */
36+
public static class Writable implements Adapter<File,IO.Writable.Seekable> {
37+
@Override
38+
public Class<File> getInputType() { return File.class; }
39+
40+
@Override
41+
public Class<IO.Writable.Seekable> getOutputType() { return IO.Writable.Seekable.class; }
42+
43+
@Override
44+
public boolean canAdapt(File input) {
45+
return input.isFile();
46+
}
47+
48+
@Override
49+
public IO.Writable.Seekable adapt(File input) {
50+
return new FileIO.ReadWrite(input, Task.PRIORITY_NORMAL);
51+
}
52+
}
53+
54+
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
package net.lecousin.framework.collections.map;
2-
3-
/**
4-
* Interface for a Map that accept byte keys (primitive type instead of Byte).
5-
* @param <ValueType> type of values
6-
*/
7-
public interface ByteMap<ValueType> {
8-
9-
/** Put a value and return the previous value associated with this key, or null. */
10-
public ValueType put(byte key, ValueType entry);
11-
12-
/** Get a value. */
13-
public ValueType get(byte key);
14-
15-
/** Remove a value. */
16-
public ValueType remove(byte key);
17-
18-
/** Return true if the map contains the given key. */
19-
public boolean containsKey(byte key);
20-
21-
/** Return the number of elements. */
22-
public int size();
23-
24-
/** Return true if this map is empty. */
25-
public default boolean isEmpty() { return size() == 0; }
26-
27-
}
1+
package net.lecousin.framework.collections.map;
2+
3+
/**
4+
* Interface for a Map that accept byte keys (primitive type instead of Byte).
5+
* @param <ValueType> type of values
6+
*/
7+
public interface ByteMap<ValueType> {
8+
9+
/** Put a value and return the previous value associated with this key, or null. */
10+
public ValueType put(byte key, ValueType entry);
11+
12+
/** Get a value. */
13+
public ValueType get(byte key);
14+
15+
/** Remove a value. */
16+
public ValueType remove(byte key);
17+
18+
/** Return true if the map contains the given key. */
19+
public boolean containsKey(byte key);
20+
21+
/** Return the number of elements. */
22+
public int size();
23+
24+
/** Return true if this map is empty. */
25+
public boolean isEmpty();
26+
27+
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
package net.lecousin.framework.collections.map;
2-
3-
/**
4-
* Interface for a Map that accept short keys (primitive type instead of Short).
5-
* @param <ValueType> type of values
6-
*/
7-
public interface ShortMap<ValueType> {
8-
9-
/** Put a value and return the previous value associated with this key, or null. */
10-
public ValueType put(short key, ValueType entry);
11-
12-
/** Get a value. */
13-
public ValueType get(short key);
14-
15-
/** Remove a value. */
16-
public ValueType remove(short key);
17-
18-
/** Return true if the map contains the given key. */
19-
public boolean containsKey(short key);
20-
21-
/** Return the number of elements. */
22-
public int size();
23-
24-
/** Return true if this map is empty. */
25-
public default boolean isEmpty() { return size() == 0; }
26-
27-
}
1+
package net.lecousin.framework.collections.map;
2+
3+
/**
4+
* Interface for a Map that accept short keys (primitive type instead of Short).
5+
* @param <ValueType> type of values
6+
*/
7+
public interface ShortMap<ValueType> {
8+
9+
/** Put a value and return the previous value associated with this key, or null. */
10+
public ValueType put(short key, ValueType entry);
11+
12+
/** Get a value. */
13+
public ValueType get(short key);
14+
15+
/** Remove a value. */
16+
public ValueType remove(short key);
17+
18+
/** Return true if the map contains the given key. */
19+
public boolean containsKey(short key);
20+
21+
/** Return the number of elements. */
22+
public int size();
23+
24+
/** Return true if this map is empty. */
25+
public boolean isEmpty();
26+
27+
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/adapter/TestAdapter.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
import java.io.File;
44
import java.nio.ByteBuffer;
5+
import java.nio.file.Path;
6+
import java.util.LinkedList;
57

68
import net.lecousin.framework.adapter.Adapter;
79
import net.lecousin.framework.adapter.AdapterRegistry;
10+
import net.lecousin.framework.adapter.FileInfoToFile;
11+
import net.lecousin.framework.adapter.FileToIO;
12+
import net.lecousin.framework.adapter.LinkedAdapter;
813
import net.lecousin.framework.core.test.LCCoreAbstractTest;
914
import net.lecousin.framework.io.IO;
15+
import net.lecousin.framework.io.util.FileInfo;
1016

1117
import org.junit.Assert;
1218
import org.junit.Test;
@@ -128,4 +134,100 @@ public void testFileToIO() throws Exception {
128134
Assert.assertEquals(3, buf[2]);
129135
in.close();
130136
}
137+
138+
@Test
139+
public void basicTests() throws Exception {
140+
Assert.assertEquals(0, new AdapterRegistry().getPlugins().size());
141+
Assert.assertNull(AdapterRegistry.get().adapt(new Object(), TestAdapter.class));
142+
FileInfo fi = new FileInfo();
143+
Assert.assertNull(AdapterRegistry.get().findAdapter(fi, FileInfo.class, File.class));
144+
Assert.assertNull(AdapterRegistry.get().findAdapter(fi, FileInfo.class, Path.class));
145+
@SuppressWarnings("rawtypes")
146+
LinkedList<Adapter> list = new LinkedList<>();
147+
list.add(new FileInfoToFile());
148+
list.add(new FileToIO.Readable());
149+
LinkedAdapter la = new LinkedAdapter(list);
150+
Assert.assertEquals(FileInfo.class, la.getInputType());
151+
Assert.assertEquals(IO.Readable.Seekable.class, la.getOutputType());
152+
la.canAdapt(fi);
153+
la.adapt(fi);
154+
}
155+
156+
public static class Source3 {
157+
public int value = 0;
158+
}
159+
public static class Target3 {
160+
public int value = 0;
161+
}
162+
public static class Target4 {
163+
public int value = 0;
164+
}
165+
public static class Target4bis extends Target4 {
166+
}
167+
public static class Target4ter extends Target4bis {
168+
}
169+
public static class Adapter3 implements Adapter<Source3, Target3> {
170+
@Override
171+
public Class<Source3> getInputType() { return Source3.class; }
172+
@Override
173+
public Class<Target3> getOutputType() { return Target3.class; }
174+
@Override
175+
public boolean canAdapt(Source3 input) {
176+
return input.value > 100;
177+
}
178+
@Override
179+
public Target3 adapt(Source3 input) {
180+
Target3 t = new Target3();
181+
t.value = input.value - 100;
182+
return t;
183+
}
184+
}
185+
public static class Adapter4bis implements Adapter<Source3, Target4bis> {
186+
@Override
187+
public Class<Source3> getInputType() { return Source3.class; }
188+
@Override
189+
public Class<Target4bis> getOutputType() { return Target4bis.class; }
190+
@Override
191+
public boolean canAdapt(Source3 input) {
192+
return input.value > 100;
193+
}
194+
@Override
195+
public Target4bis adapt(Source3 input) {
196+
Target4bis t = new Target4bis();
197+
t.value = input.value - 10;
198+
return t;
199+
}
200+
}
201+
public static class Adapter4ter implements Adapter<Source3, Target4ter> {
202+
@Override
203+
public Class<Source3> getInputType() { return Source3.class; }
204+
@Override
205+
public Class<Target4ter> getOutputType() { return Target4ter.class; }
206+
@Override
207+
public boolean canAdapt(Source3 input) {
208+
return input.value > 100;
209+
}
210+
@Override
211+
public Target4ter adapt(Source3 input) {
212+
Target4ter t = new Target4ter();
213+
t.value = input.value - 1;
214+
return t;
215+
}
216+
}
217+
218+
@Test
219+
public void test3() throws Exception {
220+
AdapterRegistry.get().addPlugin(new Adapter3());
221+
Source3 src = new Source3();
222+
src.value = 1;
223+
Assert.assertNull(AdapterRegistry.get().adapt(src, Target3.class));
224+
src.value = 111;
225+
Assert.assertEquals(11, AdapterRegistry.get().adapt(src, Target3.class).value);
226+
227+
AdapterRegistry.get().addPlugin(new Adapter4bis());
228+
AdapterRegistry.get().addPlugin(new Adapter4ter());
229+
src.value = 111;
230+
Assert.assertEquals(110, AdapterRegistry.get().adapt(src, Target4.class).value);
231+
}
232+
131233
}

0 commit comments

Comments
 (0)