View Javadoc
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 java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.nio.file.Files;
23  import java.nio.file.Paths;
24  import java.util.Calendar;
25  import java.util.Enumeration;
26  import java.util.Locale;
27  import java.util.TimeZone;
28  import java.util.zip.ZipEntry;
29  import java.util.zip.ZipFile;
30  
31  import org.codehaus.plexus.archiver.ArchiverException;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  import static org.junit.jupiter.api.Assertions.assertTrue;
37  
38  public abstract class BaseJarArchiverTest {
39  
40      /*
41       * Verify that the JarArchiver implementation
42       * could create basic JAR file
43       */
44      @Test
45      void testCreateJar() throws IOException, ArchiverException {
46          File jarFile = new File("target/output/testJar.jar");
47          jarFile.delete();
48  
49          JarArchiver archiver = getJarArchiver();
50          archiver.setDestFile(jarFile);
51          archiver.addDirectory(new File("src/test/resources/java-classes"));
52  
53          archiver.createArchive();
54  
55          // verify that the JAR file is created and contains the expected files
56          try (ZipFile resultingArchive = new ZipFile(jarFile)) {
57              // verify that the JAR file contains manifest directory and file
58              // and that those are the first two entries.
59              Enumeration<? extends ZipEntry> resultingEntries = resultingArchive.entries();
60              assertEquals("META-INF/", resultingEntries.nextElement().getName());
61              assertEquals("META-INF/MANIFEST.MF", resultingEntries.nextElement().getName());
62  
63              // verify the JAR contains the class and it is not corrupted
64              ZipEntry classFileEntry = resultingArchive.getEntry("com/example/app/Main.class");
65              InputStream resultingClassFile = resultingArchive.getInputStream(classFileEntry);
66              InputStream originalClassFile =
67                      Files.newInputStream(Paths.get("src/test/resources/java-classes/com/example/app/Main.class"));
68  
69              assertTrue(IOUtil.contentEquals(originalClassFile, resultingClassFile));
70          }
71      }
72  
73      protected static long normalizeLastModifiedTime(long dosTime) {
74          Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.ROOT);
75          cal.setTimeInMillis(dosTime);
76          return dosTime - (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET));
77      }
78  
79      protected abstract JarArchiver getJarArchiver();
80  }