# HG changeset patch # User František Kučera # Date 1387891402 -3600 # Node ID eb30ad93ca8bca1883c59e094fd2293c3bf57128 # Parent e2ba2af0ef405d32c254f57251062f75a4a457fe more functions unit tests diff -r e2ba2af0ef40 -r eb30ad93ca8b java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java --- a/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Tue Dec 24 12:05:05 2013 +0100 +++ b/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java Tue Dec 24 14:23:22 2013 +0100 @@ -17,10 +17,10 @@ */ package info.globalcode.sql.dk; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.testng.annotations.Test; +import java.util.ArrayList; +import java.util.Collection; import static org.testng.Assert.*; +import org.testng.annotations.*; /** * @@ -29,31 +29,49 @@ public class FunctionsTest { @Test - public void testEscapeRegEx() { - for (String original : new String[]{"abcd", "1234", "xxx", "\\Eescape\\Q", "\\Qescape\\E", "abc\\Eescape\\Qdef.", ".", ""}) { - String patternString = Pattern.quote(original); - System.out.println(original + " → " + patternString); + public void testNotNull() { + Collection c = null; + for (String s : Functions.notNull(c)) { + fail("Should not iterate through null collection"); + } - Pattern pattern = Pattern.compile(patternString); + c = new ArrayList<>(); + c.add("ahoj"); + int count = 0; + for (String s : Functions.notNull(c)) { + assertEquals(s, "ahoj", "Wrong item in collection"); + count++; + } + assertEquals(count, 1, "Wrong number of iterations"); + } - String testString; - Matcher m; + @Test + public void testLpad() { + String original = "abc"; + String padded; - testString = original; - m = pattern.matcher(testString); - assertTrue(m.matches(), "Pattern does not match original string: " + testString); + padded = Functions.lpad(original, 5); + assertEquals(padded, " abc"); - testString = original + "x"; - m = pattern.matcher(testString); - assertFalse(m.matches(), "Pattern matches wrong string: " + testString); + padded = Functions.lpad(original, 2); + assertEquals(padded, original); + } - testString = "x" + original; - m = pattern.matcher(testString); - assertFalse(m.matches(), "Pattern matches wrong string: " + testString); + @Test + public void testRpad() { + String original = "abc"; + String padded; - testString = (original + "ab").substring(1); - m = pattern.matcher(testString); - assertFalse(m.matches(), "Pattern matches wrong string: " + testString); - } + padded = Functions.rpad(original, 5); + assertEquals(padded, "abc "); + + padded = Functions.rpad(original, 2); + assertEquals(padded, original); + } + + @Test + public void testRepeat() { + assertEquals(Functions.repeat('f', 0), ""); + assertEquals(Functions.repeat('f', 3), "fff"); } }