mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-26 01:58:34 +00:00
(NEW) Module "retry"
(NEW) Illustrative classes:
- App: simulates a production application
- BusinessOperation<T>: abstraction over any operation that can
potentially fail
- FindCustomer <: BusinessOperation<String>: illustrative
operation that can throw an error
- Retry <: BusinessOperation<T>: transparently implements the
retry mechanism
- Several "business" exceptions:
- BusinessException: top-level
- CustomerNotFoundException: can be ignored
- DatabaseNotAvailableException: fatal error
(NEW) .puml and .png for UML
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* 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.iluwatar.retry;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link FindCustomer}.
|
||||
*
|
||||
* @author George Aristy (george.aristy@gmail.com)
|
||||
* @since 1.17.0
|
||||
*/
|
||||
public class FindCustomerTest {
|
||||
/**
|
||||
* Returns the given result with no exceptions.
|
||||
*
|
||||
* @since 1.17.0
|
||||
*/
|
||||
@Test
|
||||
public void noExceptions() throws Exception {
|
||||
assertThat(
|
||||
new FindCustomer("123").perform(),
|
||||
is("123")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws the given exception.
|
||||
*
|
||||
* @throws Exception the expected exception
|
||||
* @since 1.17.0
|
||||
*/
|
||||
@Test(expected = BusinessException.class)
|
||||
public void oneException() throws Exception {
|
||||
new FindCustomer("123", new BusinessException("test")).perform();
|
||||
}
|
||||
|
||||
/**
|
||||
* Should first throw the given exceptions, then return the given result.
|
||||
*
|
||||
* @throws Exception not an expected exception
|
||||
* @since 1.17.0
|
||||
*/
|
||||
@Test
|
||||
public void resultAfterExceptions() throws Exception {
|
||||
final BusinessOperation<String> op = new FindCustomer(
|
||||
"123",
|
||||
new CustomerNotFoundException("not found"),
|
||||
new DatabaseNotAvailableException("not available")
|
||||
);
|
||||
try {
|
||||
op.perform();
|
||||
} catch (CustomerNotFoundException e) {
|
||||
//ignore
|
||||
}
|
||||
try {
|
||||
op.perform();
|
||||
} catch (DatabaseNotAvailableException e) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
op.perform(),
|
||||
is("123")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* 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.iluwatar.retry;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Retry}.
|
||||
*
|
||||
* @author George Aristy (george.aristy@gmail.com)
|
||||
* @since 1.17.0
|
||||
*/
|
||||
public class RetryTest {
|
||||
/**
|
||||
* Should contain all errors thrown.
|
||||
*
|
||||
* @since 1.17.0
|
||||
*/
|
||||
@Test
|
||||
public void errors() throws Exception {
|
||||
final BusinessException e = new BusinessException("unhandled");
|
||||
final Retry<String> retry = new Retry<>(
|
||||
() -> { throw e; },
|
||||
2,
|
||||
0
|
||||
);
|
||||
try {
|
||||
retry.perform();
|
||||
} catch (BusinessException ex) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.errors(),
|
||||
hasItem(e)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* No exceptions will be ignored, hence final number of attempts should be 1 even if we're asking
|
||||
* it to attempt twice.
|
||||
*
|
||||
* @since 1.17.0
|
||||
*/
|
||||
@Test
|
||||
public void attempts() {
|
||||
final BusinessException e = new BusinessException("unhandled");
|
||||
final Retry<String> retry = new Retry<>(
|
||||
() -> { throw e; },
|
||||
2,
|
||||
0
|
||||
);
|
||||
try {
|
||||
retry.perform();
|
||||
} catch (BusinessException ex) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.attempts(),
|
||||
is(1)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Final number of attempts should be equal to the number of attempts asked because we are
|
||||
* asking it to ignore the exception that will be thrown.
|
||||
*
|
||||
* @since 1.17.0
|
||||
*/
|
||||
@Test
|
||||
public void ignore() throws Exception {
|
||||
final BusinessException e = new CustomerNotFoundException("customer not found");
|
||||
final Retry<String> retry = new Retry<>(
|
||||
() -> { throw e; },
|
||||
2,
|
||||
0,
|
||||
ex -> CustomerNotFoundException.class.isAssignableFrom(ex.getClass())
|
||||
);
|
||||
try {
|
||||
retry.perform();
|
||||
} catch (BusinessException ex) {
|
||||
//ignore
|
||||
}
|
||||
|
||||
assertThat(
|
||||
retry.attempts(),
|
||||
is(2)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user