15 * You should have received a copy of the GNU General Public License |
15 * You should have received a copy of the GNU General Public License |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 package info.globalcode.sql.dk; |
18 package info.globalcode.sql.dk; |
19 |
19 |
20 import java.util.regex.Matcher; |
20 import java.util.ArrayList; |
21 import java.util.regex.Pattern; |
21 import java.util.Collection; |
22 import org.testng.annotations.Test; |
|
23 import static org.testng.Assert.*; |
22 import static org.testng.Assert.*; |
|
23 import org.testng.annotations.*; |
24 |
24 |
25 /** |
25 /** |
26 * |
26 * |
27 * @author Ing. František Kučera (frantovo.cz) |
27 * @author Ing. František Kučera (frantovo.cz) |
28 */ |
28 */ |
29 public class FunctionsTest { |
29 public class FunctionsTest { |
30 |
30 |
31 @Test |
31 @Test |
32 public void testEscapeRegEx() { |
32 public void testNotNull() { |
33 for (String original : new String[]{"abcd", "1234", "xxx", "\\Eescape\\Q", "\\Qescape\\E", "abc\\Eescape\\Qdef.", ".", ""}) { |
33 Collection<String> c = null; |
34 String patternString = Pattern.quote(original); |
34 for (String s : Functions.notNull(c)) { |
35 System.out.println(original + " → " + patternString); |
35 fail("Should not iterate through null collection"); |
|
36 } |
36 |
37 |
37 Pattern pattern = Pattern.compile(patternString); |
38 c = new ArrayList<>(); |
|
39 c.add("ahoj"); |
|
40 int count = 0; |
|
41 for (String s : Functions.notNull(c)) { |
|
42 assertEquals(s, "ahoj", "Wrong item in collection"); |
|
43 count++; |
|
44 } |
|
45 assertEquals(count, 1, "Wrong number of iterations"); |
|
46 } |
38 |
47 |
39 String testString; |
48 @Test |
40 Matcher m; |
49 public void testLpad() { |
|
50 String original = "abc"; |
|
51 String padded; |
41 |
52 |
42 testString = original; |
53 padded = Functions.lpad(original, 5); |
43 m = pattern.matcher(testString); |
54 assertEquals(padded, " abc"); |
44 assertTrue(m.matches(), "Pattern does not match original string: " + testString); |
|
45 |
55 |
46 testString = original + "x"; |
56 padded = Functions.lpad(original, 2); |
47 m = pattern.matcher(testString); |
57 assertEquals(padded, original); |
48 assertFalse(m.matches(), "Pattern matches wrong string: " + testString); |
58 } |
49 |
59 |
50 testString = "x" + original; |
60 @Test |
51 m = pattern.matcher(testString); |
61 public void testRpad() { |
52 assertFalse(m.matches(), "Pattern matches wrong string: " + testString); |
62 String original = "abc"; |
|
63 String padded; |
53 |
64 |
54 testString = (original + "ab").substring(1); |
65 padded = Functions.rpad(original, 5); |
55 m = pattern.matcher(testString); |
66 assertEquals(padded, "abc "); |
56 assertFalse(m.matches(), "Pattern matches wrong string: " + testString); |
67 |
57 } |
68 padded = Functions.rpad(original, 2); |
|
69 assertEquals(padded, original); |
|
70 } |
|
71 |
|
72 @Test |
|
73 public void testRepeat() { |
|
74 assertEquals(Functions.repeat('f', 0), ""); |
|
75 assertEquals(Functions.repeat('f', 3), "fff"); |
58 } |
76 } |
59 } |
77 } |