Skip to content

Commit d285476

Browse files
committed
##122 - Test ZipArchiverTest.testFixedEntryModificationTime() is
TimeZone dependent - Make the test independent of the TimeZone. - ZipFile now used inside try-with-resources so it is closed after the asserts.
1 parent 0300fa8 commit d285476

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@
3232
import java.io.InputStream;
3333
import java.nio.file.Files;
3434
import java.nio.file.attribute.FileTime;
35+
import java.text.DateFormat;
36+
import java.text.ParseException;
37+
import java.text.SimpleDateFormat;
3538
import java.util.Arrays;
3639
import java.util.Date;
3740
import java.util.Enumeration;
3841
import java.util.Map;
42+
import java.util.TimeZone;
3943
import java.util.zip.ZipEntry;
4044
import java.util.zip.ZipInputStream;
4145
import java.util.zip.ZipOutputStream;
46+
4247
import javax.annotation.Nonnull;
48+
4349
import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
4450
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
4551
import org.apache.commons.compress.archivers.zip.ZipExtraField;
@@ -889,18 +895,48 @@ public void testForcedFileModes()
889895
public void testFixedEntryModificationTime()
890896
throws IOException
891897
{
892-
final long almostMinDosTime = 315532802000L;
898+
final long almostMinDosTime = toLocalTimeZone( 315532802000L );
899+
893900
final File zipFile = getTestFile( "target/output/zip-with-fixed-entry-modification-times.zip" );
894901
final ZipArchiver archiver = getZipArchiver( zipFile );
895902
archiver.setLastModifiedDate( new Date( almostMinDosTime ) );
896903
archiver.addDirectory( new File( "src/test/resources/zip-timestamp" ) );
897904
archiver.createArchive();
898905

899906
assertTrue( zipFile.exists() );
900-
final ZipFile zf = new ZipFile( zipFile );
901-
assertEquals( almostMinDosTime, zf.getEntry( "file-with-even-time.txt" ).getTime() );
902-
assertEquals( almostMinDosTime, zf.getEntry( "file-with-odd-time.txt" ).getTime() );
903-
assertEquals( almostMinDosTime, zf.getEntry( "foo/" ).getTime() );
907+
try ( final ZipFile zf = new ZipFile( zipFile ) )
908+
{
909+
assertEquals( almostMinDosTime, zf.getEntry( "file-with-even-time.txt" ).getTime() );
910+
assertEquals( almostMinDosTime, zf.getEntry( "file-with-odd-time.txt" ).getTime() );
911+
assertEquals( almostMinDosTime, zf.getEntry( "foo/" ).getTime() );
912+
}
913+
}
914+
915+
/**
916+
* Takes a timestamp located in GMT TZ and convert it to the default system TZ. This makes the test independent of
917+
* the current TZ.
918+
*
919+
* @param timestamp the epoch time to convert.
920+
* @return the timestamp matching the same input date but in the local TZ.
921+
*/
922+
private long toLocalTimeZone( long timestamp )
923+
{
924+
String dateFormat = "dd-MM-yyyy hh:mm:ss a";
925+
DateFormat formatterWithTimeZone = new SimpleDateFormat( dateFormat );
926+
formatterWithTimeZone.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
927+
String sDate = formatterWithTimeZone.format( new Date( timestamp ) );
928+
929+
DateFormat formatter = new SimpleDateFormat( dateFormat );
930+
try
931+
{
932+
Date dateWithTimeZone = formatter.parse( sDate );
933+
return dateWithTimeZone.getTime();
934+
}
935+
catch ( ParseException e )
936+
{
937+
fail( "Date '" + sDate + "' can not be parsed!" );
938+
return 0L;
939+
}
904940
}
905941

906942
}

0 commit comments

Comments
 (0)