Skip to content

Commit 093029b

Browse files
author
spg
committed
Merge pull request #340 from ArcBees/spg_fixed_action_exception
Custom ActionException subclasses are not rethrown as new ActionException
2 parents cbfd802 + f51282c commit 093029b

File tree

10 files changed

+360
-13
lines changed

10 files changed

+360
-13
lines changed

gwtp-core/gwtp-dispatch-server-guice/pom.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<groupId>com.gwtplatform</groupId>
3535
<artifactId>gwtp-dispatch-test</artifactId>
3636
<scope>test</scope>
37-
</dependency>
38-
</dependencies>
39-
</project>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.jukito</groupId>
40+
<artifactId>jukito</artifactId>
41+
</dependency>
42+
</dependencies>
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import com.gwtplatform.dispatch.shared.ActionException;
20+
21+
public class ActionExceptionThrownByHandler extends ActionException {
22+
public ActionExceptionThrownByHandler(Throwable cause) {
23+
super("", cause);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import com.gwtplatform.dispatch.shared.ActionException;
20+
21+
public class ActionExceptionThrownByValidator extends ActionException {
22+
public ActionExceptionThrownByValidator(Throwable cause) {
23+
super("", cause);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import javax.inject.Inject;
20+
21+
import org.jukito.JukitoModule;
22+
import org.jukito.JukitoRunner;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
26+
import com.gwtplatform.dispatch.server.guice.DispatchServiceImpl;
27+
import com.gwtplatform.dispatch.server.guice.actionvalidator.DefaultActionValidator;
28+
import com.gwtplatform.dispatch.shared.ActionException;
29+
import com.gwtplatform.dispatch.shared.ServiceException;
30+
31+
import static org.hamcrest.CoreMatchers.instanceOf;
32+
import static org.hamcrest.MatcherAssert.assertThat;
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.fail;
35+
36+
@RunWith(JukitoRunner.class)
37+
public class ActionThrownByHandlerTest {
38+
public static class MyModule extends JukitoModule {
39+
@Override
40+
protected void configureTest() {
41+
install(new ServiceModule(DefaultActionValidator.class));
42+
}
43+
}
44+
45+
@Inject
46+
DispatchServiceImpl service;
47+
48+
@Test
49+
public void exceptionThrownByHandlerIsNotWrappedInActionException() throws ServiceException {
50+
try {
51+
service.execute("", new SomeAction());
52+
fail();
53+
} catch (ActionException e) {
54+
assertThat(e, instanceOf(ActionExceptionThrownByHandler.class));
55+
assertEquals(0, e.getStackTrace().length);
56+
assertEquals(0, e.getCause().getStackTrace().length);
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import javax.inject.Inject;
20+
21+
import org.jukito.JukitoModule;
22+
import org.jukito.JukitoRunner;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
26+
import com.gwtplatform.dispatch.server.guice.DispatchServiceImpl;
27+
import com.gwtplatform.dispatch.shared.ActionException;
28+
import com.gwtplatform.dispatch.shared.ServiceException;
29+
30+
import static org.hamcrest.CoreMatchers.instanceOf;
31+
import static org.hamcrest.MatcherAssert.assertThat;
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.fail;
34+
35+
@RunWith(JukitoRunner.class)
36+
public class ActionThrownByValidatorTest {
37+
public static class MyModule extends JukitoModule {
38+
@Override
39+
protected void configureTest() {
40+
install(new ServiceModule(ActionValidatorThatThrows.class));
41+
}
42+
}
43+
44+
@Inject
45+
DispatchServiceImpl service;
46+
47+
@Test
48+
public void exceptionThrownByValidatorIsNotWrappedInActionException() throws ServiceException {
49+
try {
50+
service.execute("", new SomeAction());
51+
fail();
52+
} catch (ActionException e) {
53+
assertThat(e, instanceOf(ActionExceptionThrownByValidator.class));
54+
assertEquals(0, e.getStackTrace().length);
55+
assertEquals(0, e.getCause().getStackTrace().length);
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator;
20+
import com.gwtplatform.dispatch.shared.Action;
21+
import com.gwtplatform.dispatch.shared.ActionException;
22+
import com.gwtplatform.dispatch.shared.Result;
23+
24+
public class ActionValidatorThatThrows implements ActionValidator {
25+
@Override
26+
public boolean isValid(Action<? extends Result> action) throws ActionException {
27+
throw new ActionExceptionThrownByValidator(new Exception());
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import javax.inject.Inject;
20+
21+
import com.gwtplatform.dispatch.server.actionhandler.AbstractActionHandler;
22+
import com.gwtplatform.dispatch.shared.ActionException;
23+
import com.gwtplatform.dispatch.shared.NoResult;
24+
25+
public class HandlerThatThrowsActionException extends AbstractActionHandler<SomeAction, NoResult> {
26+
@Inject
27+
HandlerThatThrowsActionException() {
28+
super(SomeAction.class);
29+
}
30+
31+
@Override
32+
public NoResult execute(SomeAction action, ExecutionContext context) throws ActionException {
33+
throw new ActionExceptionThrownByHandler(new Exception());
34+
}
35+
36+
@Override
37+
public void undo(SomeAction action, NoResult result, ExecutionContext context) throws ActionException {
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import com.google.inject.AbstractModule;
20+
import com.google.inject.Singleton;
21+
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
22+
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorClass;
23+
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorMap;
24+
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorMapImpl;
25+
import com.gwtplatform.dispatch.server.actionhandlervalidator.ActionHandlerValidatorRegistry;
26+
import com.gwtplatform.dispatch.server.actionvalidator.ActionValidator;
27+
import com.gwtplatform.dispatch.server.guice.DispatchImpl;
28+
import com.gwtplatform.dispatch.server.guice.actionhandlervalidator.ActionHandlerValidatorLinker;
29+
import com.gwtplatform.dispatch.server.guice.actionhandlervalidator.LazyActionHandlerValidatorRegistryImpl;
30+
import com.gwtplatform.dispatch.shared.Action;
31+
import com.gwtplatform.dispatch.shared.Result;
32+
33+
public class ServiceModule extends AbstractModule {
34+
private final Class<? extends ActionValidator> actionValidator;
35+
36+
public ServiceModule(Class<? extends ActionValidator> actionValidator) {
37+
this.actionValidator = actionValidator;
38+
}
39+
40+
@Override
41+
protected void configure() {
42+
bind(Dispatch.class).to(DispatchImpl.class);
43+
bind(ActionHandlerValidatorRegistry.class).to(
44+
LazyActionHandlerValidatorRegistryImpl.class).in(Singleton.class);
45+
46+
bindHandler(SomeAction.class, HandlerThatThrowsActionException.class, actionValidator);
47+
requestStaticInjection(ActionHandlerValidatorLinker.class);
48+
}
49+
50+
protected <A extends Action<R>, R extends Result> void bindHandler(
51+
Class<A> actionClass, Class<? extends ActionHandler<A, R>> handlerClass,
52+
Class<? extends ActionValidator> actionValidator) {
53+
54+
bind(ActionHandlerValidatorMap.class).toInstance(
55+
new ActionHandlerValidatorMapImpl<A, R>(actionClass,
56+
new ActionHandlerValidatorClass<A, R>(handlerClass, actionValidator)));
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2011 ArcBees Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.gwtplatform.dispatch.server;
18+
19+
import com.gwtplatform.dispatch.shared.Action;
20+
import com.gwtplatform.dispatch.shared.NoResult;
21+
22+
public class SomeAction implements Action<NoResult> {
23+
@Override
24+
public String getServiceName() {
25+
return null;
26+
}
27+
28+
@Override
29+
public boolean isSecured() {
30+
return false;
31+
}
32+
}

0 commit comments

Comments
 (0)