Author: snicoll Date: Sun Apr 22 08:46:28 2007 New Revision: 531206 URL: http://svn.apache.org/viewvc?view=rev&rev=531206 Log: MWAR-97: cleaned up the tests a bit; added helpers to create a full test suite of the overlay functionnality.
Added: maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/WarOverlaysTest.java maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/stub/WarOverlayStub.java maven/plugins/branches/MWAR-97/src/test/resources/overlays/ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/WEB-INF/ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/WEB-INF/web.xml maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/index.jsp maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/login.jsp maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/WEB-INF/ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/WEB-INF/web.xml maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/admin.jsp maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/index.jsp maven/plugins/trunk/maven-war-plugin/src/test/resources/unit/waroverlays/ maven/plugins/trunk/maven-war-plugin/src/test/resources/unit/waroverlays/default.xml Modified: maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java Modified: maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java?view=diff&rev=531206&r1=531205&r2=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java (original) +++ maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java Sun Apr 22 08:46:28 2007 @@ -20,11 +20,17 @@ */ import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.stubs.ArtifactStub; import org.apache.maven.plugin.war.stub.MavenProjectBasicStub; +import org.apache.maven.plugin.war.stub.WarOverlayStub; +import org.codehaus.plexus.archiver.Archiver; +import org.codehaus.plexus.archiver.ArchiverException; +import org.codehaus.plexus.archiver.jar.JarArchiver; import org.codehaus.plexus.archiver.war.WarArchiver; import org.codehaus.plexus.util.FileUtils; import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -33,6 +39,10 @@ extends AbstractMojoTestCase { + protected static final File OVERLAYS_TEMP_DIR = new File( getBasedir(), "target/test-overlays/" ); + + protected static final File OVERLAYS_ROOT_DIR = new File( getBasedir(), "target/test-classes/overlays/" ); + protected abstract File getTestDirectory() throws Exception; @@ -235,4 +245,64 @@ assertTrue( "Can't delete directory: WEB-INF", ( new File( warDirectory, "WEB-INF" ) ).delete() ); return warFile; } + + // Overlay utilities + + /** + * Builds a test overlay on the fly. + * + * @param id the id of the overlay (see test/resources/overlays) + * @return a test war artifact with the content of the given test overlay + */ + protected ArtifactStub buildWarOverlayStub( String id ) + { + // Create war file + final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" ); + createArchive( new File( OVERLAYS_ROOT_DIR, id ), destFile ); + + return new WarOverlayStub( getBasedir(), id, destFile ); + } + + protected File getOverlayFile( String id, String filePath ) + { + final File overlayDir = new File( OVERLAYS_ROOT_DIR, id ); + final File file = new File( overlayDir, filePath ); + + // Make sure the file exists + assertTrue( "Overlay file " + filePath + " does not exist for overlay " + id + " at " + file.getAbsolutePath(), + file.exists() ); + return file; + + } + + protected void createArchive( final File directory, final File destinationFile ) + { + try + { + //WarArchiver archiver = new WarArchiver(); + + Archiver archiver = new JarArchiver(); + + archiver.setDestFile( destinationFile ); + archiver.addDirectory( directory ); + + //archiver.setWebxml( new File(directory, "WEB-INF/web.xml")); + + // create archive + archiver.createArchive(); + + } + catch ( ArchiverException e ) + { + e.printStackTrace(); + fail( "Failed to create overlay archive " + e.getMessage() ); + } + catch ( IOException e ) + { + e.printStackTrace(); + fail( "Unexpected exception " + e.getMessage() ); + } + } + + } Added: maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/WarOverlaysTest.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/WarOverlaysTest.java?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/WarOverlaysTest.java (added) +++ maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/WarOverlaysTest.java Sun Apr 22 08:46:28 2007 @@ -0,0 +1,375 @@ +package org.apache.maven.plugin.war; + +import org.apache.maven.plugin.testing.stubs.ArtifactStub; +import org.apache.maven.plugin.war.stub.MavenProjectArtifactsStub; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/** + * @author Stephane Nicoll + */ +public class WarOverlaysTest + extends AbstractWarMojoTest +{ + + private static File pomFile = new File( getBasedir(), "target/test-classes/unit/waroverlays/default.xml" ); + + private WarExplodedMojo mojo; + + + public void setUp() + throws Exception + { + super.setUp(); + mojo = (WarExplodedMojo) lookupMojo( "exploded", pomFile ); + } + + protected File getTestDirectory() + { + return new File( getBasedir(), "target/test-classes/unit/waroverlays" ); + } + + public void testEnvironment() + throws Exception + { + // see setup + } + + public void testNoOverlay() + throws Exception + { + // setup test data + final String testId = "no-overlay"; + final File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} ); + + final File webAppDirectory = setUpMojo( testId, null ); + try + { + mojo.setWebXml( new File( xmlSource, "web.xml" ) ); + mojo.execute(); + + // Validate content of the webapp + assertDefaultContent( webAppDirectory ); + assertWebXml( webAppDirectory ); + } + finally + { + cleanWebAppDirectory( webAppDirectory ); + } + } + + public void testDefaultOverlay() + throws Exception + { + // setup test data + final String testId = "default-overlay"; + + // Add an overlay + final ArtifactStub overlay = buildWarOverlayStub( "overlay-one" ); + + final File webAppDirectory = setUpMojo( testId, new ArtifactStub[]{overlay} ); + final List assertedFiles = new ArrayList(); + try + { + mojo.execute(); + assertedFiles.addAll( assertDefaultContent( webAppDirectory ) ); + assertedFiles.addAll( assertWebXml( webAppDirectory ) ); + assertedFiles.addAll( assertCustomContent( webAppDirectory, new String[]{"index.jsp", "login.jsp"}, + "overlay file not found" ) ); + + // index and login come from overlay1 + assertOverlayedFile( webAppDirectory, "overlay-one", "index.jsp" ); + assertOverlayedFile( webAppDirectory, "overlay-one", "login.jsp" ); + + // Ok now check that there is no more files/directories + final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[]{"META-INF/MANIFEST.MF"} ); + assertWebAppContent( webAppDirectory, assertedFiles, filter ); + } + finally + { + cleanWebAppDirectory( webAppDirectory ); + } + } + + public void testDefaultOverlays() + throws Exception + { + // setup test data + final String testId = "default-overlays"; + + // Add an overlay + final ArtifactStub overlay = buildWarOverlayStub( "overlay-one" ); + final ArtifactStub overlay2 = buildWarOverlayStub( "overlay-two" ); + + final File webAppDirectory = setUpMojo( testId, new ArtifactStub[]{overlay, overlay2} ); + final List assertedFiles = new ArrayList(); + try + { + mojo.execute(); + assertedFiles.addAll( assertDefaultContent( webAppDirectory ) ); + assertedFiles.addAll( assertWebXml( webAppDirectory ) ); + assertedFiles.addAll( assertCustomContent( webAppDirectory, + new String[]{"index.jsp", "login.jsp", "admin.jsp"}, + "overlay file not found" )); + + // index and login come from overlay1 + assertOverlayedFile( webAppDirectory, "overlay-one", "index.jsp" ); + assertOverlayedFile( webAppDirectory, "overlay-one", "login.jsp" ); + + //admin comes from overlay2 + // index and login comes from overlay1 + assertOverlayedFile( webAppDirectory, "overlay-two", "admin.jsp" ); + + // Ok now check that there is no more files/directories + final FileFilter filter = new FileFilterImpl( webAppDirectory, new String[]{"META-INF/MANIFEST.MF"} ); + assertWebAppContent( webAppDirectory, assertedFiles, filter ); + } + finally + { + cleanWebAppDirectory( webAppDirectory ); + } + } + + // Helpers + + + /** + * Asserts the default content of the war based on the specified + * webapp directory. + * + * @param webAppDirectory the webapp directory + * @return a list of File objects that have been asserted + */ + protected List assertDefaultContent( File webAppDirectory ) + { + // Validate content of the webapp + File expectedWebSourceFile = new File( webAppDirectory, "pansit.jsp" ); + File expectedWebSource2File = new File( webAppDirectory, "org/web/app/last-exile.jsp" ); + + assertTrue( "source file not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists() ); + assertTrue( "source file not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists() ); + + final List content = new ArrayList(); + content.add( expectedWebSourceFile ); + content.add( expectedWebSource2File ); + + return content; + } + + + /** + * Asserts the web.xml file of the war based on the specified + * webapp directory. + * + * @param webAppDirectory the webapp directory + * @return a list with the web.xml File object + */ + protected List assertWebXml( File webAppDirectory ) + { + File expectedWEBXMLFile = new File( webAppDirectory, "WEB-INF/web.xml" ); + assertTrue( "web xml not found: " + expectedWEBXMLFile.toString(), expectedWEBXMLFile.exists() ); + + final List content = new ArrayList(); + content.add( expectedWEBXMLFile ); + + return content; + } + + /** + * Asserts custom content of the war based on the specified webapp + * directory. + * + * @param webAppDirectory the webapp directory + * @param filePaths an array of file paths relative to the webapp directory + * @param customMessage a custom message if an assertion fails + * @return a list of File objects that have been inspected + */ + protected List assertCustomContent( File webAppDirectory, String[] filePaths, String customMessage ) + { + final List content = new ArrayList(); + for ( int i = 0; i < filePaths.length; i++ ) + { + String filePath = filePaths[i]; + final File expectedFile = new File( webAppDirectory, filePath ); + if ( customMessage != null ) + { + assertTrue( customMessage + " - " + expectedFile.toString(), expectedFile.exists() ); + } + else + { + assertTrue( "source file not found: " + expectedFile.toString(), expectedFile.exists() ); + } + content.add( expectedFile ); + } + return content; + } + + /** + * Asserts that the content of an overlayed file is correct. + * <p/> + * Note that the <tt>filePath</tt> is relative to both the webapp + * directory and the overlayed directory, defined by the <tt>overlayId</tt>. + * + * @param webAppDirectory the webapp directory + * @param overlayId the id of the overlay + * @param filePath the relative path + * @throws IOException if an error occured while reading the files + */ + protected void assertOverlayedFile( File webAppDirectory, String overlayId, String filePath ) + throws IOException + { + final File webAppFile = new File( webAppDirectory, filePath ); + final File overlayFile = getOverlayFile( overlayId, filePath ); + assertEquals( "Wrong content for overlayed file " + filePath, FileUtils.fileRead( overlayFile ), + FileUtils.fileRead( webAppFile ) ); + + } + + /** + * Asserts that the webapp contains only the specified files. + * + * @param webAppDirectory the webapp directory + * @param expectedFiles the expected files + * @param filter an optional filter to ignore some resources + */ + protected void assertWebAppContent( File webAppDirectory, List expectedFiles, FileFilter filter ) + { + final List webAppContent = new ArrayList(); + if ( filter != null ) + { + buildFilesList( webAppDirectory, filter, webAppContent ); + } + else + { + buildFilesList( webAppDirectory, new FileFilterImpl( webAppDirectory, null ), webAppContent ); + } + + // Now we have the files, sort them. + Collections.sort( expectedFiles ); + Collections.sort( webAppContent ); + assertEquals( "Invalid webapp content, expected " + expectedFiles.size() + "file(s) " + expectedFiles + + " but got " + webAppContent.size() + " file(s) " + webAppContent, expectedFiles, webAppContent ); + } + + /** + * Configures the exploded mojo for the specified test. + * + * @param testId the id of the test + * @param artifactStubs the dependencies (may be null) + * @return the webapp directory + * @throws Exception if an error occurs while configuring the mojo + */ + protected File setUpMojo( final String testId, ArtifactStub[] artifactStubs ) + throws Exception + { + final MavenProjectArtifactsStub project = new MavenProjectArtifactsStub(); + final File webAppDirectory = new File( getTestDirectory(), testId ); + final File webAppSource = createWebAppSource( testId ); + final File classesDir = createClassesDir( testId, true ); + + final File workDirectory = new File( getTestDirectory(), "/war/work-" + testId ); + createDir( workDirectory ); + + if ( artifactStubs != null ) + { + for ( int i = 0; i < artifactStubs.length; i++ ) + { + ArtifactStub artifactStub = artifactStubs[i]; + project.addArtifact( artifactStub ); + } + } + + configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project ); + setVariableValueToObject( mojo, "workDirectory", workDirectory ); + + return webAppDirectory; + } + + /** + * Cleans up the webapp directory. + * + * @param webAppDirectory the directory to remove + * @throws IOException if an error occured while removing the directory + */ + protected void cleanWebAppDirectory( File webAppDirectory ) + throws IOException + { + FileUtils.deleteDirectory( webAppDirectory ); + } + + /** + * Builds the list of files and directories from the specified dir. + * <p/> + * Note that the filter is not used the usual way. If the filter does + * not accept the current file, it's not added but yet the subdirectories + * are added if any. + * + * @param dir the base directory + * @param filter the filter + * @param content the current content, updated recursivly + */ + private void buildFilesList( final File dir, FileFilter filter, final List content ) + { + final File[] files = dir.listFiles(); + + for ( int i = 0; i < files.length; i++ ) + { + File file = files[i]; + + // Add the file if the filter is ok with it + if ( filter.accept( file ) ) + { + content.add( file ); + } + + // Even if the file is not accepted and is a directory, add it + if ( file.isDirectory() ) + { + buildFilesList( file, filter, content ); + } + + } + } + + class FileFilterImpl + implements FileFilter + { + + private final List rejectedFilePaths; + + private final int webAppDirIndex; + + + public FileFilterImpl( File webAppDirectory, String[] rejectedFilePaths ) + { + if ( rejectedFilePaths != null ) + { + this.rejectedFilePaths = Arrays.asList( rejectedFilePaths ); + } + else + { + this.rejectedFilePaths = new ArrayList(); + } + this.webAppDirIndex = webAppDirectory.getAbsolutePath().length() + 1; + } + + public boolean accept( File file ) + { + String effectiveRelativePath = buildRelativePath( file ); + return !( rejectedFilePaths.contains( effectiveRelativePath ) || file.isDirectory() ); + } + + private String buildRelativePath( File f ) + { + return f.getAbsolutePath().substring( webAppDirIndex ); + } + } +} \ No newline at end of file Added: maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/stub/WarOverlayStub.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/stub/WarOverlayStub.java?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/stub/WarOverlayStub.java (added) +++ maven/plugins/branches/MWAR-97/src/test/java/org/apache/maven/plugin/war/stub/WarOverlayStub.java Sun Apr 22 08:46:28 2007 @@ -0,0 +1,52 @@ +package org.apache.maven.plugin.war.stub; + +import java.io.File; + +/** + * @author Stephane Nicoll + */ +public class WarOverlayStub + extends AbstractArtifactStub +{ + + + private final String artifactId; + + private File file; + + public WarOverlayStub( String _basedir, String artifactId, File warFile ) + { + super( _basedir ); + if ( artifactId == null ) + { + throw new NullPointerException( "Id could not be null." ); + } + if ( warFile == null ) + { + throw new NullPointerException( "warFile could not be null." ); + + } + else if ( !warFile.exists() ) + { + throw new IllegalStateException( "warFile[" + file.getAbsolutePath() + "] should exist." ); + } + this.artifactId = artifactId; + this.file = warFile; + } + + public String getType() + { + return "war"; + } + + public String getArtifactId() + { + return artifactId; + } + + public File getFile() + { + return file; + } + +} Added: maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/WEB-INF/web.xml?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/WEB-INF/web.xml (added) +++ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/WEB-INF/web.xml Sun Apr 22 08:46:28 2007 @@ -0,0 +1,3 @@ +<web-app> + <display-name>Sample one overlay</display-name> +</web-app> Added: maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/index.jsp URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/index.jsp?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/index.jsp (added) +++ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/index.jsp Sun Apr 22 08:46:28 2007 @@ -0,0 +1,7 @@ +<html> +<body> +<p> + Hello World, this is overlay-one! +</p> +</body> +</html> \ No newline at end of file Added: maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/login.jsp URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/login.jsp?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/login.jsp (added) +++ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-one/login.jsp Sun Apr 22 08:46:28 2007 @@ -0,0 +1,3 @@ +<html> + +</html> \ No newline at end of file Added: maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/WEB-INF/web.xml?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/WEB-INF/web.xml (added) +++ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/WEB-INF/web.xml Sun Apr 22 08:46:28 2007 @@ -0,0 +1,3 @@ +<web-app> + <display-name>Sample two overlay</display-name> +</web-app> Added: maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/admin.jsp URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/admin.jsp?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/admin.jsp (added) +++ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/admin.jsp Sun Apr 22 08:46:28 2007 @@ -0,0 +1,3 @@ +<html> + +</html> \ No newline at end of file Added: maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/index.jsp URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/index.jsp?view=auto&rev=531206 ============================================================================== --- maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/index.jsp (added) +++ maven/plugins/branches/MWAR-97/src/test/resources/overlays/overlay-two/index.jsp Sun Apr 22 08:46:28 2007 @@ -0,0 +1,7 @@ +<html> +<body> +<p> + Hello World, this is overlay-two! +</p> +</body> +</html> \ No newline at end of file Added: maven/plugins/trunk/maven-war-plugin/src/test/resources/unit/waroverlays/default.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/test/resources/unit/waroverlays/default.xml?view=auto&rev=531206 ============================================================================== --- maven/plugins/trunk/maven-war-plugin/src/test/resources/unit/waroverlays/default.xml (added) +++ maven/plugins/trunk/maven-war-plugin/src/test/resources/unit/waroverlays/default.xml Sun Apr 22 08:46:28 2007 @@ -0,0 +1,32 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project> + <name>war-plugin-test</name> + <build> + <plugins> + <plugin> + <artifactId>maven-war-plugin</artifactId> + <configuration> + <!-- no extra config so default is applied --> + </configuration> + </plugin> + </plugins> + </build> +</project>