Skip to content

Commit

Permalink
Merge pull request #112 from adrienlauer/fix-111-missing-throw
Browse files Browse the repository at this point in the history
Fix missing throw statements
  • Loading branch information
adrienlauer committed Sep 4, 2015
2 parents f8b024b + e72e509 commit 79d8d3e
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private void injectCommandLineHandler(CommandLineHandler commandLineHandler) {

if (value != null) {
if (cliOption.valueCount() != -1 && cliOption.valueCount() != value.length) {
SeedException.createNew(CliErrorCode.WRONG_NUMBER_OF_OPTION_ARGUMENTS).put("command", cliCommand);
throw SeedException.createNew(CliErrorCode.WRONG_NUMBER_OF_OPTION_ARGUMENTS).put("command", cliCommand);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,15 @@ public static Map<Key<?>, Class<?>> resolveBindingDefinitions(Class<?> injecteeC
parentTypeLiteral = TypeLiteral.get(resolvedType);
}
Annotation annotation = SeedReflectionUtils.getAnnotationMetaAnnotatedFromAncestor(subClass, Qualifier.class);
Key<?> key = null;
Key<?> key;
if (annotation != null) {
key = Key.get(parentTypeLiteral, annotation);
} else {
key = Key.get(parentTypeLiteral);
}
if (typeLiterals.containsKey(key)) {
SeedException.createNew(CoreUtilsErrorCode.DUPLICATED_KEYS_FOUND).put("duplicatedKey", key)
.put("firstClass", subClass.getName()).put("secondClass", typeLiterals.get(key).getName())
.thenThrows();
throw SeedException.createNew(CoreUtilsErrorCode.DUPLICATED_KEYS_FOUND).put("duplicatedKey", key)
.put("firstClass", subClass.getName()).put("secondClass", typeLiterals.get(key).getName());
}
typeLiterals.put(key, subClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,102 +19,86 @@
public class SeedExceptionTest {


static enum TotoErrorCode implements ErrorCode
{
JOKE_MODE , CARAMBAR_MODE ;
}

static class TotoException extends SeedException
{
private static final long serialVersionUID = 1L;

public TotoException(ErrorCode errorCode) {
super(errorCode);
}

public TotoException(ErrorCode errorCode, Throwable throwable) {
super(errorCode, throwable);
}
}

@Test(expected=TotoException.class)
public void testCreateNewTotoException1()
{
TotoException te = SeedException.createNew(TotoException.class,TotoErrorCode.CARAMBAR_MODE)
.put("key1", "value1")
.put("key2", "value2");
throw te;
}

@Test(expected=SeedException.class)
public void wrap_Should_Work_Fine ()
{
try
{
throw new NullPointerException();
}
catch (Exception exception)
{
SeedException.wrap(exception, TotoErrorCode.JOKE_MODE)
.put("Error Code", "this is how we do it !")
.thenThrows();
}
}

@Test(expected = TotoException.class)
public void wrap_Should_Work_Fine_WIth_Descendant ()
{
try
{
throw new NullPointerException();
}
catch (Exception exception)
{
SeedException.wrap ( TotoException.class , exception, TotoErrorCode.JOKE_MODE )
.put("Error Code", "this is how we do it !")
.thenThrows();
}
}

@Test(expected = SeedException.class)
public void wrap_Should_Work_Fine_WIth_Change_Of_ErrorCode(){
try{
throw new TotoException(TotoErrorCode.JOKE_MODE);
}catch (TotoException e) {
SeedException.wrap(TotoException.class, e, TotoErrorCode.CARAMBAR_MODE).thenThrows();
}
}

@Test(expected=SeedException.class)
public void wrap_Should_Work_Fine_1 () {
try{
throw new TotoException(TotoErrorCode.JOKE_MODE);
}catch (TotoException exception){
SeedException.wrap(exception,TotoErrorCode.JOKE_MODE)
.put("Error Code", "this is how we do it !")
.thenThrows();
}
}

@Test(expected=SeedException.class)
public void wrap_Should_Work_Fine_2 () {
try{
throw new TotoException(TotoErrorCode.JOKE_MODE);
}catch (TotoException exception){
SeedException.wrap(exception,TotoErrorCode.CARAMBAR_MODE)
.put("Error Code", "this is how we do it !")
.thenThrows();
}
}

@Test
public void multiple_causes_should_be_visible() throws Exception {
StringWriter stringWriter = new StringWriter();
SeedException.wrap(SeedException.wrap(new RuntimeException("yop"), TotoErrorCode.CARAMBAR_MODE), TotoErrorCode.JOKE_MODE).printStackTrace(new PrintWriter(stringWriter));
String text = stringWriter.toString();

assertThat(text).contains("Caused by: java.lang.RuntimeException: yop");
assertThat(text).contains("Caused by: org.seedstack.seed.core.api.SeedException: (TOTO) Carambar mode");
assertThat(text).contains("org.seedstack.seed.core.api.SeedException: (TOTO) Joke mode");
}
static enum TotoErrorCode implements ErrorCode {
JOKE_MODE, CARAMBAR_MODE;
}

static class TotoException extends SeedException {
private static final long serialVersionUID = 1L;

public TotoException(ErrorCode errorCode) {
super(errorCode);
}

public TotoException(ErrorCode errorCode, Throwable throwable) {
super(errorCode, throwable);
}
}

@Test(expected = TotoException.class)
public void testCreateNewTotoException1() {
throw SeedException.createNew(TotoException.class, TotoErrorCode.CARAMBAR_MODE)
.put("key1", "value1")
.<TotoException>put("key2", "value2");
}

@Test(expected = SeedException.class)
public void wrap_Should_Work_Fine() {
try {
throw new NullPointerException();
} catch (Exception exception) {
throw SeedException.wrap(exception, TotoErrorCode.JOKE_MODE)
.put("Error Code", "this is how we do it !");
}
}

@Test(expected = TotoException.class)
public void wrap_Should_Work_Fine_WIth_Descendant() {
try {
throw new NullPointerException();
} catch (Exception exception) {
throw SeedException.wrap(TotoException.class, exception, TotoErrorCode.JOKE_MODE)
.put("Error Code", "this is how we do it !");
}
}

@Test(expected = SeedException.class)
public void wrap_Should_Work_Fine_WIth_Change_Of_ErrorCode() {
try {
throw new TotoException(TotoErrorCode.JOKE_MODE);
} catch (TotoException e) {
throw SeedException.wrap(TotoException.class, e, TotoErrorCode.CARAMBAR_MODE);
}
}

@Test(expected = SeedException.class)
public void wrap_Should_Work_Fine_1() {
try {
throw new TotoException(TotoErrorCode.JOKE_MODE);
} catch (TotoException exception) {
throw SeedException.wrap(exception, TotoErrorCode.JOKE_MODE)
.put("Error Code", "this is how we do it !");
}
}

@Test(expected = SeedException.class)
public void wrap_Should_Work_Fine_2() {
try {
throw new TotoException(TotoErrorCode.JOKE_MODE);
} catch (TotoException exception) {
throw SeedException.wrap(exception, TotoErrorCode.CARAMBAR_MODE)
.put("Error Code", "this is how we do it !");
}
}

@Test
public void multiple_causes_should_be_visible() throws Exception {
StringWriter stringWriter = new StringWriter();
SeedException.wrap(SeedException.wrap(new RuntimeException("yop"), TotoErrorCode.CARAMBAR_MODE), TotoErrorCode.JOKE_MODE).printStackTrace(new PrintWriter(stringWriter));
String text = stringWriter.toString();

assertThat(text).contains("Caused by: java.lang.RuntimeException: yop");
assertThat(text).contains("Caused by: org.seedstack.seed.core.api.SeedException: (TOTO) Carambar mode");
assertThat(text).contains("org.seedstack.seed.core.api.SeedException: (TOTO) Joke mode");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private void initKernel() {
try {
beforeKernelMethod.invokeExplosively(null);
} catch (Throwable throwable) {
SeedException.wrap(throwable, ITErrorCode.EXCEPTION_OCCURRED_BEFORE_KERNEL);
throw SeedException.wrap(throwable, ITErrorCode.EXCEPTION_OCCURRED_BEFORE_KERNEL);
}
}

Expand Down Expand Up @@ -348,15 +348,22 @@ private void stopKernel() {
if (kernel != null) {
kernel.stop();
}
} catch (Exception e) {
try {
processException(e);
} catch (Exception e2) {
throw SeedException.wrap(e2, ITErrorCode.FAILED_TO_STOP_KERNEL);
}
} finally {
List<FrameworkMethod> beforeKernelMethods = getTestClass().getAnnotatedMethods(AfterKernel.class);
for (FrameworkMethod beforeKernelMethod : beforeKernelMethods) {
List<FrameworkMethod> afterKernelMethods = getTestClass().getAnnotatedMethods(AfterKernel.class);
for (FrameworkMethod afterKernelMethod : afterKernelMethods) {
try {
beforeKernelMethod.invokeExplosively(null);
} catch (Throwable throwable) {
SeedException.wrap(throwable, ITErrorCode.EXCEPTION_OCCURRED_AFTER_KERNEL);
afterKernelMethod.invokeExplosively(null);
} catch (Throwable t) {
throw SeedException.wrap(t, ITErrorCode.EXCEPTION_OCCURRED_AFTER_KERNEL);
}
}

kernel = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public enum ITErrorCode implements ErrorCode {
UNEXPECTED_EXCEPTION_OCCURRED,
EXPECTED_EXCEPTION_DID_NOT_OCCURRED,
ANOTHER_EXCEPTION_THAN_EXPECTED_OCCURRED,
FAILED_TO_INITIALIZE_KERNEL
FAILED_TO_STOP_KERNEL, FAILED_TO_INITIALIZE_KERNEL
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void refresh(Session session) {
messageConsumer.setMessageListener(messageListener);
}
} catch (JMSException e) {
SeedException.wrap(e, JmsErrorCodes.INITIALIZATION_EXCEPTION);
throw SeedException.wrap(e, JmsErrorCodes.INITIALIZATION_EXCEPTION);
} finally {
messageConsumerLock.writeLock().unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private ElasticSearchClientProxy(Client client) {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // NOSONAR
if (METHOD_CLOSE.contentEquals(method.getName())) {
SeedException.createNew(ElasticSearchErrorCode.FORBIDDEN_CLIENT_CLOSE).thenThrows();
throw SeedException.createNew(ElasticSearchErrorCode.FORBIDDEN_CLIENT_CLOSE);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private Client buildRemoteClient(Settings settings, String[] hosts) {
for (String host : hosts) {
String[] hostInfo = host.split(":");
if (hostInfo.length > 2) {
SeedException.createNew(ElasticSearchErrorCode.INVALID_HOST).put("host", host).thenThrows();
throw SeedException.createNew(ElasticSearchErrorCode.INVALID_HOST).put("host", host);
}
String address = hostInfo[0].trim();
int port = DEFAULT_ELASTIC_SEARCH_PORT;
Expand All @@ -162,7 +162,7 @@ private Client buildRemoteClient(Settings settings, String[] hosts) {
port = Integer.valueOf(hostInfo[1]);
}
} catch (NumberFormatException e) {
SeedException.wrap(e, ElasticSearchErrorCode.CLIENT_INVALID_PORT).put("host", hostInfo[0]).thenThrows();
throw SeedException.wrap(e, ElasticSearchErrorCode.CLIENT_INVALID_PORT).put("host", hostInfo[0]);
}

transportClient.addTransportAddress(new InetSocketTransportAddress(address, port));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void doCommitTransaction(SolrClient currentTransaction) {
try {
solrClientLink.pop().commit();
} catch (Exception e) {
SeedException.wrap(e, SolrErrorCodes.UNABLE_TO_COMMIT);
throw SeedException.wrap(e, SolrErrorCodes.UNABLE_TO_COMMIT);
}
}

Expand All @@ -58,7 +58,7 @@ public void doRollbackTransaction(SolrClient currentTransaction) {
try {
solrClientLink.pop().rollback();
} catch (Exception e) {
SeedException.wrap(e, SolrErrorCodes.UNABLE_TO_ROLLBACK);
throw SeedException.wrap(e, SolrErrorCodes.UNABLE_TO_ROLLBACK);
}
}

Expand Down

0 comments on commit 79d8d3e

Please sign in to comment.