This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.models.api-1.3.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-api.git
commit 6b521a30e577eb1be430008c48ca65eeafb4bbb1 Author: Konrad Windszus <[email protected]> AuthorDate: Thu Dec 8 13:22:54 2016 +0000 SLING-6369 MissingElementsException should not hide any stack traces of contained exceptions Rely on suppressed exceptions being introduced with Java7. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/api@1773229 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 15 ++++++ .../models/factory/MissingElementsException.java | 16 +------ .../apache/sling/models/factory/package-info.java | 2 +- .../factory/MissingElementsExceptionTest.java | 53 ++++++++++++++++++++++ 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 7da4d5f..7591337 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,9 @@ <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api</url> </scm> + <properties> + <sling.java.version>7</sling.java.version> + </properties> <build> <plugins> <plugin> @@ -79,5 +82,17 @@ <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </dependency> + <!-- testing dependencies --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-junit</artifactId> + <version>2.0.0.0</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/apache/sling/models/factory/MissingElementsException.java b/src/main/java/org/apache/sling/models/factory/MissingElementsException.java index e166847..fe712e8 100644 --- a/src/main/java/org/apache/sling/models/factory/MissingElementsException.java +++ b/src/main/java/org/apache/sling/models/factory/MissingElementsException.java @@ -44,21 +44,9 @@ public final class MissingElementsException extends RuntimeException { missingElements = new ArrayList<MissingElementException>(); } - @Override - public String getMessage() { - StringBuilder message = new StringBuilder(super.getMessage()); - for (MissingElementException e : missingElements) { - message.append('\n'); - message.append(e.getMessage()); - if (e.getCause() != null) { - message.append(" caused by "); - message.append(e.getCause().getMessage()); - } - } - return message.toString(); - } - public void addMissingElementExceptions(MissingElementException e) { + // also add to suppressed list to make sure they appear as well with their full stack traces in the printStackTrace for this throwable + addSuppressed(e); missingElements.add(e); } diff --git a/src/main/java/org/apache/sling/models/factory/package-info.java b/src/main/java/org/apache/sling/models/factory/package-info.java index 8ee2982..2adb00f 100644 --- a/src/main/java/org/apache/sling/models/factory/package-info.java +++ b/src/main/java/org/apache/sling/models/factory/package-info.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -@Version("1.3.0") +@Version("1.3.1") package org.apache.sling.models.factory; import aQute.bnd.annotation.Version; \ No newline at end of file diff --git a/src/test/java/org/apache/sling/models/factory/MissingElementsExceptionTest.java b/src/test/java/org/apache/sling/models/factory/MissingElementsExceptionTest.java new file mode 100644 index 0000000..1cec82b --- /dev/null +++ b/src/test/java/org/apache/sling/models/factory/MissingElementsExceptionTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.sling.models.factory; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; + +public class MissingElementsExceptionTest { + + @Test + public void testMissingElementsExceptionStackTraceContainsTracesOfAggregatedExceptions() { + MissingElementsException wrapperException = new MissingElementsException("Test wrapper"); + try { + try { + throw new IllegalStateException("Root exception"); + } catch(IllegalStateException rootException) { + throw new MissingElementException(null, rootException); + } + } catch(MissingElementException e) { + wrapperException.addMissingElementExceptions(e); + } + + // now evaluate exception message + Assert.assertThat(wrapperException.getMessage(), Matchers.not(Matchers.containsString("Root Exception"))); + Assert.assertThat(wrapperException.getMessage(), Matchers.containsString("Test wrapper")); + + // make sure the aggregated exceptions appear in the stack trace + StringWriter stringWriter = new StringWriter(); + wrapperException.printStackTrace(new PrintWriter(stringWriter)); + Assert.assertThat(stringWriter.toString(), Matchers.containsString("Root exception")); + Assert.assertThat(stringWriter.toString(), Matchers.containsString("Test wrapper")); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
