Skip to content

Commit

Permalink
Merge pull request #340 from ArcBees/spg_fixed_action_exception
Browse files Browse the repository at this point in the history
Custom ActionException subclasses are not rethrown as new ActionException
  • Loading branch information
spg committed Nov 15, 2013
2 parents f831b3c + b590798 commit 0fb839e
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 13 deletions.
10 changes: 7 additions & 3 deletions gwtp-core/gwtp-dispatch-server-guice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<groupId>com.gwtplatform</groupId>
<artifactId>gwtp-dispatch-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
</dependency>
<dependency>
<groupId>org.jukito</groupId>
<artifactId>jukito</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import com.gwtplatform.dispatch.shared.ActionException;

public class ActionExceptionThrownByHandler extends ActionException {
public ActionExceptionThrownByHandler(Throwable cause) {
super("", cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import com.gwtplatform.dispatch.shared.ActionException;

public class ActionExceptionThrownByValidator extends ActionException {
public ActionExceptionThrownByValidator(Throwable cause) {
super("", cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import javax.inject.Inject;

import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.gwtplatform.dispatch.server.guice.DispatchServiceImpl;
import com.gwtplatform.dispatch.server.guice.actionvalidator.DefaultActionValidator;
import com.gwtplatform.dispatch.shared.ActionException;
import com.gwtplatform.dispatch.shared.ServiceException;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(JukitoRunner.class)
public class ActionThrownByHandlerTest {
public static class MyModule extends JukitoModule {
@Override
protected void configureTest() {
install(new ServiceModule(DefaultActionValidator.class));
}
}

@Inject
DispatchServiceImpl service;

@Test
public void exceptionThrownByHandlerIsNotWrappedInActionException() throws ServiceException {
try {
service.execute("", new SomeAction());
fail();
} catch (ActionException e) {
assertThat(e, instanceOf(ActionExceptionThrownByHandler.class));
assertEquals(0, e.getStackTrace().length);
assertEquals(0, e.getCause().getStackTrace().length);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import javax.inject.Inject;

import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.gwtplatform.dispatch.server.guice.DispatchServiceImpl;
import com.gwtplatform.dispatch.shared.ActionException;
import com.gwtplatform.dispatch.shared.ServiceException;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(JukitoRunner.class)
public class ActionThrownByValidatorTest {
public static class MyModule extends JukitoModule {
@Override
protected void configureTest() {
install(new ServiceModule(ActionValidatorThatThrows.class));
}
}

@Inject
DispatchServiceImpl service;

@Test
public void exceptionThrownByValidatorIsNotWrappedInActionException() throws ServiceException {
try {
service.execute("", new SomeAction());
fail();
} catch (ActionException e) {
assertThat(e, instanceOf(ActionExceptionThrownByValidator.class));
assertEquals(0, e.getStackTrace().length);
assertEquals(0, e.getCause().getStackTrace().length);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator;
import com.gwtplatform.dispatch.shared.Action;
import com.gwtplatform.dispatch.shared.ActionException;
import com.gwtplatform.dispatch.shared.Result;

public class ActionValidatorThatThrows implements ActionValidator {
@Override
public boolean isValid(Action<? extends Result> action) throws ActionException {
throw new ActionExceptionThrownByValidator(new Exception());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import javax.inject.Inject;

import com.gwtplatform.dispatch.server.actionhandler.AbstractActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;
import com.gwtplatform.dispatch.shared.NoResult;

public class HandlerThatThrowsActionException extends AbstractActionHandler<SomeAction, NoResult> {
@Inject
HandlerThatThrowsActionException() {
super(SomeAction.class);
}

@Override
public NoResult execute(SomeAction action, ExecutionContext context) throws ActionException {
throw new ActionExceptionThrownByHandler(new Exception());
}

@Override
public void undo(SomeAction action, NoResult result, ExecutionContext context) throws ActionException {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorClass;
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorMap;
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorMapImpl;
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorRegistry;
import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator;
import com.gwtplatform.dispatch.server.guice.DispatchImpl;
import com.gwtplatform.dispatch.server.guice.actionhandlervalidator.ActionHandlerValidatorLinker;
import com.gwtplatform.dispatch.server.guice.actionhandlervalidator.LazyActionHandlerValidatorRegistryImpl;
import com.gwtplatform.dispatch.shared.Action;
import com.gwtplatform.dispatch.shared.Result;

public class ServiceModule extends AbstractModule {
private final Class<? extends ActionValidator> actionValidator;

public ServiceModule(Class<? extends ActionValidator> actionValidator) {
this.actionValidator = actionValidator;
}

@Override
protected void configure() {
bind(Dispatch.class).to(DispatchImpl.class);
bind(ActionHandlerValidatorRegistry.class).to(
LazyActionHandlerValidatorRegistryImpl.class).in(Singleton.class);

bindHandler(SomeAction.class, HandlerThatThrowsActionException.class, actionValidator);
requestStaticInjection(ActionHandlerValidatorLinker.class);
}

protected <A extends Action<R>, R extends Result> void bindHandler(
Class<A> actionClass, Class<? extends ActionHandler<A, R>> handlerClass,
Class<? extends ActionValidator> actionValidator) {

bind(ActionHandlerValidatorMap.class).toInstance(
new ActionHandlerValidatorMapImpl<A, R>(actionClass,
new ActionHandlerValidatorClass<A, R>(handlerClass, actionValidator)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.gwtplatform.dispatch.server;

import com.gwtplatform.dispatch.shared.Action;
import com.gwtplatform.dispatch.shared.NoResult;

public class SomeAction implements Action<NoResult> {
@Override
public String getServiceName() {
return null;
}

@Override
public boolean isSecured() {
return false;
}
}
Loading

0 comments on commit 0fb839e

Please sign in to comment.