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