SerializerTest.java
/***************************************************************************
Copyright 2015 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
public final class SerializerTest {
public static void assertTrue(final boolean result) {
if(!result) { throw new AssertionError("Expected true"); }
}
public static void main(final String[] args) throws Exception {
// public static void write(final Object r, final OutputStream out, final Charset charEncode) throws IOException
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Serializer.write("Test", baos, StandardCharsets.UTF_8);
assertTrue(Arrays.equals(baos.toByteArray(), "Test".getBytes(StandardCharsets.UTF_8)));
baos.reset();
final byte[] testBytes = new byte[]{ 0, 1, 2, -1, -5, 6, 8, 0 };
Serializer.write(testBytes, baos, StandardCharsets.UTF_8);
assertTrue(Arrays.equals(baos.toByteArray(), testBytes));
baos.reset();
final String testString = "Another test";
Serializer.write(new StringReader(testString), baos, StandardCharsets.UTF_8);
assertTrue(Arrays.equals(baos.toByteArray(), testString.getBytes(StandardCharsets.UTF_8)));
baos.reset();
Serializer.write(new ByteArrayInputStream(testBytes), baos, StandardCharsets.UTF_8);
assertTrue(Arrays.equals(baos.toByteArray(), testBytes));
baos.reset();
final String testString2 = "an interesting string.";
Serializer.write(new Object() { @Override public String toString() { return testString2; }},
baos, StandardCharsets.UTF_8);
assertTrue(Arrays.equals(baos.toByteArray(), testString2.getBytes(StandardCharsets.UTF_8)));
baos.reset();
final List<String> arr = Arrays.asList("a", "b", "c", " ", "defghijk");
Serializer.write(arr, baos, StandardCharsets.UTF_8);
assertTrue(Arrays.equals(baos.toByteArray(), "abc defghijk".getBytes(StandardCharsets.UTF_8)));
baos.reset();
Serializer.write(arr.iterator(), baos, StandardCharsets.UTF_8);
assertTrue(Arrays.equals(baos.toByteArray(), "abc defghijk".getBytes(StandardCharsets.UTF_8)));
}
}