Skip to content

Commit 38e2880

Browse files
committed
Add base test class for JAR archivers
We're going to add Archiver implementation that could be used for both regular and modular JAR archives. This base test class is going to be useful to reuse tests between it and the existing JarArchiver.
1 parent 24444fe commit 38e2880

File tree

6 files changed

+97
-5
lines changed

6 files changed

+97
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
*
3+
* Copyright 2018 The Apache Software Foundation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.codehaus.plexus.archiver.jar;
18+
19+
import org.codehaus.plexus.archiver.ArchiverException;
20+
import org.codehaus.plexus.util.IOUtil;
21+
import org.junit.Test;
22+
23+
import java.io.File;
24+
import java.io.FileInputStream;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.util.zip.ZipEntry;
28+
import java.util.zip.ZipFile;
29+
30+
import static org.junit.Assert.assertNotNull;
31+
import static org.junit.Assert.assertTrue;
32+
33+
public abstract class BaseJarArchiverTest
34+
{
35+
36+
/*
37+
* Verify that the JarArchiver implementation
38+
* could create basic JAR file
39+
*/
40+
@Test
41+
public void testCreateJar()
42+
throws IOException, ArchiverException
43+
{
44+
File jarFile = new File( "target/output/testJar.jar" );
45+
jarFile.delete();
46+
47+
JarArchiver archiver = getJarArchiver();
48+
archiver.setDestFile( jarFile );
49+
archiver.addDirectory( new File( "src/test/resources/java-classes" ) );
50+
51+
archiver.createArchive();
52+
53+
// verify that the JAR file is created and contains the expected files
54+
try ( ZipFile resultingArchive = new ZipFile( jarFile ) )
55+
{
56+
// verify that the JAR file contains manifest file
57+
assertNotNull( resultingArchive.getEntry( "META-INF/MANIFEST.MF" ) );
58+
59+
// verify the JAR contains the class and it is not corrupted
60+
ZipEntry classFileEntry = resultingArchive.getEntry( "com/example/app/Main.class" );
61+
InputStream resultingClassFile = resultingArchive.getInputStream( classFileEntry );
62+
InputStream originalClassFile =
63+
new FileInputStream( "src/test/resources/java-classes/com/example/app/Main.class" );
64+
65+
assertTrue( IOUtil.contentEquals( originalClassFile, resultingClassFile ) );
66+
}
67+
}
68+
69+
protected abstract JarArchiver getJarArchiver();
70+
71+
}

src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
import java.io.IOException;
66
import java.util.Random;
77
import org.codehaus.plexus.archiver.ArchiverException;
8-
import junit.framework.TestCase;
8+
import org.junit.Test;
99

1010
public class JarArchiverTest
11-
extends TestCase
11+
extends BaseJarArchiverTest
1212
{
1313

14+
@Test
1415
public void testCreateManifestOnlyJar()
1516
throws IOException, ManifestException, ArchiverException
1617
{
1718
File jarFile = File.createTempFile( "JarArchiverTest.", ".jar" );
1819
jarFile.deleteOnExit();
1920

20-
JarArchiver archiver = new JarArchiver();
21+
JarArchiver archiver = getJarArchiver();
2122
archiver.setDestFile( jarFile );
2223

2324
Manifest manifest = new Manifest();
@@ -30,18 +31,20 @@ public void testCreateManifestOnlyJar()
3031
archiver.createArchive();
3132
}
3233

34+
@Test
3335
public void testNonCompressed()
3436
throws IOException, ManifestException, ArchiverException
3537
{
3638
File jarFile = new File( "target/output/jarArchiveNonCompressed.jar" );
3739

38-
JarArchiver archiver = new JarArchiver();
40+
JarArchiver archiver = getJarArchiver();
3941
archiver.setDestFile( jarFile );
4042
archiver.setCompress( false );
4143
archiver.addDirectory( new File( "src/test/resources/mjar179" ) );
4244
archiver.createArchive();
4345
}
4446

47+
@Test
4548
public void testVeryLargeJar()
4649
throws IOException, ManifestException, ArchiverException
4750
{
@@ -65,10 +68,15 @@ public void testVeryLargeJar()
6568

6669
File jarFile = new File( "target/output/veryLargeJar.jar" );
6770

68-
JarArchiver archiver = new JarArchiver();
71+
JarArchiver archiver = getJarArchiver();
6972
archiver.setDestFile( jarFile );
7073
archiver.addDirectory( tmpDir );
7174
archiver.createArchive();
7275
}
7376

77+
@Override
78+
protected JarArchiver getJarArchiver()
79+
{
80+
return new JarArchiver();
81+
}
7482
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ley=value

src/test/resources/java-src/REAMDE.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is the source code for the Java classes
2+
inside `java-classes` directory.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.app;
2+
3+
public class Main
4+
{
5+
6+
public static void main( String[] args )
7+
{
8+
System.out.println( "Hello from Main" );
9+
}
10+
}

0 commit comments

Comments
 (0)