/** Tests {@link TextInput#readCounter()}. */ public void testReadCounter() { qedeqInput.setRow(18); assertEquals("1789", qedeqInput.readCounter()); try { qedeqInput.readCounter(); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // expected } qedeqInput.read(); try { qedeqInput.readCounter(); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // expected } qedeqInput.read(); assertEquals("1239", qedeqInput.readCounter()); try { qedeqInput.readCounter(); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // expected } try { emptyInput.readCounter(); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // expected } }
/** Test {@link TextInput#getChar(int)}. */ public void testGetCharInt() { final String first = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; for (int i = 0; i < first.length(); i++) { assertEquals(first.charAt(i), qedeqInput.getChar(0)); assertEquals(first.charAt(i), qedeqInput.read()); } qedeqInput.setPosition(qedeqInput.getMaximumPosition()); for (int i = 0; i < 10; i++) { assertEquals(-1, qedeqInput.getChar(0)); } for (int i = 0; i < 10; i++) { assertEquals(-1, qedeqInput.getChar(i)); } assertEquals('>', qedeqInput.getChar(-1)); assertEquals('Q', qedeqInput.getChar(-2)); assertEquals('E', qedeqInput.getChar(-3)); assertEquals('D', qedeqInput.getChar(-4)); assertEquals('E', qedeqInput.getChar(-5)); assertEquals('Q', qedeqInput.getChar(-6)); assertEquals('/', qedeqInput.getChar(-7)); assertEquals('<', qedeqInput.getChar(-8)); qedeqInput.setPosition(0); for (int i = 0; i < first.length(); i++) { assertEquals(first.charAt(i), qedeqInput.getChar(0)); assertEquals(first.charAt(i), qedeqInput.read()); } qedeqInput.setPosition(0); for (int i = 0; i < first.length(); i++) { assertEquals(first.charAt(i), qedeqInput.getChar(i)); } }
/** * Test constructor {@link TextInput#TextInput(File)}. * * @throws Exception Test failed. */ public void testTextInputFileString() throws Exception { final File file = new File(getOutdir(), this.getClass().getName() + ".testTexFileInput.impl"); IoUtility.saveFile(file, XML_DATA, IoUtility.getDefaultEncoding()); TextInput ti = new TextInput(file, IoUtility.getDefaultEncoding()); while (!qedeqInput.isEmpty()) { assertEquals(qedeqInput.read(), ti.read()); } try { new TextInput((File) null, "UTF-8"); fail("Exception expected"); } catch (RuntimeException e) { // OK } assertTrue(file.delete()); }
/** Test constructor {@link TextInput#TextInput(Reader)}. */ public void testTextInputReader() throws Exception { final TextInput ti = new TextInput( new Reader() { int pos = 0; public void close() throws IOException {} public int read(char[] cbuf, int off, int len) throws IOException { if (pos >= XML_DATA.length()) { return -1; } final String result = StringUtility.substring(XML_DATA, pos, len); for (int i = 0; i < result.length(); i++) { cbuf[off + i] = result.charAt(i); } pos += result.length(); return result.length(); } }); int i = 0; while (!ti.isEmpty()) { assertEquals(ti.read(), XML_DATA.charAt(i++)); } try { new TextInput((Reader) null); fail("NullPointerException expected"); } catch (NullPointerException e) { // expected } }
public void testReadXmlName() { final TextInput ti = new TextInput("<Head this=\">is not the end\">zulu"); try { ti.readNextXmlName(); fail("Exception expected"); // we are not within the tag yet } catch (RuntimeException e) { // OK } ti.read(); assertEquals("Head", ti.readNextXmlName()); assertEquals("this", ti.readNextXmlName()); try { System.out.println("name=" + ti.readNextXmlName()); fail("Exception expected"); // now we have the value } catch (RuntimeException e) { // OK } assertEquals(">is not the end", ti.readNextAttributeValue()); try { System.out.println("name=" + ti.readNextXmlName()); fail("Exception expected"); // now we have the value } catch (RuntimeException e) { // OK } }
/** * Searches a string in a file. * * @param path file path * @param search codepoints of search string * @return success flag */ private static boolean filterContent(final String path, final int[] search) { final int cl = search.length; if (cl == 0) return true; try (final TextInput ti = new TextInput(new IOFile(path))) { final IntList il = new IntList(cl - 1); int c = 0; while (true) { if (!il.isEmpty()) { if (il.remove(0) == search[c++]) continue; c = 0; } while (true) { final int cp = ti.read(); if (cp == -1 || !XMLToken.valid(cp)) return false; final int lc = Token.lc(cp); if (c > 0) il.add(lc); if (lc == search[c]) { if (++c == cl) return true; } else { c = 0; break; } } } } catch (final IOException ex) { // file may not be accessible Util.debug(ex); return false; } }
public void testReadNextAttributeValue2() { final TextInput ti = new TextInput("<Head this=\">is not \n the end\" we\n=\tzorg " + " go = \"hi"); try { System.out.println("attribute=" + ti.readNextAttributeValue()); fail("Exception expected"); // now we have the value } catch (RuntimeException e) { // OK } ti.read(); // skip < try { System.out.println("attribute=" + ti.readNextAttributeValue()); fail("Exception expected"); // now we have the value } catch (RuntimeException e) { // OK } assertEquals("Head", ti.readNextXmlName()); try { System.out.println("attribute=" + ti.readNextAttributeValue()); fail("Exception expected"); // now we have the value } catch (RuntimeException e) { // OK } assertEquals("this", ti.readNextXmlName()); assertEquals(">is not \n the end", ti.readNextAttributeValue()); assertEquals("we", ti.readNextXmlName()); assertEquals("zorg", ti.readNextAttributeValue()); assertEquals("go", ti.readNextXmlName()); try { System.out.println("attribute=" + ti.readNextAttributeValue()); fail("Exception expected"); // now we have the value } catch (RuntimeException e) { // OK } }
public void testSkipForwardToEndOfXmlTag() { qedeqInput.skipForwardToEndOfXmlTag(); assertEquals('\n', qedeqInput.getChar()); qedeqInput.skipForwardToEndOfXmlTag(); assertEquals('\n', qedeqInput.read()); assertEquals(" <HEADER", qedeqInput.readString(" <HEADER".length())); final TextInput ti = new TextInput("<Head this=\">is not the end\">zulu"); ti.skipForwardToEndOfXmlTag(); assertEquals("zulu", ti.readString(4)); final TextInput ti2 = new TextInput("<Head this=\"one\" that=\"two\">zulu"); ti2.skipForwardToEndOfXmlTag(); assertEquals("zulu", ti2.readString(4)); final TextInput ti3 = new TextInput("<Head this=\"one\" that=\"two>zulu"); try { ti3.skipForwardToEndOfXmlTag(); fail("Exception expected"); // the quotation is not ended! } catch (RuntimeException e) { // OK } final TextInput ti4 = new TextInput("<Head this=\"one\" that=\"two\" this= that"); try { ti4.skipForwardToEndOfXmlTag(); fail("Exception expected"); // the tag is not ended with > } catch (RuntimeException e) { // OK } }
/** Test {@link TextInput#getChar()}. */ public void testGetChar() { final String first = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; for (int i = 0; i < first.length(); i++) { assertEquals(first.charAt(i), qedeqInput.getChar()); assertEquals(first.charAt(i), qedeqInput.read()); } qedeqInput.setPosition(qedeqInput.getMaximumPosition()); for (int i = 0; i < 10; i++) { assertEquals(-1, qedeqInput.getChar()); assertEquals(-1, qedeqInput.read()); } qedeqInput.setPosition(0); for (int i = 0; i < first.length(); i++) { assertEquals(first.charAt(i), qedeqInput.getChar()); assertEquals(first.charAt(i), qedeqInput.read()); } }
/** Test {@link TextInput#isEmpty()}. */ public void testIsEmpty() { assertFalse(qedeqInput.isEmpty()); final String first = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; for (int i = 0; i < first.length(); i++) { qedeqInput.read(); assertFalse(qedeqInput.isEmpty()); } qedeqInput.setPosition(qedeqInput.getMaximumPosition()); assertTrue(qedeqInput.isEmpty()); }
/** * Performs a test on the specified data. * * @param data data to be tested * @throws IOException I/O exception */ private static void run(final byte[] data) throws IOException { final TokenBuilder tb = new TokenBuilder(); final TextInput ti = new TextInput(new IOContent(data)); ti.read(); ti.reset(); for (int b; (b = ti.read()) != -1; ) tb.add(b); try { ti.reset(); assertTrue( "Mark should not be supported for data size of " + data.length, data.length < IO.BLOCKSIZE); tb.reset(); for (int b; (b = ti.read()) != -1; ) tb.add(b); assertSame(data, tb.finish()); } catch (final IOException ex) { assertTrue( "Mark could not be reset for data size of " + data.length, data.length >= IO.BLOCKSIZE); } }
/** Test constructor {@link TextInput#TextInput(String)}. */ public void testTextInputStringStringString() { int i = 0; while (!qedeqInput.isEmpty()) { assertEquals(qedeqInput.read(), XML_DATA.charAt(i++)); } try { new TextInput((String) null); fail("NullPointerException expected"); } catch (NullPointerException e) { // expected } }
/** Test constructor {@link TextInput#TextInput(StringBuffer)}. */ public void testTextInputStringBufferStringString() { final StringBuffer buffer = new StringBuffer(XML_DATA); final TextInput ti = new TextInput(buffer); int i = 0; while (!ti.isEmpty()) { assertEquals(ti.read(), XML_DATA.charAt(i++)); } try { new TextInput((StringBuffer) null); fail("NullPointerException expected"); } catch (NullPointerException e) { // expected } }
/** Test {@link TextInput#skipWhiteSpace()}. */ public void testSkipWhiteSpaceInverse() { final String first = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"; qedeqInput.skipWhiteSpaceInverse(); assertEquals(0, qedeqInput.getPosition()); qedeqInput.setPosition(first.length() + 1); qedeqInput.skipWhiteSpaceInverse(); assertEquals('\n', qedeqInput.getChar()); assertEquals(first.length(), qedeqInput.getPosition()); qedeqInput.setRow(8); assertEquals( " \t\r <LOCATION value=\"http://qedeq.org/0.01.06/sample1\"/>", qedeqInput.getLine()); qedeqInput.setColumn(9); qedeqInput.skipWhiteSpaceInverse(); assertEquals(7, qedeqInput.getRow()); assertEquals('>', qedeqInput.getChar(-1)); qedeqInput.read(); assertEquals(8, qedeqInput.getRow()); }
/** Test {@link TextInput#readLetterDigitString()}. */ public void testReadLetterDigitString() { qedeqInput.read(); qedeqInput.read(); assertEquals("xml", qedeqInput.readLetterDigitString()); qedeqInput.setRow(13); assertEquals("Example1", qedeqInput.readLetterDigitString()); qedeqInput.setRow(13); qedeqInput.setColumn(1); qedeqInput.skipWhiteSpace(); assertEquals("Example1", qedeqInput.readLetterDigitString()); try { qedeqInput.readLetterDigitString(); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // expected } try { emptyInput.readLetterDigitString(); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // expected } }
/** Test {@link TextInput#read()}. */ public void testReadString() { qedeqInput.read(); qedeqInput.read(); assertEquals("xml", qedeqInput.readString(3)); }
/** Test {@link TextInput#readInverse()}. */ public void testReadInverse() { assertEquals(-1, qedeqInput.readInverse()); assertEquals('<', qedeqInput.read()); assertEquals('<', qedeqInput.readInverse()); }