OpearFile implements Opear { public static Result<OpearFile> fromDirectory(final @NotNull File directory) { return Result.success(Optional.of(new File(directory, JarFile.MANIFEST_NAME))) .flatMap(oMfFile -> oMfFile .filter(File::exists) .map(OpearFile::readExpectedManifest) .map(manResult -> manResult.flatMap(OpearFile::validateOpearManifest)) .orElseGet(() -> Result.success(metaForSimpleDir(directory))) ) .map(meta -> new OpearFile(directory, meta)); } OpearFile(final File cacheDir, final OpearMetadata metadata); @Override URL getDefaultPlan(); @Override Result<URL> getSpecificPlan(final @NotNull String planName); @Override ClassLoader getPlanClassLoader(final @NotNull ClassLoader parent); static Result<OpearFile> fromDirectory(final @NotNull File directory); static Result<OpearFile> fromJar(final @NotNull JarFile jarFile, final @NotNull File cacheBaseDir); static final Attributes.Name NAME_BUNDLE_SYMBOLICNAME; static final Attributes.Name NAME_BUNDLE_VERSION; static final Attributes.Name NAME_CLASS_PATH; static final Attributes.Name NAME_OAKPAL_VERSION; static final Attributes.Name NAME_OAKPAL_PLAN; }
@Test public void testFindDefaultPlanLocation() { Result<OpearFile> opearResult = OpearFile.fromDirectory(new File("src/test/resources/plans/bar")); assertFalse("opearResult should not be a failure", opearResult.isFailure()); Result<URL> urlResult = opearResult.map(Opear::getDefaultPlan); assertFalse("urlResult should not be a failure", urlResult.isFailure()); Result<OakpalPlan> planResult = urlResult.flatMap(OakpalPlan::fromJson); assertFalse("plan should load successfully", planResult.isFailure()); List<String> checklists = planResult.map(OakpalPlan::getChecklists).getOrDefault(Collections.emptyList()); assertEquals("checklists should contain test/bar checklist", Collections.singletonList("test/bar"), checklists); Result<OpearFile> notAPlanResult = OpearFile.fromDirectory(new File("src/test/resources/plans/none")); assertFalse("notAPlanResult should not be a failure", notAPlanResult.isFailure()); Result<URL> notAPlanUrlResult = notAPlanResult.map(Opear::getDefaultPlan); assertFalse("notAPlanUrlResult should not be a failure", notAPlanUrlResult.isFailure()); Result<OakpalPlan> notAPlanPlanResult = notAPlanUrlResult.flatMap(OakpalPlan::fromJson); assertFalse("notAPlanPlan should load successfully", notAPlanPlanResult.isFailure()); List<String> notAPlanChecklists = notAPlanPlanResult.map(OakpalPlan::getChecklists).getOrDefault(Collections.emptyList()); assertEquals("notAPlanChecklists should contain no checklists", Collections.singletonList("net.adamcin.oakpal.core/basic"), notAPlanChecklists); }
@Test public void testGetPlanClassPath() { Result<OpearFile> opearResult = OpearFile.fromDirectory( new File("src/test/resources/OpearFileTest/folders_on_classpath")); assertFalse("opearResult should not be a failure", opearResult.isFailure()); OpearFile opearFile = opearResult.getOrDefault(null); assertNotNull("opearFile is not null", opearFile); assertArrayEquals("classpath should be", new String[]{"classes", "test-classes"}, opearFile.metadata.getPlanClassPath()); }