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