001: /*
002: * Copyright 2003-2004 Michael Franken, Zilverline.
003: *
004: * The contents of this file, or the files included with this file, are subject to
005: * the current version of ZILVERLINE Collaborative Source License for the
006: * Zilverline Search Engine (the "License"); You may not use this file except in
007: * compliance with the License.
008: *
009: * You may obtain a copy of the License at
010: *
011: * http://www.zilverline.org.
012: *
013: * See the License for the rights, obligations and
014: * limitations governing use of the contents of the file.
015: *
016: * The Original and Upgraded Code is the Zilverline Search Engine. The developer of
017: * the Original and Upgraded Code is Michael Franken. Michael Franken owns the
018: * copyrights in the portions it created. All Rights Reserved.
019: *
020: */
021:
022: package org.zilverline.core;
023:
024: import java.io.File;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.ListIterator;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
033: import org.springframework.util.StringUtils;
034:
035: import org.zilverline.service.CollectionManager;
036:
037: /**
038: * Test class for Collection, self documenting.
039: *
040: * @author Michael Franken
041: *
042: * @see org.zilverline.core.FileSystemCollection
043: */
044: public class TestFileSystemCollection extends
045: AbstractDependencyInjectionSpringContextTests {
046: /** logger for Commons logging. */
047: private static Log log = LogFactory
048: .getLog(TestFileSystemCollection.class);
049:
050: protected String[] getConfigLocations() {
051: return new String[] { "applicationContext.xml" };
052: }
053:
054: public void testName() {
055: FileSystemCollection col = new FileSystemCollection();
056: assertEquals("", col.getName());
057: col.setName("test");
058: assertEquals("test", col.getName());
059: }
060:
061: public void testNoURL() {
062: FileSystemCollection col = new FileSystemCollection();
063: assertNull(col.getUrlDefault());
064: col
065: .setContentDir(new File(System
066: .getProperty("java.io.tmpdir")));
067: col.setUrl(null);
068: assertTrue(new File(System.getProperty("java.io.tmpdir"))
069: .exists());
070: assertTrue("need a URL", StringUtils.hasLength(col
071: .getUrlDefault()));
072: assertTrue(col.getUrlDefault().endsWith("/"));
073: }
074:
075: public void testURL() {
076: FileSystemCollection col = new FileSystemCollection();
077: assertNull(col.getUrlDefault());
078: String testString = "http://localhost/books";
079: col.setUrl(testString);
080: assertEquals("http://localhost/books/", col.getUrlDefault());
081: assertTrue(col.getUrlDefault().endsWith("/"));
082: }
083:
084: public void testNoCacheURL() {
085: FileSystemCollection col = new FileSystemCollection();
086: assertNotNull(col.getCacheDirWithManagerDefaults());
087: assertNotNull(col.getCacheUrlWithManagerDefaults());
088: col.setCacheDir(new File(System.getProperty("java.io.tmpdir")));
089: col.setCacheUrl(null);
090: assertTrue(new File(System.getProperty("java.io.tmpdir"))
091: .exists());
092: assertTrue("need a URL", StringUtils.hasLength(col
093: .getCacheUrlWithManagerDefaults()));
094: assertTrue(col.getCacheUrlWithManagerDefaults().endsWith("/"));
095: }
096:
097: public void testCacheURL() {
098: FileSystemCollection col = new FileSystemCollection();
099: assertNull(col.getUrlDefault());
100: String testString = "http://localhost/cachedBooks";
101: col.setCacheUrl(testString);
102: assertEquals("http://localhost/cachedBooks/", col
103: .getCacheUrlWithManagerDefaults());
104: assertTrue(col.getCacheUrlWithManagerDefaults().endsWith("/"));
105: }
106:
107: public final void testContentDir() {
108: try {
109: CollectionManager colMan = (CollectionManager) applicationContext
110: .getBean("collectionMan");
111: List collections = colMan.getCollections();
112:
113: for (Iterator iter = collections.iterator(); iter.hasNext();) {
114: DocumentCollection col = (DocumentCollection) iter
115: .next();
116:
117: assertTrue(col.getName() + " does not exist", col
118: .isIndexValid());
119: assertTrue(
120: col.getName() + " with url: '"
121: + col.getUrlDefault()
122: + "' does not end with /", col
123: .getUrlDefault().endsWith("/"));
124: }
125: } catch (Exception e) {
126: fail("Should not happen, Exception: " + e.getMessage());
127: }
128: }
129:
130: public final void testCollectionAddToManager() {
131: try {
132: CollectionManager colMan = (CollectionManager) applicationContext
133: .getBean("collectionMan");
134: int size = colMan.getCollections().size();
135:
136: FileSystemCollection col1 = new FileSystemCollection();
137: col1.setName("collection1");
138: colMan.addCollection(col1);
139: log.debug("New collection has got Id: " + col1.getId());
140: assertEquals(size + 1, colMan.getCollections().size());
141:
142: FileSystemCollection col2 = new FileSystemCollection();
143: col2.setName("collection2");
144: // give the new Collection an existing ID, then add it, it should replace instead of being added
145: col2.setId(col1.getId());
146: // test overridden equals method
147: assertTrue(col1.equals(col2));
148: colMan.addCollection(col2);
149: assertEquals(size + 1, colMan.getCollections().size());
150: colMan.deleteCollection(col1);
151: colMan.deleteCollection(col2);
152: } catch (Exception e) {
153: fail("Should not happen, Exception: " + e.getMessage());
154: }
155: }
156:
157: public final void testCacheDir() {
158: CollectionManager colMan = (CollectionManager) applicationContext
159: .getBean("collectionMan");
160: List collections = colMan.getCollections();
161:
162: for (Iterator iter = collections.iterator(); iter.hasNext();) {
163: DocumentCollection col = (DocumentCollection) iter.next();
164: // the following assertion should hold if the cacheDir exists, and is a Directory
165: if (col.getCacheDirWithManagerDefaults().exists()) {
166: assertTrue(col.getCacheUrlWithManagerDefaults(), col
167: .getCacheUrlWithManagerDefaults().endsWith("/"));
168: }
169: }
170: }
171:
172: public void testAllCollections() throws Exception {
173: CollectionManager c = (CollectionManager) applicationContext
174: .getBean("collectionMan");
175: assertNotNull(c);
176:
177: ListIterator li = c.getCollections().listIterator();
178: while (li.hasNext()) {
179: DocumentCollection col = (DocumentCollection) li.next();
180: log.debug("Collection " + col.getName());
181: if (col instanceof FileSystemCollection) {
182: assertTrue(col.getName(), ((FileSystemCollection) col)
183: .getContentDir().isDirectory());
184: log.debug(((FileSystemCollection) col).getContentDir()
185: + ", " + col.getIndexDirWithManagerDefaults()
186: + ", " + col.getCacheDirWithManagerDefaults());
187: log.debug(((FileSystemCollection) col).getContentDir()
188: .getAbsoluteFile());
189: }
190: log.debug(col.getName() + " has %%% "
191: + col.getNumberOfDocs() + " documents.");
192: }
193: }
194:
195: }
|