Source Code Cross Referenced for DistPackageTest.java in  » J2EE » fleXive » com » flexive » tests » disttools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » fleXive » com.flexive.tests.disttools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.