001: package com.tctest;
002:
003: import org.apache.lucene.analysis.standard.StandardAnalyzer;
004: import org.apache.lucene.document.Document;
005: import org.apache.lucene.document.Field;
006: import org.apache.lucene.index.IndexWriter;
007: import org.apache.lucene.queryParser.QueryParser;
008: import org.apache.lucene.search.BooleanQuery;
009: import org.apache.lucene.search.Hit;
010: import org.apache.lucene.search.Hits;
011: import org.apache.lucene.search.IndexSearcher;
012: import org.apache.lucene.search.Query;
013: import org.apache.lucene.store.RAMDirectory;
014:
015: import com.tc.object.config.ConfigVisitor;
016: import com.tc.object.config.DSOClientConfigHelper;
017: import com.tc.object.config.TransparencyClassSpec;
018: import com.tc.simulator.app.ApplicationConfig;
019: import com.tc.simulator.listener.ListenerProvider;
020: import com.tc.util.Assert;
021: import com.tc.util.TIMUtil;
022: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
023:
024: import java.util.Iterator;
025: import java.util.concurrent.BrokenBarrierException;
026: import java.util.concurrent.CyclicBarrier;
027:
028: /**
029: * RAMDirectory clustering test
030: *
031: * @author jgalang
032: */
033: public class RAMDirectoryTestApp extends
034: AbstractErrorCatchingTransparentApp {
035:
036: static final int EXPECTED_THREAD_COUNT = 2;
037:
038: private final CyclicBarrier barrier;
039:
040: private final RAMDirectory clusteredDirectory;
041:
042: private final StandardAnalyzer analyzer;
043:
044: /**
045: *
046: * @param appId
047: * @param cfg
048: * @param listenerProvider
049: */
050: public RAMDirectoryTestApp(final String appId,
051: final ApplicationConfig cfg,
052: final ListenerProvider listenerProvider) {
053: super (appId, cfg, listenerProvider);
054: clusteredDirectory = new RAMDirectory();
055: analyzer = new StandardAnalyzer();
056: barrier = new CyclicBarrier(getParticipantCount());
057: }
058:
059: /**
060: * Inject Lucene 2.0.0 configuration, and instrument this test class
061: *
062: * @param visitor
063: * @param config
064: */
065: public static void visitL1DSOConfig(final ConfigVisitor visitor,
066: final DSOClientConfigHelper config) {
067: config.addModule(TIMUtil.LUCENE_2_0_0, TIMUtil
068: .getVersion(TIMUtil.LUCENE_2_0_0));
069:
070: final String testClass = RAMDirectoryTestApp.class.getName();
071: final TransparencyClassSpec spec = config
072: .getOrCreateSpec(testClass);
073: spec.addRoot("barrier", "barrier");
074: spec.addRoot("clusteredDirectory", "clusteredDirectory");
075: }
076:
077: /**
078: * Test that the data written in the clustered RAMDirectory by one node, becomes available in the other.
079: */
080: protected void runTest() throws Throwable {
081: if (barrier.await() == 0) {
082: addDataToMap("DATA1");
083: letOtherNodeProceed();
084: waitForPermissionToProceed();
085: verifyEntries("DATA2");
086: } else {
087: waitForPermissionToProceed();
088: verifyEntries("DATA1");
089: addDataToMap("DATA2");
090: letOtherNodeProceed();
091: }
092: barrier.await();
093: }
094:
095: // This is lame but it makes runTest() slightly more readable
096: private void letOtherNodeProceed() throws InterruptedException,
097: BrokenBarrierException {
098: barrier.await();
099: }
100:
101: // This is lame but it makes runTest() slightly more readable
102: private void waitForPermissionToProceed()
103: throws InterruptedException, BrokenBarrierException {
104: barrier.await();
105: }
106:
107: /**
108: * Add datum to RAMDirectory
109: *
110: * @param value The datum to add
111: * @throws Throwable
112: */
113: private void addDataToMap(final String value) throws Throwable {
114: final Document doc = new Document();
115: doc.add(new Field("key", value, Field.Store.YES,
116: Field.Index.TOKENIZED));
117: doc.add(new Field("value", value, Field.Store.YES,
118: Field.Index.TOKENIZED));
119:
120: final IndexWriter writer = new IndexWriter(
121: this .clusteredDirectory, this .analyzer,
122: this .clusteredDirectory.list().length == 0);
123: writer.addDocument(doc);
124: writer.optimize();
125: writer.close();
126: }
127:
128: /**
129: * Attempt to retrieve datum from RAMDirectory.
130: *
131: * @param value The datum to retrieve
132: * @throws Exception
133: */
134: private void verifyEntries(final String value) throws Exception {
135: final StringBuffer data = new StringBuffer();
136:
137: final QueryParser parser = new QueryParser("key", this .analyzer);
138: final Query query = parser.parse(value);
139: BooleanQuery.setMaxClauseCount(100000);
140: final IndexSearcher is = new IndexSearcher(
141: this .clusteredDirectory);
142: final Hits hits = is.search(query);
143:
144: for (Iterator i = hits.iterator(); i.hasNext();) {
145: final Hit hit = (Hit) i.next();
146: final Document document = hit.getDocument();
147: data.append(document.get("value"));
148: }
149:
150: Assert.assertEquals(value, data.toString());
151: }
152: }
|