From 95b021e8cf705086cee016427b05988a56dbb42d Mon Sep 17 00:00:00 2001 From: Austin Date: Fri, 27 Jul 2018 15:22:48 -0400 Subject: [PATCH] fixes issue #1432 about asserting charSequence equal --- .classpath | 41 ++++++++++++++++--- .project | 6 +++ .settings/org.eclipse.jdt.core.prefs | 8 ++-- src/main/java/org/junit/Assert.java | 32 +++++++++++++++ .../junit/tests/assertion/AssertionTest.java | 15 +++++++ 5 files changed, 92 insertions(+), 10 deletions(-) diff --git a/.classpath b/.classpath index f648ca63c5ff..e6b57972a1c3 100644 --- a/.classpath +++ b/.classpath @@ -1,10 +1,39 @@ - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + diff --git a/.project b/.project index 88756880f282..df67bf0ce28d 100644 --- a/.project +++ b/.project @@ -10,8 +10,14 @@ + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature org.eclipse.team.cvs.core.cvsnature diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index d54f4c25de99..50326049484c 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,4 +1,3 @@ -#Mon Oct 12 21:57:10 EDT 2009 eclipse.preferences.version=1 org.eclipse.jdt.core.codeComplete.argumentPrefixes= org.eclipse.jdt.core.codeComplete.argumentSuffixes= @@ -9,9 +8,9 @@ org.eclipse.jdt.core.codeComplete.localSuffixes= org.eclipse.jdt.core.codeComplete.staticFieldPrefixes= org.eclipse.jdt.core.codeComplete.staticFieldSuffixes= org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.compliance=1.6 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate @@ -92,7 +91,8 @@ org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disa org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.6 org.eclipse.jdt.core.compiler.taskCaseSensitive=enabled org.eclipse.jdt.core.compiler.taskPriorities=NORMAL,NORMAL,HIGH,NORMAL org.eclipse.jdt.core.compiler.taskTags=TODO,REVISIT,HACK,QUESTION diff --git a/src/main/java/org/junit/Assert.java b/src/main/java/org/junit/Assert.java index a3d7905e6d24..17e7b2247fe6 100644 --- a/src/main/java/org/junit/Assert.java +++ b/src/main/java/org/junit/Assert.java @@ -29,6 +29,38 @@ public class Assert { protected Assert() { } + // fixes issue #1432 + /** + * Asserts that charSequence has the same string of chars. If the sequence is not the same, throws a + * {@link AssertionError} with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expected the sequence to check against + * @param actual the sequence that is being checked + */ + public static void assertContentsEqual(String message, CharSequence expected, + CharSequence actual) { + if (expected.length() != actual.length()) { + failNotEquals(message, expected, actual); + } + for (int i = 0; i < expected.length(); i++) { + if (expected.charAt(i) != actual.charAt(i)) + failNotEquals(message, expected, actual); + } + } + + /** + * Asserts that charSequence has the same string of chars. If the sequence is not the same, throws a + * {@link AssertionError} with the given message. + * + * @param expected the sequence to check against + * @param actual the sequence that is being checked + */ + public static void assertContentsEqual(CharSequence expected, CharSequence actual) { + assertContentsEqual(null, expected, actual); + } + /** * Asserts that a condition is true. If it isn't it throws an * {@link AssertionError} with the given message. diff --git a/src/test/java/org/junit/tests/assertion/AssertionTest.java b/src/test/java/org/junit/tests/assertion/AssertionTest.java index d0c3bdfddc59..8ac4e94fde04 100644 --- a/src/test/java/org/junit/tests/assertion/AssertionTest.java +++ b/src/test/java/org/junit/tests/assertion/AssertionTest.java @@ -36,6 +36,21 @@ public class AssertionTest { private static final String ASSERTION_ERROR_EXPECTED = "AssertionError expected"; + @Test(expected = AssertionError.class) + public void stringContentEqual() { + Assert.assertContentsEqual("String", "String2"); + } + + @Test(expected = AssertionError.class) + public void stringbuilderContentEqual() { + Assert.assertContentsEqual(new StringBuilder("String"), new StringBuilder("String2")); + } + + @Test + public void stringbufferContentEqual() { + Assert.assertContentsEqual(new StringBuffer("String"), new StringBuffer("String")); + } + @Test(expected = AssertionError.class) public void fails() { Assert.fail();