1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.its.util;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26 import java.util.Enumeration;
27 import java.util.HashSet;
28 import java.util.LinkedHashSet;
29 import java.util.Set;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipFile;
32
33 import org.apache.commons.compress.archivers.ArchiveEntry;
34 import org.apache.maven.shared.verifier.VerificationException;
35 import org.apache.maven.shared.verifier.Verifier;
36 import org.codehaus.plexus.archiver.tar.GZipTarFile;
37
38 import static org.junit.jupiter.api.Assertions.assertTrue;
39 import static org.junit.jupiter.api.Assertions.fail;
40
41 public class TestUtils {
42
43 private static final String IT_REPO_LOCAL = "it.repo.local";
44
45 public static Verifier createVerifier(File testDir) throws VerificationException {
46 Verifier verifier = new Verifier(testDir.getAbsolutePath());
47
48
49 String itRepoLocal = System.getProperty(IT_REPO_LOCAL);
50 if (itRepoLocal != null) {
51 verifier.setLocalRepo(itRepoLocal);
52 }
53 return verifier;
54 }
55
56 public static String archivePathFromChild(String artifactId, String version, String childName, String childPath) {
57 if (!childPath.startsWith("/")) {
58 childPath = "/" + childPath;
59 }
60
61 return (artifactId + "-" + version + "/" + artifactId + "-" + childName + childPath);
62 }
63
64 public static String archivePathFromProject(String artifactId, String version, String path) {
65 if (!path.startsWith("/")) {
66 path = "/" + path;
67 }
68
69 return (artifactId + "-" + version + path);
70 }
71
72 public static void assertTarContents(Set<String> required, Set<String> banned, File assembly) throws IOException {
73 assertTrue(assembly.isFile(), "Assembly archive missing: " + assembly);
74
75 GZipTarFile tarFile = null;
76 try {
77 tarFile = new GZipTarFile(assembly);
78
79 LinkedHashSet<String> pathSet = new LinkedHashSet<>();
80
81 for (Enumeration<ArchiveEntry> enumeration = tarFile.getEntries(); enumeration.hasMoreElements(); ) {
82 pathSet.add(enumeration.nextElement().getName());
83 }
84 assertArchiveContents(required, banned, assembly.getAbsolutePath(), pathSet);
85 } finally {
86 if (tarFile != null) {
87 tarFile.close();
88 }
89 }
90 }
91
92 public static void assertZipContents(Set<String> required, Set<String> banned, File assembly) throws IOException {
93 assertTrue(assembly.isFile(), "Assembly archive missing: " + assembly);
94
95 try (ZipFile zf = new ZipFile(assembly)) {
96 LinkedHashSet<String> pathSet = new LinkedHashSet<>();
97
98 for (Enumeration<? extends ZipEntry> enumeration = zf.entries(); enumeration.hasMoreElements(); ) {
99 pathSet.add(enumeration.nextElement().getName());
100 }
101 assertArchiveContents(required, banned, assembly.getAbsolutePath(), pathSet);
102 }
103 }
104
105 private static void assertArchiveContents(
106 Set<String> required, Set<String> banned, String assemblyName, Set<String> contents) {
107
108 Set<String> missing = new HashSet<>();
109 for (String name : required) {
110 if (!contents.contains(name)) {
111 missing.add(name);
112 }
113 }
114
115 Set<String> banViolations = new HashSet<>();
116 for (String name : banned) {
117 if (contents.contains(name)) {
118 banViolations.add(name);
119 }
120 }
121
122 if (!missing.isEmpty() || !banViolations.isEmpty()) {
123 StringBuilder msg = new StringBuilder();
124 msg.append("The following errors were found in:\n\n");
125 msg.append(assemblyName);
126 msg.append("\n");
127 msg.append("\nThe following REQUIRED entries were missing from the bundle archive:\n");
128
129 if (missing.isEmpty()) {
130 msg.append("\nNone.");
131 } else {
132 for (String name : missing) {
133 msg.append("\n").append(name);
134 }
135 }
136
137 msg.append("\n\nThe following BANNED entries were present from the bundle archive:\n");
138
139 if (banViolations.isEmpty()) {
140 msg.append("\nNone.\n");
141 } else {
142 for (String name : banViolations) {
143 msg.append("\n").append(name);
144 }
145 }
146
147 msg.append("\n").append("Archive contents:\n");
148 for (String path : contents) {
149 msg.append("\n").append(path);
150 }
151
152 fail(msg.toString());
153 }
154 }
155
156 public static File getTestDir(String name) throws IOException, URISyntaxException {
157 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
158 URL resource = cloader.getResource("maven-projects/" + name);
159
160 if (resource == null) {
161 throw new IOException("Cannot find test directory: " + name);
162 }
163
164 return new File(new URI(resource.toExternalForm()).normalize().getPath());
165 }
166 }