001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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 org.apache.pluto.util.assemble;
018:
019: import java.io.File;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: /**
027: * Common base test class that should be used by assembler implementations that
028: * work with files using jar packaging (war, ear, etc.).
029: *
030: * This test ensures consistent behaviour across assembler implementations.
031: */
032: public abstract class ArchiveBasedAssemblyTest extends TestCase {
033:
034: private static final Log LOG = LogFactory
035: .getLog(ArchiveBasedAssemblyTest.class);
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: }
040:
041: /**
042: * Obtain the Assembler implementation from the subclass used
043: * to assemble the archive.
044: * @return the Assembler
045: */
046: protected abstract Assembler getAssemblerUnderTest();
047:
048: /**
049: * Obtain the archive (war, ear) to assemble from the subclass.
050: * @return the archive to assemble.
051: */
052: protected abstract File getFileToAssemble();
053:
054: public void testAssembleToNonExistantFile() throws Exception {
055: File fileToAssemble = getFileToAssemble();
056: Assembler underTest = getAssemblerUnderTest();
057: File destFile = File.createTempFile("jarAssemblyTest", ".tmp");
058: destFile.delete();
059:
060: assertFalse("Destination file [" + destFile.getAbsolutePath()
061: + "] already exists.", destFile.exists());
062:
063: AssemblerConfig config = prepareConfig(fileToAssemble, destFile);
064:
065: LOG.debug("Assembling [" + fileToAssemble.getAbsolutePath()
066: + "] to file [" + destFile.getAbsolutePath() + "]");
067:
068: underTest.assemble(config);
069:
070: assertTrue("Source archive ["
071: + fileToAssemble.getAbsolutePath()
072: + "] doesn't exist! "
073: + "Assembly may have deleted it by accident.",
074: fileToAssemble.exists());
075: assertTrue("Destination directory ["
076: + destFile.getParentFile().getAbsolutePath()
077: + "] does not exist, "
078: + "assembly did not complete properly.", destFile
079: .getParentFile().exists());
080: assertTrue("Assembled war file [" + destFile
081: + "] does not exist, "
082: + "assembly did not complete properly.", destFile
083: .exists());
084:
085: destFile.delete();
086: }
087:
088: public void testAssembleToDirectory() throws Exception {
089: File fileToAssemble = getFileToAssemble();
090: Assembler underTest = getAssemblerUnderTest();
091:
092: File tmpFile = File.createTempFile("jarAssemblyTest", ".tmp");
093: File destDir = new File(tmpFile.getParent(), tmpFile.getName()
094: + ".dir");
095: destDir.mkdir();
096:
097: assertTrue("Destination directory ["
098: + destDir.getAbsolutePath() + "] doesn't exist.",
099: destDir.exists());
100:
101: AssemblerConfig config = prepareConfig(fileToAssemble, destDir);
102:
103: LOG.debug("Assembling [" + fileToAssemble.getAbsolutePath()
104: + "] to directory [" + destDir.getAbsolutePath() + "]");
105:
106: underTest.assemble(config);
107:
108: File destFile = new File(destDir, fileToAssemble.getName());
109:
110: assertTrue(
111: "Source archive doesn't exist! Assembly may have deleted it by accident.",
112: fileToAssemble.exists());
113: assertTrue(
114: "Destination directory does not exist, assembly did not complete properly.",
115: destDir.exists());
116: assertTrue(
117: "Assembled war file does not exist, assembly did not complete properly.",
118: destFile.exists());
119:
120: tmpFile.delete();
121: destFile.delete();
122: destDir.delete();
123: }
124:
125: public void testAssembleToExistingFile() throws Exception {
126: File fileToAssemble = getFileToAssemble();
127: Assembler underTest = getAssemblerUnderTest();
128:
129: File destFile = File.createTempFile("jarAssemblyTest", ".tmp");
130:
131: assertTrue("Destination file [" + destFile.getAbsolutePath()
132: + "] should already exist.", destFile.exists());
133:
134: AssemblerConfig config = prepareConfig(fileToAssemble, destFile);
135:
136: LOG.debug("Assembling [" + fileToAssemble.getAbsolutePath()
137: + "] to file [" + destFile.getAbsolutePath() + "]");
138:
139: underTest.assemble(config);
140:
141: assertTrue(
142: "Source archive doesn't exist! Assembly may have deleted it by accident.",
143: fileToAssemble.exists());
144: assertTrue(
145: "Assembled war file does not exist, assembly did not complete properly.",
146: destFile.exists());
147:
148: destFile.delete();
149: }
150:
151: public void testAssembleToExistingFileInSubDirectory()
152: throws Exception {
153: File fileToAssemble = getFileToAssemble();
154: Assembler underTest = getAssemblerUnderTest();
155:
156: File tmpFile = File.createTempFile("jarAssemblyTest", ".tmp");
157: File destDir = new File(tmpFile.getName() + ".dir");
158: destDir.mkdirs();
159: File destFile = new File(destDir, fileToAssemble.getName());
160: destFile.createNewFile();
161:
162: assertTrue("Destination file [" + destFile.getAbsolutePath()
163: + "] should already exist.", destFile.exists());
164:
165: AssemblerConfig config = prepareConfig(fileToAssemble, destFile);
166:
167: LOG.debug("Assembling [" + fileToAssemble.getAbsolutePath()
168: + "] to file [" + destFile.getAbsolutePath() + "]");
169:
170: underTest.assemble(config);
171:
172: assertTrue(
173: "Source archive doesn't exist! Assembly may have deleted it by accident.",
174: fileToAssemble.exists());
175: assertTrue(
176: "Assembled war file does not exist, assembly did not complete properly.",
177: destFile.exists());
178:
179: destFile.delete();
180: destDir.delete();
181: tmpFile.delete();
182: }
183:
184: private AssemblerConfig prepareConfig(File source, File dest) {
185: AssemblerConfig config = new AssemblerConfig();
186: config.setDestination(dest);
187: config.setSource(source);
188: return config;
189: }
190:
191: }
|