package com.puppycrawl.tools.checkstyle.api; import static org.junit.Assert.*; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.util.Arrays; import java.util.List; import org.junit.Test; public class TestFileText { List list1 = Arrays.asList(new String[]{ "hello", "linebreak", "funkylinebreak", "" }); File file = new File("./test.txt"); @Test public void testFileConstructor() throws IOException{ FileText ft = new FileText(file, "UTF-8"); assertEquals(file, ft.getFile()); assertEquals(Charset.forName("UTF-8"), ft.getCharset()); assertNotNull(ft.getBytes()); assertTrue(ft.getFullText().length() > 0); assertTrue(ft.toLinesArray().length > 0); assertTrue(ft.lineColumn(17).getLine() == 3); try{ ft.lineColumn(-1); fail(); }catch(ArrayIndexOutOfBoundsException aioobe){} assertNotNull(ft.get(3)); assertEquals(ft.size(), 6); try{ ft = new FileText(new File("does not exist anywhere"), "UTF-8"); fail(); }catch(IOException ioe){} try{ ft = new FileText(file, null); fail(); }catch(IllegalArgumentException iae){} try{ ft = new FileText(file, "non-existing charset"); fail(); }catch(IllegalCharsetNameException icsne){} } @Test public void testLineConstructor() throws IOException{ FileText ft = FileText.fromLines(file, list1); assertNull(ft.getCharset()); assertNotNull(ft.getBytes()); assertTrue(ft.getFullText().length() > 0); assertNotNull(ft.get(3)); assertEquals(ft.size(), 4); assertTrue(ft.toLinesArray().length > 0); assertEquals(ft.lineColumn(3).getLine(), 1); assertEquals(ft.lineColumn(17).getLine(), 3); } }