Skip to content

Commit

Permalink
Issue #51 - Add SAXException support
Browse files Browse the repository at this point in the history
  • Loading branch information
boretti committed Sep 1, 2019
1 parent b91a8fb commit 61e6484
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ The various methods `forExceptions` provides a way to chain several Exception Ma
Also, some dedicated, _ready to used_, Exception Mapper are provided :

* `sqlExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the SQL Error from the underlying exception. **This is only usable when the module java.sql is available**.
* `jaxbExceptionMapper()` - Return an exception mapper that adds to the message of the `RuntimeException` the JAXB Error from the underlying exception. **This is only usable when JAXB is available**.
* `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**.

## Reference

Expand Down
56 changes: 29 additions & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down Expand Up @@ -276,23 +276,23 @@

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-java-module-source</id>
<phase>prepare-package</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/module</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-java-module-source</id>
<phase>prepare-package</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/module</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
Expand Down Expand Up @@ -533,15 +533,17 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb-api.version}</version>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>${jaxb-api.version}</version>
<groupId>sax</groupId>
<artifactId>sax</artifactId>
<version>2.0.1</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>
Expand Down
45 changes: 44 additions & 1 deletion src/it/module-simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@
<goal>exec</goal>
</goals>
<phase>package</phase>
<configuration>
<executable>java</executable>
<longModulepath>false</longModulepath>
<arguments>
<argument>--module-path</argument>
<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>
<argument>--module</argument>
<argument>powerunit.test/ch.powerunittest.samples.FunctionSamplesTest</argument>
<argument>NOXML</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>Test main 4</id>
<goals>
<goal>exec</goal>
</goals>
<phase>package</phase>
<configuration>
<executable>java</executable>
<longModulepath>false</longModulepath>
Expand All @@ -109,7 +127,7 @@
</configuration>
</execution>
<execution>
<id>Test main 4</id>
<id>Test main 5</id>
<goals>
<goal>exec</goal>
</goals>
Expand All @@ -128,6 +146,26 @@
</arguments>
</configuration>
</execution>
<execution>
<id>Test main 6</id>
<goals>
<goal>exec</goal>
</goals>
<phase>package</phase>
<configuration>
<executable>java</executable>
<longModulepath>false</longModulepath>
<arguments>
<argument>--module-path</argument>
<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>
<argument>--add-modules</argument>
<argument>java.xml</argument>
<argument>--module</argument>
<argument>powerunit.test/ch.powerunittest.samples.FunctionSamplesTest</argument>
<argument>XML</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand All @@ -145,6 +183,11 @@
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>sax</groupId>
<artifactId>sax</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import javax.xml.bind.JAXBException;

import org.xml.sax.SAXParseException;

import java.io.IOException;
import java.sql.SQLException;

Expand Down Expand Up @@ -115,18 +117,59 @@ public static void sample5() {

}

public static void sample6() {

try {
Function<Exception, RuntimeException> mapper = ExceptionMapper.saxExceptionMapper();
} catch (NoClassDefFoundError e) {
e.printStackTrace();
return;
}
throw new IllegalArgumentException("No exception thrown");
}

public static void sample7() {

Function<Exception, RuntimeException> mapper = ExceptionMapper.saxExceptionMapper();
FunctionWithException<String, String, Exception> fonctionThrowingException = FunctionWithException
.failing(() -> new SAXParseException("msg", "pid", "sid", 1, 2));

Function<String, String> functionThrowingRuntimeException = FunctionWithException
.unchecked(fonctionThrowingException, mapper);

try {
functionThrowingRuntimeException.apply("x");
} catch (WrappedException e) {
e.printStackTrace();
if ("org.xml.sax.SAXParseExceptionpublicId: pid; systemId: sid; lineNumber: 1; columnNumber: 2; msg"
.equals(e.getMessage())) {
return;
}
throw new IllegalArgumentException("Wrong exception : " + e.getMessage(), e);

}
throw new IllegalArgumentException("No exception thrown");

}

public static void main(String[] args) {
sample1();
if (args.length > 0 && "NOSQL".equals(args[0])) {
sample2();
} else if (args.length > 0 && "NOJAXB".equals(args[0])) {
sample4();
} else if (args.length > 0 && "NOXML".equals(args[0])) {
// sample6();
} else if (args.length > 0 && "SQL".equals(args[0])) {
sample3();
sample4();
} else if (args.length > 0 && "JAXB".equals(args[0])) {
sample5();
sample2();
} else if (args.length > 0 && "XML".equals(args[0])) {
sample7();
sample4();
sample2();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/it/module-simple/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
requires powerunit.exceptions;
requires static java.sql;
requires static java.xml.bind;
requires static java.xml;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ final class Constants {
public static final ExceptionMapper JAXBEXCEPTION_EXCEPTION_MAPPER = SupplierWithException
.lifted(Constants::buildJAXBExceptionMapper).get().orElse(null);

public static final ExceptionMapper SAXEXCEPTION_EXCEPTION_MAPPER = SupplierWithException
.lifted(Constants::buildSAXExceptionMapper).get().orElse(null);

@SuppressWarnings("unchecked")
private static ExceptionMapper buildSQLExceptionMapper() throws ClassNotFoundException {
return ExceptionMapper.forException((Class<Exception>) Class.forName("java.sql.SQLException"),
Expand All @@ -58,6 +61,12 @@ private static ExceptionMapper buildJAXBExceptionMapper() throws ClassNotFoundEx
e -> new WrappedException(String.format("%s", e.toString()), e));
}

@SuppressWarnings("unchecked")
private static ExceptionMapper buildSAXExceptionMapper() throws ClassNotFoundException {
return ExceptionMapper.forException((Class<Exception>) Class.forName("org.xml.sax.SAXException"),
e -> new WrappedException(String.format("%s", e.toString()), e));
}

private Constants() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,26 @@ static ExceptionMapper sqlExceptionMapper() {
*/
static ExceptionMapper jaxbExceptionMapper() {
return Optional.ofNullable(Constants.JAXBEXCEPTION_EXCEPTION_MAPPER)
.orElseThrow(() -> new NoClassDefFoundError("Unable to find the jaxbException"));
.orElseThrow(() -> new NoClassDefFoundError("Unable to find the JAXBException"));
}

/**
* Exception wrapper, that may be used to copy sax information to the
* {@code WrappedException} message.
* <p>
* <b>This mapper will only works correctly if the class <i>SAXException</i> is
* available.</b>
* <p>
*
* @return the Mapper for {@code SAXException}.
* @throws NoClassDefFoundError
* In case the {@code SAXException} is not available (java.xml
* module missing).
* @since 2.1.0
*/
static ExceptionMapper saxExceptionMapper() {
return Optional.ofNullable(Constants.SAXEXCEPTION_EXCEPTION_MAPPER)
.orElseThrow(() -> new NoClassDefFoundError("Unable to find the SAXException"));
}

Class<? extends Exception> targetException();
Expand Down
1 change: 1 addition & 0 deletions src/main/module/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
exports ch.powerunit.extensions.exceptions;

requires static java.sql;
requires static java.xml;
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ public void testForExceptionsAnyMapperForOtherException() {

@Test
public void testJaxbException() {
assertWhen(x -> ExceptionMapper.jaxbExceptionMapper()).throwException(instanceOf(NoClassDefFoundError.class));
ExceptionMapper.jaxbExceptionMapper();
}

@Test
public void testsaxException() {
ExceptionMapper.saxExceptionMapper();
}

}

0 comments on commit 61e6484

Please sign in to comment.