java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 13:21:26 +0200
branchv_0
changeset 213 39d154429f7a
parent 53 eb30ad93ca8b
permissions -rw-r--r--
getClassHierarchy function
franta-hg@46
     1
/**
franta-hg@46
     2
 * SQL-DK
franta-hg@46
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@46
     4
 *
franta-hg@46
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@46
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@46
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@46
     8
 * (at your option) any later version.
franta-hg@46
     9
 *
franta-hg@46
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@46
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@46
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@46
    13
 * GNU General Public License for more details.
franta-hg@46
    14
 *
franta-hg@46
    15
 * You should have received a copy of the GNU General Public License
franta-hg@46
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@46
    17
 */
franta-hg@46
    18
package info.globalcode.sql.dk;
franta-hg@46
    19
franta-hg@53
    20
import java.util.ArrayList;
franta-hg@53
    21
import java.util.Collection;
franta-hg@213
    22
import java.util.List;
franta-hg@46
    23
import static org.testng.Assert.*;
franta-hg@53
    24
import org.testng.annotations.*;
franta-hg@46
    25
franta-hg@46
    26
/**
franta-hg@46
    27
 *
franta-hg@46
    28
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@46
    29
 */
franta-hg@46
    30
public class FunctionsTest {
franta-hg@46
    31
franta-hg@46
    32
	@Test
franta-hg@53
    33
	public void testNotNull() {
franta-hg@53
    34
		Collection<String> c = null;
franta-hg@53
    35
		for (String s : Functions.notNull(c)) {
franta-hg@53
    36
			fail("Should not iterate through null collection");
franta-hg@53
    37
		}
franta-hg@46
    38
franta-hg@53
    39
		c = new ArrayList<>();
franta-hg@53
    40
		c.add("ahoj");
franta-hg@53
    41
		int count = 0;
franta-hg@53
    42
		for (String s : Functions.notNull(c)) {
franta-hg@53
    43
			assertEquals(s, "ahoj", "Wrong item in collection");
franta-hg@53
    44
			count++;
franta-hg@53
    45
		}
franta-hg@53
    46
		assertEquals(count, 1, "Wrong number of iterations");
franta-hg@53
    47
	}
franta-hg@46
    48
franta-hg@53
    49
	@Test
franta-hg@53
    50
	public void testLpad() {
franta-hg@53
    51
		String original = "abc";
franta-hg@53
    52
		String padded;
franta-hg@46
    53
franta-hg@53
    54
		padded = Functions.lpad(original, 5);
franta-hg@53
    55
		assertEquals(padded, "  abc");
franta-hg@46
    56
franta-hg@53
    57
		padded = Functions.lpad(original, 2);
franta-hg@53
    58
		assertEquals(padded, original);
franta-hg@53
    59
	}
franta-hg@46
    60
franta-hg@53
    61
	@Test
franta-hg@53
    62
	public void testRpad() {
franta-hg@53
    63
		String original = "abc";
franta-hg@53
    64
		String padded;
franta-hg@46
    65
franta-hg@53
    66
		padded = Functions.rpad(original, 5);
franta-hg@53
    67
		assertEquals(padded, "abc  ");
franta-hg@53
    68
franta-hg@53
    69
		padded = Functions.rpad(original, 2);
franta-hg@53
    70
		assertEquals(padded, original);
franta-hg@53
    71
	}
franta-hg@53
    72
franta-hg@53
    73
	@Test
franta-hg@53
    74
	public void testRepeat() {
franta-hg@53
    75
		assertEquals(Functions.repeat('f', 0), "");
franta-hg@53
    76
		assertEquals(Functions.repeat('f', 3), "fff");
franta-hg@46
    77
	}
franta-hg@213
    78
franta-hg@213
    79
	@Test
franta-hg@213
    80
	public void testGetClassHierarchy() {
franta-hg@213
    81
		List<Class<? extends HierarchyMockClass2>> hierarchy = Functions.getClassHierarchy(HierarchyMockClass0.class, HierarchyMockClass2.class);
franta-hg@213
    82
		assertEquals(hierarchy.size(), 3, "invalid number of classes in the hierarchy");
franta-hg@213
    83
		assertEquals(hierarchy.get(0), HierarchyMockClass0.class);
franta-hg@213
    84
		assertEquals(hierarchy.get(1), HierarchyMockClass1.class);
franta-hg@213
    85
		assertEquals(hierarchy.get(2), HierarchyMockClass2.class);
franta-hg@213
    86
	}
franta-hg@213
    87
franta-hg@213
    88
	private static class HierarchyMockClass0 extends HierarchyMockClass1 {
franta-hg@213
    89
	}
franta-hg@213
    90
franta-hg@213
    91
	private static class HierarchyMockClass1 extends HierarchyMockClass2 {
franta-hg@213
    92
	}
franta-hg@213
    93
franta-hg@213
    94
	private static class HierarchyMockClass2 {
franta-hg@213
    95
	}
franta-hg@46
    96
}