package 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; public class TestUtils { @Test public void test() throws IOException{ assertNotNull(Utils.getExceptionLogger()); //whitespaceBefore() String testString1 = " hello "; assertTrue(Utils.whitespaceBefore(0, testString1)); assertTrue(Utils.whitespaceBefore(2, testString1)); assertTrue(Utils.whitespaceBefore(3, testString1)); assertFalse(Utils.whitespaceBefore(4, testString1)); assertFalse(Utils.whitespaceBefore(8, testString1)); assertFalse(Utils.whitespaceBefore(9, testString1)); String testString2 = "h ello "; assertTrue(Utils.whitespaceBefore(0, testString2)); assertFalse(Utils.whitespaceBefore(1, testString2)); //lengthMinusTrailingWhitespace() assertEquals(8, Utils.lengthMinusTrailingWhitespace(testString1)); assertEquals(7, Utils.lengthMinusTrailingWhitespace(testString2)); String testString3 = "hello b\t\t\n\r\r\n"; assertEquals(8, Utils.lengthMinusTrailingWhitespace(testString3)); //lengthExpandedTabs() String testString4 = "hello\ttest"; assertEquals(8, Utils.lengthExpandedTabs(testString4, 6, 4)); assertEquals(12, Utils.lengthExpandedTabs(testString4, 10, 4)); String testString5 = "hell\ttest"; assertEquals(12, Utils.lengthExpandedTabs(testString5, 9, 4)); String testString6 = ""; assertEquals(0, Utils.lengthExpandedTabs(testString6, 0, 4)); String testString7 = "\t"; assertEquals(4, Utils.lengthExpandedTabs(testString7, 1, 4)); //getPattern() assertNotNull(Utils.getPattern("")); assertTrue(Utils.getPattern("a.*bc.+test").matcher("axyzdebcytest").matches()); assertNotNull(Utils.getPattern("a.*bc.+test.*", 0)); assertNotNull(Utils.getPattern("a.*bc.+test.*", 5)); //getLines() assertEquals(6, Utils.getLines("./test.txt").length); try{ Utils.getLines("./test.txt", "somefunkynon-existingencoding"); fail(); }catch(UnsupportedEncodingException ex){} //createPattern() assertNotNull(Utils.createPattern("abc.*")); try{ Utils.createPattern("invalidPattern.("); fail(); }catch(ConversionException ce){} //baseClassname() assertEquals(Utils.baseClassname("org.fruit.alayer.Finder"), "Finder"); assertEquals(Utils.baseClassname("Finder"), "Finder"); assertEquals(Utils.baseClassname("F"), "F"); assertEquals(Utils.baseClassname(""), ""); //getStrippedFileName assertEquals("/usr/bin/sh", Utils.getStrippedFileName(null, "/usr/bin/sh")); assertEquals("/usr/bin/sh", Utils.getStrippedFileName("differentPrefix", "/usr/bin/sh")); assertEquals("sh", Utils.getStrippedFileName("/usr/bin/", "/usr/bin/sh")); assertEquals("sh", Utils.getStrippedFileName("/usr/bin", "/usr/bin/sh")); assertFalse("sh".equals(Utils.getStrippedFileName("/uxr/bin/", "/usr/bin/sh"))) ; //closeQuietly Utils.closeQuietly(null); Utils.closeQuietly(new FileInputStream("./test.txt")); } }