Source Code Cross Referenced for TestDocument.java in  » Net » lucene-connector » org » apache » lucene » document » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » lucene connector » org.apache.lucene.document 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.document;
002:
003:        import org.apache.lucene.util.LuceneTestCase;
004:
005:        import org.apache.lucene.store.RAMDirectory;
006:        import org.apache.lucene.document.Document;
007:        import org.apache.lucene.document.Field;
008:        import org.apache.lucene.analysis.standard.StandardAnalyzer;
009:        import org.apache.lucene.index.IndexWriter;
010:        import org.apache.lucene.index.Term;
011:        import org.apache.lucene.search.Query;
012:        import org.apache.lucene.search.TermQuery;
013:        import org.apache.lucene.search.IndexSearcher;
014:        import org.apache.lucene.search.Searcher;
015:        import org.apache.lucene.search.Hits;
016:
017:        /**
018:         * Licensed to the Apache Software Foundation (ASF) under one or more
019:         * contributor license agreements.  See the NOTICE file distributed with
020:         * this work for additional information regarding copyright ownership.
021:         * The ASF licenses this file to You under the Apache License, Version 2.0
022:         * (the "License"); you may not use this file except in compliance with
023:         * the License.  You may obtain a copy of the License at
024:         *
025:         *     http://www.apache.org/licenses/LICENSE-2.0
026:         *
027:         * Unless required by applicable law or agreed to in writing, software
028:         * distributed under the License is distributed on an "AS IS" BASIS,
029:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
030:         * See the License for the specific language governing permissions and
031:         * limitations under the License.
032:         */
033:
034:        /**
035:         * Tests {@link Document} class.
036:         *
037:         *
038:         * @version $Id: TestDocument.java 583534 2007-10-10 16:46:35Z mikemccand $
039:         */
040:        public class TestDocument extends LuceneTestCase {
041:
042:            String binaryVal = "this text will be stored as a byte array in the index";
043:            String binaryVal2 = "this text will be also stored as a byte array in the index";
044:
045:            public void testBinaryField() throws Exception {
046:                Document doc = new Document();
047:                Fieldable stringFld = new Field("string", binaryVal,
048:                        Field.Store.YES, Field.Index.NO);
049:                Fieldable binaryFld = new Field("binary", binaryVal.getBytes(),
050:                        Field.Store.YES);
051:                Fieldable binaryFld2 = new Field("binary", binaryVal2
052:                        .getBytes(), Field.Store.YES);
053:
054:                doc.add(stringFld);
055:                doc.add(binaryFld);
056:
057:                assertEquals(2, doc.fields.size());
058:
059:                assertTrue(binaryFld.isBinary());
060:                assertTrue(binaryFld.isStored());
061:                assertFalse(binaryFld.isIndexed());
062:                assertFalse(binaryFld.isTokenized());
063:
064:                String binaryTest = new String(doc.getBinaryValue("binary"));
065:                assertTrue(binaryTest.equals(binaryVal));
066:
067:                String stringTest = doc.get("string");
068:                assertTrue(binaryTest.equals(stringTest));
069:
070:                doc.add(binaryFld2);
071:
072:                assertEquals(3, doc.fields.size());
073:
074:                byte[][] binaryTests = doc.getBinaryValues("binary");
075:
076:                assertEquals(2, binaryTests.length);
077:
078:                binaryTest = new String(binaryTests[0]);
079:                String binaryTest2 = new String(binaryTests[1]);
080:
081:                assertFalse(binaryTest.equals(binaryTest2));
082:
083:                assertTrue(binaryTest.equals(binaryVal));
084:                assertTrue(binaryTest2.equals(binaryVal2));
085:
086:                doc.removeField("string");
087:                assertEquals(2, doc.fields.size());
088:
089:                doc.removeFields("binary");
090:                assertEquals(0, doc.fields.size());
091:            }
092:
093:            /**
094:             * Tests {@link Document#removeField(String)} method for a brand new Document
095:             * that has not been indexed yet.
096:             *
097:             * @throws Exception on error
098:             */
099:            public void testRemoveForNewDocument() throws Exception {
100:                Document doc = makeDocumentWithFields();
101:                assertEquals(8, doc.fields.size());
102:                doc.removeFields("keyword");
103:                assertEquals(6, doc.fields.size());
104:                doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
105:                doc.removeFields("keyword"); // removing a field more than once
106:                assertEquals(6, doc.fields.size());
107:                doc.removeField("text");
108:                assertEquals(5, doc.fields.size());
109:                doc.removeField("text");
110:                assertEquals(4, doc.fields.size());
111:                doc.removeField("text");
112:                assertEquals(4, doc.fields.size());
113:                doc.removeField("doesnotexists"); // removing non-existing fields is siltenlty ignored
114:                assertEquals(4, doc.fields.size());
115:                doc.removeFields("unindexed");
116:                assertEquals(2, doc.fields.size());
117:                doc.removeFields("unstored");
118:                assertEquals(0, doc.fields.size());
119:                doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
120:                assertEquals(0, doc.fields.size());
121:            }
122:
123:            public void testConstructorExceptions() {
124:                new Field("name", "value", Field.Store.YES, Field.Index.NO); // okay
125:                new Field("name", "value", Field.Store.NO,
126:                        Field.Index.UN_TOKENIZED); // okay
127:                try {
128:                    new Field("name", "value", Field.Store.NO, Field.Index.NO);
129:                    fail();
130:                } catch (IllegalArgumentException e) {
131:                    // expected exception
132:                }
133:                new Field("name", "value", Field.Store.YES, Field.Index.NO,
134:                        Field.TermVector.NO); // okay
135:                try {
136:                    new Field("name", "value", Field.Store.YES, Field.Index.NO,
137:                            Field.TermVector.YES);
138:                    fail();
139:                } catch (IllegalArgumentException e) {
140:                    // expected exception
141:                }
142:            }
143:
144:            /**
145:             * Tests {@link Document#getValues(String)} method for a brand new Document
146:             * that has not been indexed yet.
147:             *
148:             * @throws Exception on error
149:             */
150:            public void testGetValuesForNewDocument() throws Exception {
151:                doAssert(makeDocumentWithFields(), false);
152:            }
153:
154:            /**
155:             * Tests {@link Document#getValues(String)} method for a Document retrieved from
156:             * an index.
157:             *
158:             * @throws Exception on error
159:             */
160:            public void testGetValuesForIndexedDocument() throws Exception {
161:                RAMDirectory dir = new RAMDirectory();
162:                IndexWriter writer = new IndexWriter(dir,
163:                        new StandardAnalyzer(), true);
164:                writer.addDocument(makeDocumentWithFields());
165:                writer.close();
166:
167:                Searcher searcher = new IndexSearcher(dir);
168:
169:                // search for something that does exists
170:                Query query = new TermQuery(new Term("keyword", "test1"));
171:
172:                // ensure that queries return expected results without DateFilter first
173:                Hits hits = searcher.search(query);
174:                assertEquals(1, hits.length());
175:
176:                doAssert(hits.doc(0), true);
177:                searcher.close();
178:            }
179:
180:            private Document makeDocumentWithFields() {
181:                Document doc = new Document();
182:                doc.add(new Field("keyword", "test1", Field.Store.YES,
183:                        Field.Index.UN_TOKENIZED));
184:                doc.add(new Field("keyword", "test2", Field.Store.YES,
185:                        Field.Index.UN_TOKENIZED));
186:                doc.add(new Field("text", "test1", Field.Store.YES,
187:                        Field.Index.TOKENIZED));
188:                doc.add(new Field("text", "test2", Field.Store.YES,
189:                        Field.Index.TOKENIZED));
190:                doc.add(new Field("unindexed", "test1", Field.Store.YES,
191:                        Field.Index.NO));
192:                doc.add(new Field("unindexed", "test2", Field.Store.YES,
193:                        Field.Index.NO));
194:                doc.add(new Field("unstored", "test1", Field.Store.NO,
195:                        Field.Index.TOKENIZED));
196:                doc.add(new Field("unstored", "test2", Field.Store.NO,
197:                        Field.Index.TOKENIZED));
198:                return doc;
199:            }
200:
201:            private void doAssert(Document doc, boolean fromIndex) {
202:                String[] keywordFieldValues = doc.getValues("keyword");
203:                String[] textFieldValues = doc.getValues("text");
204:                String[] unindexedFieldValues = doc.getValues("unindexed");
205:                String[] unstoredFieldValues = doc.getValues("unstored");
206:
207:                assertTrue(keywordFieldValues.length == 2);
208:                assertTrue(textFieldValues.length == 2);
209:                assertTrue(unindexedFieldValues.length == 2);
210:                // this test cannot work for documents retrieved from the index
211:                // since unstored fields will obviously not be returned
212:                if (!fromIndex) {
213:                    assertTrue(unstoredFieldValues.length == 2);
214:                }
215:
216:                assertTrue(keywordFieldValues[0].equals("test1"));
217:                assertTrue(keywordFieldValues[1].equals("test2"));
218:                assertTrue(textFieldValues[0].equals("test1"));
219:                assertTrue(textFieldValues[1].equals("test2"));
220:                assertTrue(unindexedFieldValues[0].equals("test1"));
221:                assertTrue(unindexedFieldValues[1].equals("test2"));
222:                // this test cannot work for documents retrieved from the index
223:                // since unstored fields will obviously not be returned
224:                if (!fromIndex) {
225:                    assertTrue(unstoredFieldValues[0].equals("test1"));
226:                    assertTrue(unstoredFieldValues[1].equals("test2"));
227:                }
228:            }
229:
230:            public void testFieldSetValue() throws Exception {
231:
232:                Field field = new Field("id", "id1", Field.Store.YES,
233:                        Field.Index.UN_TOKENIZED);
234:                Document doc = new Document();
235:                doc.add(field);
236:                doc.add(new Field("keyword", "test", Field.Store.YES,
237:                        Field.Index.UN_TOKENIZED));
238:
239:                RAMDirectory dir = new RAMDirectory();
240:                IndexWriter writer = new IndexWriter(dir,
241:                        new StandardAnalyzer(), true);
242:                writer.addDocument(doc);
243:                field.setValue("id2");
244:                writer.addDocument(doc);
245:                field.setValue("id3");
246:                writer.addDocument(doc);
247:                writer.close();
248:
249:                Searcher searcher = new IndexSearcher(dir);
250:
251:                Query query = new TermQuery(new Term("keyword", "test"));
252:
253:                // ensure that queries return expected results without DateFilter first
254:                Hits hits = searcher.search(query);
255:                assertEquals(3, hits.length());
256:                int result = 0;
257:                for (int i = 0; i < 3; i++) {
258:                    Document doc2 = hits.doc(i);
259:                    Field f = doc2.getField("id");
260:                    if (f.stringValue().equals("id1"))
261:                        result |= 1;
262:                    else if (f.stringValue().equals("id2"))
263:                        result |= 2;
264:                    else if (f.stringValue().equals("id3"))
265:                        result |= 4;
266:                    else
267:                        fail("unexpected id field");
268:                }
269:                searcher.close();
270:                dir.close();
271:                assertEquals("did not see all IDs", 7, result);
272:            }
273:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.