Source Code Cross Referenced for TestRAMDirectory.java in  » Net » lucene-connector » org » apache » lucene » index » store » 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.index.store 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.index.store;
002:
003:        /**
004:         * Licensed to the Apache Software Foundation (ASF) under one or more
005:         * contributor license agreements.  See the NOTICE file distributed with
006:         * this work for additional information regarding copyright ownership.
007:         * The ASF licenses this file to You under the Apache License, Version 2.0
008:         * (the "License"); you may not use this file except in compliance with
009:         * the License.  You may obtain a copy of the License at
010:         *
011:         *     http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS,
015:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:         * See the License for the specific language governing permissions and
017:         * limitations under the License.
018:         */
019:
020:        import java.io.File;
021:        import java.io.IOException;
022:        import java.io.ObjectOutput;
023:        import java.io.ObjectOutputStream;
024:        import java.io.ByteArrayOutputStream;
025:
026:        import org.apache.lucene.util.LuceneTestCase;
027:
028:        import org.apache.lucene.analysis.WhitespaceAnalyzer;
029:        import org.apache.lucene.document.Document;
030:        import org.apache.lucene.document.Field;
031:        import org.apache.lucene.index.IndexReader;
032:        import org.apache.lucene.index.IndexWriter;
033:        import org.apache.lucene.search.IndexSearcher;
034:        import org.apache.lucene.store.Directory;
035:        import org.apache.lucene.store.FSDirectory;
036:        import org.apache.lucene.store.RAMDirectory;
037:        import org.apache.lucene.util.English;
038:
039:        import org.apache.lucene.store.MockRAMDirectory;
040:
041:        /**
042:         * JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases,
043:         * but not one of them uses an different constructor other than the default constructor.
044:         * 
045:         * @author Bernhard Messer
046:         * 
047:         * @version $Id: RAMDirectory.java 150537 2004-09-28 22:45:26 +0200 (Di, 28 Sep 2004) cutting $
048:         */
049:        public class TestRAMDirectory extends LuceneTestCase {
050:
051:            private File indexDir = null;
052:
053:            // add enough document so that the index will be larger than RAMDirectory.READ_BUFFER_SIZE
054:            private final int docsToAdd = 500;
055:
056:            // setup the index
057:            public void setUp() throws Exception {
058:                super .setUp();
059:                String tempDir = System.getProperty("java.io.tmpdir");
060:                if (tempDir == null)
061:                    throw new IOException(
062:                            "java.io.tmpdir undefined, cannot run test");
063:                indexDir = new File(tempDir, "RAMDirIndex");
064:
065:                IndexWriter writer = new IndexWriter(indexDir,
066:                        new WhitespaceAnalyzer(), true);
067:                // add some documents
068:                Document doc = null;
069:                for (int i = 0; i < docsToAdd; i++) {
070:                    doc = new Document();
071:                    doc.add(new Field("content",
072:                            English.intToEnglish(i).trim(), Field.Store.YES,
073:                            Field.Index.UN_TOKENIZED));
074:                    writer.addDocument(doc);
075:                }
076:                assertEquals(docsToAdd, writer.docCount());
077:                writer.close();
078:            }
079:
080:            public void testRAMDirectory() throws IOException {
081:
082:                Directory dir = FSDirectory.getDirectory(indexDir);
083:                MockRAMDirectory ramDir = new MockRAMDirectory(dir);
084:
085:                // close the underlaying directory
086:                dir.close();
087:
088:                // Check size
089:                assertEquals(ramDir.sizeInBytes(), ramDir
090:                        .getRecomputedSizeInBytes());
091:
092:                // open reader to test document count
093:                IndexReader reader = IndexReader.open(ramDir);
094:                assertEquals(docsToAdd, reader.numDocs());
095:
096:                // open search zo check if all doc's are there
097:                IndexSearcher searcher = new IndexSearcher(reader);
098:
099:                // search for all documents
100:                for (int i = 0; i < docsToAdd; i++) {
101:                    Document doc = searcher.doc(i);
102:                    assertTrue(doc.getField("content") != null);
103:                }
104:
105:                // cleanup
106:                reader.close();
107:                searcher.close();
108:            }
109:
110:            public void testRAMDirectoryFile() throws IOException {
111:
112:                MockRAMDirectory ramDir = new MockRAMDirectory(indexDir);
113:
114:                // Check size
115:                assertEquals(ramDir.sizeInBytes(), ramDir
116:                        .getRecomputedSizeInBytes());
117:
118:                // open reader to test document count
119:                IndexReader reader = IndexReader.open(ramDir);
120:                assertEquals(docsToAdd, reader.numDocs());
121:
122:                // open search zo check if all doc's are there
123:                IndexSearcher searcher = new IndexSearcher(reader);
124:
125:                // search for all documents
126:                for (int i = 0; i < docsToAdd; i++) {
127:                    Document doc = searcher.doc(i);
128:                    assertTrue(doc.getField("content") != null);
129:                }
130:
131:                // cleanup
132:                reader.close();
133:                searcher.close();
134:            }
135:
136:            public void testRAMDirectoryString() throws IOException {
137:
138:                MockRAMDirectory ramDir = new MockRAMDirectory(indexDir
139:                        .getCanonicalPath());
140:
141:                // Check size
142:                assertEquals(ramDir.sizeInBytes(), ramDir
143:                        .getRecomputedSizeInBytes());
144:
145:                // open reader to test document count
146:                IndexReader reader = IndexReader.open(ramDir);
147:                assertEquals(docsToAdd, reader.numDocs());
148:
149:                // open search zo check if all doc's are there
150:                IndexSearcher searcher = new IndexSearcher(reader);
151:
152:                // search for all documents
153:                for (int i = 0; i < docsToAdd; i++) {
154:                    Document doc = searcher.doc(i);
155:                    assertTrue(doc.getField("content") != null);
156:                }
157:
158:                // cleanup
159:                reader.close();
160:                searcher.close();
161:            }
162:
163:            private final int numThreads = 50;
164:            private final int docsPerThread = 40;
165:
166:            public void testRAMDirectorySize() throws IOException,
167:                    InterruptedException {
168:
169:                final MockRAMDirectory ramDir = new MockRAMDirectory(indexDir
170:                        .getCanonicalPath());
171:                final IndexWriter writer = new IndexWriter(ramDir,
172:                        new WhitespaceAnalyzer(), false);
173:                writer.optimize();
174:
175:                assertEquals(ramDir.sizeInBytes(), ramDir
176:                        .getRecomputedSizeInBytes());
177:
178:                Thread[] threads = new Thread[numThreads];
179:                for (int i = 0; i < numThreads; i++) {
180:                    final int num = i;
181:                    threads[i] = new Thread() {
182:                        public void run() {
183:                            for (int j = 1; j < docsPerThread; j++) {
184:                                Document doc = new Document();
185:                                doc.add(new Field("sizeContent", English
186:                                        .intToEnglish(num * docsPerThread + j)
187:                                        .trim(), Field.Store.YES,
188:                                        Field.Index.UN_TOKENIZED));
189:                                try {
190:                                    writer.addDocument(doc);
191:                                } catch (IOException e) {
192:                                    throw new RuntimeException(e);
193:                                }
194:                                synchronized (ramDir) {
195:                                    assertEquals(ramDir.sizeInBytes(), ramDir
196:                                            .getRecomputedSizeInBytes());
197:                                }
198:                            }
199:                        }
200:                    };
201:                }
202:                for (int i = 0; i < numThreads; i++)
203:                    threads[i].start();
204:                for (int i = 0; i < numThreads; i++)
205:                    threads[i].join();
206:
207:                writer.optimize();
208:                assertEquals(ramDir.sizeInBytes(), ramDir
209:                        .getRecomputedSizeInBytes());
210:
211:                writer.close();
212:            }
213:
214:            public void testSerializable() throws IOException {
215:                Directory dir = new RAMDirectory();
216:                ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
217:                assertEquals("initially empty", 0, bos.size());
218:                ObjectOutput out = new ObjectOutputStream(bos);
219:                int headerSize = bos.size();
220:                out.writeObject(dir);
221:                out.close();
222:                assertTrue("contains more then just header", headerSize < bos
223:                        .size());
224:            }
225:
226:            public void tearDown() throws Exception {
227:                super .tearDown();
228:                // cleanup 
229:                if (indexDir != null && indexDir.exists()) {
230:                    rmDir(indexDir);
231:                }
232:            }
233:
234:            private void rmDir(File dir) {
235:                File[] files = dir.listFiles();
236:                for (int i = 0; i < files.length; i++) {
237:                    files[i].delete();
238:                }
239:                dir.delete();
240:            }
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.