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 com.google.common.base.Function; import static com.google.common.base.Preconditions.*; import com.google.common.base.Supplier; import com.google.common.collect.Lists; import com.google.common.collect.Range; import com.google.common.io.Closer; import com.google.common.primitives.Ints; import com.tinspx.util.base.BasicError; import com.tinspx.util.base.Content; import com.tinspx.util.base.ContentCallback; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; import lombok.NonNull; import org.apache.commons.io.FileUtils; import static org.junit.Assert.*; /** * * @author Ian */ public class FileTests { public static Closeable teardownTestDir(@NonNull final File testDir) throws IOException { return new Closeable() { @Override public void close() throws IOException { FileUtils.deleteDirectory(testDir); } }; } public static File setupTestDir() throws IOException { File testDir = new File(new File(System.getProperty("user.dir")), "test"); if (testDir.exists()) { if (testDir.list().length > 0) { throw new IOException(testDir + " exists and is not empty"); } } else if (!testDir.mkdirs()) { throw new IOException("could not create " + testDir); } return testDir; // boolean complete = false; // try { // Resources.asByteSource(ChannelSource.class.getResource("/" + FORMATTED)).copyTo(Files.asByteSink(new File(testDir, FORMATTED))); // Resources.asByteSource(ChannelSource.class.getResource("/" + SAMPLE)).copyTo(Files.asByteSink(new File(testDir, SAMPLE))); // complete = true; // return testDir; // } finally { // if(!complete) { // teardownTestDir(testDir).close(); // } // } } public static boolean deleteIfExists(File file) { if (file.exists()) { checkArgument(file.isFile(), "%s is not a file", file); if (!file.delete()) { throw new RuntimeException("could not delete: " + file); } return true; } return false; } public static void delete(File file) { if (file.exists()) { checkArgument(file.isFile(), "%s is not a file", file); if (!file.delete()) { throw new RuntimeException("could not delete: " + file); } } else { throw new RuntimeException(file + " DNE"); } } public static <T extends ContentCallback<? super BasicError.Listener, ? super ByteBuffer>> void testContentCallback( byte[] ref, Supplier<T> supplier, Function<? super T, byte[]> function) throws IOException { testWriteSingle(ref, supplier, function); testWriteParts(ref, supplier, function); testWriteFull(ref, supplier, function); } private static <T extends ContentCallback<? super BasicError.Listener, ? super ByteBuffer>> void testWriteSingle( byte[] ref, Supplier<T> supplier, Function<? super T, byte[]> function) throws IOException { Closer closer = Closer.create(); T c = supplier.get(); try { closer.register(Content.completeOnClose(null, c)); c.onContentStart(null); for (byte b : ref) { c.onContent(null, ByteBuffer.wrap(new byte[] { b })); } c.onContentComplete(null); assertArrayEquals(ref, function.apply(c)); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } private static <T extends ContentCallback<? super BasicError.Listener, ? super ByteBuffer>> void testWriteParts( byte[] ref, Supplier<T> supplier, Function<? super T, byte[]> function) throws IOException { Closer closer = Closer.create(); T c = supplier.get(); try { closer.register(Content.completeOnClose(null, c)); c.onContentStart(null); for (Range<Integer> r : generateCloseOpen(ref.length, 13)) { c.onContent(null, ByteBuffer.wrap(ref, r.lowerEndpoint(), r.upperEndpoint() - r.lowerEndpoint())); } c.onContentComplete(null); assertArrayEquals(ref, function.apply(c)); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } private static <T extends ContentCallback<? super BasicError.Listener, ? super ByteBuffer>> void testWriteFull( byte[] ref, Supplier<T> supplier, Function<? super T, byte[]> function) throws IOException { Closer closer = Closer.create(); T c = supplier.get(); try { closer.register(Content.completeOnClose(null, c)); c.onContentStart(null); c.onContent(null, ByteBuffer.wrap(ref)); c.onContentComplete(null); assertArrayEquals(ref, function.apply(c)); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } public 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; } }