AdhocOpear implements Opear { public static Result<AdhocOpear> fromPlanFile(final @NotNull File planFile, final @Nullable File baseDir) { final Function<File, Result<URL>> fnFileUrl = compose1(File::toURI, result1(URI::toURL)); final Result<URL> planFileUrlResult = fnFileUrl.apply(planFile); return planFileUrlResult .flatMap(planFileUrl -> fnFileUrl.apply(Optional.ofNullable(baseDir).orElse(planFile.getParentFile())) .flatMap(baseUrl -> OakpalPlan.fromJson(planFileUrl).map(plan -> new AdhocOpear(planFileUrl, baseUrl)))); } AdhocOpear(final @NotNull URL planFileUrl, final @NotNull URL baseUrl); @Override URL getDefaultPlan(); @Override Result<URL> getSpecificPlan(final @NotNull String planName); @Override ClassLoader getPlanClassLoader(final @NotNull ClassLoader parent); static Result<AdhocOpear> fromPlanFile(final @NotNull File planFile, final @Nullable File baseDir);  }
@Test public void testFromPlanFile() { final File planFile = new File("src/test/resources/OpearFileTest/folders_on_classpath/plan.json"); Result<AdhocOpear> opearResult = AdhocOpear.fromPlanFile(planFile, null); assertFalse("opear should not be a failure", opearResult.isFailure()); AdhocOpear opear = opearResult.getOrDefault(null); assertNotNull("opear is not null", opear); assertEquals("expect planFile is referenced by default plan url", planFile.getAbsolutePath(), Fun.tryOrOptional0(() -> new File(opear.getDefaultPlan().toURI()).getAbsolutePath()).get().orElse("")); assertEquals("expect planFile is referenced by specific plan referenced by empty plan name", planFile.getAbsolutePath(), opear.getSpecificPlan("") .flatMap(Fun.result1(URL::toURI)) .map(Fun.compose1(File::new, File::getAbsolutePath)).getOrDefault("")); final ClassLoader parent = getClass().getClassLoader(); final ClassLoader planCl = opear.getPlanClassLoader(parent); assertNotSame("not same classloader", parent, planCl); assertSame("expect same parent", parent, planCl.getParent()); assertTrue("expect instance of URLClassLoader", planCl instanceof URLClassLoader); final URL firstUrl = ((URLClassLoader) planCl).getURLs()[0]; assertEquals("expect planFile parent is referenced by first url", planFile.getParentFile().getAbsolutePath(), Fun.tryOrOptional0(() -> new File(firstUrl.toURI()).getAbsolutePath()).get().orElse("")); }