From d4c7a69c415e1f0624096837ba4c069c66981a7e Mon Sep 17 00:00:00 2001 From: Dennis Hendriks Date: Fri, 1 Oct 2021 16:51:04 +0200 Subject: [PATCH 1/2] #200 Improved Assert class messages. - Include more information from arguments in the messages by default. - For 'ifAndOnlyIf'. - Allow Object type values for msg. - Avoids needing to convert to string for arguments. - Applied where relevant. --- .../html/multipage/AsciiDocHtmlAnalyzer.java | 6 +-- .../html/multipage/AsciiDocHtmlModifier.java | 20 +++++----- .../AsciiDocMultiPageHtmlSplitter.java | 8 ++-- .../org/eclipse/escet/common/java/Assert.java | 37 +++++++++++-------- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlAnalyzer.java b/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlAnalyzer.java index adc21a4aa..3b462b1bd 100644 --- a/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlAnalyzer.java +++ b/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlAnalyzer.java @@ -143,8 +143,8 @@ class AsciiDocHtmlAnalyzer { } } }); - Assert.check(pageStack.size() == 1, String.valueOf(pageStack.size())); - Assert.check(tocStack.size() == 1, String.valueOf(tocStack.size())); + Assert.check(pageStack.size() == 1, pageStack.size()); + Assert.check(tocStack.size() == 1, tocStack.size()); // Ensure content for each page. for (AsciiDocHtmlPage page: htmlPages.pages) { @@ -152,7 +152,7 @@ class AsciiDocHtmlAnalyzer { } // Return the TOC. - Assert.check(tocStack.peek().right == 0, String.valueOf(tocStack.peek().right)); + Assert.check(tocStack.peek().right == 0, tocStack.peek().right); htmlPages.toc = tocStack.pop().left; } } diff --git a/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlModifier.java b/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlModifier.java index 316980220..a400ab132 100644 --- a/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlModifier.java +++ b/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocHtmlModifier.java @@ -345,9 +345,9 @@ class AsciiDocHtmlModifier { // Move copyright/version. Element elemBodyCopyrightVersion = single(doc.select("#header div.details")); elemBodyCopyrightVersion.remove(); - Assert.check(elemBodyCopyrightVersion.children().size() == 3, elemBodyCopyrightVersion.toString()); + Assert.check(elemBodyCopyrightVersion.children().size() == 3, elemBodyCopyrightVersion); Elements elemBodyCopyrightVersionSpans = elemBodyCopyrightVersion.children().select("span"); - Assert.check(elemBodyCopyrightVersionSpans.size() == 2, elemBodyCopyrightVersionSpans.toString()); + Assert.check(elemBodyCopyrightVersionSpans.size() == 2, elemBodyCopyrightVersionSpans); elemBodyCopyrightVersionSpans.removeAttr("id"); elemBodyCopyrightVersionSpans.removeAttr("class"); for (Element elem: Lists.reverse(elemBodyCopyrightVersionSpans)) { @@ -357,7 +357,7 @@ class AsciiDocHtmlModifier { // Move title. Element elemBodyTitle = single(doc.select("#header h1")); - Assert.check(elemBodyTitle.children().isEmpty(), elemBodyTitle.toString()); + Assert.check(elemBodyTitle.children().isEmpty(), elemBodyTitle); elemBodyTitle.tagName("span"); elemBodyFooterText.prependChild(elemBodyTitle); } @@ -468,8 +468,8 @@ class AsciiDocHtmlModifier { minHeaderNr = Math.min(headerNr, minHeaderNr); } } - Assert.check(minHeaderNr > 0, String.valueOf(minHeaderNr)); - Assert.check(minHeaderNr < Integer.MAX_VALUE, String.valueOf(minHeaderNr)); + Assert.check(minHeaderNr > 0, minHeaderNr); + Assert.check(minHeaderNr < Integer.MAX_VALUE, minHeaderNr); // Normalize header numbers to ensure minimum header number is '2'. for (Element elem: elemContent.getAllElements()) { @@ -477,7 +477,7 @@ class AsciiDocHtmlModifier { if (matcher.matches()) { int headerNr = Integer.parseInt(matcher.group(1), 10); int newHeaderNr = headerNr - minHeaderNr + 2; - Assert.check(newHeaderNr <= 6, String.valueOf(newHeaderNr)); // Only h1-h6 are defined in HTML. + Assert.check(newHeaderNr <= 6, newHeaderNr); // Only h1-h6 are defined in HTML. elem.tagName("h" + newHeaderNr); } } @@ -503,11 +503,11 @@ class AsciiDocHtmlModifier { // Sanity checks. if (htmlPages.homePage == page) { - Assert.check(tocLinkCurPageCount == 0, String.valueOf(tocLinkCurPageCount)); + Assert.check(tocLinkCurPageCount == 0, tocLinkCurPageCount); } else { // If the TOC level setting used to generate the single page HTML file is too limited, the page will not be // in the TOC, and this will fail (count is zero). - Assert.check(tocLinkCurPageCount == 1, String.valueOf(tocLinkCurPageCount)); + Assert.check(tocLinkCurPageCount == 1, tocLinkCurPageCount); } } @@ -599,7 +599,7 @@ class AsciiDocHtmlModifier { if (allowEmptyRefIfNoChildren) { // Occurs for 'a.href' for bibliography entries. // But then they have no child nodes, and are thus not clickable. - Assert.check(elem.childNodeSize() == 0, String.valueOf(elem.childNodeSize())); + Assert.check(elem.childNodeSize() == 0, elem.childNodeSize()); continue; } else { throw new RuntimeException( @@ -631,7 +631,7 @@ class AsciiDocHtmlModifier { Assert.check(uriScheme == null, uriScheme); Assert.check(uri.getUserInfo() == null, uri.getUserInfo()); Assert.check(uri.getHost() == null, uri.getHost()); - Assert.check(uri.getPort() == -1, String.valueOf(uri.getPort())); + Assert.check(uri.getPort() == -1, uri.getPort()); Assert.check(uri.getAuthority() == null, uri.getAuthority()); Assert.check(uri.getQuery() == null, uri.getQuery()); Assert.check(uri.getFragment() == null, uri.getFragment()); diff --git a/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocMultiPageHtmlSplitter.java b/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocMultiPageHtmlSplitter.java index c7a92ce49..11ae51b51 100644 --- a/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocMultiPageHtmlSplitter.java +++ b/common/org.eclipse.escet.common.asciidoc/src/org/eclipse/escet/common/asciidoc/html/multipage/AsciiDocMultiPageHtmlSplitter.java @@ -69,7 +69,7 @@ public class AsciiDocMultiPageHtmlSplitter { */ public static void main(String[] args) throws IOException { System.out.println("Command line arguments: " + Arrays.toString(args)); - Assert.check(args.length == 4, args.toString()); + Assert.check(args.length == 4, args); Path sourceRootPath = Paths.get(args[0]); Path singleHtmlPagePath = Paths.get(args[1]); @@ -98,8 +98,8 @@ public class AsciiDocMultiPageHtmlSplitter { throws IOException { // Check inputs exist. - Assert.check(Files.isDirectory(sourceRootPath), sourceRootPath.toString()); - Assert.check(Files.isRegularFile(singleHtmlPagePath), singleHtmlPagePath.toString()); + Assert.check(Files.isDirectory(sourceRootPath), sourceRootPath); + Assert.check(Files.isRegularFile(singleHtmlPagePath), singleHtmlPagePath); // Ensure empty directory for output. if (Files.isDirectory(outputRootPath)) { @@ -146,7 +146,7 @@ public class AsciiDocMultiPageHtmlSplitter { if (htmlType == HtmlType.WEBSITE) { System.out.println("Copying single-page HTML file to: " + outputRootPath.toString()); Path singlePathOutputPath = outputRootPath.resolve("index-single-page.html"); - Assert.check(!Files.exists(singlePathOutputPath), singlePathOutputPath.toString()); + Assert.check(!Files.exists(singlePathOutputPath), singlePathOutputPath); Files.copy(singleHtmlPagePath, singlePathOutputPath); } diff --git a/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java b/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java index da3cfd79c..0ff4d9ea7 100644 --- a/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java +++ b/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java @@ -38,15 +38,16 @@ public class Assert { * Checks a condition and throws an {@link AssertionError}, with the given message, if the condition doesn't hold. * * @param condition The condition that should hold. - * @param msg The message to use for the exception. + * @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a + * message. * * @throws AssertionError If the condition doesn't hold. */ - public static void check(boolean condition, String msg) { + public static void check(boolean condition, Object msg) { if (condition) { return; } - throw new AssertionError(msg); + throw new AssertionError(String.valueOf(msg)); } /** @@ -70,15 +71,16 @@ public class Assert { * * @param left The left side of the implication. * @param right The right side of the implication. - * @param msg The message to use for the exception. + * @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a + * message. * * @throws AssertionError If the condition doesn't hold. */ - public static void implies(boolean left, boolean right, String msg) { + public static void implies(boolean left, boolean right, Object msg) { if (!left || right) { return; } - throw new AssertionError(msg); + throw new AssertionError(String.valueOf(msg)); } /** @@ -93,7 +95,7 @@ public class Assert { if (left == right) { return; } - throw new AssertionError(); + throw new AssertionError(left + " != " + right); } /** @@ -102,15 +104,16 @@ public class Assert { * * @param left The left side of the biimplication. * @param right The right side of the biimplication. - * @param msg The message to use for the exception. + * @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a + * message. * * @throws AssertionError If the biimplication doesn't hold. */ - public static void ifAndOnlyIf(boolean left, boolean right, String msg) { + public static void ifAndOnlyIf(boolean left, boolean right, Object msg) { if (left == right) { return; } - throw new AssertionError(msg); + throw new AssertionError(String.valueOf(msg), new AssertionError(left + " != " + right)); } /** @@ -125,12 +128,13 @@ public class Assert { /** * Unconditionally throws an {@link AssertionError}, with the given message. * - * @param msg The message to use for the exception. + * @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a + * message. * * @throws AssertionError Always thrown. */ - public static void fail(String msg) { - throw new AssertionError(msg); + public static void fail(Object msg) { + throw new AssertionError(String.valueOf(msg)); } /** @@ -153,14 +157,15 @@ public class Assert { * message, if it is {@code null}. * * @param value The value to check. - * @param msg The message to use for the exception. + * @param msg The message to use for the exception. {@link String#valueOf} is used to convert the object to a + * message. * * @throws AssertionError If the value is {@code null}. */ - public static void notNull(Object value, String msg) { + public static void notNull(Object value, Object msg) { if (value != null) { return; } - throw new AssertionError(msg); + throw new AssertionError(String.valueOf(msg)); } } -- GitLab From 70d9bf448e6e7748e7d87e9bd102346c5e37efc6 Mon Sep 17 00:00:00 2001 From: Dennis Hendriks Date: Sun, 3 Oct 2021 21:30:15 +0200 Subject: [PATCH 2/2] #200 Assert.jva: improved assertion failure messages. --- .../src/org/eclipse/escet/common/java/Assert.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java b/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java index 0ff4d9ea7..46233fb4c 100644 --- a/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java +++ b/common/org.eclipse.escet.common.java/src/org/eclipse/escet/common/java/Assert.java @@ -13,6 +13,8 @@ package org.eclipse.escet.common.java; +import static org.eclipse.escet.common.java.Strings.fmt; + /** A class for performing assertion checks. Unlike the Java assert statement, these are always checked. */ public class Assert { /** Constructor for the {@link Assert} class. */ @@ -62,7 +64,7 @@ public class Assert { if (!left || right) { return; } - throw new AssertionError(); + throw new AssertionError(fmt("'%s => %s' doesn't hold", left, right)); } /** @@ -80,7 +82,7 @@ public class Assert { if (!left || right) { return; } - throw new AssertionError(String.valueOf(msg)); + throw new AssertionError(String.valueOf(msg), new AssertionError(fmt("'%s => %s' doesn't hold", left, right))); } /** @@ -95,7 +97,7 @@ public class Assert { if (left == right) { return; } - throw new AssertionError(left + " != " + right); + throw new AssertionError(fmt("'%s <=> %s' doesn't hold", left, right)); } /** @@ -113,7 +115,7 @@ public class Assert { if (left == right) { return; } - throw new AssertionError(String.valueOf(msg), new AssertionError(left + " != " + right)); + throw new AssertionError(String.valueOf(msg), new AssertionError(fmt("'%s <=> %s' doesn't hold", left, right))); } /** -- GitLab