java/sql-dk/src/test/java/info/globalcode/sql/dk/FunctionsTest.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 241 f332033ed66c
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * SQL-DK
 * Copyright © 2013 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

/**
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class FunctionsTest {

	@Test
	public void testNotNull() {
		Collection<String> c = null;
		for (String s : Functions.notNull(c)) {
			fail("Should not iterate through null collection");
		}

		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");
	}

	@Test
	public void testLpad() {
		String original = "abc";
		String padded;

		padded = Functions.lpad(original, 5);
		assertEquals(padded, "  abc");

		padded = Functions.lpad(original, 2);
		assertEquals(padded, original);
	}

	@Test
	public void testRpad() {
		String original = "abc";
		String padded;

		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");
	}

	@Test
	public void testGetClassHierarchy() {
		List<Class<? extends HierarchyMockClass2>> hierarchy = Functions.getClassHierarchy(HierarchyMockClass0.class, HierarchyMockClass2.class);
		assertEquals(hierarchy.size(), 3, "invalid number of classes in the hierarchy");
		assertEquals(hierarchy.get(0), HierarchyMockClass0.class);
		assertEquals(hierarchy.get(1), HierarchyMockClass1.class);
		assertEquals(hierarchy.get(2), HierarchyMockClass2.class);
	}

	private static class HierarchyMockClass0 extends HierarchyMockClass1 {
	}

	private static class HierarchyMockClass1 extends HierarchyMockClass2 {
	}

	private static class HierarchyMockClass2 {
	}
}