From f4a0214f28d0d5e150f7063cf3d36bf65c8dcc88 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 30 Jun 2024 19:13:06 +0200 Subject: [PATCH] use submodule system --- test_code/BufferedInputStreamTest.java | 115 ----- test_code/BufferedOutputStreamTest.java | 136 ----- test_code/BundleTest.java | 249 --------- test_code/CryptUtilsTest.java | 155 ------ test_code/IOUtilsTest.java | 24 - test_code/NextableInputStreamTest.java | 340 ------------- test_code/ProgressTest.java | 319 ------------ test_code/ReplaceInputStreamTest.java | 210 -------- test_code/ReplaceOutputStreamTest.java | 168 ------- test_code/SerialServerTest.java | 637 ------------------------ test_code/SerialTest.java | 281 ----------- test_code/StringUtilsTest.java | 304 ----------- test_code/TempFilesTest.java | 109 ---- test_code/Test.java | 68 --- test_code/VersionTest.java | 140 ------ test_code/bundle_test.properties | 3 - 16 files changed, 3258 deletions(-) delete mode 100644 test_code/BufferedInputStreamTest.java delete mode 100644 test_code/BufferedOutputStreamTest.java delete mode 100644 test_code/BundleTest.java delete mode 100644 test_code/CryptUtilsTest.java delete mode 100644 test_code/IOUtilsTest.java delete mode 100644 test_code/NextableInputStreamTest.java delete mode 100644 test_code/ProgressTest.java delete mode 100644 test_code/ReplaceInputStreamTest.java delete mode 100644 test_code/ReplaceOutputStreamTest.java delete mode 100644 test_code/SerialServerTest.java delete mode 100644 test_code/SerialTest.java delete mode 100644 test_code/StringUtilsTest.java delete mode 100644 test_code/TempFilesTest.java delete mode 100644 test_code/Test.java delete mode 100644 test_code/VersionTest.java delete mode 100644 test_code/bundle_test.properties diff --git a/test_code/BufferedInputStreamTest.java b/test_code/BufferedInputStreamTest.java deleted file mode 100644 index c715585..0000000 --- a/test_code/BufferedInputStreamTest.java +++ /dev/null @@ -1,115 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.streams.BufferedInputStream; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class BufferedInputStreamTest extends TestLauncher { - public BufferedInputStreamTest(String[] args) { - super("BufferedInputStream test", args); - - addTest(new TestCase("Simple InputStream reading") { - @Override - public void test() throws Exception { - byte[] expected = new byte[] { 42, 12, 0, 127 }; - BufferedInputStream in = new BufferedInputStream( - new ByteArrayInputStream(expected)); - checkArrays(this, "FIRST", in, expected); - } - }); - - addTest(new TestCase("Simple byte array reading") { - @Override - public void test() throws Exception { - byte[] expected = new byte[] { 42, 12, 0, 127 }; - BufferedInputStream in = new BufferedInputStream(expected); - checkArrays(this, "FIRST", in, expected); - } - }); - - addTest(new TestCase("Byte array is(byte[])") { - @Override - public void test() throws Exception { - byte[] expected = new byte[] { 42, 12, 0, 127 }; - BufferedInputStream in = new BufferedInputStream(expected); - assertEquals( - "The array should be considered identical to its source", - true, in.is(expected)); - assertEquals( - "The array should be considered different to that one", - false, in.is(new byte[] { 42, 12, 0, 121 })); - in.close(); - } - }); - - addTest(new TestCase("InputStream is(byte[])") { - @Override - public void test() throws Exception { - byte[] expected = new byte[] { 42, 12, 0, 127 }; - BufferedInputStream in = new BufferedInputStream( - new ByteArrayInputStream(expected)); - assertEquals( - "The array should be considered identical to its source", - true, in.is(expected)); - assertEquals( - "The array should be considered different to that one", - false, in.is(new byte[] { 42, 12, 0, 121 })); - in.close(); - } - }); - - addTest(new TestCase("Byte array is(String)") { - @Override - public void test() throws Exception { - String expected = "Testy"; - BufferedInputStream in = new BufferedInputStream( - expected.getBytes("UTF-8")); - assertEquals( - "The array should be considered identical to its source", - true, in.is(expected)); - assertEquals( - "The array should be considered different to that one", - false, in.is("Autre")); - assertEquals( - "The array should be considered different to that one", - false, in.is("Test")); - in.close(); - } - }); - - addTest(new TestCase("InputStream is(String)") { - @Override - public void test() throws Exception { - String expected = "Testy"; - BufferedInputStream in = new BufferedInputStream( - new ByteArrayInputStream(expected.getBytes("UTF-8"))); - assertEquals( - "The array should be considered identical to its source", - true, in.is(expected)); - assertEquals( - "The array should be considered different to that one", - false, in.is("Autre")); - assertEquals( - "The array should be considered different to that one", - false, in.is("Testy.")); - in.close(); - } - }); - } - - static void checkArrays(TestCase test, String prefix, InputStream in, - byte[] expected) throws Exception { - byte[] actual = IOUtils.toByteArray(in); - test.assertEquals("The " + prefix - + " resulting array has not the correct number of items", - expected.length, actual.length); - for (int i = 0; i < actual.length; i++) { - test.assertEquals(prefix + ": item " + i - + " (0-based) is not the same", expected[i], actual[i]); - } - } -} diff --git a/test_code/BufferedOutputStreamTest.java b/test_code/BufferedOutputStreamTest.java deleted file mode 100644 index 5646e61..0000000 --- a/test_code/BufferedOutputStreamTest.java +++ /dev/null @@ -1,136 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.List; - -import be.nikiroo.utils.streams.BufferedOutputStream; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class BufferedOutputStreamTest extends TestLauncher { - public BufferedOutputStreamTest(String[] args) { - super("BufferedOutputStream test", args); - - addTest(new TestCase("Single write") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - BufferedOutputStream out = new BufferedOutputStream(bout); - - byte[] data = new byte[] { 42, 12, 0, 127 }; - - out.write(data); - out.close(); - - checkArrays(this, "FIRST", bout, data); - } - }); - - addTest(new TestCase("Single write of 5000 bytes") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - BufferedOutputStream out = new BufferedOutputStream(bout); - - byte[] data = new byte[5000]; - for (int i = 0; i < data.length; i++) { - data[i] = (byte) (i % 255); - } - - out.write(data); - out.close(); - - checkArrays(this, "FIRST", bout, data); - } - }); - - addTest(new TestCase("Multiple writes") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - BufferedOutputStream out = new BufferedOutputStream(bout); - - byte[] data1 = new byte[] { 42, 12, 0, 127 }; - byte[] data2 = new byte[] { 15, 55 }; - byte[] data3 = new byte[] {}; - - byte[] dataAll = new byte[] { 42, 12, 0, 127, 15, 55 }; - - out.write(data1); - out.write(data2); - out.write(data3); - out.close(); - - checkArrays(this, "FIRST", bout, dataAll); - } - }); - - addTest(new TestCase("Multiple writes for a 5000 bytes total") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - BufferedOutputStream out = new BufferedOutputStream(bout); - - byte[] data = new byte[] { 42, 12, 0, 127, 51, 2, 32, 66, 7, 87 }; - - List bytes = new ArrayList(); - - // write 400 * 10 + 1000 bytes = 5000 - for (int i = 0; i < 400; i++) { - for (int j = 0; j < data.length; j++) { - bytes.add(data[j]); - } - out.write(data); - } - - for (int i = 0; i < 1000; i++) { - for (int j = 0; j < data.length; j++) { - bytes.add(data[j]); - } - out.write(data); - } - - out.close(); - - byte[] abytes = new byte[bytes.size()]; - for (int i = 0; i < bytes.size(); i++) { - abytes[i] = bytes.get(i); - } - - checkArrays(this, "FIRST", bout, abytes); - } - }); - } - - static void checkArrays(TestCase test, String prefix, - ByteArrayOutputStream bout, byte[] expected) throws Exception { - byte[] actual = bout.toByteArray(); - - if (false) { - System.out.print("\nExpected data: [ "); - for (int i = 0; i < expected.length; i++) { - if (i > 0) - System.out.print(", "); - System.out.print(expected[i]); - } - System.out.println(" ]"); - - System.out.print("Actual data : [ "); - for (int i = 0; i < actual.length; i++) { - if (i > 0) - System.out.print(", "); - System.out.print(actual[i]); - } - System.out.println(" ]"); - } - - test.assertEquals("The " + prefix - + " resulting array has not the correct number of items", - expected.length, actual.length); - for (int i = 0; i < actual.length; i++) { - test.assertEquals(prefix + ": item " + i - + " (0-based) is not the same", expected[i], actual[i]); - } - } -} diff --git a/test_code/BundleTest.java b/test_code/BundleTest.java deleted file mode 100644 index 2e25eb0..0000000 --- a/test_code/BundleTest.java +++ /dev/null @@ -1,249 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.resources.Bundle; -import be.nikiroo.utils.resources.Bundles; -import be.nikiroo.utils.resources.Meta; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class BundleTest extends TestLauncher { - private File tmp; - private B b = new B(); - - public BundleTest(String[] args) { - this("Bundle test", args); - } - - protected BundleTest(String name, String[] args) { - super(name, args); - - for (TestCase test : getSimpleTests()) { - addTest(test); - } - - addSeries(new TestLauncher("After saving/reloading the resources", args) { - { - for (TestCase test : getSimpleTests()) { - addTest(test); - } - } - - @Override - protected void start() throws Exception { - tmp = File.createTempFile("nikiroo-utils", ".test"); - tmp.delete(); - tmp.mkdir(); - b.updateFile(tmp.getAbsolutePath()); - Bundles.setDirectory(tmp.getAbsolutePath()); - b.reload(false); - } - - @Override - protected void stop() { - IOUtils.deltree(tmp); - } - }); - - addSeries(new TestLauncher("Read/Write support", args) { - { - addTest(new TestCase("Reload") { - @Override - public void test() throws Exception { - String def = b.getString(E.ONE); - String val = "Something"; - - b.setString(E.ONE, val); - b.updateFile(); - b.reload(true); - - assertEquals("We should have reset the bundle", def, - b.getString(E.ONE)); - - b.reload(false); - - assertEquals("We should have reloaded the same files", - val, b.getString(E.ONE)); - - // reset values for next tests - b.reload(true); - b.updateFile(); - } - }); - - addTest(new TestCase("Set/Get") { - @Override - public void test() throws Exception { - String val = "Newp"; - b.setString(E.ONE, val); - String setGet = b.getString(E.ONE); - - assertEquals(val, setGet); - - // reset values for next tests - b.restoreSnapshot(null); - } - }); - - addTest(new TestCase("Snapshots") { - @Override - public void test() throws Exception { - String val = "Newp"; - String def = b.getString(E.ONE); - - b.setString(E.ONE, val); - Object snap = b.takeSnapshot(); - - b.restoreSnapshot(null); - assertEquals( - "restoreChanges(null) should clear the changes", - def, b.getString(E.ONE)); - b.restoreSnapshot(snap); - assertEquals( - "restoreChanges(snapshot) should restore the changes", - val, b.getString(E.ONE)); - - // reset values for next tests - b.restoreSnapshot(null); - } - }); - - addTest(new TestCase("updateFile with changes") { - @Override - public void test() throws Exception { - String val = "Go to disk! (UTF-8 test: 日本語)"; - - String def = b.getString(E.ONE); - b.setString(E.ONE, val); - b.updateFile(tmp.getAbsolutePath()); - b.reload(false); - - assertEquals(val, b.getString(E.ONE)); - - // reset values for next tests - b.setString(E.ONE, def); - b.updateFile(tmp.getAbsolutePath()); - b.reload(false); - } - }); - } - - @Override - protected void start() throws Exception { - tmp = File.createTempFile("nikiroo-utils", ".test"); - tmp.delete(); - tmp.mkdir(); - b.updateFile(tmp.getAbsolutePath()); - Bundles.setDirectory(tmp.getAbsolutePath()); - b.reload(false); - } - - @Override - protected void stop() { - IOUtils.deltree(tmp); - } - }); - } - - private List getSimpleTests() { - String pre = ""; - - List list = new ArrayList(); - - list.add(new TestCase(pre + "getString simple") { - @Override - public void test() throws Exception { - assertEquals("un", b.getString(E.ONE)); - } - }); - - list.add(new TestCase(pre + "getStringX with null suffix") { - @Override - public void test() throws Exception { - assertEquals("un", b.getStringX(E.ONE, null)); - } - }); - - list.add(new TestCase(pre + "getStringX with empty suffix") { - @Override - public void test() throws Exception { - assertEquals(null, b.getStringX(E.ONE, "")); - } - }); - - list.add(new TestCase(pre + "getStringX with existing suffix") { - @Override - public void test() throws Exception { - assertEquals("un + suffix", b.getStringX(E.ONE, "suffix")); - } - }); - - list.add(new TestCase(pre + "getStringX with not existing suffix") { - @Override - public void test() throws Exception { - assertEquals(null, b.getStringX(E.ONE, "fake")); - } - }); - - list.add(new TestCase(pre + "getString with UTF-8 content") { - @Override - public void test() throws Exception { - assertEquals("日本語 Nihongo", b.getString(E.JAPANESE)); - } - }); - - return list; - } - - /** - * {@link Bundle}. - * - * @author niki - */ - private class B extends Bundle { - protected B() { - super(E.class, N.bundle_test, null); - } - - @Override - // ...and make it public - public Object takeSnapshot() { - return super.takeSnapshot(); - } - - @Override - // ...and make it public - public void restoreSnapshot(Object snap) { - super.restoreSnapshot(snap); - } - } - - /** - * Key enum for the {@link Bundle}. - * - * @author niki - */ - private enum E { - @Meta - ONE, // - @Meta - ONE_SUFFIX, // - @Meta - TWO, // - @Meta - JAPANESE - } - - /** - * Name enum for the {@link Bundle}. - * - * @author niki - */ - private enum N { - bundle_test - } -} diff --git a/test_code/CryptUtilsTest.java b/test_code/CryptUtilsTest.java deleted file mode 100644 index 0c53461..0000000 --- a/test_code/CryptUtilsTest.java +++ /dev/null @@ -1,155 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; - -import be.nikiroo.utils.CryptUtils; -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class CryptUtilsTest extends TestLauncher { - private String key; - private CryptUtils crypt; - - public CryptUtilsTest(String[] args) { - super("CryptUtils test", args); - - String longKey = "some long string with more than 128 bits (=32 bytes) of data"; - - addSeries(new CryptUtilsTest(args, "Manual input wuth NULL key", null, - 1)); - addSeries(new CryptUtilsTest(args, "Streams with NULL key", null, true)); - - addSeries(new CryptUtilsTest(args, "Manual input with emptykey", "", 1)); - addSeries(new CryptUtilsTest(args, "Streams with empty key", "", true)); - - addSeries(new CryptUtilsTest(args, "Manual input with long key", - longKey, 1)); - addSeries(new CryptUtilsTest(args, "Streams with long key", longKey, - true)); - } - - @Override - protected void addTest(final TestCase test) { - super.addTest(new TestCase(test.getName()) { - @Override - public void test() throws Exception { - test.test(); - } - - @Override - public void setUp() throws Exception { - crypt = new CryptUtils(key); - test.setUp(); - } - - @Override - public void tearDown() throws Exception { - test.tearDown(); - crypt = null; - } - }); - } - - private CryptUtilsTest(String[] args, String title, String key, - @SuppressWarnings("unused") int dummy) { - super(title, args); - this.key = key; - - final String longData = "Le premier jour, Le Grand Barbu dans le cloud fit la lumière, et il vit que c'était bien. Ou quelque chose comme ça. Je préfère la Science-Fiction en général, je trouve ça plus sain :/"; - - addTest(new TestCase("Short") { - @Override - public void test() throws Exception { - String orig = "data"; - byte[] encrypted = crypt.encrypt(orig); - String decrypted = crypt.decrypts(encrypted); - - assertEquals(orig, decrypted); - } - }); - - addTest(new TestCase("Short, base64") { - @Override - public void test() throws Exception { - String orig = "data"; - String encrypted = crypt.encrypt64(orig); - String decrypted = crypt.decrypt64s(encrypted); - - assertEquals(orig, decrypted); - } - }); - - addTest(new TestCase("Empty") { - @Override - public void test() throws Exception { - String orig = ""; - byte[] encrypted = crypt.encrypt(orig); - String decrypted = crypt.decrypts(encrypted); - - assertEquals(orig, decrypted); - } - }); - - addTest(new TestCase("Empty, base64") { - @Override - public void test() throws Exception { - String orig = ""; - String encrypted = crypt.encrypt64(orig); - String decrypted = crypt.decrypt64s(encrypted); - - assertEquals(orig, decrypted); - } - }); - - addTest(new TestCase("Long") { - @Override - public void test() throws Exception { - String orig = longData; - byte[] encrypted = crypt.encrypt(orig); - String decrypted = crypt.decrypts(encrypted); - - assertEquals(orig, decrypted); - } - }); - - addTest(new TestCase("Long, base64") { - @Override - public void test() throws Exception { - String orig = longData; - String encrypted = crypt.encrypt64(orig); - String decrypted = crypt.decrypt64s(encrypted); - - assertEquals(orig, decrypted); - } - }); - } - - private CryptUtilsTest(String[] args, String title, String key, - @SuppressWarnings("unused") boolean dummy) { - super(title, args); - this.key = key; - - addTest(new TestCase("Simple test") { - @Override - public void test() throws Exception { - InputStream in = new ByteArrayInputStream(new byte[] { 42, 127, - 12 }); - crypt.encrypt(in); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - IOUtils.write(in, out); - byte[] result = out.toByteArray(); - - assertEquals( - "We wrote 3 bytes, we expected 3 bytes back but got: " - + result.length, result.length, result.length); - - assertEquals(42, result[0]); - assertEquals(127, result[1]); - assertEquals(12, result[2]); - } - }); - } -} diff --git a/test_code/IOUtilsTest.java b/test_code/IOUtilsTest.java deleted file mode 100644 index 9f22896..0000000 --- a/test_code/IOUtilsTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.InputStream; - -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class IOUtilsTest extends TestLauncher { - public IOUtilsTest(String[] args) { - super("IOUtils test", args); - - addTest(new TestCase("openResource") { - @Override - public void test() throws Exception { - InputStream in = IOUtils.openResource("VERSION"); - assertNotNull( - "The VERSION file is supposed to be present in the binaries", - in); - in.close(); - } - }); - } -} diff --git a/test_code/NextableInputStreamTest.java b/test_code/NextableInputStreamTest.java deleted file mode 100644 index 4e59823..0000000 --- a/test_code/NextableInputStreamTest.java +++ /dev/null @@ -1,340 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.ByteArrayInputStream; - -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.streams.NextableInputStream; -import be.nikiroo.utils.streams.NextableInputStreamStep; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -public class NextableInputStreamTest extends TestLauncher { - public NextableInputStreamTest(String[] args) { - super("NextableInputStream test", args); - - addTest(new TestCase("Simple byte array reading") { - @Override - public void test() throws Exception { - byte[] expected = new byte[] { 42, 12, 0, 127 }; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(expected), null); - checkNext(this, "READ", in, expected); - } - }); - - addTest(new TestCase("Stop at 12") { - @Override - public void test() throws Exception { - byte[] expected = new byte[] { 42, 12, 0, 127 }; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(expected), - new NextableInputStreamStep(12)); - - checkNext(this, "FIRST", in, new byte[] { 42 }); - } - }); - - addTest(new TestCase("Stop at 12, resume, stop again, resume") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(data), - new NextableInputStreamStep(12)); - - checkNext(this, "FIRST", in, new byte[] { 42 }); - checkNext(this, "SECOND", in, new byte[] { 0, 127 }); - checkNext(this, "THIRD", in, new byte[] { 51, 11 }); - } - }); - - addTest(new TestCase("Encapsulation") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 }; - NextableInputStream in4 = new NextableInputStream( - new ByteArrayInputStream(data), - new NextableInputStreamStep(4)); - NextableInputStream subIn12 = new NextableInputStream(in4, - new NextableInputStreamStep(12)); - - in4.next(); - checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 }); - checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 }); - - assertEquals("The subIn still has some data", false, - subIn12.next()); - - checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 }); - } - }); - - addTest(new TestCase("UTF-8 text lines test") { - @Override - public void test() throws Exception { - String ln1 = "Ligne première"; - String ln2 = "Ligne la deuxième du nom"; - byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8"); - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(data), - new NextableInputStreamStep('\n')); - - checkNext(this, "FIRST", in, ln1.getBytes("UTF-8")); - checkNext(this, "SECOND", in, ln2.getBytes("UTF-8")); - } - }); - - addTest(new TestCase("nextAll()") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(data), - new NextableInputStreamStep(12)); - - checkNext(this, "FIRST", in, new byte[] { 42 }); - checkNextAll(this, "REST", in, new byte[] { 0, 127, 12, 51, 11, - 12 }); - assertEquals("The stream still has some data", false, in.next()); - } - }); - - addTest(new TestCase("getBytesRead()") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(data), - new NextableInputStreamStep(12)); - - in.nextAll(); - IOUtils.toByteArray(in); - - assertEquals("The number of bytes read is not correct", - data.length, in.getBytesRead()); - } - }); - - addTest(new TestCase("bytes array input") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; - NextableInputStream in = new NextableInputStream(data, - new NextableInputStreamStep(12)); - - checkNext(this, "FIRST", in, new byte[] { 42 }); - checkNext(this, "SECOND", in, new byte[] { 0, 127 }); - checkNext(this, "THIRD", in, new byte[] { 51, 11 }); - } - }); - - addTest(new TestCase("Skip data") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; - NextableInputStream in = new NextableInputStream(data, null); - in.next(); - - byte[] rest = new byte[] { 12, 51, 11, 12 }; - - in.skip(4); - assertEquals("STARTS_WITH OK_1", true, in.startsWith(rest)); - assertEquals("STARTS_WITH KO_1", false, - in.startsWith(new byte[] { 0 })); - assertEquals("STARTS_WITH KO_2", false, in.startsWith(data)); - assertEquals("STARTS_WITH KO_3", false, - in.startsWith(new byte[] { 1, 2, 3 })); - assertEquals("STARTS_WITH OK_2", true, in.startsWith(rest)); - assertEquals("READ REST", IOUtils.readSmallStream(in), - new String(rest)); - in.close(); - } - }); - - addTest(new TestCase("Starts with") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; - NextableInputStream in = new NextableInputStream(data, null); - in.next(); - - // yes - assertEquals("It actually starts with that", true, - in.startsWith(new byte[] { 42 })); - assertEquals("It actually starts with that", true, - in.startsWith(new byte[] { 42, 12 })); - assertEquals("It actually is the same array", true, - in.startsWith(data)); - - // no - assertEquals("It actually does not start with that", false, - in.startsWith(new byte[] { 12 })); - assertEquals( - "It actually does not start with that", - false, - in.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11, - 11 })); - - // too big - assertEquals( - "A search term bigger than the whole data cannot be found in the data", - false, in.startsWith(new byte[] { 42, 12, 0, 127, 12, - 51, 11, 12, 0 })); - - in.close(); - } - }); - - addTest(new TestCase("Starts with strings") { - @Override - public void test() throws Exception { - String text = "Fanfan et Toto vont à la mer"; - byte[] data = text.getBytes("UTF-8"); - NextableInputStream in = new NextableInputStream(data, null); - in.next(); - - // yes - assertEquals("It actually starts with that", true, - in.startsWith("F")); - assertEquals("It actually starts with that", true, - in.startsWith("Fanfan et")); - assertEquals("It actually is the same text", true, - in.startsWith(text)); - - // no - assertEquals("It actually does not start with that", false, - in.startsWith("Toto")); - assertEquals("It actually does not start with that", false, - in.startsWith("Fanfan et Toto vont à la mee")); - - // too big - assertEquals( - "A search term bigger than the whole data cannot be found in the data", - false, in.startsWith("Fanfan et Toto vont à la mer.")); - - in.close(); - } - }); - - addTest(new TestCase("Starts With strings + steps") { - @Override - public void test() throws Exception { - String data = "{\nREF: fanfan\n}"; - NextableInputStream in = new NextableInputStream( - data.getBytes("UTF-8"), new NextableInputStreamStep( - '\n')); - in.next(); - - assertEquals("STARTS_WITH OK", true, in.startsWith("{")); - in.skip(1); - assertEquals("STARTS_WITH WHEN SPENT", false, - in.startsWith("{")); - - checkNext(this, "PARTIAL CONTENT", in, - "REF: fanfan".getBytes("UTF-8")); - } - }); - - addTest(new TestCase("InputStream is(String)") { - @Override - public void test() throws Exception { - String data = "{\nREF: fanfan\n}"; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(data.getBytes("UTF-8")), - new NextableInputStreamStep('\n')); - - in.next(); - assertEquals("Item 1 OK", true, in.is("{")); - assertEquals("Item 1 KO_1", false, in.is("|")); - assertEquals("Item 1 KO_2", false, in.is("{}")); - in.skip(1); - in.next(); - assertEquals("Item 2 OK", true, in.is("REF: fanfan")); - assertEquals("Item 2 KO", false, in.is("REF: fanfan.")); - IOUtils.readSmallStream(in); - in.next(); - assertEquals("Item 3 OK", true, in.is("}")); - - in.close(); - } - }); - - addTest(new TestCase("Bytes NextAll test") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(data), - new NextableInputStreamStep(12)); - - checkNext(this, "FIRST", in, new byte[] { 42 }); - checkNextAll(this, "SECOND", in, new byte[] { 0, 127, 12, 51, - 11, 12 }); - } - }); - - addTest(new TestCase("String NextAll test") { - @Override - public void test() throws Exception { - String d1 = "^java.lang.String"; - String d2 = "\"http://example.com/query.html\""; - String data = d1 + ":" + d2; - NextableInputStream in = new NextableInputStream( - new ByteArrayInputStream(data.getBytes("UTF-8")), - new NextableInputStreamStep(':')); - - checkNext(this, "FIRST", in, d1.getBytes("UTF-8")); - checkNextAll(this, "SECOND", in, d2.getBytes("UTF-8")); - } - }); - - addTest(new TestCase("NextAll in Next test") { - @Override - public void test() throws Exception { - String line1 = "première ligne"; - String d1 = "^java.lang.String"; - String d2 = "\"http://example.com/query.html\""; - String line3 = "end of lines"; - String data = line1 + "\n" + d1 + ":" + d2 + "\n" + line3; - - NextableInputStream inL = new NextableInputStream( - new ByteArrayInputStream(data.getBytes("UTF-8")), - new NextableInputStreamStep('\n')); - - checkNext(this, "Line 1", inL, line1.getBytes("UTF-8")); - inL.next(); - - NextableInputStream in = new NextableInputStream(inL, - new NextableInputStreamStep(':')); - - checkNext(this, "Line 2 FIRST", in, d1.getBytes("UTF-8")); - checkNextAll(this, "Line 2 SECOND", in, d2.getBytes("UTF-8")); - } - }); - } - - static void checkNext(TestCase test, String prefix, NextableInputStream in, - byte[] expected) throws Exception { - test.assertEquals("Cannot get " + prefix + " entry", true, in.next()); - checkArrays(test, prefix, in, expected); - } - - static void checkNextAll(TestCase test, String prefix, - NextableInputStream in, byte[] expected) throws Exception { - test.assertEquals("Cannot get " + prefix + " entries", true, - in.nextAll()); - checkArrays(test, prefix, in, expected); - } - - static void checkArrays(TestCase test, String prefix, - NextableInputStream in, byte[] expected) throws Exception { - byte[] actual = IOUtils.toByteArray(in); - test.assertEquals("The " + prefix - + " resulting array has not the correct number of items", - expected.length, actual.length); - for (int i = 0; i < actual.length; i++) { - test.assertEquals("Item " + i + " (0-based) is not the same", - expected[i], actual[i]); - } - } -} diff --git a/test_code/ProgressTest.java b/test_code/ProgressTest.java deleted file mode 100644 index 22e36cb..0000000 --- a/test_code/ProgressTest.java +++ /dev/null @@ -1,319 +0,0 @@ -package be.nikiroo.utils.test_code; - -import be.nikiroo.utils.Progress; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class ProgressTest extends TestLauncher { - public ProgressTest(String[] args) { - super("Progress reporting", args); - - addSeries(new TestLauncher("Simple progress", args) { - { - addTest(new TestCase("Relative values and direct values") { - @Override - public void test() throws Exception { - Progress p = new Progress(); - assertEquals(0, p.getProgress()); - assertEquals(0, p.getRelativeProgress()); - p.setProgress(33); - assertEquals(33, p.getProgress()); - assertEquals(0.33, p.getRelativeProgress()); - p.setMax(3); - p.setProgress(1); - assertEquals(1, p.getProgress()); - assertEquals( - generateAssertMessage("0.33..", - p.getRelativeProgress()), true, - p.getRelativeProgress() >= 0.332); - assertEquals( - generateAssertMessage("0.33..", - p.getRelativeProgress()), true, - p.getRelativeProgress() <= 0.334); - } - }); - - addTest(new TestCase("Listeners at first level") { - int pg; - - @Override - public void test() throws Exception { - Progress p = new Progress(); - p.addProgressListener(new Progress.ProgressListener() { - @Override - public void progress(Progress progress, String name) { - pg = progress.getProgress(); - } - }); - - p.setProgress(42); - assertEquals(42, pg); - p.setProgress(0); - assertEquals(0, pg); - } - }); - } - }); - - addSeries(new TestLauncher("Progress with children", args) { - { - addTest(new TestCase("One child") { - @Override - public void test() throws Exception { - Progress p = new Progress(); - Progress child = new Progress(); - - p.addProgress(child, 100); - - child.setProgress(42); - assertEquals(42, p.getProgress()); - } - }); - - addTest(new TestCase("Multiple children") { - @Override - public void test() throws Exception { - Progress p = new Progress(); - Progress child1 = new Progress(); - Progress child2 = new Progress(); - Progress child3 = new Progress(); - - p.addProgress(child1, 20); - p.addProgress(child2, 60); - p.addProgress(child3, 20); - - child1.setProgress(50); - assertEquals(10, p.getProgress()); - child2.setProgress(100); - assertEquals(70, p.getProgress()); - child3.setProgress(100); - assertEquals(90, p.getProgress()); - child1.setProgress(100); - assertEquals(100, p.getProgress()); - } - }); - - addTest(new TestCase("Listeners with children") { - int pg; - - @Override - public void test() throws Exception { - final Progress p = new Progress(); - Progress child1 = new Progress(); - Progress child2 = new Progress(); - p.addProgress(child1, 50); - p.addProgress(child2, 50); - - p.addProgressListener(new Progress.ProgressListener() { - @Override - public void progress(Progress progress, String name) { - pg = p.getProgress(); - } - }); - - child1.setProgress(50); - assertEquals(25, pg); - child2.setProgress(100); - assertEquals(75, pg); - child1.setProgress(100); - assertEquals(100, pg); - } - }); - - addTest(new TestCase("Listeners with children, not 1-100") { - int pg; - - @Override - public void test() throws Exception { - final Progress p = new Progress(); - p.setMax(1000); - - Progress child1 = new Progress(); - child1.setMax(2); - - Progress child2 = new Progress(); - p.addProgress(child1, 500); - p.addProgress(child2, 500); - - p.addProgressListener(new Progress.ProgressListener() { - @Override - public void progress(Progress progress, String name) { - pg = p.getProgress(); - } - }); - - child1.setProgress(1); - assertEquals(250, pg); - child2.setProgress(100); - assertEquals(750, pg); - child1.setProgress(2); - assertEquals(1000, pg); - } - }); - - addTest(new TestCase( - "Listeners with children, not 1-100, local progress") { - int pg; - - @Override - public void test() throws Exception { - final Progress p = new Progress(); - p.setMax(1000); - - Progress child1 = new Progress(); - child1.setMax(2); - - Progress child2 = new Progress(); - p.addProgress(child1, 400); - p.addProgress(child2, 400); - // 200 = local progress - - p.addProgressListener(new Progress.ProgressListener() { - @Override - public void progress(Progress progress, String name) { - pg = p.getProgress(); - } - }); - - child1.setProgress(1); - assertEquals(200, pg); - child2.setProgress(100); - assertEquals(600, pg); - p.setProgress(100); - assertEquals(700, pg); - child1.setProgress(2); - assertEquals(900, pg); - p.setProgress(200); - assertEquals(1000, pg); - } - }); - - addTest(new TestCase("Listeners with 5+ children, 4+ depth") { - int pg; - - @Override - public void test() throws Exception { - final Progress p = new Progress(); - Progress child1 = new Progress(); - Progress child2 = new Progress(); - p.addProgress(child1, 50); - p.addProgress(child2, 50); - Progress child11 = new Progress(); - child1.addProgress(child11, 100); - Progress child111 = new Progress(); - child11.addProgress(child111, 100); - Progress child1111 = new Progress(); - child111.addProgress(child1111, 20); - Progress child1112 = new Progress(); - child111.addProgress(child1112, 20); - Progress child1113 = new Progress(); - child111.addProgress(child1113, 20); - Progress child1114 = new Progress(); - child111.addProgress(child1114, 20); - Progress child1115 = new Progress(); - child111.addProgress(child1115, 20); - - p.addProgressListener(new Progress.ProgressListener() { - @Override - public void progress(Progress progress, String name) { - pg = p.getProgress(); - } - }); - - child1111.setProgress(100); - child1112.setProgress(50); - child1113.setProgress(25); - child1114.setProgress(25); - child1115.setProgress(50); - assertEquals(25, pg); - child2.setProgress(100); - assertEquals(75, pg); - child1111.setProgress(100); - child1112.setProgress(100); - child1113.setProgress(100); - child1114.setProgress(100); - child1115.setProgress(100); - assertEquals(100, pg); - } - }); - - addTest(new TestCase("Listeners with children, multi-thread") { - int pg; - boolean decrease; - Object lock1 = new Object(); - Object lock2 = new Object(); - int currentStep1; - int currentStep2; - - @Override - public void test() throws Exception { - final Progress p = new Progress(0, 200); - - final Progress child1 = new Progress(); - final Progress child2 = new Progress(); - p.addProgress(child1, 100); - p.addProgress(child2, 100); - - p.addProgressListener(new Progress.ProgressListener() { - @Override - public void progress(Progress progress, String name) { - int now = p.getProgress(); - if (now < pg) { - decrease = true; - } - pg = now; - } - }); - - // Run 200 concurrent threads, 2 at a time allowed to - // make progress (each on a different child) - for (int i = 0; i <= 100; i++) { - final int step = i; - new Thread(new Runnable() { - @Override - public void run() { - synchronized (lock1) { - if (step > currentStep1) { - currentStep1 = step; - child1.setProgress(step); - } - } - } - }).start(); - - new Thread(new Runnable() { - @Override - public void run() { - synchronized (lock2) { - if (step > currentStep2) { - currentStep2 = step; - child2.setProgress(step); - } - } - } - }).start(); - } - - int i; - int timeout = 20; // in 1/10th of seconds - for (i = 0; i < timeout - && (currentStep1 + currentStep2) < 200; i++) { - Thread.sleep(100); - } - - assertEquals("The test froze at step " + currentStep1 - + " + " + currentStep2, true, i < timeout); - assertEquals( - "There should not have any decresing steps", - decrease, false); - assertEquals("The progress should have reached 200", - 200, p.getProgress()); - assertEquals( - "The progress should have reached completion", - true, p.isDone()); - } - }); - } - }); - } -} diff --git a/test_code/ReplaceInputStreamTest.java b/test_code/ReplaceInputStreamTest.java deleted file mode 100644 index efab8c7..0000000 --- a/test_code/ReplaceInputStreamTest.java +++ /dev/null @@ -1,210 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.streams.ReplaceInputStream; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class ReplaceInputStreamTest extends TestLauncher { - public ReplaceInputStreamTest(String[] args) { - super("ReplaceInputStream test", args); - - addTest(new TestCase("Empty replace") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127 }; - ReplaceInputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), new byte[0], - new byte[0]); - - checkArrays(this, "FIRST", in, data); - } - }); - - addTest(new TestCase("Simple replace") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127 }; - ReplaceInputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), new byte[] { 0 }, - new byte[] { 10 }); - - checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 127 }); - } - }); - - addTest(new TestCase("3/4 replace") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127 }; - ReplaceInputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), - new byte[] { 12, 0, 127 }, new byte[] { 10, 10, 10 }); - - checkArrays(this, "FIRST", in, new byte[] { 42, 10, 10, 10 }); - } - }); - - addTest(new TestCase("Longer replace") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127 }; - ReplaceInputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), new byte[] { 0 }, - new byte[] { 10, 10, 10 }); - - checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 10, 10, - 127 }); - } - }); - - addTest(new TestCase("Shorter replace") { - @Override - public void test() throws Exception { - byte[] data = new byte[] { 42, 12, 0, 127 }; - ReplaceInputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), - new byte[] { 42, 12, 0 }, new byte[] { 10 }); - - checkArrays(this, "FIRST", in, new byte[] { 10, 127 }); - } - }); - - addTest(new TestCase("String replace") { - @Override - public void test() throws Exception { - byte[] data = "I like red".getBytes("UTF-8"); - ReplaceInputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), - "red", "blue"); - - checkArrays(this, "FIRST", in, "I like blue".getBytes("UTF-8")); - - data = "I like blue hammers".getBytes("UTF-8"); - in = new ReplaceInputStream(new ByteArrayInputStream(data), - "blue", "red"); - - checkArrays(this, "SECOND", in, "I like red hammers".getBytes("UTF-8")); - } - }); - - addTest(new TestCase("Multiple replaces") { - @Override - public void test() throws Exception { - byte[] data = "I like red cabage".getBytes("UTF-8"); - ReplaceInputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), // - new String[] { "red", "like" }, // - new String[] { "green", "very very much like" } // - ); - - String result = new String(IOUtils.toByteArray(in), "UTF-8"); - assertEquals("I very very much like green cabage", result); - } - }); - - addTest(new TestCase("Multiple replaces") { - @Override - public void test() throws Exception { - String str= ("" // - + "\n" // - + "\n" // - + "\n" // - + "\n" // - + "\t\n" // - + "\t\n" // - + "\t${title}\n" // - + "\t\n" // - + "\t\n" // - + "\n" // - + "\n" // - + "\t
\n" // - + "${banner}${content}\t
\n" // - + "\n" // - + "" // - ); - byte[] data = str.getBytes("UTF-8"); - - String title = "Fanfix"; - String banner = ""; - String content = ""; - - InputStream in = new ReplaceInputStream( - new ByteArrayInputStream(data), // - new String[] { "${title}", "${banner}", "${content}" }, // - new String[] { title, banner, content } // - ); - - String result = new String(IOUtils.toByteArray(in), "UTF-8"); - assertEquals(str // - .replace("${title}", title) // - .replace("${banner}", banner) // - .replace("${content}", content) // - , result); - } - }); - - - } - - static void checkArrays(TestCase test, String prefix, InputStream in, - byte[] expected) throws Exception { - byte[] actual = IOUtils.toByteArray(in); - -// System.out.println("\nActual:"); -// for(byte byt : actual) { -// System.out.print(byt+" "); -// } -// System.out.println("\nExpected:"); -// for(byte byt : expected) { -// System.out.print(byt+" "); -// } - - test.assertEquals("The " + prefix - + " resulting array has not the correct number of items", - expected.length, actual.length); - for (int i = 0; i < actual.length; i++) { - test.assertEquals("Item " + i + " (0-based) is not the same", - expected[i], actual[i]); - } - } -} diff --git a/test_code/ReplaceOutputStreamTest.java b/test_code/ReplaceOutputStreamTest.java deleted file mode 100644 index 1db3397..0000000 --- a/test_code/ReplaceOutputStreamTest.java +++ /dev/null @@ -1,168 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.ByteArrayOutputStream; - -import be.nikiroo.utils.streams.ReplaceOutputStream; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class ReplaceOutputStreamTest extends TestLauncher { - public ReplaceOutputStreamTest(String[] args) { - super("ReplaceOutputStream test", args); - - addTest(new TestCase("Single write, empty bytes replaces") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ReplaceOutputStream out = new ReplaceOutputStream(bout, - new byte[0], new byte[0]); - - byte[] data = new byte[] { 42, 12, 0, 127 }; - - out.write(data); - out.close(); - - checkArrays(this, "FIRST", bout, data); - } - }); - - addTest(new TestCase("Multiple writes, empty Strings replaces") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ReplaceOutputStream out = new ReplaceOutputStream(bout, "", ""); - - byte[] data1 = new byte[] { 42, 12, 0, 127 }; - byte[] data2 = new byte[] { 15, 55 }; - byte[] data3 = new byte[] {}; - - byte[] dataAll = new byte[] { 42, 12, 0, 127, 15, 55 }; - - out.write(data1); - out.write(data2); - out.write(data3); - out.close(); - - checkArrays(this, "FIRST", bout, dataAll); - } - }); - - addTest(new TestCase("Single write, bytes replaces") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ReplaceOutputStream out = new ReplaceOutputStream(bout, - new byte[] { 12 }, new byte[] { 55 }); - - byte[] data = new byte[] { 42, 12, 0, 127 }; - - out.write(data); - out.close(); - - checkArrays(this, "FIRST", bout, new byte[] { 42, 55, 0, 127 }); - } - }); - - addTest(new TestCase("Multiple writes, Strings replaces") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ReplaceOutputStream out = new ReplaceOutputStream(bout, "(-)", - "(.)"); - - byte[] data1 = "un mot ".getBytes("UTF-8"); - byte[] data2 = "(-) of twee ".getBytes("UTF-8"); - byte[] data3 = "(-) makes the difference".getBytes("UTF-8"); - - out.write(data1); - out.write(data2); - out.write(data3); - out.close(); - - checkArrays(this, "FIRST", bout, - "un mot (.) of twee (.) makes the difference" - .getBytes("UTF-8")); - } - }); - - addTest(new TestCase("Single write, longer bytes replaces") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ReplaceOutputStream out = new ReplaceOutputStream(bout, - new byte[] { 12 }, new byte[] { 55, 55, 66 }); - - byte[] data = new byte[] { 42, 12, 0, 127 }; - - out.write(data); - out.close(); - - checkArrays(this, "FIRST", bout, new byte[] { 42, 55, 55, 66, - 0, 127 }); - } - }); - - addTest(new TestCase("Single write, shorter bytes replaces") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ReplaceOutputStream out = new ReplaceOutputStream(bout, - new byte[] { 12, 0 }, new byte[] { 55 }); - - byte[] data = new byte[] { 42, 12, 0, 127 }; - - out.write(data); - out.close(); - - checkArrays(this, "FIRST", bout, new byte[] { 42, 55, 127 }); - } - }); - - addTest(new TestCase("Single write, remove bytes replaces") { - @Override - public void test() throws Exception { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - ReplaceOutputStream out = new ReplaceOutputStream(bout, - new byte[] { 12 }, new byte[] {}); - - byte[] data = new byte[] { 42, 12, 0, 127 }; - - out.write(data); - out.close(); - - checkArrays(this, "FIRST", bout, new byte[] { 42, 0, 127 }); - } - }); - } - - static void checkArrays(TestCase test, String prefix, - ByteArrayOutputStream bout, byte[] expected) throws Exception { - byte[] actual = bout.toByteArray(); - - if (false) { - System.out.print("\nExpected data: [ "); - for (int i = 0; i < expected.length; i++) { - if (i > 0) - System.out.print(", "); - System.out.print(expected[i]); - } - System.out.println(" ]"); - - System.out.print("Actual data : [ "); - for (int i = 0; i < actual.length; i++) { - if (i > 0) - System.out.print(", "); - System.out.print(actual[i]); - } - System.out.println(" ]"); - } - - test.assertEquals("The " + prefix - + " resulting array has not the correct number of items", - expected.length, actual.length); - for (int i = 0; i < actual.length; i++) { - test.assertEquals(prefix + ": item " + i - + " (0-based) is not the same", expected[i], actual[i]); - } - } -} diff --git a/test_code/SerialServerTest.java b/test_code/SerialServerTest.java deleted file mode 100644 index c10a158..0000000 --- a/test_code/SerialServerTest.java +++ /dev/null @@ -1,637 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.net.URL; - -import be.nikiroo.utils.Version; -import be.nikiroo.utils.serial.server.ConnectActionClientObject; -import be.nikiroo.utils.serial.server.ConnectActionClientString; -import be.nikiroo.utils.serial.server.ConnectActionServerObject; -import be.nikiroo.utils.serial.server.ConnectActionServerString; -import be.nikiroo.utils.serial.server.ServerBridge; -import be.nikiroo.utils.serial.server.ServerObject; -import be.nikiroo.utils.serial.server.ServerString; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class SerialServerTest extends TestLauncher { - public SerialServerTest(String[] args) { - super("SerialServer test", args); - - for (String key : new String[] { null, - "some super secret encryption key" }) { - for (boolean bridge : new Boolean[] { false, true }) { - final String skey = (key != null ? "(encrypted)" - : "(plain text)"); - final String sbridge = (bridge ? " with bridge" : ""); - - addSeries(new SerialServerTest(args, key, skey, bridge, - sbridge, "ServerString")); - - addSeries(new SerialServerTest(args, key, skey, bridge, - sbridge, new Object() { - @Override - public String toString() { - return "ServerObject"; - } - })); - } - } - } - - private SerialServerTest(final String[] args, final String key, - final String skey, final boolean bridge, final String sbridge, - final String title) { - - super(title + " " + skey + sbridge, args); - - addTest(new TestCase("Simple connection " + skey) { - @Override - public void test() throws Exception { - final String[] rec = new String[1]; - - ServerString server = new ServerString(this.getName(), 0, key) { - @Override - protected String onRequest( - ConnectActionServerString action, Version version, - String data, long id) throws Exception { - return null; - } - - @Override - protected void onError(Exception e) { - } - }; - - int port = server.getPort(); - assertEquals("A port should have been assigned", true, port > 0); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - - port = br.getPort(); - assertEquals( - "A port should have been assigned to the bridge", - true, port > 0); - - br.start(); - } - - try { - try { - new ConnectActionClientString(null, port, key) { - @Override - public void action(Version version) - throws Exception { - rec[0] = "ok"; - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - assertNotNull("The client action was not run", rec[0]); - assertEquals("ok", rec[0]); - } - }); - - addTest(new TestCase("Simple exchange " + skey) { - final String[] sent = new String[1]; - final String[] recd = new String[1]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, key) { - @Override - protected String onRequest( - ConnectActionServerString action, Version version, - String data, long id) throws Exception { - sent[0] = data; - return "pong"; - } - - @Override - protected void onError(Exception e) { - err[0] = e; - } - }; - - int port = server.getPort(); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientString(null, port, key) { - @Override - public void action(Version version) - throws Exception { - recd[0] = send("ping"); - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage(), - err[0]); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - } - }); - - addTest(new TestCase("Multiple exchanges " + skey) { - final String[] sent = new String[3]; - final String[] recd = new String[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, key) { - @Override - protected String onRequest( - ConnectActionServerString action, Version version, - String data, long id) throws Exception { - sent[0] = data; - action.send("pong"); - sent[1] = action.rec(); - return "pong2"; - } - - @Override - protected void onError(Exception e) { - err[0] = e; - } - }; - - int port = server.getPort(); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientString(null, port, key) { - @Override - public void action(Version version) - throws Exception { - recd[0] = send("ping"); - recd[1] = send("ping2"); - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage(), - err[0]); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - assertEquals("ping2", sent[1]); - assertEquals("pong2", recd[1]); - } - }); - - addTest(new TestCase("Multiple call from client " + skey) { - final String[] sent = new String[3]; - final String[] recd = new String[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, key) { - @Override - protected String onRequest( - ConnectActionServerString action, Version version, - String data, long id) throws Exception { - sent[Integer.parseInt(data)] = data; - return "" + (Integer.parseInt(data) * 2); - } - - @Override - protected void onError(Exception e) { - err[0] = e; - } - }; - - int port = server.getPort(); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientString(null, port, key) { - @Override - public void action(Version version) - throws Exception { - for (int i = 0; i < 3; i++) { - recd[i] = send("" + i); - } - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage(), - err[0]); - } - - assertEquals("0", sent[0]); - assertEquals("0", recd[0]); - assertEquals("1", sent[1]); - assertEquals("2", recd[1]); - assertEquals("2", sent[2]); - assertEquals("4", recd[2]); - } - }); - } - - private SerialServerTest(final String[] args, final String key, - final String skey, final boolean bridge, final String sbridge, - final Object title) { - - super(title + " " + skey + sbridge, args); - - addTest(new TestCase("Simple connection " + skey) { - @Override - public void test() throws Exception { - final Object[] rec = new Object[1]; - - ServerObject server = new ServerObject(this.getName(), 0, key) { - @Override - protected Object onRequest( - ConnectActionServerObject action, Version version, - Object data, long id) throws Exception { - return null; - } - - @Override - protected void onError(Exception e) { - } - }; - - int port = server.getPort(); - assertEquals("A port should have been assigned", true, port > 0); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientObject(null, port, key) { - @Override - public void action(Version version) - throws Exception { - rec[0] = true; - } - - @Override - protected void onError(Exception e) { - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - assertNotNull("The client action was not run", rec[0]); - assertEquals(true, (boolean) ((Boolean) rec[0])); - } - }); - - addTest(new TestCase("Simple exchange " + skey) { - final Object[] sent = new Object[1]; - final Object[] recd = new Object[1]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, key) { - @Override - protected Object onRequest( - ConnectActionServerObject action, Version version, - Object data, long id) throws Exception { - sent[0] = data; - return "pong"; - } - - @Override - protected void onError(Exception e) { - err[0] = e; - } - }; - - int port = server.getPort(); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientObject(null, port, key) { - @Override - public void action(Version version) - throws Exception { - recd[0] = send("ping"); - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage(), - err[0]); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - } - }); - - addTest(new TestCase("Multiple exchanges " + skey) { - final Object[] sent = new Object[3]; - final Object[] recd = new Object[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, key) { - @Override - protected Object onRequest( - ConnectActionServerObject action, Version version, - Object data, long id) throws Exception { - sent[0] = data; - action.send("pong"); - sent[1] = action.rec(); - return "pong2"; - } - - @Override - protected void onError(Exception e) { - err[0] = e; - } - }; - - int port = server.getPort(); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientObject(null, port, key) { - @Override - public void action(Version version) - throws Exception { - recd[0] = send("ping"); - recd[1] = send("ping2"); - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage(), - err[0]); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - assertEquals("ping2", sent[1]); - assertEquals("pong2", recd[1]); - } - }); - - addTest(new TestCase("Object array of URLs " + skey) { - final Object[] sent = new Object[1]; - final Object[] recd = new Object[1]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, key) { - @Override - protected Object onRequest( - ConnectActionServerObject action, Version version, - Object data, long id) throws Exception { - sent[0] = data; - return new Object[] { "ACK" }; - } - - @Override - protected void onError(Exception e) { - err[0] = e; - } - }; - - int port = server.getPort(); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientObject(null, port, key) { - @Override - public void action(Version version) - throws Exception { - recd[0] = send(new Object[] { - "key", - new URL( - "https://example.com/from_client"), - "https://example.com/from_client" }); - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage(), - err[0]); - } - - Object[] sento = (Object[]) (sent[0]); - Object[] recdo = (Object[]) (recd[0]); - - assertEquals("key", sento[0]); - assertEquals("https://example.com/from_client", - ((URL) sento[1]).toString()); - assertEquals("https://example.com/from_client", sento[2]); - assertEquals("ACK", recdo[0]); - } - }); - - addTest(new TestCase("Multiple call from client " + skey) { - final Object[] sent = new Object[3]; - final Object[] recd = new Object[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, key) { - @Override - protected Object onRequest( - ConnectActionServerObject action, Version version, - Object data, long id) throws Exception { - sent[(Integer) data] = data; - return ((Integer) data) * 2; - } - - @Override - protected void onError(Exception e) { - err[0] = e; - } - }; - - int port = server.getPort(); - - server.start(); - - ServerBridge br = null; - if (bridge) { - br = new ServerBridge(0, key, "", port, key); - br.setTraceHandler(null); - port = br.getPort(); - br.start(); - } - - try { - try { - new ConnectActionClientObject(null, port, key) { - @Override - public void action(Version version) - throws Exception { - for (int i = 0; i < 3; i++) { - recd[i] = send(i); - } - } - }.connect(); - } finally { - server.stop(); - } - } finally { - if (br != null) { - br.stop(); - } - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage(), - err[0]); - } - - assertEquals(0, sent[0]); - assertEquals(0, recd[0]); - assertEquals(1, sent[1]); - assertEquals(2, recd[1]); - assertEquals(2, sent[2]); - assertEquals(4, recd[2]); - } - }); - } -} diff --git a/test_code/SerialTest.java b/test_code/SerialTest.java deleted file mode 100644 index bf08f5c..0000000 --- a/test_code/SerialTest.java +++ /dev/null @@ -1,281 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.NotSerializableException; -import java.net.URL; -import java.util.Arrays; - -import be.nikiroo.utils.serial.Exporter; -import be.nikiroo.utils.serial.Importer; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class SerialTest extends TestLauncher { - /** - * Required for Import/Export of objects. - */ - public SerialTest() { - this(null); - } - - private void encodeRecodeTest(TestCase test, Object data) throws Exception { - byte[] encoded = toBytes(data, true); - Object redata = fromBytes(toBytes(data, false)); - byte[] reencoded = toBytes(redata, true); - - // We suppose text mode - if (encoded.length < 256 && reencoded.length < 256) { - test.assertEquals("Different data after encode/decode/encode", - new String(encoded, "UTF-8"), - new String(reencoded, "UTF-8")); - } else { - test.assertEquals("Different data after encode/decode/encode", - true, Arrays.equals(encoded, reencoded)); - } - } - - // try to remove pointer addresses - private byte[] toBytes(Object data, boolean clearRefs) - throws NotSerializableException, IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Exporter(out).append(data); - out.flush(); - - if (clearRefs) { - String tmp = new String(out.toByteArray(), "UTF-8"); - tmp = tmp.replaceAll("@[0-9]*", "@REF"); - return tmp.getBytes("UTF-8"); - } - - return out.toByteArray(); - } - - private Object fromBytes(byte[] data) throws NoSuchFieldException, - NoSuchMethodException, ClassNotFoundException, - NullPointerException, IOException { - - InputStream in = new ByteArrayInputStream(data); - try { - return new Importer().read(in).getValue(); - } finally { - in.close(); - } - } - - public SerialTest(String[] args) { - super("Serial test", args); - - addTest(new TestCase("Simple class Import/Export") { - @Override - public void test() throws Exception { - Data data = new Data(42); - encodeRecodeTest(this, data); - } - }); - - addTest(new TestCase() { - @SuppressWarnings("unused") - private TestCase me = setName("Anonymous inner class"); - - @Override - public void test() throws Exception { - Data data = new Data() { - @SuppressWarnings("unused") - int value = 42; - }; - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase() { - @SuppressWarnings("unused") - private TestCase me = setName("Array of anonymous inner classes"); - - @Override - public void test() throws Exception { - Data[] data = new Data[] { new Data() { - @SuppressWarnings("unused") - int value = 42; - } }; - - byte[] encoded = toBytes(data, false); - Object redata = fromBytes(encoded); - - // Comparing the 2 arrays won't be useful, because the @REFs - // will be ZIP-encoded; so we parse and re-encode each object - - byte[] encoded1 = toBytes(data[0], true); - byte[] reencoded1 = toBytes(((Object[]) redata)[0], true); - - assertEquals("Different data after encode/decode/encode", true, - Arrays.equals(encoded1, reencoded1)); - } - }); - addTest(new TestCase("URL Import/Export") { - @Override - public void test() throws Exception { - URL data = new URL("https://fanfan.be/"); - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase("URL-String Import/Export") { - @Override - public void test() throws Exception { - String data = new URL("https://fanfan.be/").toString(); - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase("URL/URL-String arrays Import/Export") { - @Override - public void test() throws Exception { - final String url = "https://fanfan.be/"; - Object[] data = new Object[] { new URL(url), url }; - - byte[] encoded = toBytes(data, false); - Object redata = fromBytes(encoded); - - // Comparing the 2 arrays won't be useful, because the @REFs - // will be ZIP-encoded; so we parse and re-encode each object - - byte[] encoded1 = toBytes(data[0], true); - byte[] reencoded1 = toBytes(((Object[]) redata)[0], true); - byte[] encoded2 = toBytes(data[1], true); - byte[] reencoded2 = toBytes(((Object[]) redata)[1], true); - - assertEquals("Different data 1 after encode/decode/encode", - true, Arrays.equals(encoded1, reencoded1)); - assertEquals("Different data 2 after encode/decode/encode", - true, Arrays.equals(encoded2, reencoded2)); - } - }); - addTest(new TestCase("Import/Export with nested objects") { - @Override - public void test() throws Exception { - Data data = new DataObject(new Data(21)); - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase("Import/Export String in object") { - @Override - public void test() throws Exception { - Data data = new DataString("fanfan"); - encodeRecodeTest(this, data); - data = new DataString("http://example.com/query.html"); - encodeRecodeTest(this, data); - data = new DataString("Test|Ché|http://|\"\\\"Pouch\\"); - encodeRecodeTest(this, data); - data = new DataString("Test|Ché\\n|\nhttp://|\"\\\"Pouch\\"); - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase("Import/Export with nested objects forming a loop") { - @Override - public void test() throws Exception { - DataLoop data = new DataLoop("looping"); - data.next = new DataLoop("level 2"); - data.next.next = data; - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase("Array in Object Import/Export") { - @Override - public void test() throws Exception { - Object data = new DataArray();// new String[] { "un", "deux" }; - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase("Array Import/Export") { - @Override - public void test() throws Exception { - Object data = new String[] { "un", "deux" }; - encodeRecodeTest(this, data); - } - }); - addTest(new TestCase("Enum Import/Export") { - @Override - public void test() throws Exception { - Object data = EnumToSend.FANFAN; - encodeRecodeTest(this, data); - } - }); - } - - class DataArray { - public String[] data = new String[] { "un", "deux" }; - } - - class Data { - private int value; - - private Data() { - } - - public Data(int value) { - this.value = value; - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof Data) { - Data other = (Data) obj; - return other.value == this.value; - } - - return false; - } - - @Override - public int hashCode() { - return new Integer(value).hashCode(); - } - } - - @SuppressWarnings("unused") - class DataObject extends Data { - private Data data; - - @SuppressWarnings("synthetic-access") - private DataObject() { - } - - @SuppressWarnings("synthetic-access") - public DataObject(Data data) { - this.data = data; - } - } - - @SuppressWarnings("unused") - class DataString extends Data { - private String data; - - @SuppressWarnings("synthetic-access") - private DataString() { - } - - @SuppressWarnings("synthetic-access") - public DataString(String data) { - this.data = data; - } - } - - @SuppressWarnings("unused") - class DataLoop extends Data { - public DataLoop next; - private String value; - - @SuppressWarnings("synthetic-access") - private DataLoop() { - } - - @SuppressWarnings("synthetic-access") - public DataLoop(String value) { - this.value = value; - } - } - - enum EnumToSend { - FANFAN, TULIPE, - } -} diff --git a/test_code/StringUtilsTest.java b/test_code/StringUtilsTest.java deleted file mode 100644 index a441195..0000000 --- a/test_code/StringUtilsTest.java +++ /dev/null @@ -1,304 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import be.nikiroo.utils.StringUtils; -import be.nikiroo.utils.StringUtils.Alignment; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class StringUtilsTest extends TestLauncher { - public StringUtilsTest(String[] args) { - super("StringUtils test", args); - - addTest(new TestCase("Time serialisation") { - @Override - public void test() throws Exception { - for (long fullTime : new Long[] { 0l, 123456l, 123456000l, - new Date().getTime() }) { - // precise to the second, no more - long time = (fullTime / 1000) * 1000; - - String displayTime = StringUtils.fromTime(time); - assertNotNull("The stringified time for " + time - + " should not be null", displayTime); - assertEquals("The stringified time for " + time - + " should not be empty", false, displayTime.trim() - .isEmpty()); - - assertEquals("The time " + time - + " should be loop-convertable", time, - StringUtils.toTime(displayTime)); - - assertEquals("The time " + displayTime - + " should be loop-convertable", displayTime, - StringUtils.fromTime(StringUtils - .toTime(displayTime))); - } - } - }); - - addTest(new TestCase("MD5") { - @Override - public void test() throws Exception { - String mess = "The String we got is not what 'md5sum' said it should heve been"; - assertEquals(mess, "34ded48fcff4221d644be9a37e1cb1d9", - StringUtils.getMd5Hash("fanfan la tulipe")); - assertEquals(mess, "7691b0cb74ed0f94b4d8cd858abe1165", - StringUtils.getMd5Hash("je te do-o-o-o-o-o-nne")); - } - }); - - addTest(new TestCase("Padding") { - @Override - public void test() throws Exception { - for (String data : new String[] { "fanfan", "la tulipe", - "1234567890", "12345678901234567890", "1", "" }) { - String result = StringUtils.padString(data, -1); - assertEquals("A size of -1 is expected to produce a noop", - true, data.equals(result)); - for (int size : new Integer[] { 0, 1, 5, 10, 40 }) { - result = StringUtils.padString(data, size); - assertEquals( - "Padding a String at a certain size should give a String of the given size", - size, result.length()); - assertEquals( - "Padding a String should not change the content", - true, data.trim().startsWith(result.trim())); - - result = StringUtils.padString(data, size, false, null); - assertEquals( - "Padding a String without cutting should not shorten the String", - true, data.length() <= result.length()); - assertEquals( - "Padding a String without cutting should keep the whole content", - true, data.trim().equals(result.trim())); - - result = StringUtils.padString(data, size, false, - Alignment.RIGHT); - if (size > data.length()) { - assertEquals( - "Padding a String to the end should work as expected", - true, result.endsWith(data)); - } - - result = StringUtils.padString(data, size, false, - Alignment.JUSTIFY); - if (size > data.length()) { - String unspacedData = data.trim(); - String unspacedResult = result.trim(); - for (int i = 0; i < size; i++) { - unspacedData = unspacedData.replace(" ", " "); - unspacedResult = unspacedResult.replace(" ", - " "); - } - - assertEquals( - "Justified text trimmed with all spaces collapsed " - + "sould be identical to original text " - + "trimmed with all spaces collapsed", - unspacedData, unspacedResult); - } - - result = StringUtils.padString(data, size, false, - Alignment.CENTER); - if (size > data.length()) { - int before = 0; - for (int i = 0; i < result.length() - && result.charAt(i) == ' '; i++) { - before++; - } - - int after = 0; - for (int i = result.length() - 1; i >= 0 - && result.charAt(i) == ' '; i--) { - after++; - } - - if (result.trim().isEmpty()) { - after = before / 2; - if (before > (2 * after)) { - before = after + 1; - } else { - before = after; - } - } - - assertEquals( - "Padding a String on center should work as expected", - result.length(), before + data.length() - + after); - assertEquals( - "Padding a String on center should not uncenter the content", - true, Math.abs(before - after) <= 1); - } - } - } - } - }); - - addTest(new TestCase("Justifying") { - @Override - public void test() throws Exception { - Map>>> source = new HashMap>>>(); - addValue(source, Alignment.LEFT, "testy", -1, "testy"); - addValue(source, Alignment.RIGHT, "testy", -1, "testy"); - addValue(source, Alignment.CENTER, "testy", -1, "testy"); - addValue(source, Alignment.JUSTIFY, "testy", -1, "testy"); - addValue(source, Alignment.LEFT, "testy", 5, "testy"); - addValue(source, Alignment.LEFT, "testy", 3, "te-", "sty"); - addValue(source, Alignment.LEFT, - "Un petit texte qui se mettra sur plusieurs lignes", - 10, "Un petit", "texte qui", "se mettra", "sur", - "plusieurs", "lignes"); - addValue(source, Alignment.LEFT, - "Un petit texte qui se mettra sur plusieurs lignes", 7, - "Un", "petit", "texte", "qui se", "mettra", "sur", - "plusie-", "urs", "lignes"); - addValue(source, Alignment.RIGHT, - "Un petit texte qui se mettra sur plusieurs lignes", 7, - " Un", " petit", " texte", " qui se", " mettra", - " sur", "plusie-", " urs", " lignes"); - addValue(source, Alignment.CENTER, - "Un petit texte qui se mettra sur plusieurs lignes", 7, - " Un ", " petit ", " texte ", "qui se ", "mettra ", - " sur ", "plusie-", " urs ", "lignes "); - addValue(source, Alignment.JUSTIFY, - "Un petit texte qui se mettra sur plusieurs lignes", 7, - "Un pet-", "it tex-", "te qui", "se met-", "tra sur", - "plusie-", "urs li-", "gnes"); - addValue(source, Alignment.JUSTIFY, - "Un petit texte qui se mettra sur plusieurs lignes", - 14, "Un petit", "texte qui se", - "mettra sur", "plusieurs lig-", "nes"); - addValue(source, Alignment.JUSTIFY, "le dash-test", 9, - "le dash-", "test"); - - for (String data : source.keySet()) { - for (int size : source.get(data).keySet()) { - Alignment align = source.get(data).get(size).getKey(); - List values = source.get(data).get(size) - .getValue(); - - List result = StringUtils.justifyText(data, - size, align); - - // System.out.println("[" + data + " (" + size + ")" + - // "] -> ["); - // for (int i = 0; i < result.size(); i++) { - // String resultLine = result.get(i); - // System.out.println(i + ": " + resultLine); - // } - // System.out.println("]"); - - assertEquals(values, result); - } - } - } - }); - - addTest(new TestCase("unhtml") { - @Override - public void test() throws Exception { - Map data = new HashMap(); - data.put("aa", "aa"); - data.put("test with spaces ", "test with spaces "); - data.put("link", "link"); - data.put("Digimon", "Digimon"); - data.put("", ""); - data.put(" ", " "); - - for (Entry entry : data.entrySet()) { - String result = StringUtils.unhtml(entry.getKey()); - assertEquals("Result is not what we expected", - entry.getValue(), result); - } - } - }); - - addTest(new TestCase("zip64") { - @Override - public void test() throws Exception { - String orig = "test"; - String zipped = StringUtils.zip64(orig); - String unzipped = StringUtils.unzip64s(zipped); - assertEquals(orig, unzipped); - } - }); - - addTest(new TestCase("format/toNumber simple") { - @Override - public void test() throws Exception { - assertEquals(263l, StringUtils.toNumber("263")); - assertEquals(21200l, StringUtils.toNumber("21200")); - assertEquals(0l, StringUtils.toNumber("0")); - assertEquals("263", StringUtils.formatNumber(263l)); - assertEquals("21 k", StringUtils.formatNumber(21000l)); - assertEquals("0", StringUtils.formatNumber(0l)); - } - }); - - addTest(new TestCase("format/toNumber not 000") { - @Override - public void test() throws Exception { - assertEquals(263200l, StringUtils.toNumber("263.2 k")); - assertEquals(42000l, StringUtils.toNumber("42.0 k")); - assertEquals(12000000l, StringUtils.toNumber("12 M")); - assertEquals(2000000000l, StringUtils.toNumber("2 G")); - assertEquals("263 k", StringUtils.formatNumber(263012l)); - assertEquals("42 k", StringUtils.formatNumber(42012l)); - assertEquals("12 M", StringUtils.formatNumber(12012121l)); - assertEquals("7 G", StringUtils.formatNumber(7364635928l)); - } - }); - - addTest(new TestCase("format/toNumber decimals") { - @Override - public void test() throws Exception { - assertEquals(263200l, StringUtils.toNumber("263.2 k")); - assertEquals(1200l, StringUtils.toNumber("1.2 k")); - assertEquals(42700000l, StringUtils.toNumber("42.7 M")); - assertEquals(1220l, StringUtils.toNumber("1.22 k")); - assertEquals(1432l, StringUtils.toNumber("1.432 k")); - assertEquals(6938l, StringUtils.toNumber("6.938 k")); - assertEquals("1.3 k", StringUtils.formatNumber(1300l, 1)); - assertEquals("263.2020 k", StringUtils.formatNumber(263202l, 4)); - assertEquals("1.26 k", StringUtils.formatNumber(1267l, 2)); - assertEquals("42.7 M", StringUtils.formatNumber(42712121l, 1)); - assertEquals("5.09 G", StringUtils.formatNumber(5094837485l, 2)); - } - }); - } - - static private void addValue( - Map>>> source, - final Alignment align, String input, int size, - final String... result) { - if (!source.containsKey(input)) { - source.put(input, - new HashMap>>()); - } - - source.get(input).put(size, new Entry>() { - @Override - public Alignment getKey() { - return align; - } - - @Override - public List getValue() { - return Arrays.asList(result); - } - - @Override - public List setValue(List value) { - return null; - } - }); - } -} diff --git a/test_code/TempFilesTest.java b/test_code/TempFilesTest.java deleted file mode 100644 index dad4cac..0000000 --- a/test_code/TempFilesTest.java +++ /dev/null @@ -1,109 +0,0 @@ -package be.nikiroo.utils.test_code; - -import java.io.File; -import java.io.IOException; - -import be.nikiroo.utils.TempFiles; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class TempFilesTest extends TestLauncher { - public TempFilesTest(String[] args) { - super("TempFiles", args); - - addTest(new TestCase("Name is correct") { - @Override - public void test() throws Exception { - RootTempFiles files = new RootTempFiles("testy"); - try { - assertEquals("The root was not created", true, files - .getRoot().exists()); - assertEquals( - "The root is not prefixed with the expected name", - true, files.getRoot().getName().startsWith("testy")); - - } finally { - files.close(); - } - } - }); - - addTest(new TestCase("Clean after itself no use") { - @Override - public void test() throws Exception { - RootTempFiles files = new RootTempFiles("testy2"); - try { - assertEquals("The root was not created", true, files - .getRoot().exists()); - } finally { - files.close(); - assertEquals("The root was not deleted", false, files - .getRoot().exists()); - } - } - }); - - addTest(new TestCase("Clean after itself after usage") { - @Override - public void test() throws Exception { - RootTempFiles files = new RootTempFiles("testy3"); - try { - assertEquals("The root was not created", true, files - .getRoot().exists()); - files.createTempFile("test"); - } finally { - files.close(); - assertEquals("The root was not deleted", false, files - .getRoot().exists()); - assertEquals("The main root in /tmp was not deleted", - false, files.getRoot().getParentFile().exists()); - } - } - }); - - addTest(new TestCase("Temporary directories") { - @Override - public void test() throws Exception { - RootTempFiles files = new RootTempFiles("testy4"); - File file = null; - try { - File dir = files.createTempDir("test"); - file = new File(dir, "fanfan"); - file.createNewFile(); - assertEquals( - "Cannot create a file in a temporary directory", - true, file.exists()); - } finally { - files.close(); - if (file != null) { - assertEquals( - "A file created in a temporary directory should be deleted on close", - false, file.exists()); - } - assertEquals("The root was not deleted", false, files - .getRoot().exists()); - } - } - }); - } - - private class RootTempFiles extends TempFiles { - private File root = null; - - public RootTempFiles(String name) throws IOException { - super(name); - } - - public File getRoot() { - if (root != null) - return root; - return super.root; - } - - @Override - public synchronized void close() throws IOException { - root = super.root; - super.close(); - } - } -} diff --git a/test_code/Test.java b/test_code/Test.java deleted file mode 100644 index 8d99cba..0000000 --- a/test_code/Test.java +++ /dev/null @@ -1,68 +0,0 @@ -package be.nikiroo.utils.test_code; - -import be.nikiroo.utils.Cache; -import be.nikiroo.utils.CacheMemory; -import be.nikiroo.utils.Downloader; -import be.nikiroo.utils.Proxy; -import be.nikiroo.utils.main.bridge; -import be.nikiroo.utils.main.img2aa; -import be.nikiroo.utils.main.justify; -import be.nikiroo.utils.test.TestLauncher; -import be.nikiroo.utils.ui.UIUtils; - -/** - * Tests for nikiroo-utils. - * - * @author niki - */ -public class Test extends TestLauncher { - /** - * Start the tests. - * - * @param args - * the arguments (which are passed as-is to the other test - * classes) - */ - public Test(String[] args) { - super("Nikiroo-utils", args); - - // setDetails(true); - - addSeries(new ProgressTest(args)); - addSeries(new BundleTest(args)); - addSeries(new IOUtilsTest(args)); - addSeries(new VersionTest(args)); - addSeries(new SerialTest(args)); - addSeries(new SerialServerTest(args)); - addSeries(new StringUtilsTest(args)); - addSeries(new TempFilesTest(args)); - addSeries(new CryptUtilsTest(args)); - addSeries(new BufferedInputStreamTest(args)); - addSeries(new NextableInputStreamTest(args)); - addSeries(new ReplaceInputStreamTest(args)); - addSeries(new BufferedOutputStreamTest(args)); - addSeries(new ReplaceOutputStreamTest(args)); - - // TODO: test cache and downloader - Cache cache = null; - CacheMemory memcache = null; - Downloader downloader = null; - - // To include the sources: - img2aa siu; - justify ssu; - bridge aa; - Proxy proxy; - UIUtils uiUtils; - } - - /** - * Main entry point of the program. - * - * @param args - * the arguments passed to the {@link TestLauncher}s. - */ - static public void main(String[] args) { - System.exit(new Test(args).launch()); - } -} diff --git a/test_code/VersionTest.java b/test_code/VersionTest.java deleted file mode 100644 index 2d84476..0000000 --- a/test_code/VersionTest.java +++ /dev/null @@ -1,140 +0,0 @@ -package be.nikiroo.utils.test_code; - -import be.nikiroo.utils.Version; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class VersionTest extends TestLauncher { - public VersionTest(String[] args) { - super("Version test", args); - - addTest(new TestCase("String <-> int") { - @Override - public void test() throws Exception { - assertEquals("Cannot parse version 1.2.3 from int to String", - "1.2.3", new Version(1, 2, 3).toString()); - assertEquals( - "Cannot parse major version \"1.2.3\" from String to int", - 1, new Version("1.2.3").getMajor()); - assertEquals( - "Cannot parse minor version \"1.2.3\" from String to int", - 2, new Version("1.2.3").getMinor()); - assertEquals( - "Cannot parse patch version \"1.2.3\" from String to int", - 3, new Version("1.2.3").getPatch()); - } - }); - - addTest(new TestCase("Bad input") { - @Override - public void test() throws Exception { - assertEquals( - "Bad input should return an empty version", - true, - new Version( - "Doors 98 SE Special Deluxe Edition Pro++ Not-Home") - .isEmpty()); - - assertEquals( - "Bad input should return [unknown]", - "[unknown]", - new Version( - "Doors 98 SE Special Deluxe Edition Pro++ Not-Home") - .toString()); - } - }); - - addTest(new TestCase("Read current version") { - @Override - public void test() throws Exception { - assertNotNull("The version should not be NULL (in any case!)", - Version.getCurrentVersion()); - assertEquals("The current version should not be empty", false, - Version.getCurrentVersion().isEmpty()); - } - }); - - addTest(new TestCase("Tag version") { - @Override - public void test() throws Exception { - Version version = new Version("1.0.0-debian0"); - assertEquals("debian", version.getTag()); - assertEquals(0, version.getTagVersion()); - version = new Version("1.0.0-debian.0"); - assertEquals("debian.", version.getTag()); - assertEquals(0, version.getTagVersion()); - version = new Version("1.0.0-debian-0"); - assertEquals("debian-", version.getTag()); - assertEquals(0, version.getTagVersion()); - version = new Version("1.0.0-debian-12"); - assertEquals("debian-", version.getTag()); - assertEquals(12, version.getTagVersion()); - - // tag with no tag version - version = new Version("1.0.0-dev"); - assertEquals(1, version.getMajor()); - assertEquals(0, version.getMinor()); - assertEquals(0, version.getPatch()); - assertEquals("dev", version.getTag()); - assertEquals(-1, version.getTagVersion()); - } - }); - - addTest(new TestCase("Comparing versions") { - @Override - public void test() throws Exception { - assertEquals(true, - new Version(1, 1, 1).isNewerThan(new Version(1, 1, 0))); - assertEquals(true, - new Version(2, 0, 0).isNewerThan(new Version(1, 1, 1))); - assertEquals(true, - new Version(10, 7, 8).isNewerThan(new Version(9, 9, 9))); - assertEquals(true, - new Version(0, 0, 0).isOlderThan(new Version(0, 0, 1))); - assertEquals(1, - new Version(1, 1, 1).compareTo(new Version(0, 1, 1))); - assertEquals(-1, - new Version(0, 0, 1).compareTo(new Version(0, 1, 1))); - assertEquals(0, - new Version(0, 0, 1).compareTo(new Version(0, 0, 1))); - assertEquals(true, - new Version(0, 0, 1).equals(new Version(0, 0, 1))); - assertEquals(false, - new Version(0, 2, 1).equals(new Version(0, 0, 1))); - - assertEquals(true, - new Version(1, 0, 1, "my.tag.", 2).equals(new Version( - 1, 0, 1, "my.tag.", 2))); - assertEquals(false, - new Version(1, 0, 1, "my.tag.", 2).equals(new Version( - 1, 0, 0, "my.tag.", 2))); - assertEquals(false, - new Version(1, 0, 1, "my.tag.", 2).equals(new Version( - 1, 0, 1, "not-my.tag.", 2))); - } - }); - - addTest(new TestCase("toString") { - @Override - public void test() throws Exception { - // Check leading 0s: - Version version = new Version("01.002.4"); - assertEquals("Leading 0s not working", "1.2.4", - version.toString()); - - // Check spacing - version = new Version("1 . 2.4 "); - assertEquals("Additional spaces not working", "1.2.4", - version.toString()); - - String[] tests = new String[] { "1.0.0", "1.2.3", "1.0.0-dev", - "1.1.2-niki0" }; - for (String test : tests) { - version = new Version(test); - assertEquals("toString and back conversion failed", test, - version.toString()); - } - } - }); - } -} diff --git a/test_code/bundle_test.properties b/test_code/bundle_test.properties deleted file mode 100644 index 5222c59..0000000 --- a/test_code/bundle_test.properties +++ /dev/null @@ -1,3 +0,0 @@ -ONE = un -ONE_SUFFIX = un + suffix -JAPANESE = 日本語 Nihongo \ No newline at end of file -- 2.27.0