import com.puppycrawl.tools.checkstyle.api.* ; import static org.junit.Assert.*; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; //import org.apache.commons.beanutils.ConversionException; import org.junit.Test; /** * WP note: the original test is changed because I changed the CUT to become * non-static based. */ public class TestUtils { @Test public void test() throws IOException{ //assertNotNull(new Utils().getExceptionLogger()); //whitespaceBefore() String testString1 = " hello "; assertTrue(new Utils().whitespaceBefore(0, testString1)); assertTrue(new Utils().whitespaceBefore(2, testString1)); assertTrue(new Utils().whitespaceBefore(3, testString1)); String testString2 = "h ello "; assertTrue(new Utils().whitespaceBefore(0, testString2)); assertFalse(new Utils().whitespaceBefore(1, testString2)); //lengthMinusTrailingWhitespace() assertEquals(8, new Utils().lengthMinusTrailingWhitespace(testString1)); assertEquals(7, new Utils().lengthMinusTrailingWhitespace(testString2)); String testString3 = "hello b\t\t\n\r\r\n"; assertEquals(8, new Utils().lengthMinusTrailingWhitespace(testString3)); //lengthExpandedTabs() String testString4 = "hello\ttest"; assertEquals(8, new Utils().lengthExpandedTabs(testString4, 6, 4)); assertEquals(12, new Utils().lengthExpandedTabs(testString4, 10, 4)); String testString5 = "hell\ttest"; assertEquals(12, new Utils().lengthExpandedTabs(testString5, 9, 4)); String testString7 = "\t"; assertEquals(4, new Utils().lengthExpandedTabs(testString7, 1, 4)); //getPattern() assertNotNull(new Utils().getPattern("")); assertTrue(new Utils().getPattern("a.*bc.+test").matcher("axyzdebcytest").matches()); assertNotNull(new Utils().getPattern("a.*bc.+test.*", 0)); assertNotNull(new Utils().getPattern("a.*bc.+test.*", 5)); //getLines() assertEquals(3, new Utils().getLines("./test.txt").length); //createPattern() assertNotNull(new Utils().createPattern("abc.*")); //baseClassname() assertEquals(new Utils().baseClassname("org.fruit.alayer.Finder"), "Finder"); assertEquals(new Utils().baseClassname("Finder"), "Finder"); assertEquals(new Utils().baseClassname("F"), "F"); assertEquals(new Utils().baseClassname(""), ""); //getStrippedFileName assertEquals("/usr/bin/sh", new Utils().getStrippedFileName(null, "/usr/bin/sh")); assertEquals("/usr/bin/sh", new Utils().getStrippedFileName("differentPrefix", "/usr/bin/sh")); assertFalse("sh".equals(new Utils().getStrippedFileName("/usr/bin/", "/usr/bin/sh"))) ; assertEquals("sh", new Utils().getStrippedFileName("/usr/bin", "/usr/bin/sh")); assertFalse("sh".equals(new Utils().getStrippedFileName("/uxr/bin/", "/usr/bin/sh"))); //closeQuietly new Utils().closeQuietly(new FileInputStream("./test.txt")); } // break-off tests //@Test public void testBO() throws IOException{ String testString1 = " hello "; assertFalse(new Utils().whitespaceBefore(4, testString1)); assertFalse(new Utils().whitespaceBefore(8, testString1)); assertFalse(new Utils().whitespaceBefore(9, testString1)); String testString6 = ""; assertEquals(0, new Utils().lengthExpandedTabs(testString6, 0, 4)); try{ new Utils().getLines("./test.txt", "somefunkynon-existingencoding"); fail(); }catch(UnsupportedEncodingException ex){} try{ new Utils().createPattern("invalidPattern.("); fail(); }catch(ConversionException ce){} new Utils().closeQuietly(null); } }