Skip to content

Commit 07a7588

Browse files
committed
Add tests for StandardJarScanner scanManifest option to verify Class-Path manifest entries are followed when enabled and ignored when disabled.
1 parent 98596f4 commit 07a7588

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

test/org/apache/tomcat/util/scan/TestStandardJarScanner.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
package org.apache.tomcat.util.scan;
1818

1919
import java.io.File;
20+
import java.io.FileOutputStream;
2021
import java.io.IOException;
2122
import java.net.MalformedURLException;
2223
import java.net.URL;
2324
import java.net.URLClassLoader;
2425
import java.util.ArrayList;
26+
import java.util.Deque;
2527
import java.util.List;
28+
import java.util.jar.Attributes;
29+
import java.util.jar.JarOutputStream;
30+
import java.util.jar.Manifest;
2631

2732
import org.junit.Assert;
2833
import org.junit.Assume;
@@ -103,6 +108,57 @@ public ClassLoader getClassLoader() {
103108
scanner.scan(JarScanType.PLUGGABILITY, context, callback);
104109
}
105110

111+
@Test
112+
public void testScanManifestDefault() throws Exception {
113+
Assert.assertTrue("Referenced JAR from manifest Class-Path should be scanned",
114+
doTestScanManifest(true));
115+
}
116+
117+
@Test
118+
public void testScanManifestDisabled() throws Exception {
119+
Assert.assertFalse("Referenced JAR from manifest Class-Path should not be scanned",
120+
doTestScanManifest(false));
121+
}
122+
123+
private boolean doTestScanManifest(boolean scanManifest) throws Exception {
124+
File referencedJar = new File(System.getProperty("java.io.tmpdir"), "referenced.jar");
125+
referencedJar.deleteOnExit();
126+
JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(referencedJar), new Manifest());
127+
jarOutputStream.close();
128+
129+
File testJar = new File(System.getProperty("java.io.tmpdir"), "manifest-test.jar");
130+
testJar.deleteOnExit();
131+
132+
Manifest manifest = new Manifest();
133+
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
134+
manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, "referenced.jar");
135+
136+
jarOutputStream = new JarOutputStream(new FileOutputStream(testJar), manifest);
137+
jarOutputStream.close();
138+
139+
StandardJarScanner scanner = new StandardJarScanner() {
140+
@Override
141+
protected void addClassPath(Deque<URL> classPathUrlsToProcess) {
142+
super.addClassPath(classPathUrlsToProcess);
143+
try {
144+
classPathUrlsToProcess.add(testJar.toURI().toURL());
145+
} catch (MalformedURLException e) {
146+
throw new RuntimeException(e);
147+
}
148+
}
149+
};
150+
scanner.setScanManifest(scanManifest);
151+
152+
LoggingCallback callback = new LoggingCallback();
153+
scanner.scan(JarScanType.PLUGGABILITY, new TesterServletContext(), callback);
154+
155+
for (String cb : callback.callbacks) {
156+
if (cb.contains("referenced.jar")) {
157+
return true;
158+
}
159+
}
160+
return false;
161+
}
106162

107163
private static class LoggingCallback implements JarScannerCallback {
108164

0 commit comments

Comments
 (0)