KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > RAMDirectoryTestApp


1 package com.tctest;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.concurrent.BrokenBarrierException JavaDoc;
5 import java.util.concurrent.CyclicBarrier JavaDoc;
6
7 import org.apache.lucene.analysis.standard.StandardAnalyzer;
8 import org.apache.lucene.document.Document;
9 import org.apache.lucene.document.Field;
10 import org.apache.lucene.index.IndexWriter;
11 import org.apache.lucene.queryParser.QueryParser;
12 import org.apache.lucene.store.RAMDirectory;
13 import org.apache.lucene.search.Query;
14 import org.apache.lucene.search.IndexSearcher;
15 import org.apache.lucene.search.BooleanQuery;
16 import org.apache.lucene.search.Hit;
17 import org.apache.lucene.search.Hits;
18
19 import com.tc.util.Assert;
20 import com.tc.object.config.ConfigLockLevel;
21 import com.tc.object.config.ConfigVisitor;
22 import com.tc.object.config.DSOClientConfigHelper;
23 import com.tc.object.config.TransparencyClassSpec;
24 import com.tc.simulator.app.ApplicationConfig;
25 import com.tc.simulator.listener.ListenerProvider;
26 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
27
28 /**
29  * RAMDirectory clustering test
30  * @author jgalang
31  */

32 public class RAMDirectoryTestApp extends AbstractErrorCatchingTransparentApp {
33
34     static final int EXPECTED_THREAD_COUNT = 2;
35
36     private final CyclicBarrier JavaDoc barrier;
37
38     private final RAMDirectory clusteredDirectory;
39
40     private final StandardAnalyzer analyzer;
41
42     /**
43      * Inject Lucene 2.0.0 configuration, and instrument this test class
44      * @param visitor
45      * @param config
46      */

47     public static void visitL1DSOConfig(final ConfigVisitor visitor,
48             final DSOClientConfigHelper config) {
49         config.addNewModule("clustered-lucene-2.0.0", "1.0.0");
50         config.addAutolock("* *..*.*(..)", ConfigLockLevel.WRITE);
51
52         final String JavaDoc testClass = RAMDirectoryTestApp.class.getName();
53         final TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
54         spec.addRoot("barrier", "barrier");
55         spec.addRoot("clusteredDirectory", "clusteredDirectory");
56     }
57
58     public RAMDirectoryTestApp(final String JavaDoc appId, final ApplicationConfig cfg,
59             final ListenerProvider listenerProvider) {
60         super(appId, cfg, listenerProvider);
61         clusteredDirectory = new RAMDirectory();
62         analyzer = new StandardAnalyzer();
63         barrier = new CyclicBarrier JavaDoc(getParticipantCount());
64     }
65     
66     /**
67      * Test that the data written in the clustered RAMDirectory
68      * by one node, becomes available in the other.
69      */

70     protected void runTest() throws Throwable JavaDoc {
71         if (barrier.await() == 0) {
72             addDataToMap("DATA1");
73             letOtherNodeProceed();
74             waitForPermissionToProceed();
75             verifyEntries("DATA2");
76         } else {
77             waitForPermissionToProceed();
78             verifyEntries("DATA1");
79             addDataToMap("DATA2");
80             letOtherNodeProceed();
81         }
82         barrier.await();
83     }
84
85     // This is lame but it makes runTest() slightly more readable
86
private void letOtherNodeProceed() throws InterruptedException JavaDoc,
87             BrokenBarrierException JavaDoc {
88         barrier.await();
89     }
90
91     // This is lame but it makes runTest() slightly more readable
92
private void waitForPermissionToProceed() throws InterruptedException JavaDoc,
93             BrokenBarrierException JavaDoc {
94         barrier.await();
95     }
96
97     /**
98      * Add datum to RAMDirectory
99      * @param value The datum to add
100      * @throws Throwable
101      */

102     private void addDataToMap(final String JavaDoc value)
103             throws Throwable JavaDoc {
104         final Document doc = new Document();
105         doc.add(new Field("key", value, Field.Store.YES, Field.Index.TOKENIZED));
106         doc.add(new Field("value", value, Field.Store.YES,
107                 Field.Index.TOKENIZED));
108
109         synchronized (clusteredDirectory) {
110             final IndexWriter writer = new IndexWriter(this.clusteredDirectory,
111                     this.analyzer, this.clusteredDirectory.list().length == 0);
112             writer.addDocument(doc);
113             writer.optimize();
114             writer.close();
115         }
116     }
117
118     /**
119      * Attempt to retrieve datum from RAMDirectory.
120      * @param value The datum to retrieve
121      * @throws Exception
122      */

123     private void verifyEntries(final String JavaDoc value) throws Exception JavaDoc {
124         final StringBuffer JavaDoc data = new StringBuffer JavaDoc();
125         synchronized (clusteredDirectory) {
126             final QueryParser parser = new QueryParser("key", this.analyzer);
127             final Query query = parser.parse(value);
128             BooleanQuery.setMaxClauseCount(100000);
129             final IndexSearcher is = new IndexSearcher(this.clusteredDirectory);
130             final Hits hits = is.search(query);
131
132             for (Iterator JavaDoc i = hits.iterator(); i.hasNext();) {
133                 final Hit hit = (Hit) i.next();
134                 final Document document = hit.getDocument();
135                 data.append(document.get("value"));
136             }
137         }
138         Assert.assertEquals(value, data.toString());
139     }
140 }
141
Popular Tags