public void testExceptions() { CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append("test"); assertEquals("test", t.toString()); try { t.charAt(-1); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.charAt(4); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.subSequence(0, 5); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.subSequence(5, 0); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } }
public void testAppendableInterfaceWithLongSequences() { CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append((CharSequence) "01234567890123456789012345678901234567890123456789"); t.append( (CharSequence) CharBuffer.wrap("01234567890123456789012345678901234567890123456789".toCharArray()), 3, 50); assertEquals( "0123456789012345678901234567890123456789012345678934567890123456789012345678901234567890123456789", t.toString()); t.setEmpty().append((CharSequence) new StringBuilder("01234567890123456789"), 5, 17); assertEquals((CharSequence) "567890123456", t.toString()); t.append(new StringBuffer(t)); assertEquals((CharSequence) "567890123456567890123456", t.toString()); // very wierd, to test if a subSlice is wrapped correct :) CharBuffer buf = CharBuffer.wrap("012345678901234567890123456789".toCharArray(), 3, 15); assertEquals("345678901234567", buf.toString()); t.setEmpty().append(buf, 1, 14); assertEquals("4567890123456", t.toString()); // finally use a completely custom CharSequence that is not catched by instanceof checks final String longTestString = "012345678901234567890123456789"; t.append( new CharSequence() { @Override public char charAt(int i) { return longTestString.charAt(i); } @Override public int length() { return longTestString.length(); } @Override public CharSequence subSequence(int start, int end) { return longTestString.subSequence(start, end); } @Override public String toString() { return longTestString; } }); assertEquals("4567890123456" + longTestString, t.toString()); }
public void testAttributeReflection() throws Exception { CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append("foobar"); TestUtil.assertAttributeReflection( t, new HashMap<String, Object>() { { put(CharTermAttribute.class.getName() + "#term", "foobar"); put(TermToBytesRefAttribute.class.getName() + "#bytes", new BytesRef("foobar")); } }); }
public void testCharSequenceInterface() { final String s = "0123456789"; final CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append(s); assertEquals(s.length(), t.length()); assertEquals("12", t.subSequence(1, 3).toString()); assertEquals(s, t.subSequence(0, s.length()).toString()); assertTrue(Pattern.matches("01\\d+", t)); assertTrue(Pattern.matches("34", t.subSequence(3, 5))); assertEquals(s.subSequence(3, 7).toString(), t.subSequence(3, 7).toString()); for (int i = 0; i < s.length(); i++) { assertTrue(t.charAt(i) == s.charAt(i)); } }
public void testNonCharSequenceAppend() { CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append("0123456789"); t.append("0123456789"); assertEquals("01234567890123456789", t.toString()); t.append(new StringBuilder("0123456789")); assertEquals("012345678901234567890123456789", t.toString()); CharTermAttribute t2 = new CharTermAttributeImpl(); t2.append("test"); t.append(t2); assertEquals("012345678901234567890123456789test", t.toString()); t.append((String) null); t.append((StringBuilder) null); t.append((CharTermAttribute) null); assertEquals("012345678901234567890123456789testnullnullnull", t.toString()); }
public void testAppendableInterface() { CharTermAttributeImpl t = new CharTermAttributeImpl(); Formatter formatter = new Formatter(t, Locale.ROOT); formatter.format("%d", 1234); assertEquals("1234", t.toString()); formatter.format("%d", 5678); assertEquals("12345678", t.toString()); t.append('9'); assertEquals("123456789", t.toString()); t.append((CharSequence) "0"); assertEquals("1234567890", t.toString()); t.append((CharSequence) "0123456789", 1, 3); assertEquals("123456789012", t.toString()); t.append((CharSequence) CharBuffer.wrap("0123456789".toCharArray()), 3, 5); assertEquals("12345678901234", t.toString()); t.append((CharSequence) t); assertEquals("1234567890123412345678901234", t.toString()); t.append((CharSequence) new StringBuilder("0123456789"), 5, 7); assertEquals("123456789012341234567890123456", t.toString()); t.append((CharSequence) new StringBuffer(t)); assertEquals("123456789012341234567890123456123456789012341234567890123456", t.toString()); // very wierd, to test if a subSlice is wrapped correct :) CharBuffer buf = CharBuffer.wrap("0123456789".toCharArray(), 3, 5); assertEquals("34567", buf.toString()); t.setEmpty().append((CharSequence) buf, 1, 2); assertEquals("4", t.toString()); CharTermAttribute t2 = new CharTermAttributeImpl(); t2.append("test"); t.append((CharSequence) t2); assertEquals("4test", t.toString()); t.append((CharSequence) t2, 1, 2); assertEquals("4teste", t.toString()); try { t.append((CharSequence) t2, 1, 5); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.append((CharSequence) t2, 1, 0); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } t.append((CharSequence) null); assertEquals("4testenull", t.toString()); }