View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.dependency.analyzer;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.nio.charset.StandardCharsets;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.util.HashSet;
28  import java.util.Set;
29  import java.util.jar.JarOutputStream;
30  import java.util.zip.ZipEntry;
31  import java.util.zip.ZipException;
32  
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  import org.junit.jupiter.api.io.TempDir;
36  
37  import static org.assertj.core.api.Assertions.assertThat;
38  import static org.assertj.core.api.Assertions.fail;
39  
40  /**
41   * Tests <code>DefaultClassAnalyzer</code>.
42   *
43   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
44   * @see DefaultClassAnalyzer
45   */
46  class DefaultClassAnalyzerTest {
47  
48      @TempDir
49      private Path tempDir;
50  
51      private Path file;
52  
53      @BeforeEach
54      void setUp() throws IOException {
55          file = Files.createTempFile(tempDir, "test", ".jar");
56          try (JarOutputStream out = new JarOutputStream(new FileOutputStream(file.toFile()))) {
57              addZipEntry(out, "a/b/c.class", "class a.b.c");
58              addZipEntry(out, "x/y/z.class", "class x.y.z");
59          }
60      }
61  
62      @Test
63      void testAnalyzeWithJar() throws IOException {
64          Set<String> expectedClasses = new HashSet<>();
65          expectedClasses.add("a.b.c");
66          expectedClasses.add("x.y.z");
67  
68          DefaultClassAnalyzer analyzer = new DefaultClassAnalyzer();
69          Set<String> actualClasses = analyzer.analyze(file.toUri().toURL());
70  
71          assertThat(actualClasses).isEqualTo(expectedClasses);
72      }
73  
74      @Test
75      void testAnalyzeBadJar() throws IOException {
76          // to reproduce MDEP-143
77          // corrupt the jar file by altering its contents
78          ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
79          Files.copy(file, baos);
80          byte[] ba = baos.toByteArray();
81          ba[50] = 1;
82          Files.write(file, ba);
83  
84          ClassAnalyzer analyzer = new DefaultClassAnalyzer();
85  
86          try {
87              analyzer.analyze(file.toUri().toURL());
88              fail("Exception expected");
89          } catch (ZipException e) {
90              assertThat(e).hasMessageStartingWith("Cannot process Jar entry on URL:");
91          }
92      }
93  
94      private void addZipEntry(JarOutputStream out, String fileName, String content) throws IOException {
95          out.putNextEntry(new ZipEntry(fileName));
96          byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
97          out.write(bytes, 0, bytes.length);
98      }
99  }