001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.batch;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: import org.junit.Test;
028: import org.kuali.workflow.test.WorkflowTestCase;
029: import org.springframework.core.io.Resource;
030: import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
031: import org.springframework.core.io.support.ResourcePatternResolver;
032: import org.springframework.util.FileCopyUtils;
033:
034: /**
035: * Tests XML "ingestion" pipeline
036: * @author Aaron Hamid (arh14 at cornell dot edu)
037: */
038: public class XmlIngestionTest extends WorkflowTestCase {
039:
040: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
041: .getLogger(XmlIngestionTest.class);
042:
043: private static final File TMP_DIR = new File(System
044: .getProperty("java.io.tmpdir"), "XmlIngestionTest_dir");
045: private static final File PENDING_DIR = new File(TMP_DIR, "pending");
046: private static final File LOADED_DIR = new File(TMP_DIR, "loaded");
047: private static final File PROBLEM_DIR = new File(TMP_DIR, "problem");
048:
049: public void setUp() throws Exception {
050: super .setUp();
051: deleteDirectories();
052: TMP_DIR.mkdirs();
053: PENDING_DIR.mkdirs();
054: LOADED_DIR.mkdirs();
055: PROBLEM_DIR.mkdirs();
056: }
057:
058: private void deleteContentsOfDir(File dir, int depth) {
059: File[] files = dir.listFiles();
060: if (files == null)
061: return;
062: for (int i = 0; i < files.length; i++) {
063: if (files[i].isDirectory() && depth > 0) {
064: // decrement depth
065: // to avoid the possibility of inadvertent
066: // recursive delete!
067: deleteContentsOfDir(files[i], depth - 1);
068: }
069: boolean success = files[i].delete();
070: LOG.info("deleting: " + files[i] + "..."
071: + (success ? "succeeded" : "failed"));
072: }
073: }
074:
075: public void tearDown() throws Exception {
076: try {
077: deleteDirectories();
078: } finally {
079: super .tearDown();
080: }
081: }
082:
083: protected void deleteDirectories() {
084: deleteContentsOfDir(PENDING_DIR, 0);
085: deleteContentsOfDir(LOADED_DIR, 2);
086: deleteContentsOfDir(PROBLEM_DIR, 2);
087: deleteContentsOfDir(TMP_DIR, 0);
088: TMP_DIR.delete();
089: }
090:
091: protected boolean verifyFileExists(File dir, File file)
092: throws IOException {
093: ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
094: Resource[] resources = resolver.getResources(dir.toURL()
095: + "/**/" + file.getName());
096: if (resources == null) {
097: return false;
098: }
099: for (int i = 0; i < resources.length; i++) {
100: if (resources[i].exists()) {
101: return true;
102: }
103: }
104: return false;
105: }
106:
107: /**
108: * TODO: beef this up
109: * need a reliable way to test if the file arrived in the right date-stamped
110: * subdirectory (maybe just pick the last, or first directory?)
111: */
112: @Test
113: public void testXmlIngestion() throws IOException {
114: XmlPollerServiceImpl poller = new XmlPollerServiceImpl();
115: poller.setPollIntervalSecs(1);
116: poller.setXmlParentDirectory(TMP_DIR.toString());
117: poller.setXmlPendingLocation(PENDING_DIR.toString());
118: poller.setXmlCompletedLocation(LOADED_DIR.toString());
119: poller.setXmlProblemLocation(PROBLEM_DIR.toString());
120:
121: Properties filesToIngest = new Properties();
122: filesToIngest.load(getClass().getResourceAsStream(
123: "XmlIngestionTest.txt"));
124: List pendingFiles = new LinkedList();
125: List shouldPass = new LinkedList();
126: List shouldFail = new LinkedList();
127: Iterator entries = filesToIngest.entrySet().iterator();
128: int i = 0;
129: while (entries.hasNext()) {
130: Map.Entry entry = (Map.Entry) entries.next();
131: File testFile = new File(entry.getKey().toString());
132: File pendingDir = new File(PENDING_DIR + "/TestDoc-" + i);
133: pendingDir.mkdirs();
134: assertTrue(pendingDir.isDirectory());
135: File pending = new File(pendingDir, testFile.getName());
136: pendingFiles.add(pending);
137: if (Boolean.valueOf(entry.getValue().toString())
138: .booleanValue()) {
139: shouldPass.add(pending);
140: } else {
141: shouldFail.add(pending);
142: }
143: FileCopyUtils.copy(testFile, pending);
144: LOG.info("created: " + pending);
145: i++;
146: }
147:
148: // poller should not throw exceptions
149: poller.run();
150:
151: // check that all files have been processed
152: Iterator it = pendingFiles.iterator();
153: while (it.hasNext()) {
154: File pending = (File) it.next();
155: assertTrue(!pending.isFile());
156: }
157:
158: // check that they landed in the appropriate location
159:
160: // loaded files should be in the loaded dir...
161: it = shouldPass.iterator();
162: while (it.hasNext()) {
163: File file = (File) it.next();
164: assertTrue("Loaded file " + file
165: + " was not moved to loaded directory "
166: + LOADED_DIR, verifyFileExists(LOADED_DIR, file));
167: assertFalse("Loaded file " + file
168: + " was moved to problem directory " + PROBLEM_DIR,
169: verifyFileExists(PROBLEM_DIR, file));
170: }
171: // and problem files should be in the problem dir...
172: it = shouldFail.iterator();
173: while (it.hasNext()) {
174: File file = (File) it.next();
175: assertTrue("Problem file " + file
176: + " was not moved to problem directory"
177: + PROBLEM_DIR, verifyFileExists(PROBLEM_DIR, file));
178: assertFalse("Problem file " + file
179: + " was moved to loaded directory" + LOADED_DIR,
180: verifyFileExists(LOADED_DIR, file));
181: }
182: }
183: }
|