Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE #6388

Merged
merged 1 commit into from
Dec 5, 2024
Merged

Fix NPE #6388

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions biz.aQute.bndlib.tests/test/test/AnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -30,10 +31,13 @@
import aQute.bnd.osgi.Clazz;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.EmbeddedResource;
import aQute.bnd.osgi.FileResource;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Packages;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Resource;
import aQute.bnd.osgi.metainf.MetaInfService;
import aQute.lib.io.IO;

class T0 {}
Expand Down Expand Up @@ -1416,6 +1420,35 @@ public void testSuperfluous() throws Exception {
}
}

@Test
public void testEmptyMetaInfServicesFolder() throws Exception {

Resource r = new EmbeddedResource("foo", 0L);
try (Jar jar = new Jar("test")) {
jar.putResource("META-INF/services/subfolder/", r);

Map<String, Resource> map = jar.getDirectories()
.getOrDefault("META-INF/services", Collections.emptyMap());

assertTrue(jar.getDirectories()
.containsKey("META-INF/services/subfolder"));
assertNotNull(jar.getDirectories()
.get("META-INF/services/subfolder"));
assertEquals(1, jar.getDirectories()
.get("META-INF/services/subfolder")
.size());

assertTrue(jar.getDirectories()
.containsKey("META-INF/services"));
assertNull(jar.getDirectories()
.get("META-INF/services"));

Map<String, MetaInfService> serviceFiles = MetaInfService.getServiceFiles(jar);
assertNotNull(serviceFiles);
assertTrue(serviceFiles.isEmpty());
}
}

static void assertNotPresent(Collection<?> map, String string) {
Collection<String> ss = new HashSet<>();
for (Object o : map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public static Map<String, MetaInfService> getServiceFiles(Jar jar) throws Except
Map<String, Resource> map = jar.getDirectories()
.getOrDefault(META_INF_SERVICES_STEM, Collections.emptyMap());

if (map == null) {
// can happen when META-INF/services is empty, but has subfolders
return result;
}

for (Map.Entry<String, Resource> e : map.entrySet()) {
String path = e.getKey();
Resource resource = e.getValue();
Expand Down
Loading