001: /*
002: * Copyright 2008 Outerthought bvba and Schaubroeck nv
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: package org.outerj.daisy.repository.test;
017:
018: import org.outerj.daisy.repository.*;
019: import org.outerj.daisy.repository.schema.RepositorySchema;
020: import org.outerj.daisy.repository.schema.FieldType;
021: import org.outerj.daisy.repository.schema.DocumentType;
022: import org.outerj.daisy.repository.schema.PartType;
023: import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase;
024: import org.outerj.daisy.repository.user.Role;
025: import org.outerj.daisy.tools.importexport.import_.BaseImportListener;
026: import org.outerj.daisy.tools.importexport.import_.Importer;
027: import org.outerj.daisy.tools.importexport.import_.config.ImportOptions;
028: import org.outerj.daisy.tools.importexport.import_.config.ImportOptionsFactory;
029: import org.outerj.daisy.tools.importexport.import_.fs.ImportFile;
030: import org.outerj.daisy.tools.importexport.import_.fs.local.LocalImportFile;
031: import org.outerj.daisy.tools.importexport.import_.schema.SchemaLoadListener;
032: import org.outerj.daisy.tools.importexport.import_.schema.BaseSchemaLoadListener;
033: import org.outerj.daisy.tools.importexport.docset.DocumentSet;
034: import org.outerj.daisy.tools.importexport.docset.QueryDocumentSet;
035: import org.outerj.daisy.tools.importexport.export.fs.ExportFile;
036: import org.outerj.daisy.tools.importexport.export.fs.LocalExportFile;
037: import org.outerj.daisy.tools.importexport.export.config.ExportOptions;
038: import org.outerj.daisy.tools.importexport.export.config.ExportOptionsFactory;
039: import org.outerj.daisy.tools.importexport.export.BaseExportListener;
040: import org.outerj.daisy.tools.importexport.export.Exporter;
041: import org.xml.sax.ContentHandler;
042: import org.xml.sax.SAXException;
043:
044: import java.io.File;
045: import java.util.Date;
046:
047: /**
048: * Testcase for the import/export tools.
049: */
050: public abstract class AbstractImpExpTest extends AbstractDaisyTestCase {
051: private RepositoryManager repositoryManager;
052: private Repository repository;
053:
054: protected boolean resetDataStores() {
055: return true;
056: }
057:
058: private void initRepositoryFields() throws Exception {
059: repositoryManager = getRepositoryManager();
060: repository = repositoryManager.getRepository(new Credentials(
061: "testuser", "testuser"));
062: repository.switchRole(Role.ADMINISTRATOR);
063: }
064:
065: public void testImpExp() throws Exception {
066: initRepositoryFields();
067:
068: //
069: // Common config
070: //
071:
072: String tmpDir = System.getProperty("java.io.tmpdir");
073: File expDir = new File(tmpDir, "daisy-impexp-test");
074: if (expDir.exists())
075: deleteDirectory(expDir);
076:
077: //
078: // Test export / import with empty repository
079: //
080: {
081: // Export
082: DocumentSet documentSet = new QueryDocumentSet(
083: "select id where true", repository);
084: ExportFile exportFile = new LocalExportFile(expDir);
085: Date exportTime = new Date();
086: ExportOptions exportOptions = ExportOptionsFactory
087: .getDefaultExportOptions();
088: MyExportListener listener = new MyExportListener();
089:
090: Exporter.run(documentSet, exportFile, exportTime,
091: repository, exportOptions, listener);
092: assertEquals(0, listener.getSucceeded().size());
093:
094: // Import
095: ImportFile importFile = new LocalImportFile(expDir);
096: ImportOptions importOptions = ImportOptionsFactory
097: .getDefaultImportOptions();
098: MyImportListener importListener = new MyImportListener();
099:
100: Importer.run(importFile, null, repository, importOptions,
101: importListener);
102:
103: // Normally there are no failures, but printing failure info is useful for debugging
104: System.out.println("Failed because access denied: "
105: + importListener.getFailedBecauseAccessDenied()
106: .size());
107: System.out.println("Failed because locked: "
108: + importListener.getFailedBecauseLockedDocument()
109: .size());
110: for (BaseImportListener.FailureInfo failureInfo : importListener
111: .getFailedDocuments()) {
112: System.out
113: .println("---------------------------------------------------------------------------");
114: System.out.println("Failure importing "
115: + failureInfo.getVariantKey());
116: System.out.println(failureInfo.getErrorDescription());
117: System.out.println(failureInfo.getStackTrace());
118: System.out
119: .println("---------------------------------------------------------------------------");
120: }
121: assertEquals(0, importListener.getSucceeded().size());
122: }
123:
124: //
125: // Test export / import with non-empty repository
126: // Before import, the repository is reset
127: //
128: {
129: // Create some content
130: RepositorySchema schema = repository.getRepositorySchema();
131:
132: FieldType stringField = schema.createFieldType(
133: "StringField", ValueType.STRING);
134: stringField.save();
135:
136: PartType partType = schema.createPartType("BinaryContent",
137: "");
138: partType.save();
139:
140: DocumentType docType = schema.createDocumentType("Basic");
141: docType.addFieldType(stringField, false);
142: docType.addPartType(partType, false);
143: docType.save();
144:
145: Document document = repository.createDocument("Document 1",
146: "Basic", "main", "default");
147: document.setField("StringField", "some value");
148: document
149: .setPart("BinaryContent",
150: "application/octet-stream", "abc"
151: .getBytes("UTF-8"));
152: document.save();
153:
154: // Export
155: DocumentSet documentSet = new QueryDocumentSet(
156: "select id where true", repository);
157: ExportFile exportFile = new LocalExportFile(expDir);
158: Date exportTime = new Date();
159: ExportOptions exportOptions = ExportOptionsFactory
160: .getDefaultExportOptions();
161: MyExportListener listener = new MyExportListener();
162:
163: Exporter.run(documentSet, exportFile, exportTime,
164: repository, exportOptions, listener);
165: assertEquals(1, listener.getSucceeded().size());
166:
167: // Clean repository, use another default namespace
168: restartRepository(true, "FOOBAR");
169: initRepositoryFields();
170:
171: // Import
172: ImportFile importFile = new LocalImportFile(expDir);
173: ImportOptions importOptions = ImportOptionsFactory
174: .getDefaultImportOptions();
175: MyImportListener importListener = new MyImportListener();
176:
177: Importer.run(importFile, null, repository, importOptions,
178: importListener);
179:
180: // Normally there are no failures, but printing failure info is useful for debugging
181: System.out.println("Failed because access denied: "
182: + importListener.getFailedBecauseAccessDenied()
183: .size());
184: System.out.println("Failed because locked: "
185: + importListener.getFailedBecauseLockedDocument()
186: .size());
187: for (BaseImportListener.FailureInfo failureInfo : importListener
188: .getFailedDocuments()) {
189: System.out
190: .println("---------------------------------------------------------------------------");
191: System.out.println("Failure importing "
192: + failureInfo.getVariantKey());
193: System.out.println(failureInfo.getErrorDescription());
194: System.out.println(failureInfo.getStackTrace());
195: System.out
196: .println("---------------------------------------------------------------------------");
197: }
198: assertEquals(1, importListener.getSucceeded().size());
199: }
200:
201: // TODO the testcase should test much more behavior:
202: // - test for conflicting namespaces
203: // - test fields with all datatypes and multivalue/hierarchical
204: // - test updates
205: // - test error handling
206: // - test various options
207: // - ...
208: }
209:
210: private void deleteDirectory(File dir) {
211: File[] files = dir.listFiles();
212: for (File file : files) {
213: if (file.isDirectory()) {
214: deleteDirectory(file);
215: } else {
216: file.delete();
217: }
218: }
219: dir.delete();
220: }
221:
222: private static class MyExportListener extends BaseExportListener {
223:
224: protected boolean includeFullStackTracesOfFailures() {
225: return true;
226: }
227:
228: protected String getStackTraceDisabledMessage() {
229: return null;
230: }
231:
232: public void info(String message) {
233: }
234:
235: public void startDocumentProgress(int total) {
236: }
237:
238: public void updateDocumentProgress(int current) {
239: }
240:
241: public void endDocumentProgress() {
242: }
243:
244: public boolean isInterrupted() {
245: return false;
246: }
247: }
248:
249: private static class MyImportListener extends BaseImportListener {
250: private SchemaLoadListener schemaListener;
251:
252: public MyImportListener() {
253: schemaListener = new BaseSchemaLoadListener() {
254: public boolean isInterrupted() {
255: return false;
256: }
257: };
258: }
259:
260: protected boolean includeFullStackTracesOfFailures() {
261: return true;
262: }
263:
264: protected String getStackTraceDisabledMessage() {
265: return null;
266: }
267:
268: public void generateSchemaSaxFragment(
269: ContentHandler contentHandler) throws SAXException {
270: }
271:
272: public void startActivity(String name) {
273: }
274:
275: public void info(String message) {
276: }
277:
278: public void debug(String message) {
279: }
280:
281: public void startDocumentProgress(int total) {
282: }
283:
284: public void updateDocumentProgress(int current) {
285: }
286:
287: public void endDocumentProgress() {
288: }
289:
290: public SchemaLoadListener getSchemaListener() {
291: return schemaListener;
292: }
293:
294: public boolean isInterrupted() {
295: return false;
296: }
297: }
298:
299: protected abstract RepositoryManager getRepositoryManager()
300: throws Exception;
301: }
|