From a3addb98d4fd03d2d7693075389b532088a791b8 Mon Sep 17 00:00:00 2001 From: RoyMontoya Date: Thu, 22 Oct 2015 11:14:19 -0700 Subject: [PATCH] refactor compareRight added tests for Compare method --- .../java/com/orm/util/NumberComparator.java | 32 ++-------------- .../java/com/orm/util/NamingHelperTest.java | 37 +++++++++++++++++++ .../com/orm/util/NumberComparatorTest.java | 34 +++++++++++++++++ 3 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 library/src/test/java/com/orm/util/NamingHelperTest.java create mode 100644 library/src/test/java/com/orm/util/NumberComparatorTest.java diff --git a/library/src/main/java/com/orm/util/NumberComparator.java b/library/src/main/java/com/orm/util/NumberComparator.java index b39ca9c9..92965827 100644 --- a/library/src/main/java/com/orm/util/NumberComparator.java +++ b/library/src/main/java/com/orm/util/NumberComparator.java @@ -13,34 +13,10 @@ private static char charAt(String s, int i) { } private int compareRight(String a, String b) { - int bias = 0; - int ia = 0; - int ib = 0; - while (true) { - char ca = charAt(a, ia); - char cb = charAt(b, ib); - - if ((!Character.isDigit(ca)) && (!Character.isDigit(cb))) { - return bias; - } - if (!Character.isDigit(ca)) { - return -1; - } - if (!Character.isDigit(cb)) { - return 1; - } - if (ca < cb) { - if (bias == 0) { - bias = -1; - } - } else if (ca > cb) { - if (bias == 0) - bias = 1; - } else if ((ca == 0) && (cb == 0)) - return bias; - ia++; - ib++; - } + int result = a.compareTo(b); + if(result < 0) return -1; + else if(result > 0) return 1; + else return 0; } public int compare(Object o1, Object o2) { diff --git a/library/src/test/java/com/orm/util/NamingHelperTest.java b/library/src/test/java/com/orm/util/NamingHelperTest.java new file mode 100644 index 00000000..718ff284 --- /dev/null +++ b/library/src/test/java/com/orm/util/NamingHelperTest.java @@ -0,0 +1,37 @@ +package com.orm.util; + +import com.orm.util.NamingHelper; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +public class NamingHelperTest { + @Test + public void testToSQLNameCaseConversion() throws Exception { + assertToSqlNameEquals("TESTLOWERCASE", "testlowercase"); + assertToSqlNameEquals("TESTUPPERCASE", "TESTUPPERCASE"); + } + + @Test + public void testToSQLNameUnderscore() { + assertToSqlNameEquals("TEST_UNDERSCORE", "testUnderscore"); + assertToSqlNameEquals("AB_CD", "AbCd"); + assertToSqlNameEquals("AB_CD", "ABCd"); + assertToSqlNameEquals("AB_CD", "AbCD"); + assertToSqlNameEquals("SOME_DETAILS_OBJECT", "SomeDetailsObject"); + assertToSqlNameEquals("H_OL_A","hOlA"); + assertToSqlNameEquals("A","a"); + } + + /** + * Helper method that asserts a CamelCaseString is converted to UPPER_CASE_UNDER_SCORE. + * + * @param expected a CamelCaseString + * @param actual the expected UPPER_CASE_UNDER_SCORE string + */ + private static void assertToSqlNameEquals(String expected, String actual) { + assertEquals(expected, NamingHelper.toSQLNameDefault(actual)); + } + +} diff --git a/library/src/test/java/com/orm/util/NumberComparatorTest.java b/library/src/test/java/com/orm/util/NumberComparatorTest.java new file mode 100644 index 00000000..1e062c7b --- /dev/null +++ b/library/src/test/java/com/orm/util/NumberComparatorTest.java @@ -0,0 +1,34 @@ +package com.orm.util; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +public class NumberComparatorTest { + private NumberComparator numberComparator = new NumberComparator(); + + @Test + public void testNumberComparatorEquals() throws Exception { + String a = "a"; + String b = "a"; + int compare = numberComparator.compare(a, b); + assertEquals(0, compare); + } + + @Test + public void testNumberComparatorGreater() throws Exception { + String a = "test"; + String b = "foo"; + int compare = numberComparator.compare(a, b); + assertEquals(1, compare); + } + + @Test + public void testNumberComparatorLesser() throws Exception { + String a = "foo"; + String b = "test"; + int compare = numberComparator.compare(a, b); + assertEquals(-1, compare); + } + + +} \ No newline at end of file