1 package org.apache.lucene.analysis.tokenattributes;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.util.LuceneTestCase;
22 public class TestTermAttributeImpl extends LuceneTestCase {
24 public void testResize() {
25 TermAttributeImpl t = new TermAttributeImpl();
26 char[] content = "hello".toCharArray();
27 t.setTermBuffer(content, 0, content.length);
28 for (int i = 0; i < 2000; i++)
30 t.resizeTermBuffer(i);
31 assertTrue(i <= t.termBuffer().length);
32 assertEquals("hello", t.term());
36 public void testGrow() {
37 TermAttributeImpl t = new TermAttributeImpl();
38 StringBuilder buf = new StringBuilder("ab");
39 for (int i = 0; i < 20; i++)
41 char[] content = buf.toString().toCharArray();
42 t.setTermBuffer(content, 0, content.length);
43 assertEquals(buf.length(), t.termLength());
44 assertEquals(buf.toString(), t.term());
45 buf.append(buf.toString());
47 assertEquals(1048576, t.termLength());
49 // now as a string, first variant
50 t = new TermAttributeImpl();
51 buf = new StringBuilder("ab");
52 for (int i = 0; i < 20; i++)
54 String content = buf.toString();
55 t.setTermBuffer(content, 0, content.length());
56 assertEquals(content.length(), t.termLength());
57 assertEquals(content, t.term());
60 assertEquals(1048576, t.termLength());
62 // now as a string, second variant
63 t = new TermAttributeImpl();
64 buf = new StringBuilder("ab");
65 for (int i = 0; i < 20; i++)
67 String content = buf.toString();
68 t.setTermBuffer(content);
69 assertEquals(content.length(), t.termLength());
70 assertEquals(content, t.term());
73 assertEquals(1048576, t.termLength());
75 // Test for slow growth to a long term
76 t = new TermAttributeImpl();
77 buf = new StringBuilder("a");
78 for (int i = 0; i < 20000; i++)
80 String content = buf.toString();
81 t.setTermBuffer(content);
82 assertEquals(content.length(), t.termLength());
83 assertEquals(content, t.term());
86 assertEquals(20000, t.termLength());
88 // Test for slow growth to a long term
89 t = new TermAttributeImpl();
90 buf = new StringBuilder("a");
91 for (int i = 0; i < 20000; i++)
93 String content = buf.toString();
94 t.setTermBuffer(content);
95 assertEquals(content.length(), t.termLength());
96 assertEquals(content, t.term());
99 assertEquals(20000, t.termLength());
102 public void testToString() throws Exception {
103 char[] b = {'a', 'l', 'o', 'h', 'a'};
104 TermAttributeImpl t = new TermAttributeImpl();
105 t.setTermBuffer(b, 0, 5);
106 assertEquals("aloha", t.toString());
108 t.setTermBuffer("hi there");
109 assertEquals("hi there", t.toString());
112 public void testMixedStringArray() throws Exception {
113 TermAttributeImpl t = new TermAttributeImpl();
114 t.setTermBuffer("hello");
115 assertEquals(t.termLength(), 5);
116 assertEquals(t.term(), "hello");
117 t.setTermBuffer("hello2");
118 assertEquals(t.termLength(), 6);
119 assertEquals(t.term(), "hello2");
120 t.setTermBuffer("hello3".toCharArray(), 0, 6);
121 assertEquals(t.term(), "hello3");
123 // Make sure if we get the buffer and change a character
124 // that term() reflects the change
125 char[] buffer = t.termBuffer();
127 assertEquals(t.term(), "hollo3");
130 public void testClone() throws Exception {
131 TermAttributeImpl t = new TermAttributeImpl();
132 char[] content = "hello".toCharArray();
133 t.setTermBuffer(content, 0, 5);
134 char[] buf = t.termBuffer();
135 TermAttributeImpl copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCloneIsEqual(t);
136 assertEquals(t.term(), copy.term());
137 assertNotSame(buf, copy.termBuffer());
140 public void testEquals() throws Exception {
141 TermAttributeImpl t1a = new TermAttributeImpl();
142 char[] content1a = "hello".toCharArray();
143 t1a.setTermBuffer(content1a, 0, 5);
144 TermAttributeImpl t1b = new TermAttributeImpl();
145 char[] content1b = "hello".toCharArray();
146 t1b.setTermBuffer(content1b, 0, 5);
147 TermAttributeImpl t2 = new TermAttributeImpl();
148 char[] content2 = "hello2".toCharArray();
149 t2.setTermBuffer(content2, 0, 6);
150 assertTrue(t1a.equals(t1b));
151 assertFalse(t1a.equals(t2));
152 assertFalse(t2.equals(t1b));
155 public void testCopyTo() throws Exception {
156 TermAttributeImpl t = new TermAttributeImpl();
157 TermAttributeImpl copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
158 assertEquals("", t.term());
159 assertEquals("", copy.term());
161 t = new TermAttributeImpl();
162 char[] content = "hello".toCharArray();
163 t.setTermBuffer(content, 0, 5);
164 char[] buf = t.termBuffer();
165 copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
166 assertEquals(t.term(), copy.term());
167 assertNotSame(buf, copy.termBuffer());