Skip to content

Commit

Permalink
fix: Catch InvalidPathException and adapted registry test (#110)
Browse files Browse the repository at this point in the history
Refs: #106
  • Loading branch information
nirikash authored Sep 19, 2024
1 parent 66f4691 commit 25fad55
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
Expand Down Expand Up @@ -39,19 +40,23 @@ public class HookManifestUtils {
public static @Nullable Manifest loadManifestByClass(@NotNull Class<? extends ActionHook> hookClass) throws IOException, URISyntaxException {
CodeSource codeSource = hookClass.getProtectionDomain().getCodeSource();

if (codeSource != null) {
URL location = codeSource.getLocation();
Path jarPath = Paths.get(location.getPath());

if (Files.isRegularFile(jarPath)) {
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
return jarFile.getManifest();
try {
if (codeSource != null) {
URL location = codeSource.getLocation();
Path jarPath = Paths.get(location.getPath());

if (Files.isRegularFile(jarPath)) {
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
return jarFile.getManifest();
}
} else {
throw new IllegalArgumentException("The class %s is not loaded from a JAR file.".formatted(hookClass.getName()));
}
} else {
throw new IllegalArgumentException("The class %s is not loaded from a JAR file.".formatted(hookClass.getName()));
throw new IllegalArgumentException("Could not determine the code source location.");
}
} else {
throw new IllegalArgumentException("Could not determine the code source location.");
} catch (InvalidPathException e) {
throw new IllegalArgumentException("Code location is not convertable to path: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@
class HooksRegistryTest {

@Test
public void shouldThrowExceptionForHookLoadedNotFromJar() {
assertThatThrownBy(() -> HooksRegistry.HOOKS.addHook(new ActionHook(ActionHook.ItemType.PLAN, ActionHook.ActionType.DELETE, "Test hook") {
@Override
public @NotNull HookExecutor getExecutor() {
return new HookExecutor() {};
}

@Override
public String getDefaultSettings() {
return "";
}
}))
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("is not loaded from a JAR file");
public void shouldThrowExceptionForHookLoadedNotFromJarWithoutVersion() {
assertThatThrownBy(() -> HooksRegistry.HOOKS.addHook(new MyActionHook()))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void shouldThrowExceptionForHookWithVersion() {
assertThatThrownBy(() -> HooksRegistry.HOOKS.addHook(new MyActionHook("test version")))
.isInstanceOf(IllegalStateException.class);
}

private static class MyActionHook extends ActionHook {
public MyActionHook() {
this(null);
}

public MyActionHook(String version) {
super(ItemType.PLAN, ActionType.DELETE, version, "Test hook");
}

@Override
public @NotNull HookExecutor getExecutor() {
return new HookExecutor() {
};
}

@Override
public String getDefaultSettings() {
return "";
}
}
}

0 comments on commit 25fad55

Please sign in to comment.