001: package org.compass.core.test.analyzer;
002:
003: import java.io.StringReader;
004:
005: import org.compass.core.CompassHits;
006: import org.compass.core.CompassQuery;
007: import org.compass.core.CompassSession;
008: import org.compass.core.CompassToken;
009: import org.compass.core.CompassTransaction;
010: import org.compass.core.Resource;
011: import org.compass.core.engine.SearchEngineException;
012: import org.compass.core.xml.AliasedXmlObject;
013: import org.compass.core.xml.dom4j.Dom4jAliasedXmlObject;
014: import org.dom4j.Document;
015: import org.dom4j.io.SAXReader;
016:
017: /**
018: * @author kimchy
019: */
020: public class XsemAnalyzerTests extends AbstractAnalyzerTests {
021:
022: public static final String XML_DATA = "<data><id>1</id><value>"
023: + TEXT + "</value><value2>" + TEXT + "</value2></data>";
024:
025: public static final String XML_DATA_SIMPLE_ANALYZER = "<data><id>1</id><value>"
026: + TEXT
027: + "</value><value2>"
028: + TEXT
029: + "</value2><analyzer>simple</analyzer></data>";
030:
031: protected String[] getMappings() {
032: return new String[] { "analyzer/xsem.cpm.xml" };
033: }
034:
035: protected AliasedXmlObject buildAliasedXmlObject(String alias)
036: throws Exception {
037: return buildAliasedXmlObject(alias, XML_DATA);
038: }
039:
040: protected AliasedXmlObject buildAliasedXmlObject(String alias,
041: String data) throws Exception {
042: SAXReader reader = new SAXReader();
043: Document document = reader.read(new StringReader(data));
044: return new Dom4jAliasedXmlObject(alias, document
045: .getRootElement());
046: }
047:
048: public void testXmlObjectNoAnalyzer() throws Exception {
049: CompassSession session = openSession();
050: CompassTransaction tr = session.beginTransaction();
051:
052: session.save(buildAliasedXmlObject("a"));
053:
054: CompassHits hits = session.find("a.value:the");
055: assertEquals(0, hits.getLength());
056: // test for the all property as well
057: hits = session.find("the");
058: assertEquals(0, hits.getLength());
059:
060: // this one will use the simple analyzer
061: Resource r = session.loadResource("a", new Long(1));
062: CompassToken[] tokens = session.analyzerHelper().setAnalyzer(
063: "simple").analyze(r.getValue("value"));
064: assertEquals(9, tokens.length);
065: assertEquals("the", tokens[0].getTermText());
066: assertEquals("quick", tokens[1].getTermText());
067: assertEquals("brown", tokens[2].getTermText());
068: assertEquals("fox", tokens[3].getTermText());
069:
070: // this one will use the default analyzer
071: tokens = session.analyzerHelper().setAnalyzerByAlias("a")
072: .analyze(r.getValue("value"));
073: assertEquals(7, tokens.length);
074: assertEquals("quick", tokens[0].getTermText());
075: assertEquals("brown", tokens[1].getTermText());
076: assertEquals("fox", tokens[2].getTermText());
077:
078: tr.commit();
079: session.close();
080: }
081:
082: public void testXmlObjectAnalyzerSetForResource() throws Exception {
083: CompassSession session = openSession();
084: CompassTransaction tr = session.beginTransaction();
085:
086: session.save(buildAliasedXmlObject("b"));
087:
088: CompassHits hits = session.find("b.value:the");
089: assertEquals(1, hits.getLength());
090: // test for the all property as well
091: hits = session.find("the");
092: assertEquals(1, hits.getLength());
093:
094: tr.commit();
095: }
096:
097: public void testXmlObjectAnalyzerSetForResourceWithCompassQuery()
098: throws Exception {
099: CompassSession session = openSession();
100: CompassTransaction tr = session.beginTransaction();
101:
102: session.save(buildAliasedXmlObject("b"));
103:
104: CompassHits hits = session.find("b.value:the");
105: assertEquals(1, hits.getLength());
106: // test for the all property as well
107: hits = session.find("the");
108: assertEquals(1, hits.length());
109:
110: // this will only work if we force the analyzer so it won't take into account mappings
111: CompassQuery query = session.queryBuilder().queryString(
112: "b.value:the").setAnalyzer("default").forceAnalyzer()
113: .toQuery();
114: assertEquals(0, query.hits().getLength());
115:
116: // here we don't force so we will get it
117: query = session.queryBuilder().queryString("b.value:the")
118: .setAnalyzer("default").toQuery();
119: assertEquals(1, query.hits().getLength());
120:
121: query = session.queryBuilder().queryString("b.value:the")
122: .setAnalyzer("simple").toQuery();
123: assertEquals(1, query.hits().length());
124:
125: tr.commit();
126: }
127:
128: public void testXmlObjectAnalyzerSetForProperty() throws Exception {
129: CompassSession session = openSession();
130: CompassTransaction tr = session.beginTransaction();
131:
132: session.save(buildAliasedXmlObject("d"));
133:
134: CompassHits hits = session.find("d.value:the");
135: assertEquals(1, hits.getLength());
136: hits = session.find("d.value2:the");
137: assertEquals(0, hits.getLength());
138: // test for the all property as well
139: hits = session.find("the");
140: assertEquals(1, hits.getLength());
141:
142: tr.commit();
143: }
144:
145: public void testXmlObjectAnalyzerSetForResourceAndProperty()
146: throws Exception {
147: CompassSession session = openSession();
148: CompassTransaction tr = session.beginTransaction();
149:
150: session.save(buildAliasedXmlObject("e"));
151:
152: CompassHits hits = session.find("value:the");
153: assertEquals(0, hits.getLength());
154: hits = session.find("value2:the");
155: assertEquals(1, hits.getLength());
156:
157: tr.commit();
158: }
159:
160: public void testXmlObjectAnalyzerController() throws Exception {
161: CompassSession session = openSession();
162: CompassTransaction tr = session.beginTransaction();
163:
164: session.save(buildAliasedXmlObject("g",
165: XML_DATA_SIMPLE_ANALYZER));
166:
167: CompassHits hits = session.find("g.value:the");
168: assertEquals(1, hits.getLength());
169:
170: try {
171: session.save(buildAliasedXmlObject("g", XML_DATA));
172: fail();
173: } catch (SearchEngineException e) {
174:
175: }
176:
177: tr.commit();
178: }
179:
180: public void testXmlObjectAnalyzerControllerWithNullAnalyzer()
181: throws Exception {
182: CompassSession session = openSession();
183: CompassTransaction tr = session.beginTransaction();
184:
185: session.save(buildAliasedXmlObject("g",
186: XML_DATA_SIMPLE_ANALYZER));
187:
188: CompassHits hits = session.find("value:the");
189: assertEquals(1, hits.getLength());
190:
191: session.save(buildAliasedXmlObject("h"));
192:
193: // analyzer controller can't affect query string (since we don't have the resource), just for simple and
194: // check that both h and i were saved
195: hits = session.queryBuilder().queryString("value:the")
196: .setAnalyzer("simple").forceAnalyzer().toQuery().hits();
197: assertEquals(2, hits.getLength());
198:
199: tr.commit();
200: }
201:
202: public void testXmlObjectAnalyzerControllerWithNullAnalyzerAndPropertyAnalyzer()
203: throws Exception {
204: CompassSession session = openSession();
205: CompassTransaction tr = session.beginTransaction();
206:
207: session.save(buildAliasedXmlObject("i",
208: XML_DATA_SIMPLE_ANALYZER));
209:
210: CompassHits hits = session.queryBuilder().queryString(
211: "value:the").setAnalyzer("simple").forceAnalyzer()
212: .toQuery().hits();
213: assertEquals(0, hits.getLength());
214:
215: tr.commit();
216: }
217:
218: }
|