001: package com.flexive.tests.disttools;
002:
003: import org.testng.annotations.Test;
004: import org.testng.Assert;
005: import org.apache.commons.lang.StringUtils;
006: import org.apache.commons.lang.RandomStringUtils;
007: import org.apache.commons.collections.EnumerationUtils;
008: import org.apache.tools.ant.Project;
009: import org.apache.tools.ant.ProjectHelper;
010: import org.apache.tools.ant.MagicNames;
011: import org.apache.tools.ant.BuildException;
012: import org.apache.tools.ant.input.InputHandler;
013: import org.apache.tools.ant.input.InputRequest;
014:
015: import java.io.*;
016: import java.util.zip.ZipFile;
017: import java.util.zip.ZipEntry;
018: import java.util.*;
019:
020: /**
021: * <p>
022: * Performs basic tests on the flexive distribution package, flexive-dist.zip.
023: * </p>
024: * <p>
025: * The location of the ZIP file must be set using the flexive.test.dist.file system property.
026: * </p>
027: * <p>
028: * For these tests to work, the flexive-dist.zip is extracted to a temporary directory
029: * under System.getProperty("java.io.tmpdir"), then the packaged ant is invoked programmatically.
030: * For some tests the java compiler (javac) has to be in the system path.
031: * </p>
032: *
033: * @author Daniel Lichtenberger, UCS
034: * @version $Rev$
035: */
036: public class DistPackageTest {
037: /**
038: * An ANT input handler that works off a predefined set of inputs.
039: */
040: private static class SimpleInputHandler implements InputHandler {
041: private final Queue<String> inputs = new LinkedList<String>();
042:
043: public SimpleInputHandler(String... inputs) {
044: this .inputs.addAll(Arrays.asList(inputs));
045: }
046:
047: public void handleInput(InputRequest request)
048: throws BuildException {
049: request.setInput(this .inputs.remove());
050: }
051: }
052:
053: @Test(groups="dist")
054: public void basicDistPackageAnalysis() throws IOException {
055: final ZipFile file = getDistFile();
056:
057: // parse zipfile entries, extract names
058: final Enumeration<? extends ZipEntry> entries = file.entries();
059: final List<String> entryNames = new ArrayList<String>();
060: while (entries.hasMoreElements()) {
061: final ZipEntry entry = entries.nextElement();
062: entryNames.add(entry.getName());
063: }
064:
065: // first check some mandatory files
066: for (String requiredEntry : new String[] {
067: // flexive JAR distribution
068: "/lib/flexive-ant.jar", "/lib/flexive-ejb.jar",
069: "/lib/flexive-extractor.jar",
070: "/lib/flexive-plugin-jsf-core.jar",
071: "/lib/flexive-shared.jar",
072: "/lib/flexive-sqlParser.jar",
073: "/lib/flexive-ui-shared.jar",
074:
075: // basic build files
076: "/build.xml", "/build.project.xml",
077: "/build.component.xml", "/database.properties" }) {
078: Assert.assertTrue(entryNames.contains("flexive-dist"
079: + requiredEntry));
080: }
081: }
082:
083: @Test(groups="dist")
084: public void extractDistPackageTest() throws IOException {
085: final File distDir = extractDistPackage();
086: final Project project = createProject(distDir);
087:
088: // create a project called "test"
089: project.setInputHandler(new SimpleInputHandler(
090: "project.create", // input for the main build menu
091: "test", // project name
092: "y" // accept input
093: ));
094: final String testProjectPath = distDir.getAbsolutePath()
095: + File.separator + ".." + File.separator + "test";
096: try {
097: project.executeTarget(project.getDefaultTarget());
098:
099: // now execute ant in the project directory, should create a file called dist/test.ear
100: final Project testProject = createProject(new File(
101: testProjectPath));
102: testProject.executeTarget(testProject.getDefaultTarget());
103: final String earPath = testProjectPath + File.separator
104: + "dist" + File.separator + "test.ear";
105: Assert.assertTrue(new File(earPath).exists(), earPath
106: + " not found");
107: } finally {
108: // clean up
109: deleteDirectory(new File(testProjectPath));
110: }
111: }
112:
113: private Project createProject(File basedir) {
114: final File buildFile = new File(basedir.getPath()
115: + File.separator + "build.xml");
116: final Project project = new Project();
117: project.setBaseDir(basedir);
118: project.init();
119: project.setUserProperty(MagicNames.ANT_FILE, buildFile
120: .getAbsolutePath());
121: ProjectHelper.configureProject(project, buildFile);
122: return project;
123: }
124:
125: /**
126: * Extracts the flexive-dist.zip package and returns the (temporary) directory handle. All extracted
127: * files will be registered for deletion on the JVM exit.
128: *
129: * @return the (temporary) directory handle.
130: * @throws IOException if the package could not be extracted
131: */
132: private File extractDistPackage() throws IOException {
133: final ZipFile file = getDistFile();
134: final File tempDir = createTempDir();
135: try {
136: final Enumeration<? extends ZipEntry> entries = file
137: .entries();
138: while (entries.hasMoreElements()) {
139: final ZipEntry zipEntry = entries.nextElement();
140: final String path = getTempPath(tempDir, zipEntry);
141: if (zipEntry.isDirectory()) {
142: final File dir = new File(path);
143: dir.mkdirs();
144: dir.deleteOnExit();
145: } else {
146: final InputStream in = file
147: .getInputStream(zipEntry);
148: final OutputStream out = new BufferedOutputStream(
149: new FileOutputStream(path));
150: // copy streams
151: final byte[] buffer = new byte[32768];
152: int len;
153: while ((len = in.read(buffer)) > 0) {
154: out.write(buffer, 0, len);
155: }
156: in.close();
157: out.close();
158: new File(path).deleteOnExit();
159: }
160: }
161: } finally {
162: file.close();
163: }
164: return new File(tempDir + File.separator + "flexive-dist");
165: }
166:
167: private String getTempPath(File tempDir, ZipEntry zipEntry) {
168: return tempDir.getPath() + File.separator + zipEntry.getName();
169: }
170:
171: private ZipFile getDistFile() throws IOException {
172: final String name = System
173: .getProperty("flexive.test.dist.file");
174: assert StringUtils.isNotBlank(name) : "flexive.test.dist.file property not set.";
175: return new ZipFile(name);
176: }
177:
178: private File createTempDir() {
179: final String tempDir = System.getProperty("java.io.tmpdir");
180: assert StringUtils.isNotBlank(tempDir);
181: final File dir = new File(tempDir + File.separator
182: + "flexive-dist-tests-temp-"
183: + RandomStringUtils.randomAlphanumeric(32));
184: dir.deleteOnExit();
185: dir.mkdirs();
186: return dir;
187: }
188:
189: private static void deleteDirectory(File dir) {
190: if (!dir.exists()) {
191: return;
192: }
193: if (dir.isDirectory()) {
194: for (File file : dir.listFiles()) {
195: deleteDirectory(file);
196: }
197: }
198: dir.delete();
199: }
200: }
|