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;
|
||||
|
||||
class FunctionsTest {
|
||||
public static class SomeException extends Exception {
|
||||
private static final long serialVersionUID = -4965704778119283411L;
|
||||
public static class SomeException extends Exception {
|
||||
private static final long serialVersionUID = -4965704778119283411L;
|
||||
|
||||
private Throwable t;
|
||||
private Throwable t;
|
||||
|
||||
SomeException(String pMsg) {
|
||||
super(pMsg);
|
||||
}
|
||||
SomeException(String pMsg) {
|
||||
super(pMsg);
|
||||
}
|
||||
|
||||
public void setThrowable(Throwable pThrowable) {
|
||||
t = pThrowable;
|
||||
}
|
||||
public void setThrowable(Throwable pThrowable) {
|
||||
t = pThrowable;
|
||||
}
|
||||
|
||||
public void test() throws Throwable {
|
||||
if (t != null) {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class Testable {
|
||||
private Throwable t;
|
||||
public void test() throws Throwable {
|
||||
if (t != null) {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class Testable {
|
||||
private Throwable t;
|
||||
|
||||
Testable(Throwable pTh) {
|
||||
t = pTh;
|
||||
}
|
||||
Testable(Throwable pTh) {
|
||||
t = pTh;
|
||||
}
|
||||
|
||||
public void setThrowable(Throwable pThrowable) {
|
||||
t = pThrowable;
|
||||
}
|
||||
public void setThrowable(Throwable pThrowable) {
|
||||
t = pThrowable;
|
||||
}
|
||||
|
||||
public void test() throws Throwable {
|
||||
test(t);
|
||||
}
|
||||
public void test() throws Throwable {
|
||||
test(t);
|
||||
}
|
||||
|
||||
public void test(Throwable pThrowable) throws Throwable {
|
||||
if (pThrowable != null) {
|
||||
throw pThrowable;
|
||||
}
|
||||
}
|
||||
public void test(Throwable pThrowable) throws Throwable {
|
||||
if (pThrowable != null) {
|
||||
throw pThrowable;
|
||||
}
|
||||
}
|
||||
|
||||
public Integer testInt() throws Throwable {
|
||||
return testInt(t);
|
||||
}
|
||||
public Integer testInt() throws Throwable {
|
||||
return testInt(t);
|
||||
}
|
||||
|
||||
public Integer testInt(Throwable pThrowable) throws Throwable {
|
||||
if (pThrowable != null) {
|
||||
throw pThrowable;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public Integer testInt(Throwable pThrowable) throws Throwable {
|
||||
if (pThrowable != null) {
|
||||
throw pThrowable;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FailureOnOddInvocations {
|
||||
private static int invocation;
|
||||
FailureOnOddInvocations() throws SomeException {
|
||||
final int i = ++invocation;
|
||||
if (i % 2 == 1) {
|
||||
throw new SomeException("Odd Invocation: " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class FailureOnOddInvocations {
|
||||
private static int invocation;
|
||||
FailureOnOddInvocations() throws SomeException {
|
||||
final int i = ++invocation;
|
||||
if (i % 2 == 1) {
|
||||
throw new SomeException("Odd Invocation: " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class CloseableObject {
|
||||
private boolean closed;
|
||||
public static class CloseableObject {
|
||||
private boolean closed;
|
||||
|
||||
public void run(Throwable pTh) throws Throwable {
|
||||
if (pTh != null) {
|
||||
throw pTh;
|
||||
}
|
||||
}
|
||||
public void run(Throwable pTh) throws Throwable {
|
||||
if (pTh != null) {
|
||||
throw pTh;
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
closed = false;
|
||||
}
|
||||
public void reset() {
|
||||
closed = false;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
closed = true;
|
||||
}
|
||||
public void close() {
|
||||
closed = true;
|
||||
}
|
||||
|
||||
public boolean isClosed() {
|
||||
return closed;
|
||||
}
|
||||
}
|
||||
public boolean isClosed() {
|
||||
return closed;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRunnable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
try {
|
||||
Functions.run(FailureOnOddInvocations::new);
|
||||
fail("Expected Exception");
|
||||
} catch (UndeclaredThrowableException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
assertNotNull(cause);
|
||||
assertTrue(cause instanceof SomeException);
|
||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||
}
|
||||
Functions.run(FailureOnOddInvocations::new);
|
||||
}
|
||||
@Test
|
||||
void testRunnable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
try {
|
||||
Functions.run(FailureOnOddInvocations::new);
|
||||
fail("Expected Exception");
|
||||
} catch (UndeclaredThrowableException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
assertNotNull(cause);
|
||||
assertTrue(cause instanceof SomeException);
|
||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||
}
|
||||
Functions.run(FailureOnOddInvocations::new);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCallable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
try {
|
||||
Functions.call(FailureOnOddInvocations::new);
|
||||
fail("Expected Exception");
|
||||
} catch (UndeclaredThrowableException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
assertNotNull(cause);
|
||||
assertTrue(cause instanceof SomeException);
|
||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||
}
|
||||
final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
|
||||
assertNotNull(instance);
|
||||
}
|
||||
@Test
|
||||
void testCallable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
try {
|
||||
Functions.call(FailureOnOddInvocations::new);
|
||||
fail("Expected Exception");
|
||||
} catch (UndeclaredThrowableException e) {
|
||||
final Throwable cause = e.getCause();
|
||||
assertNotNull(cause);
|
||||
assertTrue(cause instanceof SomeException);
|
||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||
}
|
||||
final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
|
||||
assertNotNull(instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAcceptConsumer() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
testable.setThrowable(null);
|
||||
Functions.accept(Testable::test, testable);
|
||||
}
|
||||
@Test
|
||||
void testAcceptConsumer() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
testable.setThrowable(null);
|
||||
Functions.accept(Testable::test, testable);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAcceptBiConsumer() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(null);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable, ise);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
try {
|
||||
Functions.accept(Testable::test, testable, error);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable, ioe);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
testable.setThrowable(null);
|
||||
Functions.accept(Testable::test, testable, (Throwable) null);
|
||||
}
|
||||
@Test
|
||||
void testAcceptBiConsumer() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(null);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable, ise);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
try {
|
||||
Functions.accept(Testable::test, testable, error);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
try {
|
||||
Functions.accept(Testable::test, testable, ioe);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
testable.setThrowable(null);
|
||||
Functions.accept(Testable::test, testable, (Throwable) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyFunction() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
testable.setThrowable(null);
|
||||
final Integer i = Functions.apply(Testable::testInt, testable);
|
||||
assertNotNull(i);
|
||||
assertEquals(0, i.intValue());
|
||||
}
|
||||
@Test
|
||||
public void testApplyFunction() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
testable.setThrowable(null);
|
||||
final Integer i = Functions.apply(Testable::testInt, testable);
|
||||
assertNotNull(i);
|
||||
assertEquals(0, i.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyBiFunction() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(null);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable, ise);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable, error);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable, ioe);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
final Integer i = Functions.apply(Testable::testInt, testable, (Throwable) null);
|
||||
assertNotNull(i);
|
||||
assertEquals(0, i.intValue());
|
||||
}
|
||||
@Test
|
||||
public void testApplyBiFunction() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(null);
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable, ise);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
final Error error = new OutOfMemoryError();
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable, error);
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
try {
|
||||
Functions.apply(Testable::testInt, testable, ioe);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertTrue(t instanceof IOException);
|
||||
assertSame(ioe, t);
|
||||
}
|
||||
final Integer i = Functions.apply(Testable::testInt, testable, (Throwable) null);
|
||||
assertNotNull(i);
|
||||
assertEquals(0, i.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTryWithResources() {
|
||||
final CloseableObject co = new CloseableObject();
|
||||
final FailableConsumer<Throwable, ? extends Throwable> consumer = co::run;
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
try {
|
||||
Functions.tryWithResources(() -> consumer.accept(ise), co::close);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
assertTrue(co.isClosed());
|
||||
co.reset();
|
||||
final Error error = new OutOfMemoryError();
|
||||
try {
|
||||
Functions.tryWithResources(() -> consumer.accept(error), co::close);
|
||||
fail("Expected Exception");
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
assertTrue(co.isClosed());
|
||||
co.reset();
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
try {
|
||||
Functions.tryWithResources(() -> consumer.accept(ioe), co::close);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final IOException cause = e.getCause();
|
||||
assertSame(ioe, cause);
|
||||
}
|
||||
assertTrue(co.isClosed());
|
||||
co.reset();
|
||||
Functions.tryWithResources(() -> consumer.accept(null), co::close);
|
||||
assertTrue(co.isClosed());
|
||||
}
|
||||
@Test
|
||||
public void testTryWithResources() {
|
||||
final CloseableObject co = new CloseableObject();
|
||||
final FailableConsumer<Throwable, ? extends Throwable> consumer = co::run;
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
try {
|
||||
Functions.tryWithResources(() -> consumer.accept(ise), co::close);
|
||||
fail("Expected Exception");
|
||||
} catch (IllegalStateException e) {
|
||||
assertSame(ise, e);
|
||||
}
|
||||
assertTrue(co.isClosed());
|
||||
co.reset();
|
||||
final Error error = new OutOfMemoryError();
|
||||
try {
|
||||
Functions.tryWithResources(() -> consumer.accept(error), co::close);
|
||||
fail("Expected Exception");
|
||||
} catch (OutOfMemoryError e) {
|
||||
assertSame(error, e);
|
||||
}
|
||||
assertTrue(co.isClosed());
|
||||
co.reset();
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
try {
|
||||
Functions.tryWithResources(() -> consumer.accept(ioe), co::close);
|
||||
fail("Expected Exception");
|
||||
} catch (UncheckedIOException e) {
|
||||
final IOException cause = e.getCause();
|
||||
assertSame(ioe, cause);
|
||||
}
|
||||
assertTrue(co.isClosed());
|
||||
co.reset();
|
||||
Functions.tryWithResources(() -> consumer.accept(null), co::close);
|
||||
assertTrue(co.isClosed());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue