001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.sample.library;
018:
019: import java.text.SimpleDateFormat;
020: import java.util.Calendar;
021:
022: import junit.framework.TestCase;
023:
024: import org.compass.core.Compass;
025: import org.compass.core.CompassCallbackWithoutResult;
026: import org.compass.core.CompassException;
027: import org.compass.core.CompassHits;
028: import org.compass.core.CompassSession;
029: import org.compass.core.CompassTemplate;
030: import org.compass.core.CompassTransaction;
031: import org.compass.core.Resource;
032: import org.compass.core.config.CompassConfiguration;
033:
034: /**
035: * Some tests that show the power of compass using a simple library domain
036: * model. Populated with some books and authors, and by no means it is a
037: * complete list.
038: *
039: * @author kimchy
040: */
041: public class LibraryTests extends TestCase {
042:
043: private Compass compass;
044:
045: private CompassTemplate compassTemplate;
046:
047: private Author jackLondon;
048:
049: private Book whiteFang;
050:
051: private Book callOfTheWild;
052:
053: private Author jamesClavell;
054:
055: private Book shogun;
056:
057: private Book taipan;
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061: CompassConfiguration config = new CompassConfiguration()
062: .configure(
063: "/org/compass/sample/library/compass.cfg.xml")
064: .addClass(Author.class).addClass(Article.class)
065: .addClass(Book.class);
066: compass = config.buildCompass();
067: compass.getSearchEngineIndexManager().deleteIndex();
068: compass.getSearchEngineIndexManager().createIndex();
069: compassTemplate = new CompassTemplate(compass);
070: }
071:
072: protected void tearDown() throws Exception {
073: compass.close();
074: super .tearDown();
075: }
076:
077: private void assertJackLondon(Author author) {
078: assertEquals(jackLondon.getId(), author.getId());
079: SimpleDateFormat sdf = new SimpleDateFormat(
080: Library.MetaData.Birthdate.Format);
081: assertEquals(sdf.format(jackLondon.getBirthdate()), sdf
082: .format(author.getBirthdate()));
083: assertEquals(jackLondon.getName().getFirstName(), author
084: .getName().getFirstName());
085: assertEquals(jackLondon.getName().getLastName(), author
086: .getName().getLastName());
087: assertEquals(2, author.getBooks().size());
088: assertEquals("White Fang", ((Book) author.getBooks().get(0))
089: .getTitle());
090: assertEquals("The Call of the Wild", ((Book) author.getBooks()
091: .get(1)).getTitle());
092: }
093:
094: public void setUpData() throws Exception {
095: CompassSession session = compass.openSession();
096: CompassTransaction tx = session.beginTransaction();
097:
098: jackLondon = new Author();
099: jackLondon.setId(new Long(1));
100: jackLondon.setName(new Name("Mr", "Jack", "London"));
101: Calendar c = Calendar.getInstance();
102: c.set(1876, 0, 12);
103: jackLondon.setBirthdate(c.getTime());
104: jackLondon.setKeywords(new String[] { "american author" });
105:
106: whiteFang = new Book();
107: whiteFang.setId(new Long(1));
108: whiteFang.setTitle("White Fang");
109: c.set(1906, 0, 1);
110: whiteFang.setPublishDate(c.getTime());
111: whiteFang
112: .setSummary("The remarkable story of a fiercely independent creature of the wild");
113: whiteFang.setKeywords(new String[] { "jack london",
114: "call of the wild" });
115: jackLondon.addBook(whiteFang);
116: // Need to save it explicitly for now (no cascading)
117: session.save(whiteFang);
118:
119: callOfTheWild = new Book();
120: callOfTheWild.setId(new Long(2));
121: callOfTheWild.setTitle("The Call of the Wild");
122: c.set(1903, 0, 1);
123: callOfTheWild.setPublishDate(c.getTime());
124: callOfTheWild
125: .setSummary("The Call of the Wild is a tale about unbreakable spirit");
126: callOfTheWild.setKeywords(new String[] { "jack london", "buck",
127: "white fang" });
128: jackLondon.addBook(callOfTheWild);
129: // Need to save it explicitly for now (no cascading)
130: session.save(callOfTheWild);
131:
132: session.save(jackLondon);
133:
134: jamesClavell = new Author();
135: jamesClavell.setId(new Long(2));
136: jamesClavell.setName(new Name("Mr", "James", "Clavell"));
137: c.set(1924, 9, 10);
138: jamesClavell.setBirthdate(c.getTime());
139: jamesClavell.setKeywords(new String[] { "far east", "shogun",
140: "japan", "hong kong" });
141:
142: shogun = new Book();
143: shogun.setId(new Long(3));
144: shogun.setTitle("Shogun");
145: c.set(1975, 0, 1);
146: shogun.setPublishDate(c.getTime());
147: shogun
148: .setSummary("A story of a hero who is not a person but a place and a time,"
149: + " medieval Japan on the threshold of becoming a sea power");
150: shogun.setKeywords(new String[] { "james clavell",
151: "Blackthorne", "Toranaga", "japan" });
152: jamesClavell.addBook(shogun);
153: session.save(shogun);
154:
155: taipan = new Book();
156: taipan.setId(new Long(4));
157: taipan.setTitle("Taipan");
158: c.set(1966, 0, 1);
159: taipan.setPublishDate(c.getTime());
160: taipan
161: .setSummary("Tai-Pan is chinese for \"supreme leader\". This is the man with real power "
162: + "to his hands. And such a Tai-Pan is Dirk Struan who is obsessed by his plan to make Hong Kong "
163: + "the \"jewel in the crown of her British Majesty\". In 1841 he achieves his goal but he has many "
164: + "enemies who try to destroy his plans. Will they succeed?");
165: taipan.setKeywords(new String[] { "james clavell",
166: "Dirk Struan", "joss", "hong kong" });
167: jamesClavell.addBook(taipan);
168:
169: session.save(taipan);
170:
171: session.save(jamesClavell);
172:
173: tx.commit();
174: session.close();
175: }
176:
177: public void testSetUpData() throws Exception {
178: setUpData();
179: // The only test not using the template...
180: CompassSession session = compass.openSession();
181: CompassTransaction tx = null;
182: try {
183: tx = session.beginTransaction();
184: Author author = (Author) session.load(Author.class,
185: jackLondon.getId());
186: assertJackLondon(author);
187: tx.commit();
188: } catch (Exception e) {
189: if (tx != null) {
190: tx.rollback();
191: }
192: throw e;
193: } finally {
194: session.close();
195: }
196: }
197:
198: public void testDeleteJackLondon() throws Exception {
199: setUpData();
200: compassTemplate.execute(new CompassCallbackWithoutResult() {
201: protected void doInCompassWithoutResult(
202: CompassSession session) throws CompassException {
203: // load jack london
204: Author author = (Author) session.load(Author.class,
205: jackLondon.getId());
206: assertJackLondon(author);
207: // delete it
208: session.delete(author);
209: // verify that we deleted jack
210: author = (Author) session.get(Author.class, jackLondon
211: .getId());
212: assertNull(author);
213: // no jack london books are deleted, since we don't support
214: // cascading (yet)
215: Book book = (Book) session.load(Book.class, whiteFang
216: .getId());
217: assertNotNull(book);
218: }
219: });
220: }
221:
222: public void testUpdateJackLondon() throws Exception {
223: setUpData();
224: compassTemplate.execute(new CompassCallbackWithoutResult() {
225: protected void doInCompassWithoutResult(
226: CompassSession session) throws CompassException {
227: Author author = (Author) session.load(Author.class,
228: jackLondon.getId());
229: assertJackLondon(author);
230:
231: author.getName().setFirstName("New Jack");
232: // have to save it (no automatic persistance yet)
233: session.save(author);
234:
235: author = (Author) session.load(Author.class, jackLondon
236: .getId());
237: assertEquals("New Jack", author.getName()
238: .getFirstName());
239: }
240: });
241: }
242:
243: public static void main(final String[] args) throws Exception {
244: if (args.length == 0) {
245: System.out.println("Must specify query string");
246: return;
247: }
248:
249: String tempQuery = "";
250: for (int i = 0; i < args.length; i++) {
251: tempQuery += args[i] + " ";
252: }
253: final String query = tempQuery;
254:
255: LibraryTests libraryTests = new LibraryTests();
256: libraryTests.setUp();
257: libraryTests.setUpData();
258:
259: libraryTests.compassTemplate
260: .execute(new CompassCallbackWithoutResult() {
261: protected void doInCompassWithoutResult(
262: CompassSession session)
263: throws CompassException {
264: CompassHits hits = session.find(query);
265:
266: System.out.println("Found [" + hits.getLength()
267: + "] hits for [" + args[0] + "] query");
268: System.out
269: .println("======================================================");
270: for (int i = 0; i < hits.getLength(); i++) {
271: print(hits, i);
272: }
273:
274: hits.close();
275: }
276: });
277:
278: libraryTests.tearDown();
279: }
280:
281: public static void print(CompassHits hits, int hitNumber) {
282: Object value = hits.data(hitNumber);
283: Resource resource = hits.resource(hitNumber);
284: System.out.println("ALIAS [" + resource.getAlias() + "] ID ["
285: + ((Identifiable) value).getId() + "] SCORE ["
286: + hits.score(hitNumber) + "]");
287: System.out.println(":::: " + value);
288: System.out.println("");
289: }
290: }
|