Skip to content

Commit

Permalink
o Getting all ITs to pass.
Browse files Browse the repository at this point in the history
o Adding tests to make sure that the zip files retain unix modes.
  • Loading branch information
trygvis committed Feb 9, 2013
1 parent dfbb7ae commit 6cf8df6
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class FileAttributes
public final static FileAttributes EMPTY = new FileAttributes( Option.<String>none(), Option.<String>none(),
Option.<UnixFileMode>none(), List.<String>nil() );

public FileAttributes( String user, String group, UnixFileMode mode )
{
this( some( user ), some( group ), some( mode ), List.<String>nil() );
}

public FileAttributes( Option<String> user, Option<String> group, Option<UnixFileMode> mode )
{
this( user, group, mode, List.<String>nil() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* @author <a href="mailto:[email protected]">Trygve Laugst&oslash;l</a>
*/
public interface FileCollector<FsType extends Fs>
public interface FileCollector
{
void addDirectory( Directory directory )
throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author <a href="mailto:[email protected]">Trygve Laugst&oslash;l</a>
*/
public abstract class UnixPackage
implements FileCollector<Fs>
implements FileCollector
{
private final String packageFileExtension;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
*
* @author <a href="mailto:[email protected]">Trygve Laugst&oslash;l</a>
*/
public interface AssemblyOperation<FsType extends Fs>
public interface AssemblyOperation
extends LineProducer
{
void perform(FileCollector<FsType> fileCollector)
void perform(FileCollector fileCollector)
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
/**
* @author <a href="mailto:[email protected]">Trygve Laugst&oslash;l</a>
*/
public class CopyDirectoryOperation<FsType extends Fs>
implements AssemblyOperation<FsType>
public class CopyDirectoryOperation
implements AssemblyOperation
{
private final Fs<?> from;

Expand Down Expand Up @@ -72,7 +72,7 @@ public CopyDirectoryOperation( Fs<?> from, RelativePath to, List<String> include
this.directoryAttributes = directoryAttributes;
}

public void perform( FileCollector<FsType> fileCollector )
public void perform( FileCollector fileCollector )
throws IOException
{
Pattern pattern = this.pattern.isSome() ? Pattern.compile( this.pattern.some()._1() ) : null;
Expand All @@ -83,7 +83,7 @@ public void perform( FileCollector<FsType> fileCollector )
addStringExcludes( excludes ).
create() );

for ( Fs<FsType> f : files )
for ( Fs f : files )
{
// if ( f.getName().getBaseName().equals( "" ) )
// {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @author <a href="mailto:[email protected]">Trygve Laugst&oslash;l</a>
*/
public class FsFileCollector
implements FileCollector<LocalFs>
implements FileCollector
{
private final List<IoEffect> operations = new ArrayList<IoEffect>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testCopyOnACompleteDirectoryStructure()
{
assertTrue( files.files.isDirectory() );
MockControl control = MockControl.createControl( FileCollector.class );
FileCollector<LocalFs> fileCollector = (FileCollector) control.getMock();
FileCollector fileCollector = (FileCollector) control.getMock();

fileCollector.addFile( files.optJettyBinExtraApp, objects.optJettyBinExtraApp );
control.setMatcher( new FsMatcher() );
Expand Down
1 change: 0 additions & 1 deletion unix-maven-plugin/src/it/test-zip-2/postbuild.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import static org.codehaus.mojo.unix.maven.plugin.ShittyUtil.*
import static org.codehaus.mojo.unix.FileAttributes.*
import static org.codehaus.mojo.unix.UnixFsObject.*
import static fj.data.Option.*;
import org.joda.time.*;

boolean success = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,11 @@ public static List<AssemblyOperation> createAssemblyOperations(MavenProjectWrapp

// Create the complete list of assembly operations.
// Order: defaults -> mojo -> pakke
List<AssemblyOp> assemblyOps = join( list( defaultAssemblyOp.reverse(), mojoAssembly, packageAssembly ) );
List<List<AssemblyOp>> list = single( defaultAssemblyOp.reverse() ).
conss( mojoAssembly ).
conss( packageAssembly );

List<AssemblyOp> assemblyOps = join( list.reverse() );

List<AssemblyOperation> operations = nil();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static fj.Function.*;
import fj.data.*;
import fj.data.List;
import org.apache.commons.compress.archivers.zip.*;
import org.codehaus.mojo.unix.*;
import static org.codehaus.mojo.unix.BasicPackageFileSystemObject.*;
import static org.codehaus.mojo.unix.FileAttributes.*;
Expand All @@ -40,6 +41,7 @@

import static org.codehaus.mojo.unix.util.RelativePath.*;
import org.codehaus.plexus.util.*;
import static org.codehaus.plexus.util.IOUtil.copy;
import org.joda.time.*;

import java.io.*;
Expand All @@ -49,7 +51,7 @@
public class ZipUnixPackage
extends UnixPackage
{
private PackageFileSystem<F2<UnixFsObject, ZipOutputStream, IoEffect>> fileSystem;
private PackageFileSystem<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>> fileSystem;

public ZipUnixPackage()
{
Expand Down Expand Up @@ -102,24 +104,22 @@ public void beforeAssembly( FileAttributes defaultDirectoryAttributes, LocalDate
public void packageToFile( File packageFile, ScriptUtil.Strategy strategy )
throws Exception
{
F2<RelativePath, PackageFileSystemObject<F2<UnixFsObject, ZipOutputStream, IoEffect>>, Boolean> pathFilter =
F2<RelativePath, PackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>>, Boolean> pathFilter =
pathFilter();

PackageFileSystemFormatter<F2<UnixFsObject, ZipOutputStream, IoEffect>> formatter =
PackageFileSystemFormatter.flatFormatter();

fileSystem = fileSystem.prettify();

Stream<PackageFileSystemObject<F2<UnixFsObject, ZipOutputStream, IoEffect>>> items = fileSystem.
Stream<PackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>>> items = fileSystem.
toList().
filter( compose( BooleanF.invert, curry( pathFilter, BASE ) ) );

ZipOutputStream zos = null;
ZipArchiveOutputStream zos = null;
try
{
zos = new ZipOutputStream( new FileOutputStream( packageFile ) );
zos = new ZipArchiveOutputStream( packageFile );
zos.setLevel( Deflater.BEST_COMPRESSION );

for ( PackageFileSystemObject<F2<UnixFsObject, ZipOutputStream, IoEffect>> fileSystemObject : items )
for ( PackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>> fileSystemObject : items )
{
fileSystemObject.getExtension().f( fileSystemObject.getUnixFsObject(), zos ).run();
}
Expand All @@ -141,22 +141,28 @@ public Boolean f( RelativePath path, PackageFileSystemObject object )
};
}

private BasicPackageFileSystemObject<F2<UnixFsObject, ZipOutputStream, IoEffect>> directory( Directory directory )
private BasicPackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>> directory( Directory directory )
{
F2<UnixFsObject, ZipOutputStream, IoEffect> f = new F2<UnixFsObject, ZipOutputStream, IoEffect>()
F2<UnixFsObject, ZipArchiveOutputStream, IoEffect> f = new F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>()
{
public IoEffect f( final UnixFsObject object, final ZipOutputStream zipOutputStream )
public IoEffect f( final UnixFsObject file, final ZipArchiveOutputStream zos )
{
return new IoEffect()
{
public void run()
throws Exception
{
String path = object.path.isBase() ? "." : object.path.asAbsolutePath( "./" ) + "/";
String path = file.path.isBase() ? "." : file.path.asAbsolutePath( "./" ) + "/";

ZipEntry entry = new ZipEntry( path );
entry.setTime( object.lastModified.toDateTime().getMillis() );
zipOutputStream.putNextEntry( entry );
ZipArchiveEntry entry = new ZipArchiveEntry( path );
entry.setSize( file.size );
entry.setTime( file.lastModified.toDateTime().getMillis() );
if ( file.attributes.mode.isSome() )
{
entry.setUnixMode( file.attributes.mode.some().toInt() );
}
zos.putArchiveEntry( entry );
zos.closeArchiveEntry();
}
};
}
Expand All @@ -165,12 +171,12 @@ public void run()
return basicPackageFSO( directory, f );
}

private BasicPackageFileSystemObject<F2<UnixFsObject, ZipOutputStream, IoEffect>> file( final Fs<?> fromFile,
private BasicPackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>> file( final Fs<?> fromFile,
UnixFsObject file )
{
F2<UnixFsObject, ZipOutputStream, IoEffect> f = new F2<UnixFsObject, ZipOutputStream, IoEffect>()
F2<UnixFsObject, ZipArchiveOutputStream, IoEffect> f = new F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>()
{
public IoEffect f( final UnixFsObject file, final ZipOutputStream zipOutputStream )
public IoEffect f( final UnixFsObject file, final ZipArchiveOutputStream zos )
{
return new IoEffect()
{
Expand Down Expand Up @@ -241,11 +247,17 @@ public void run()
size = output.size();
}

ZipEntry zipEntry = new ZipEntry( file.path.asAbsolutePath( "./" ) );
zipEntry.setSize( size );
zipEntry.setTime( file.lastModified.toDateTime().getMillis() );
zipOutputStream.putNextEntry( zipEntry );
IOUtil.copy( inputStream, zipOutputStream, 1024 * 128 );
ZipArchiveEntry entry = new ZipArchiveEntry( file.path.asAbsolutePath( "./" ) );
entry.setSize( size );
entry.setTime( file.lastModified.toDateTime().getMillis() );
if ( file.attributes.mode.isSome() )
{
entry.setUnixMode( file.attributes.mode.some().toInt() );
}

zos.putArchiveEntry( entry );
copy( inputStream, zos, 1024 * 128 );
zos.closeArchiveEntry();
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@

import fj.*;
import fj.data.*;
import fj.data.List;
import static fj.data.List.*;
import static fj.data.Option.*;
import static java.util.regex.Pattern.*;
import junit.framework.*;
import org.apache.commons.compress.archivers.zip.*;
import org.codehaus.mojo.unix.*;
import static org.codehaus.mojo.unix.FileAttributes.*;
import static org.codehaus.mojo.unix.UnixFsObject.*;
import org.codehaus.mojo.unix.core.*;
import static org.codehaus.mojo.unix.util.RelativePath.*;
import static org.codehaus.mojo.unix.util.line.LineStreamWriter.EOL;

import org.codehaus.mojo.unix.io.*;
import org.codehaus.mojo.unix.io.LineEnding;
import org.codehaus.mojo.unix.io.fs.*;
import static org.codehaus.mojo.unix.util.RelativePath.*;
import org.codehaus.mojo.unix.util.*;
import org.joda.time.*;

import java.io.*;
import java.nio.charset.*;
import java.util.zip.*;
import java.util.*;

/**
* @author <a href="mailto:[email protected]">Trygve Laugst&oslash;l</a>
Expand All @@ -55,13 +57,17 @@ public class ZipPackageTest
private final Charset charset = Charset.forName( "utf-8" );

LocalDateTime dirsTimestamp = new LocalDateTime( 2012, 8, 19, 10, 34, 10 );

LocalDateTime dirsBarTxtTimestamp = new LocalDateTime( 2012, 8, 19, 10, 34, 10 );

LocalDateTime fileTimestamp = new LocalDateTime( 2012, 8, 19, 10, 34, 48 );

LocalDateTime fileFooTxtTimestamp = new LocalDateTime( 2012, 8, 19, 10, 34, 10 );

// Zip has a resolution of two seconds
LocalDateTime timestamp = new LocalDateTime( 2012, 1, 2, 3, 4, 6 );

@SuppressWarnings( "OctalInteger" )
public void testBasic()
throws Exception
{
Expand All @@ -76,15 +82,15 @@ public void testBasic()

ZipUnixPackage zipPackage = new ZipUnixPackage();

zipPackage.beforeAssembly( EMPTY, timestamp );
zipPackage.beforeAssembly( EMPTY.mode( UnixFileMode._0755 ), timestamp );

assertTrue( basedir.isDirectory() );

// Git set the timestamp of file objects
assertTrue(new File(zip1, "dirs").setLastModified(dirsTimestamp.toDateTime().getMillis()));
assertTrue(new File(zip1, "dirs/bar.txt").setLastModified(dirsBarTxtTimestamp.toDateTime().getMillis()));
assertTrue(new File(zip1, "file").setLastModified(fileTimestamp.toDateTime().getMillis()));
assertTrue(new File(zip1, "file/foo.txt").setLastModified(fileFooTxtTimestamp.toDateTime().getMillis()));
assertTrue( new File( zip1, "dirs" ).setLastModified( dirsTimestamp.toDateTime().getMillis() ) );
assertTrue( new File( zip1, "dirs/bar.txt" ).setLastModified( dirsBarTxtTimestamp.toDateTime().getMillis() ) );
assertTrue( new File( zip1, "file" ).setLastModified( fileTimestamp.toDateTime().getMillis() ) );
assertTrue( new File( zip1, "file/foo.txt" ).setLastModified( fileFooTxtTimestamp.toDateTime().getMillis() ) );

Replacer replacer = new Replacer( compile( "@bar@" ), "awesome" );

Expand All @@ -101,39 +107,46 @@ public void testBasic()
new FilterFilesOperation( single( "dirs/**" ), List.<String>nil(), single( replacer ), LineEnding.unix ).
perform( zipPackage );

UnixFileMode fileMode = UnixFileMode.fromInt( 0600 );
UnixFileMode dirMode = UnixFileMode.fromInt( 0777 );

FileAttributes fileAttributes = new FileAttributes( "root", "root", fileMode );
FileAttributes directoryAttributes = new FileAttributes( "root", "root", dirMode );
new SetAttributesOperation( BASE, single( "**/*" ), List.<String>nil(), some( fileAttributes ),
some( directoryAttributes ) ).
perform( zipPackage );

zipPackage.
packageToFile( zip, ScriptUtil.Strategy.SINGLE );

FileInputStream fis = new FileInputStream( zip );
ZipInputStream in = new ZipInputStream( fis );
assertDirectory( in, "./dirs/", dirsTimestamp );
ZipFile file = new ZipFile( zip );
Enumeration<ZipArchiveEntry> enumeration = file.getEntriesInPhysicalOrder();
assertDirectory( enumeration.nextElement(), "./dirs/", dirsTimestamp );
// Is it really correct that filtered files should retain the old timestamp?
assertFile( in, "./dirs/bar.txt", 8, dirsBarTxtTimestamp, "awesome\n" );
assertDirectory( in, "./file/", fileTimestamp );
assertFile( in, "./file/foo.txt", 6, fileFooTxtTimestamp, "@foo@\n" );
assertDirectory( in, "./opt/", timestamp );
assertDirectory( in, "./opt/hudson/", timestamp );
assertNull( in.getNextEntry() );
in.close();
fis.close();
assertFile( file, enumeration.nextElement(), "./dirs/bar.txt", 8, dirsBarTxtTimestamp, "awesome\n", fileMode );
assertDirectory( enumeration.nextElement(), "./file/", fileTimestamp );
assertFile( file, enumeration.nextElement(), "./file/foo.txt", 6, fileFooTxtTimestamp, "@foo@\n", fileMode );
assertDirectory( enumeration.nextElement(), "./opt/", timestamp );
assertDirectory( enumeration.nextElement(), "./opt/hudson/", timestamp );
assertFalse( enumeration.hasMoreElements() );
file.close();
}

private void assertDirectory( ZipInputStream in, String name, LocalDateTime time )
private void assertDirectory( ZipArchiveEntry entry, String name, LocalDateTime time )
throws IOException
{
ZipEntry entry = in.getNextEntry();
assertNotNull( name, entry );
assertTrue( name + " should be file", entry.isDirectory() );
assertEquals( name + ", name", name, entry.getName() );
assertEquals( name + ", timestamp", time, new LocalDateTime( entry.getTime() ) );
in.closeEntry();
}

private void assertFile( ZipInputStream in, String name, int size, LocalDateTime time, String content )
private void assertFile( ZipFile file, ZipArchiveEntry entry, String name, int size, LocalDateTime time,
String content, UnixFileMode mode )
throws IOException
{
ZipEntry entry = in.getNextEntry();
assertNotNull( entry );
InputStream in = file.getInputStream( entry );

assertFalse( name + " should be file", entry.isDirectory() );
assertEquals( name + ", name", name, entry.getName() );
assertEquals( name + ", timestamp", time, new LocalDateTime( entry.getTime() ) );
Expand All @@ -143,6 +156,8 @@ private void assertFile( ZipInputStream in, String name, int size, LocalDateTime
byte[] bytes = new byte[1000];
assertEquals( size, in.read( bytes, 0, bytes.length ) );
assertEquals( content, new String( bytes, 0, size, charset ) );
in.closeEntry();

assertEquals( ZipArchiveEntry.PLATFORM_UNIX, entry.getPlatform() );
assertEquals( name + ", mode", mode.toString(), UnixFileMode.fromInt( entry.getUnixMode() ).toString() );
}
}

0 comments on commit 6cf8df6

Please sign in to comment.