Commit 478e77d8 authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

v2.0.2 release: FileUtilc.copyToFile added. CheckedExceptionWrapper.toString...

v2.0.2 release: FileUtilc.copyToFile added. CheckedExceptionWrapper.toString made easier to read. Minor refactoring changes
parent 78e86c2c
Pipeline #17933919 passed with stage
in 1 minute and 6 seconds
......@@ -6,7 +6,7 @@
<groupId>com.rajivprab</groupId>
<artifactId>cava</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
<name>Cava: Clean Java</name>
<description>A library that enables users to write minimal, clean and simple Java</description>
......
......@@ -61,7 +61,7 @@ public class CheckedExceptionWrapper extends RuntimeException {
@Override
public String toString() {
return getClass() + " around {\n" + e.toString() + "\n}";
return getClass().getCanonicalName() + ": " + e.toString();
}
@Override
......
package org.rajivprab.cava;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BoundedInputStream;
import org.rajivprab.cava.IOUtilc.IoException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
/**
......@@ -14,6 +17,29 @@ import java.net.URISyntaxException;
* Created by rprabhakar on 12/15/15.
*/
public class FileUtilc {
// Throws SizeLimitExceededException as appropriate
// Will close the stream on exit
public static void copyToFile(InputStream stream, File file, long maxSize) {
try (InputStream ignored = stream) {
BoundedInputStream boundedStream = new BoundedInputStream(stream, maxSize);
boundedStream.setPropagateClose(false);
FileUtils.copyToFile(boundedStream, file);
// Following read prevents stream from being used outside of this method, if bytes still exist
Validatec.equals(stream.read(), IOUtils.EOF, SizeLimitExceededException.class,
"Stream exceeded " + maxSize / 1024 + " kB");
} catch (IOException e) {
throw new IoException(e);
}
}
public static void copyToFile(InputStream stream, File file) {
try {
FileUtils.copyToFile(stream, file);
} catch (IOException e) {
throw new IoException(e);
}
}
public static String readFileToString(File file) {
try {
return FileUtils.readFileToString(file, IOUtilc.CHARSET_USED);
......@@ -37,4 +63,10 @@ public class FileUtilc {
throw new CheckedExceptionWrapper(e);
}
}
public static class SizeLimitExceededException extends RuntimeException {
public SizeLimitExceededException(String message) {
super(message);
}
}
}
......@@ -62,8 +62,8 @@ public class ThreadUtilc {
} catch (TimeoutException e) {
throw new TimeException(e);
} catch (ExecutionException e) {
log.warn("Hit ExecutionException while calling future.get", e);
// Do not simply throw the cause, because that lacks the stack-trace for this get-method
// Do not wrap e.cause inside CheckedExceptionWrapper, because we would still have above problem
throw new ExecException(e);
}
}
......
......@@ -16,6 +16,11 @@ import java.io.Writer;
* Created by rajivprab on 5/18/17.
*/
public class FileUtilcTest extends TestBase {
@Test
public void copyToFile() {
// TODO Copy all tests from ellie
}
@Test
public void readResourceFileFromMain() {
Truth.assertThat(FileUtilc.readClasspathFile("sample_file.txt"))
......
......@@ -27,7 +27,7 @@ public class ThreadUtilcTest extends TestBase {
private static final int RETURN_VALUE = 42;
private static final Random RNG = new Random();
// ----------------- slepp ----------------
// ----------------- sleep ----------------
@Test
public void sleep_allGood() {
......@@ -124,8 +124,7 @@ public class ThreadUtilcTest extends TestBase {
Truth.assertThat(e.getUnderlying().getCause()).isInstanceOf(IOException.class);
Truth.assertThat(e.getCause()).isInstanceOf(IOException.class);
Truth.assertThat(e).hasMessageThat().isEqualTo("java.io.IOException: test");
Truth.assertThat(e.toString())
.matches(".*ExecException around \\{\n.*ExecutionException.*IOException.*test\n\\}");
Truth.assertThat(e.toString()).matches(".*ExecException: .*ExecutionException: .*IOException: test");
}
}
......@@ -141,7 +140,7 @@ public class ThreadUtilcTest extends TestBase {
Truth.assertThat(e.getCause()).isInstanceOf(ArithmeticException.class);
Truth.assertThat(e).hasMessageThat().isEqualTo("java.lang.ArithmeticException: test");
Truth.assertThat(e.toString())
.matches(".*ExecException around \\{\n.*ExecutionException.*ArithmeticException.*test\n\\}");
.matches(".*ExecException: .*ExecutionException: .*ArithmeticException: test");
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment