FunctionsTest whitespaces
Use four spaces instead of a tab as per the projcet's Checkstyle rules.
This commit is contained in:
parent
19a5a09b09
commit
f12cfc8d4e
|
@ -30,282 +30,282 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
class FunctionsTest {
|
class FunctionsTest {
|
||||||
public static class SomeException extends Exception {
|
public static class SomeException extends Exception {
|
||||||
private static final long serialVersionUID = -4965704778119283411L;
|
private static final long serialVersionUID = -4965704778119283411L;
|
||||||
|
|
||||||
private Throwable t;
|
private Throwable t;
|
||||||
|
|
||||||
SomeException(String pMsg) {
|
SomeException(String pMsg) {
|
||||||
super(pMsg);
|
super(pMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setThrowable(Throwable pThrowable) {
|
public void setThrowable(Throwable pThrowable) {
|
||||||
t = pThrowable;
|
t = pThrowable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() throws Throwable {
|
public void test() throws Throwable {
|
||||||
if (t != null) {
|
if (t != null) {
|
||||||
throw t;
|
throw t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static class Testable {
|
public static class Testable {
|
||||||
private Throwable t;
|
private Throwable t;
|
||||||
|
|
||||||
Testable(Throwable pTh) {
|
Testable(Throwable pTh) {
|
||||||
t = pTh;
|
t = pTh;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setThrowable(Throwable pThrowable) {
|
public void setThrowable(Throwable pThrowable) {
|
||||||
t = pThrowable;
|
t = pThrowable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() throws Throwable {
|
public void test() throws Throwable {
|
||||||
test(t);
|
test(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test(Throwable pThrowable) throws Throwable {
|
public void test(Throwable pThrowable) throws Throwable {
|
||||||
if (pThrowable != null) {
|
if (pThrowable != null) {
|
||||||
throw pThrowable;
|
throw pThrowable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer testInt() throws Throwable {
|
public Integer testInt() throws Throwable {
|
||||||
return testInt(t);
|
return testInt(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer testInt(Throwable pThrowable) throws Throwable {
|
public Integer testInt(Throwable pThrowable) throws Throwable {
|
||||||
if (pThrowable != null) {
|
if (pThrowable != null) {
|
||||||
throw pThrowable;
|
throw pThrowable;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class FailureOnOddInvocations {
|
public static class FailureOnOddInvocations {
|
||||||
private static int invocation;
|
private static int invocation;
|
||||||
FailureOnOddInvocations() throws SomeException {
|
FailureOnOddInvocations() throws SomeException {
|
||||||
final int i = ++invocation;
|
final int i = ++invocation;
|
||||||
if (i % 2 == 1) {
|
if (i % 2 == 1) {
|
||||||
throw new SomeException("Odd Invocation: " + i);
|
throw new SomeException("Odd Invocation: " + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CloseableObject {
|
public static class CloseableObject {
|
||||||
private boolean closed;
|
private boolean closed;
|
||||||
|
|
||||||
public void run(Throwable pTh) throws Throwable {
|
public void run(Throwable pTh) throws Throwable {
|
||||||
if (pTh != null) {
|
if (pTh != null) {
|
||||||
throw pTh;
|
throw pTh;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
closed = false;
|
closed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
closed = true;
|
closed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isClosed() {
|
public boolean isClosed() {
|
||||||
return closed;
|
return closed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRunnable() {
|
void testRunnable() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocation = 0;
|
||||||
try {
|
try {
|
||||||
Functions.run(FailureOnOddInvocations::new);
|
Functions.run(FailureOnOddInvocations::new);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (UndeclaredThrowableException e) {
|
} catch (UndeclaredThrowableException e) {
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
assertNotNull(cause);
|
assertNotNull(cause);
|
||||||
assertTrue(cause instanceof SomeException);
|
assertTrue(cause instanceof SomeException);
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||||
}
|
}
|
||||||
Functions.run(FailureOnOddInvocations::new);
|
Functions.run(FailureOnOddInvocations::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCallable() {
|
void testCallable() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocation = 0;
|
||||||
try {
|
try {
|
||||||
Functions.call(FailureOnOddInvocations::new);
|
Functions.call(FailureOnOddInvocations::new);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (UndeclaredThrowableException e) {
|
} catch (UndeclaredThrowableException e) {
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
assertNotNull(cause);
|
assertNotNull(cause);
|
||||||
assertTrue(cause instanceof SomeException);
|
assertTrue(cause instanceof SomeException);
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||||
}
|
}
|
||||||
final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
|
final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
|
||||||
assertNotNull(instance);
|
assertNotNull(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptConsumer() {
|
void testAcceptConsumer() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(ise);
|
final Testable testable = new Testable(ise);
|
||||||
try {
|
try {
|
||||||
Functions.accept(Testable::test, testable);
|
Functions.accept(Testable::test, testable);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
}
|
}
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
testable.setThrowable(error);
|
testable.setThrowable(error);
|
||||||
try {
|
try {
|
||||||
Functions.accept(Testable::test, testable);
|
Functions.accept(Testable::test, testable);
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
assertSame(error, e);
|
assertSame(error, e);
|
||||||
}
|
}
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
testable.setThrowable(ioe);
|
testable.setThrowable(ioe);
|
||||||
try {
|
try {
|
||||||
Functions.accept(Testable::test, testable);
|
Functions.accept(Testable::test, testable);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (UncheckedIOException e) {
|
} catch (UncheckedIOException e) {
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertTrue(t instanceof IOException);
|
assertTrue(t instanceof IOException);
|
||||||
assertSame(ioe, t);
|
assertSame(ioe, t);
|
||||||
}
|
}
|
||||||
testable.setThrowable(null);
|
testable.setThrowable(null);
|
||||||
Functions.accept(Testable::test, testable);
|
Functions.accept(Testable::test, testable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAcceptBiConsumer() {
|
void testAcceptBiConsumer() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(null);
|
final Testable testable = new Testable(null);
|
||||||
try {
|
try {
|
||||||
Functions.accept(Testable::test, testable, ise);
|
Functions.accept(Testable::test, testable, ise);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
}
|
}
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
try {
|
try {
|
||||||
Functions.accept(Testable::test, testable, error);
|
Functions.accept(Testable::test, testable, error);
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
assertSame(error, e);
|
assertSame(error, e);
|
||||||
}
|
}
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
testable.setThrowable(ioe);
|
testable.setThrowable(ioe);
|
||||||
try {
|
try {
|
||||||
Functions.accept(Testable::test, testable, ioe);
|
Functions.accept(Testable::test, testable, ioe);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (UncheckedIOException e) {
|
} catch (UncheckedIOException e) {
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertTrue(t instanceof IOException);
|
assertTrue(t instanceof IOException);
|
||||||
assertSame(ioe, t);
|
assertSame(ioe, t);
|
||||||
}
|
}
|
||||||
testable.setThrowable(null);
|
testable.setThrowable(null);
|
||||||
Functions.accept(Testable::test, testable, (Throwable) null);
|
Functions.accept(Testable::test, testable, (Throwable) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testApplyFunction() {
|
public void testApplyFunction() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(ise);
|
final Testable testable = new Testable(ise);
|
||||||
try {
|
try {
|
||||||
Functions.apply(Testable::testInt, testable);
|
Functions.apply(Testable::testInt, testable);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
}
|
}
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
testable.setThrowable(error);
|
testable.setThrowable(error);
|
||||||
try {
|
try {
|
||||||
Functions.apply(Testable::testInt, testable);
|
Functions.apply(Testable::testInt, testable);
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
assertSame(error, e);
|
assertSame(error, e);
|
||||||
}
|
}
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
testable.setThrowable(ioe);
|
testable.setThrowable(ioe);
|
||||||
try {
|
try {
|
||||||
Functions.apply(Testable::testInt, testable);
|
Functions.apply(Testable::testInt, testable);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (UncheckedIOException e) {
|
} catch (UncheckedIOException e) {
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertTrue(t instanceof IOException);
|
assertTrue(t instanceof IOException);
|
||||||
assertSame(ioe, t);
|
assertSame(ioe, t);
|
||||||
}
|
}
|
||||||
testable.setThrowable(null);
|
testable.setThrowable(null);
|
||||||
final Integer i = Functions.apply(Testable::testInt, testable);
|
final Integer i = Functions.apply(Testable::testInt, testable);
|
||||||
assertNotNull(i);
|
assertNotNull(i);
|
||||||
assertEquals(0, i.intValue());
|
assertEquals(0, i.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testApplyBiFunction() {
|
public void testApplyBiFunction() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(null);
|
final Testable testable = new Testable(null);
|
||||||
try {
|
try {
|
||||||
Functions.apply(Testable::testInt, testable, ise);
|
Functions.apply(Testable::testInt, testable, ise);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
}
|
}
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
try {
|
try {
|
||||||
Functions.apply(Testable::testInt, testable, error);
|
Functions.apply(Testable::testInt, testable, error);
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
assertSame(error, e);
|
assertSame(error, e);
|
||||||
}
|
}
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
try {
|
try {
|
||||||
Functions.apply(Testable::testInt, testable, ioe);
|
Functions.apply(Testable::testInt, testable, ioe);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (UncheckedIOException e) {
|
} catch (UncheckedIOException e) {
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertTrue(t instanceof IOException);
|
assertTrue(t instanceof IOException);
|
||||||
assertSame(ioe, t);
|
assertSame(ioe, t);
|
||||||
}
|
}
|
||||||
final Integer i = Functions.apply(Testable::testInt, testable, (Throwable) null);
|
final Integer i = Functions.apply(Testable::testInt, testable, (Throwable) null);
|
||||||
assertNotNull(i);
|
assertNotNull(i);
|
||||||
assertEquals(0, i.intValue());
|
assertEquals(0, i.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTryWithResources() {
|
public void testTryWithResources() {
|
||||||
final CloseableObject co = new CloseableObject();
|
final CloseableObject co = new CloseableObject();
|
||||||
final FailableConsumer<Throwable, ? extends Throwable> consumer = co::run;
|
final FailableConsumer<Throwable, ? extends Throwable> consumer = co::run;
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
try {
|
try {
|
||||||
Functions.tryWithResources(() -> consumer.accept(ise), co::close);
|
Functions.tryWithResources(() -> consumer.accept(ise), co::close);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
}
|
}
|
||||||
assertTrue(co.isClosed());
|
assertTrue(co.isClosed());
|
||||||
co.reset();
|
co.reset();
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
try {
|
try {
|
||||||
Functions.tryWithResources(() -> consumer.accept(error), co::close);
|
Functions.tryWithResources(() -> consumer.accept(error), co::close);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
assertSame(error, e);
|
assertSame(error, e);
|
||||||
}
|
}
|
||||||
assertTrue(co.isClosed());
|
assertTrue(co.isClosed());
|
||||||
co.reset();
|
co.reset();
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
try {
|
try {
|
||||||
Functions.tryWithResources(() -> consumer.accept(ioe), co::close);
|
Functions.tryWithResources(() -> consumer.accept(ioe), co::close);
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (UncheckedIOException e) {
|
} catch (UncheckedIOException e) {
|
||||||
final IOException cause = e.getCause();
|
final IOException cause = e.getCause();
|
||||||
assertSame(ioe, cause);
|
assertSame(ioe, cause);
|
||||||
}
|
}
|
||||||
assertTrue(co.isClosed());
|
assertTrue(co.isClosed());
|
||||||
co.reset();
|
co.reset();
|
||||||
Functions.tryWithResources(() -> consumer.accept(null), co::close);
|
Functions.tryWithResources(() -> consumer.accept(null), co::close);
|
||||||
assertTrue(co.isClosed());
|
assertTrue(co.isClosed());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue