Skip to content

Commit 669a969

Browse files
authored
Merge pull request #67 from powerunit/extensions/override-default-mapper
Support to change the default exception mapping and fixes #66
2 parents b804927 + 7041453 commit 669a969

File tree

14 files changed

+512
-4
lines changed

14 files changed

+512
-4
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ Two versions of this methods exists :
150150

151151
### Exception Mapper
152152

153-
_Since the version 2.0.0, the method `exceptionMapperFor` from interface `ExceptionHandlerSupport` is deprecated in favor of the new one `forException` from interface `ExceptionMapper`._
154-
155-
The various methods `forExceptions` provides a way to chain several Exception Mapper.
153+
The various methods `forExceptions` from `ExceptionMapper` provides a way to chain several Exception Mapper.
156154

157155
Also, some dedicated, _ready to used_, Exception Mapper are provided :
158156

@@ -161,6 +159,43 @@ Also, some dedicated, _ready to used_, Exception Mapper are provided :
161159
* `saxExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the SAX Error from the underlying exception. **This is only usable when the module java.xml is available**.
162160
* `transformerExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the Transformer Error from the underlying exception. **This is only usable when the module java.xml is available**.
163161

162+
#### Define global ExceptionMapper
163+
164+
_Since version 2.2.0, it is possible to define a default exception mappers by using service loader._
165+
166+
By default, the exception are wrapped in a `WrappedException`. This behaviour may be change by implementing the required `ExceptionMapper` and register them as _service implementation_.
167+
168+
To do so, create all the required implementation, for example :
169+
170+
```java
171+
public class MyExceptionMapper implements ch.powerunit.extensions.exceptions.ExceptionMapper {
172+
public RuntimeException apply(Exception e) {
173+
//Add code here
174+
}
175+
176+
public Class<? extends Exception> targetException() {
177+
return //Add code here;
178+
}
179+
180+
// Optional, to define the order between the ExceptionMapper
181+
public int order() {
182+
return 100;
183+
}
184+
185+
}
186+
```
187+
188+
Then this may be registered in the `module-info.java` file:
189+
190+
```java
191+
module XXX {
192+
requires powerunit.exceptions;
193+
194+
provides ch.powerunit.extensions.exceptions.ExceptionMapper
195+
with ....MyExceptionMapper;
196+
}
197+
```
198+
164199
### CommonsCollections4Helper
165200

166201
_This helper is only available since version 2.2.0 and only if the commons-collections4 library is available_
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
invoker.java.version=9
2+
invoker.goals.1=dependency:resolve
3+
invoker.goals.2=dependency:resolve-plugins
4+
invoker.goals.3=dependency:go-offline
5+
invoker.goals.4=clean install

src/it/module-override/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>ch.powerunit.demo</groupId>
6+
<artifactId>override</artifactId>
7+
<version>@project.version@ </version>
8+
<name>Powerunit - Java Testing framework for JDK 1.8 - HelloWorld Maven</name>
9+
<packaging>jar</packaging>
10+
11+
<properties>
12+
<maven.compiler.source>9</maven.compiler.source>
13+
<maven.compiler.target>9</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<jaxb-api.version>2.3.0.1</jaxb-api.version>
16+
</properties>
17+
18+
<build>
19+
<pluginManagement>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.8.1</version>
25+
</plugin>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-dependency-plugin</artifactId>
29+
<version>3.1.1</version>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.codehaus.mojo</groupId>
33+
<artifactId>exec-maven-plugin</artifactId>
34+
<version>1.6.0</version>
35+
</plugin>
36+
</plugins>
37+
</pluginManagement>
38+
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-jar-plugin</artifactId>
43+
<configuration>
44+
<archive>
45+
<manifest>
46+
<mainClass>ch.powerunittest.samples.FunctionSamplesTest</mainClass>
47+
</manifest>
48+
</archive>
49+
</configuration>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.codehaus.mojo</groupId>
53+
<artifactId>exec-maven-plugin</artifactId>
54+
<executions>
55+
<execution>
56+
<id>Test main 1</id>
57+
<goals>
58+
<goal>exec</goal>
59+
</goals>
60+
<phase>package</phase>
61+
<configuration>
62+
<executable>java</executable>
63+
<longModulepath>false</longModulepath>
64+
<arguments>
65+
<argument>--module-path</argument>
66+
<argument>${project.build.directory}/../../../local-repo/ch/powerunit/extensions/powerunit-extensions-exceptions/@project.version@/powerunit-extensions-exceptions-@project.version@.jar${path.separator}${project.build.directory}/${project.build.finalName}.jar</argument>
67+
<argument>--module</argument>
68+
<argument>powerunit.test/ch.powerunittest.samples.ModuleTest</argument>
69+
<argument>NO</argument>
70+
</arguments>
71+
</configuration>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
</plugins>
76+
77+
</build>
78+
79+
<dependencies>
80+
<dependency>
81+
<groupId>ch.powerunit.extensions</groupId>
82+
<artifactId>powerunit-extensions-exceptions</artifactId>
83+
<version>@project.version@</version>
84+
</dependency>
85+
</dependencies>
86+
87+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Powerunit - A JDK1.8 test framework
3+
* Copyright (C) 2014 Mathieu Boretti.
4+
*
5+
* This file is part of Powerunit
6+
*
7+
* Powerunit is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Powerunit is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package ch.powerunittest.samples;
21+
22+
import java.util.function.Function;
23+
24+
import java.io.IOException;
25+
26+
import ch.powerunit.extensions.exceptions.ExceptionHandlerSupport;
27+
import ch.powerunit.extensions.exceptions.ExceptionMapper;
28+
import ch.powerunit.extensions.exceptions.FunctionWithException;
29+
import ch.powerunit.extensions.exceptions.WrappedException;
30+
31+
public class ModuleTest {
32+
33+
public static void sample1() {
34+
35+
FunctionWithException<String, String, IOException> fonctionThrowingException = x -> x;
36+
37+
Function<String, String> functionThrowingRuntimeException = FunctionWithException
38+
.unchecked(fonctionThrowingException);
39+
40+
if (!"x".equals(functionThrowingRuntimeException.apply("x"))) {
41+
throw new IllegalArgumentException("The result is not correct");
42+
}
43+
44+
}
45+
46+
public static void sample2() {
47+
48+
FunctionWithException<String, String, Exception> fonctionThrowingException = FunctionWithException
49+
.failing(IllegalArgumentException::new);
50+
51+
Function<String, String> functionThrowingRuntimeException = FunctionWithException
52+
.unchecked(fonctionThrowingException);
53+
54+
try {
55+
functionThrowingRuntimeException.apply("x");
56+
} catch (UnsupportedOperationException e) {
57+
//OK
58+
59+
return;
60+
}
61+
throw new IllegalArgumentException("Expecting an exception, but none or the wrong has been thrown");
62+
}
63+
64+
public static void sample3() {
65+
66+
FunctionWithException<String, String, Exception> fonctionThrowingException = FunctionWithException
67+
.failing(RuntimeException::new);
68+
69+
Function<String, String> functionThrowingRuntimeException = FunctionWithException
70+
.unchecked(fonctionThrowingException);
71+
72+
try {
73+
functionThrowingRuntimeException.apply("x");
74+
} catch (IllegalArgumentException e) {
75+
//OK
76+
77+
return;
78+
}
79+
throw new IllegalArgumentException("Expecting an exception, but none or the wrong has been thrown");
80+
}
81+
82+
public static void sample4() {
83+
84+
FunctionWithException<String, String, Exception> fonctionThrowingException = FunctionWithException
85+
.failing(IOException::new);
86+
87+
Function<String, String> functionThrowingRuntimeException = FunctionWithException
88+
.unchecked(fonctionThrowingException);
89+
90+
try {
91+
functionThrowingRuntimeException.apply("x");
92+
} catch (WrappedException e) {
93+
//OK
94+
95+
return;
96+
}
97+
throw new IllegalArgumentException("Expecting an exception, but none or the wrong has been thrown");
98+
}
99+
100+
public static void main(String[] args) {
101+
sample1();
102+
sample2();
103+
sample3();
104+
sample4();
105+
}
106+
107+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Powerunit - A JDK1.8 test framework
3+
* Copyright (C) 2014 Mathieu Boretti.
4+
*
5+
* This file is part of Powerunit
6+
*
7+
* Powerunit is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Powerunit is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package ch.powerunittest.samples;
21+
22+
import java.util.function.Function;
23+
24+
import java.io.IOException;
25+
26+
import ch.powerunit.extensions.exceptions.ExceptionHandlerSupport;
27+
import ch.powerunit.extensions.exceptions.ExceptionMapper;
28+
import ch.powerunit.extensions.exceptions.FunctionWithException;
29+
import ch.powerunit.extensions.exceptions.WrappedException;
30+
31+
public class MyExceptionMapper implements ch.powerunit.extensions.exceptions.ExceptionMapper {
32+
public RuntimeException apply(Exception e) {
33+
return new UnsupportedOperationException(e);
34+
}
35+
36+
public Class<? extends Exception> targetException() {
37+
return IllegalArgumentException.class;
38+
}
39+
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Powerunit - A JDK1.8 test framework
3+
* Copyright (C) 2014 Mathieu Boretti.
4+
*
5+
* This file is part of Powerunit
6+
*
7+
* Powerunit is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Powerunit is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package ch.powerunittest.samples;
21+
22+
import java.util.function.Function;
23+
24+
import java.io.IOException;
25+
26+
import ch.powerunit.extensions.exceptions.ExceptionHandlerSupport;
27+
import ch.powerunit.extensions.exceptions.ExceptionMapper;
28+
import ch.powerunit.extensions.exceptions.FunctionWithException;
29+
import ch.powerunit.extensions.exceptions.WrappedException;
30+
31+
public class MyExceptionMapper2 implements ch.powerunit.extensions.exceptions.ExceptionMapper {
32+
public RuntimeException apply(Exception e) {
33+
return new IllegalArgumentException(e);
34+
}
35+
36+
public Class<? extends Exception> targetException() {
37+
return RuntimeException.class;
38+
}
39+
40+
public int order() {
41+
return 100;
42+
}
43+
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module powerunit.test {
2+
requires powerunit.exceptions;
3+
4+
provides ch.powerunit.extensions.exceptions.ExceptionMapper
5+
with ch.powerunittest.samples.MyExceptionMapper, ch.powerunittest.samples.MyExceptionMapper2;
6+
}

src/it/module-override/verify.bsh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import java.io.*;

src/main/java/ch/powerunit/extensions/exceptions/Constants.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
import static ch.powerunit.extensions.exceptions.ExceptionMapper.forException;
2323
import static ch.powerunit.extensions.exceptions.SupplierWithException.ignored;
2424
import static java.util.Objects.requireNonNull;
25+
import static java.util.stream.Collectors.toList;
2526

2627
import java.sql.SQLException;
28+
import java.util.ServiceLoader;
29+
import java.util.ServiceLoader.Provider;
30+
import java.util.function.Function;
2731

2832
import javax.xml.transform.TransformerException;
2933

@@ -44,6 +48,8 @@ final class Constants {
4448
public static final ExceptionMapper TRANSFORMEREXCEPTION_EXCEPTION_MAPPER = ignored(
4549
Constants::buildTransformerExceptionMapper).get();
4650

51+
public static final Function<Exception, RuntimeException> MAPPERS = computeDefaultMapper();
52+
4753
public static <T> T verifyOperation(T obj) {
4854
return requireNonNull(obj, "operation can't be null");
4955
}
@@ -93,6 +99,11 @@ private static ExceptionMapper buildTransformerExceptionMapper() throws ClassNot
9399
e -> new WrappedException(String.format("%s", ((TransformerException) e).getMessageAndLocation()), e));
94100
}
95101

102+
private static Function<Exception, RuntimeException> computeDefaultMapper() {
103+
return ExceptionMapper.forOrderedExceptions(
104+
ServiceLoader.load(ExceptionMapper.class).stream().map(Provider::get).collect(toList()));
105+
}
106+
96107
private Constants() {
97108
}
98109

0 commit comments

Comments
 (0)