You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by st...@apache.org on 2011/08/11 00:02:23 UTC
svn commit: r1156386 - in /maven/sandbox/trunk/plexus-utils-commons-bridge:
plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/Expand.java
plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExpandTest.java
Author: struberg
Date: Wed Aug 10 22:02:23 2011
New Revision: 1156386
URL: http://svn.apache.org/viewvc?rev=1156386&view=rev
Log:
MSANDBOX-51 Expand + TCK finished
Modified:
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/Expand.java
maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExpandTest.java
Modified: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/Expand.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/Expand.java?rev=1156386&r1=1156385&r2=1156386&view=diff
==============================================================================
--- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/Expand.java (original)
+++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-commons-bridge/src/main/java/org/codehaus/plexus/util/Expand.java Wed Aug 10 22:02:23 2011
@@ -20,6 +20,13 @@ package org.codehaus.plexus.util;
*/
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
/**
* Expand will unpack the given zip archive.
@@ -42,6 +49,7 @@ public class Expand
* if the unpackaging should get performed if the destination already exists.
*/
private boolean overwrite = false;
+ private static final int BUFFER_SIZE = 2 ^ 16;
/**
@@ -68,7 +76,8 @@ public class Expand
/**
* If the destination directory should get overwritten if the content
- * already exists.
+ * already exists. If <code>false</code> we will only overwrite if the local
+ * file or directory is older than the one in the archive.
* @param overwrite
*/
public void setOverwrite( boolean overwrite )
@@ -82,11 +91,147 @@ public class Expand
*
* @throws Exception
*/
- public void execute() throws Exception
+ public void execute()
+ throws Exception
{
- //X TODO implement
- throw new RuntimeException( "not yet implemented!" );
+ expandFile( source, dest );
}
+ /**
+ * <p>It is intended to be overwritten when implementing an own unarchiver</p>
+ *
+ * <p><b>Note:</b> we kept this protected method for the sake of backward compatibility!</p>
+ *
+ * @param srcFile
+ * @param dest
+ * @throws Exception
+ */
+ protected void expandFile( File srcFile, File dest )
+ throws Exception
+ {
+ if (source == null)
+ {
+ throw new NullPointerException( "Source Archive must not be null!" );
+ }
+
+ File destDir = dest;
+ if ( destDir == null )
+ {
+ destDir = new File( System.getProperty( "user.dir" ) );
+ }
+
+ FileInputStream fileInputStream = new FileInputStream( srcFile );
+ try
+ {
+ ZipInputStream zipInputStream = new ZipInputStream( fileInputStream );
+
+ ZipEntry zipEntry;
+
+ while( (zipEntry = zipInputStream.getNextEntry()) != null )
+ {
+ String zipEntryName = zipEntry.getName();
+ Date zipEntryDate = new Date( zipEntry.getTime() );
+
+ extractFile( source, destDir, zipInputStream, zipEntryName, zipEntryDate, zipEntry.isDirectory() );
+ }
+ }
+ finally
+ {
+ try
+ {
+ fileInputStream.close();
+ }
+ catch( IOException ioe )
+ {
+ // no worries, all is ok ...
+ }
+ }
+ }
+
+ /**
+ * Extract a single ZipEntry.
+ *
+ * <p><b>Note:</b> we kept this protected method for the sake of backward compatibility!</p>
+ *
+ * @param archive the archive to unpack
+ * @param destDir the destination dirctory
+ * @param compressedInputStream
+ * @param entryName
+ * @param entryDate
+ * @param isDirectory
+ * @throws Exception
+ */
+ protected void extractFile( File archive
+ , File destDir
+ , InputStream compressedInputStream
+ , String entryName
+ , Date entryDate
+ , boolean isDirectory )
+ throws Exception
+ {
+ File targetFile = new File( destDir, entryName );
+ // if overwrite is specified and the file type
+ // of the existing file does not match, then delete it
+ if ( overwrite && targetFile.exists() && targetFile.isDirectory() != isDirectory )
+ {
+ deleteFileOrDir( targetFile );
+ }
+
+ if ( !targetFile.exists() ||
+ overwrite ||
+ targetFile.lastModified() <= entryDate.getTime() )
+ {
+ if ( isDirectory )
+ {
+ targetFile.mkdirs();
+ }
+ else
+ {
+ byte[] buffer = new byte[ BUFFER_SIZE ];
+ FileOutputStream fileOutputStream = new FileOutputStream( targetFile );
+ try
+ {
+ int len;
+ while( (len = compressedInputStream.read( buffer ) ) > 0)
+ {
+ fileOutputStream.write( buffer, 0, len );
+ }
+
+ targetFile.setLastModified( entryDate.getTime() );
+ }
+ finally
+ {
+ try
+ {
+ fileOutputStream.close();
+ }
+ catch( IOException ioe )
+ {
+ // no worries, all is ok ...
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * small helper method who deletes the given directory or file.
+ *
+ * @param targetFile
+ * @throws IOException
+ */
+ private void deleteFileOrDir( File targetFile )
+ throws IOException
+ {
+ if ( targetFile.isDirectory() )
+ {
+ org.apache.commons.io.FileUtils.deleteDirectory( targetFile );
+ }
+ else
+ {
+ targetFile.delete();
+ }
+
+ }
}
Modified: maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExpandTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExpandTest.java?rev=1156386&r1=1156385&r2=1156386&view=diff
==============================================================================
--- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExpandTest.java (original)
+++ maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/ExpandTest.java Wed Aug 10 22:02:23 2011
@@ -23,6 +23,7 @@ import org.apache.maven.tck.FixPlexusBug
import org.junit.Rule;
import org.junit.Test;
import org.junit.Assert;
+import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileNotFoundException;
@@ -48,10 +49,10 @@ import static org.hamcrest.CoreMatchers.
public class ExpandTest extends Assert
{
- private static Logger logger = Logger.getLogger(ExceptionUtilsTest.class.getName());
+ private static Logger logger = Logger.getLogger(ExpandTest.class.getName());
private static final String TEST_ZIP_LOCATION = "/expand/expand_test.zip";
- private static final String TEST_ZIP_TARGET = "target/expand_test_target/";
+ private static final String TEST_ZIP_TARGET_FOLDER = "expand_test_target/";
private static final String TEST_UNZIPPED_FILE = "expand_test/test_file.txt";
private static final String TEST_UNZIPPED_CONTENT = "TestContent";
@@ -59,6 +60,9 @@ public class ExpandTest extends Assert
@Rule
public FixPlexusBugs fixPlexusBugs = new FixPlexusBugs();
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
private File getSourceFile()
{
@@ -77,16 +81,7 @@ public class ExpandTest extends Assert
*/
private File getTestTargetDir() throws IOException
{
- File targetDir = new File( TEST_ZIP_TARGET );
-
- if ( targetDir.exists() )
- {
- FileUtils.cleanDirectory( targetDir );
- }
- else
- {
- targetDir.mkdirs();
- }
+ File targetDir = tempFolder.newFolder( TEST_ZIP_TARGET_FOLDER );
return targetDir;
}
@@ -162,7 +157,11 @@ public class ExpandTest extends Assert
}
catch ( Exception e )
{
- Throwable cause = ExceptionUtils.getCause( e );
+ Throwable cause = ExceptionUtils.getRootCause( e );
+ if ( cause == null )
+ {
+ cause = e;
+ }
assertTrue( "cause must be a FileNotFoundException", cause instanceof FileNotFoundException );
}
@@ -232,7 +231,7 @@ public class ExpandTest extends Assert
}
// turn the clock back 10 seconds
- long time = System.currentTimeMillis() - 10000L;
+ long time = System.currentTimeMillis() - 120000L;
// round down to 1s;
time = time - time % 1000L;
@@ -276,7 +275,7 @@ public class ExpandTest extends Assert
expandedFile = verifyExpandedFile( targetDir );
// obviously the file will be overwritten anyway
- assertTrue( "file must now have newer lastModified timestamp, but was: time=" + time
+ assertTrue( "file must now have the original old lastModified timestamp, but was: time=" + time
+ " expandedFile.lastModified()= " + expandedFile.lastModified()
, time > expandedFile.lastModified() );
}