| 1 | package be.nikiroo.utils.test; |
| 2 | |
| 3 | import java.net.URL; |
| 4 | |
| 5 | import be.nikiroo.utils.serial.Exporter; |
| 6 | import be.nikiroo.utils.serial.Importer; |
| 7 | |
| 8 | class SerialTest extends TestLauncher { |
| 9 | /** |
| 10 | * Required for Import/Export of objects. |
| 11 | */ |
| 12 | public SerialTest() { |
| 13 | this(null); |
| 14 | } |
| 15 | |
| 16 | public SerialTest(String[] args) { |
| 17 | super("Serial test", args); |
| 18 | |
| 19 | addTest(new TestCase("Simple class Import/Export") { |
| 20 | @Override |
| 21 | public void test() throws Exception { |
| 22 | Data data = new Data(42); |
| 23 | String encoded = new Exporter().append(data).toString(false); |
| 24 | Object redata = new Importer().read(encoded).getValue(); |
| 25 | String reencoded = new Exporter().append(redata) |
| 26 | .toString(false); |
| 27 | |
| 28 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 29 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | addTest(new TestCase("URL Import/Export") { |
| 34 | @Override |
| 35 | public void test() throws Exception { |
| 36 | URL data = new URL("https://fanfan.be/"); |
| 37 | String encoded = new Exporter().append(data).toString(false); |
| 38 | Object redata = new Importer().read(encoded).getValue(); |
| 39 | String reencoded = new Exporter().append(redata) |
| 40 | .toString(false); |
| 41 | |
| 42 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 43 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | addTest(new TestCase("URL-String Import/Export") { |
| 48 | @Override |
| 49 | public void test() throws Exception { |
| 50 | String data = new URL("https://fanfan.be/").toString(); |
| 51 | String encoded = new Exporter().append(data).toString(false); |
| 52 | Object redata = new Importer().read(encoded).getValue(); |
| 53 | String reencoded = new Exporter().append(redata) |
| 54 | .toString(false); |
| 55 | |
| 56 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 57 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 58 | assertEquals(data, redata); |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | addTest(new TestCase("URL/URL-String arrays Import/Export") { |
| 63 | @Override |
| 64 | public void test() throws Exception { |
| 65 | final String url = "https://fanfan.be/"; |
| 66 | |
| 67 | Object[] data = new Object[] { new URL(url), url }; |
| 68 | String encoded = new Exporter().append(data).toString(false); |
| 69 | Object redata = new Importer().read(encoded).getValue(); |
| 70 | String reencoded = new Exporter().append(redata) |
| 71 | .toString(false); |
| 72 | |
| 73 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 74 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 75 | assertEquals(data[0], ((Object[]) redata)[0]); |
| 76 | assertEquals(data[1], ((Object[]) redata)[1]); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | addTest(new TestCase("Import/Export with nested objects") { |
| 81 | @Override |
| 82 | public void test() throws Exception { |
| 83 | Data data = new DataObject(new Data(21)); |
| 84 | String encoded = new Exporter().append(data).toString(false); |
| 85 | Object redata = new Importer().read(encoded).getValue(); |
| 86 | String reencoded = new Exporter().append(redata) |
| 87 | .toString(false); |
| 88 | |
| 89 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 90 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | addTest(new TestCase("Import/Export with nested objects forming a loop") { |
| 95 | @Override |
| 96 | public void test() throws Exception { |
| 97 | DataLoop data = new DataLoop("looping"); |
| 98 | data.next = new DataLoop("level 2"); |
| 99 | data.next.next = data; |
| 100 | |
| 101 | String encoded = new Exporter().append(data).toString(false); |
| 102 | Object redata = new Importer().read(encoded).getValue(); |
| 103 | String reencoded = new Exporter().append(redata) |
| 104 | .toString(false); |
| 105 | |
| 106 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 107 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | addTest(new TestCase("Array in Object Import/Export") { |
| 112 | @Override |
| 113 | public void test() throws Exception { |
| 114 | Object data = new DataArray();// new String[] { "un", "deux" }; |
| 115 | String encoded = new Exporter().append(data).toString(false); |
| 116 | Object redata = new Importer().read(encoded).getValue(); |
| 117 | String reencoded = new Exporter().append(redata) |
| 118 | .toString(false); |
| 119 | |
| 120 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 121 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | addTest(new TestCase("Array Import/Export") { |
| 126 | @Override |
| 127 | public void test() throws Exception { |
| 128 | Object data = new String[] { "un", "deux" }; |
| 129 | String encoded = new Exporter().append(data).toString(false); |
| 130 | Object redata = new Importer().read(encoded).getValue(); |
| 131 | String reencoded = new Exporter().append(redata) |
| 132 | .toString(false); |
| 133 | |
| 134 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 135 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 136 | } |
| 137 | }); |
| 138 | |
| 139 | addTest(new TestCase("Enum Import/Export") { |
| 140 | @Override |
| 141 | public void test() throws Exception { |
| 142 | Object data = EnumToSend.FANFAN; |
| 143 | String encoded = new Exporter().append(data).toString(false); |
| 144 | Object redata = new Importer().read(encoded).getValue(); |
| 145 | String reencoded = new Exporter().append(redata) |
| 146 | .toString(false); |
| 147 | |
| 148 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 149 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 150 | } |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | class DataArray { |
| 155 | public String[] data = new String[] { "un", "deux" }; |
| 156 | } |
| 157 | |
| 158 | @SuppressWarnings("unused") |
| 159 | class Data { |
| 160 | private int value; |
| 161 | |
| 162 | private Data() { |
| 163 | } |
| 164 | |
| 165 | public Data(int value) { |
| 166 | this.value = value; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @SuppressWarnings("unused") |
| 171 | class DataObject extends Data { |
| 172 | private Data data; |
| 173 | |
| 174 | @SuppressWarnings("synthetic-access") |
| 175 | private DataObject() { |
| 176 | } |
| 177 | |
| 178 | @SuppressWarnings("synthetic-access") |
| 179 | public DataObject(Data data) { |
| 180 | this.data = data; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @SuppressWarnings("unused") |
| 185 | class DataLoop extends Data { |
| 186 | public DataLoop next; |
| 187 | private String value; |
| 188 | |
| 189 | @SuppressWarnings("synthetic-access") |
| 190 | private DataLoop() { |
| 191 | } |
| 192 | |
| 193 | @SuppressWarnings("synthetic-access") |
| 194 | public DataLoop(String value) { |
| 195 | this.value = value; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | enum EnumToSend { |
| 200 | FANFAN, TULIPE, |
| 201 | } |
| 202 | } |