Java tutorial
/* Copyright (C) 2013-2014 Ian Teune <ian.teune@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.tinspx.util.io.callbacks; import static com.google.common.base.Preconditions.*; import com.google.common.collect.Lists; import com.google.common.collect.Range; import com.google.common.primitives.Ints; import com.tinspx.util.base.Content; import com.tinspx.util.base.ContentListener; import com.tinspx.util.base.ContentTest; import com.tinspx.util.io.ByteUtils; import com.tinspx.util.io.CAWriter; import com.tinspx.util.io.DecodingOutputStream; import com.tinspx.util.io.IOTest; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Random; import org.apache.commons.io.Charsets; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; /** * * @author Ian */ public class SegmentingCallbackTest { private static final Random RANDOM = new Random(); private final static List<Charset> STANDARD = Arrays.asList(com.google.common.base.Charsets.ISO_8859_1, com.google.common.base.Charsets.US_ASCII, com.google.common.base.Charsets.UTF_16, com.google.common.base.Charsets.UTF_16BE, com.google.common.base.Charsets.UTF_16LE, com.google.common.base.Charsets.UTF_8); private static char[] toChars(Charset charset, int len) { checkArgument(len >= 0); if (charset == com.google.common.base.Charsets.US_ASCII) { return IOTest.getRandomAsciiChars(RANDOM, len); } else if (charset == com.google.common.base.Charsets.ISO_8859_1) { return IOTest.getRandomISO8859Chars(RANDOM, len); } else { return IOTest.getRandomChars(RANDOM, len); } } public SegmentingCallbackTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { } @After public void tearDown() { } @Test @SuppressWarnings({ "unchecked", "rawtypes", "null" }) public void testInvalidArgs() { DecodingOutputStream d = new DecodingOutputStream(Charsets.US_ASCII.newDecoder()); try { SegmentingCallback.create(null); fail(); } catch (NullPointerException ex) { } try { SegmentingCallback.create(d, (ContentListener) null); fail(); } catch (NullPointerException ex) { } try { SegmentingCallback.create(d, (Collection) null); fail(); } catch (NullPointerException ex) { } try { SegmentingCallback.create(d, Content.emptyCallback(), null); fail(); } catch (NullPointerException ex) { } SegmentingCallback s = SegmentingCallback.create(d); assertSame(d, s.decoder()); assertFalse(s.isDecoding()); assertFalse(s.hasError()); assertFalse(s.error().isPresent()); try { s.callbacks().add(null); fail(); } catch (IllegalArgumentException ex) { } try { s.callback(null); fail(); } catch (NullPointerException ex) { } try { s.callbacks(Content.emptyCallback(), null); fail(); } catch (NullPointerException ex) { } assertFalse(s.isDecoding()); ContentTest.AssertCallback ac = new ContentTest.AssertCallback(); s.callback(ac); assertTrue(s.isDecoding()); s.onContentStart(null); s.onContentComplete(null); s.onError(null); try { s.onContent(null, null); fail(); } catch (NullPointerException ex) { } assertTrue(s.isDecoding()); assertFalse(s.hasError()); assertFalse(s.error().isPresent()); s.onContent(ac, ByteBuffer.wrap(new byte[] { (byte) 0xFF })); assertFalse(s.isDecoding()); assertTrue(s.hasError()); assertTrue(s.error().isPresent()); s.onContent(ac, ByteBuffer.wrap(new byte[] { (byte) 0xFF })); assertTrue(d.isEmpty()); ac.start(1); ac.content(0); ac.complete(1); ac.error(2); } @Test public void test() { for (Charset charset : STANDARD) { testCharset(charset); } } private static void testCharset(Charset charset) { final char[] chars = toChars(charset, 1024 * 64); final byte[] bytes = ByteUtils.toByteArray(charset.encode(CharBuffer.wrap(chars))); CAWriter writer = new CAWriter(chars.length); testWriteSingle(charset, chars, bytes, writer); testWriteParts(charset, chars, bytes, writer); testWriteFull(charset, chars, bytes, writer); } @SuppressWarnings({ "rawtypes", "unchecked" }) private static void testWriteFull(Charset charset, char[] chars, byte[] bytes, CAWriter writer) { writer.clearAndReset(); SegmentingCallback s = SegmentingCallback.create(new DecodingOutputStream(charset), new WriterListener(writer)); s.onContentStart(null); ByteBuffer buf = ByteBuffer.wrap(bytes); s.onContent(null, buf); assertFalse(buf.hasRemaining()); s.onContentComplete(null); assertArrayEquals(chars, writer.toCharArray()); } @SuppressWarnings({ "rawtypes", "unchecked" }) private static void testWriteSingle(Charset charset, char[] chars, byte[] bytes, CAWriter writer) { writer.clearAndReset(); SegmentingCallback s = SegmentingCallback.create(new DecodingOutputStream(charset), new WriterListener(writer)); s.onContentStart(null); for (byte b : bytes) { ByteBuffer buf = ByteBuffer.wrap(new byte[] { b }); s.onContent(null, buf); assertFalse(buf.hasRemaining()); } s.onContentComplete(null); assertArrayEquals(chars, writer.toCharArray()); } @SuppressWarnings({ "rawtypes", "unchecked" }) private static void testWriteParts(Charset charset, char[] chars, byte[] bytes, CAWriter writer) { writer.clearAndReset(); SegmentingCallback s = SegmentingCallback.create(new DecodingOutputStream(charset), new WriterListener(writer)); s.onContentStart(null); for (Range<Integer> r : generateCloseOpen(bytes.length, 13)) { ByteBuffer buf = ByteBuffer.wrap(bytes, r.lowerEndpoint(), r.upperEndpoint() - r.lowerEndpoint()); s.onContent(null, buf); assertFalse(buf.hasRemaining()); } s.onContentComplete(null); assertArrayEquals(chars, writer.toCharArray()); } static List<Range<Integer>> generateCloseOpen(int len, int parts) { List<Range<Integer>> ranges = Lists.newArrayListWithCapacity(parts); for (int i = 0; i < parts; i++) { ranges.add(Range.closedOpen(Ints.checkedCast((long) i * len / parts), Ints.checkedCast((long) (i + 1) * len / parts))); } assert ranges.size() == parts; return ranges; } }