Skip to content

Commit

Permalink
Renamed new CLI arguments; Added test for AdhocOpear.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed Apr 27, 2020
1 parent a737437 commit dc8d7aa
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 59 deletions.
9 changes: 4 additions & 5 deletions cli/src/main/java/net/adamcin/oakpal/cli/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -202,11 +201,11 @@ Optional<String> flipOpt(final @NotNull String wholeOpt) {
builder.setPlanName(isNoOpt ? null : args[++i]);
break;
case "-pf":
case "--plan-from-file":
builder.setPlanFromFile(isNoOpt ? null : console.getCwd().toPath().resolve(args[++i]).toFile());
case "--plan-file":
builder.setPlanFile(isNoOpt ? null : console.getCwd().toPath().resolve(args[++i]).toFile());
break;
case "--plan-from-file-base":
builder.setPlanFromFileBaseDir(isNoOpt ? null : console.getCwd().toPath().resolve(args[++i]).toFile());
case "--plan-file-base":
builder.setPlanFileBaseDir(isNoOpt ? null : console.getCwd().toPath().resolve(args[++i]).toFile());
break;
case "-pi":
case "--pre-install-file":
Expand Down
42 changes: 21 additions & 21 deletions cli/src/main/java/net/adamcin/oakpal/cli/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ final class Options {
private final ClassLoader scanClassLoader;
private final File cacheDir;
private final File opearFile;
private final File planFromFile;
private final File planFromFileBaseDir;
private final String planName;
private final File planFile;
private final File planFileBaseDir;
private final List<File> preInstallFiles;
private final List<File> extendedClassPathFiles;
private final String planName;
private final boolean noHooks;
private final List<File> scanFiles;
private final Function<StructuredMessage, IO<Nothing>> printer;
Expand All @@ -68,8 +68,8 @@ final class Options {
final @NotNull File cacheDir,
final @Nullable File opearFile,
final @Nullable String planName,
final @Nullable File planFromFile,
final @Nullable File planFromFileBaseDir,
final @Nullable File planFile,
final @Nullable File planFileBaseDir,
final @NotNull List<File> preInstallFiles,
final @NotNull List<File> extendedClassPathFiles,
final boolean noHooks,
Expand All @@ -84,8 +84,8 @@ final class Options {
this.cacheDir = cacheDir;
this.opearFile = opearFile;
this.planName = planName;
this.planFromFile = planFromFile;
this.planFromFileBaseDir = planFromFileBaseDir;
this.planFile = planFile;
this.planFileBaseDir = planFileBaseDir;
this.preInstallFiles = preInstallFiles;
this.extendedClassPathFiles = extendedClassPathFiles;
this.noHooks = noHooks;
Expand Down Expand Up @@ -130,12 +130,12 @@ public File getCacheDir() {
return planName;
}

public @Nullable File getPlanFromFile() {
return planFromFile;
public @Nullable File getPlanFile() {
return planFile;
}

public @Nullable File getPlanFromFileBaseDir() {
return planFromFileBaseDir;
public @Nullable File getPlanFileBaseDir() {
return planFileBaseDir;
}

public @NotNull List<File> getPreInstallFiles() {
Expand Down Expand Up @@ -190,8 +190,8 @@ static final class Builder {
private boolean noPlan;
private boolean noHooks;
private String planName;
private File planFromFile;
private File planFromFileBaseDir;
private File planFile;
private File planFileBaseDir;
private List<File> preInstallFiles = new ArrayList<>();
private List<File> extendedClassPathFiles = new ArrayList<>();
private File outFile;
Expand Down Expand Up @@ -235,13 +235,13 @@ public Builder setPlanName(final @Nullable String planName) {
return this;
}

public Builder setPlanFromFileBaseDir(final @Nullable File planFromFileBaseDir) {
this.planFromFileBaseDir = planFromFileBaseDir;
public Builder setPlanFileBaseDir(final @Nullable File planFileBaseDir) {
this.planFileBaseDir = planFileBaseDir;
return this;
}

public Builder setPlanFromFile(final @Nullable File planFromFile) {
this.planFromFile = planFromFile;
public Builder setPlanFile(final @Nullable File planFile) {
this.planFile = planFile;
return this;
}

Expand Down Expand Up @@ -292,7 +292,7 @@ public Builder setFailOnSeverity(final @Nullable Severity failOnSeverity) {

Result<Opear> buildOpear(final @NotNull Console console, final @NotNull File opearCache) {
final Result<Opear> baseOpear;
if (planFromFile != null) {
if (planFile != null) {
baseOpear = buildAdhocOpear(console).map(Function.identity());
} else {
baseOpear = buildOpearFile(console, opearCache).map(Function.identity());
Expand All @@ -301,7 +301,7 @@ Result<Opear> buildOpear(final @NotNull Console console, final @NotNull File ope
}

Result<AdhocOpear> buildAdhocOpear(final @NotNull Console console) {
return AdhocOpear.fromPlanFile(planFromFile, planFromFileBaseDir);
return AdhocOpear.fromPlanFile(planFile, planFileBaseDir);
}

Result<OpearFile> buildOpearFile(final @NotNull Console console, final @NotNull File opearCache) {
Expand Down Expand Up @@ -352,8 +352,8 @@ Result<Options> build(final @NotNull Console console) {
.flatMap(planUrl -> getExtendedClassLoader(opear, getClass().getClassLoader())
.flatMap(classLoader -> messageWriter(console, outputJson, outFile).map(writer ->
new Options(justHelp, justVersion, storeBlobs, planUrl,
classLoader, realCacheDir, opearFile, planName, planFromFile,
planFromFileBaseDir, preInstallFiles, extendedClassPathFiles,
classLoader, realCacheDir, opearFile, planName, planFile,
planFileBaseDir, preInstallFiles, extendedClassPathFiles,
noHooks, scanFiles, writer, Optional.ofNullable(failOnSeverity)
.orElse(DEFAULT_OPTIONS.failOnSeverity))))));
}
Expand Down
9 changes: 5 additions & 4 deletions cli/src/main/resources/net/adamcin/oakpal/cli/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ oakpal [ <options> ] <scanFile> ...
+p | --no-plan : Use no plan for the scan. Overrides the default behavior, which otherwise
uses the oakpal core "basic-plan.json".
-pi | --pre-install-file <file> : Add a preinstall package to the list specified in the plan, if any.
-pf | --plan-from-file <file> : Override the opear plan with the specified json file. The base directory for the
-pf | --plan-file <file> : Override the opear plan with the specified json file. The base directory for the
plan will default to the parent directory of the file, unless a different
directory is specified using --plan-from-file-base.
--plan-from-file-base <dir> : Specify a different base classpath directory for --plan-from-file. This is
directory is specified using --plan-file-base.
--plan-file-base <dir> : Specify a different base classpath directory for --plan-file. This is
important for resolution of script checks referenced by relative path in the plan
check impl properties.
check impl properties, but does not affect resolution of resources linked by URL,
like preInstallUrls.
--no-hooks : Disable preinstall and scan install hooks for all packages, otherwise, rely on
install hook policies configured in the selected plan.
-xp | --extend-classpath <file> : Extend the opear classpath with the specified jar file or directory.
Expand Down
24 changes: 12 additions & 12 deletions cli/src/test/java/net/adamcin/oakpal/cli/CommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,15 @@ public void testParseArgs_adhocOpear() throws Exception {

validator.expectSuccess(
args(
"--plan-from-file", planFromFile.getAbsolutePath(),
"--plan-from-file-base", planFromFile.getParentFile().getAbsolutePath(),
"--plan-file", planFromFile.getAbsolutePath(),
"--plan-file-base", planFromFile.getParentFile().getAbsolutePath(),
"--pre-install-file", contentPackageJar.getAbsolutePath(),
"--extend-classpath", testModuleJar.getAbsolutePath()),
options -> {
assertEquals("expect plan from file",
planFromFile.getAbsolutePath(), options.getPlanFromFile().getAbsolutePath());
assertEquals("expect plan from file base dir",
planFromFile.getParentFile().getAbsolutePath(), options.getPlanFromFileBaseDir().getAbsolutePath());
assertEquals("expect plan file",
planFromFile.getAbsolutePath(), options.getPlanFile().getAbsolutePath());
assertEquals("expect plan file base dir",
planFromFile.getParentFile().getAbsolutePath(), options.getPlanFileBaseDir().getAbsolutePath());
assertTrue("expect pre-install file", options.getPreInstallFiles().stream()
.anyMatch(file -> file.getAbsolutePath().equals(contentPackageJar.getAbsolutePath())));
assertTrue("expect classpath", options.getExtendedClassPathFiles().stream()
Expand All @@ -358,18 +358,18 @@ public void testParseArgs_adhocOpear() throws Exception {

validator.expectSuccess(
args(
"--plan-from-file", planFromFile.getAbsolutePath(),
"--no-plan-from-file",
"--plan-from-file-base", planFromFile.getParentFile().getAbsolutePath(),
"--no-plan-from-file-base",
"--plan-file", planFromFile.getAbsolutePath(),
"--no-plan-file",
"--plan-file-base", planFromFile.getParentFile().getAbsolutePath(),
"--no-plan-file-base",
"--pre-install-file", contentPackageJar.getAbsolutePath(),
"--no-pre-install-file",
"--extend-classpath", testModuleJar.getAbsolutePath(),
"--no-extend-classpath"),

options -> {
assertNull("expect null plan from file", options.getPlanFromFile());
assertNull("expect null plan from file base dir", options.getPlanFromFileBaseDir());
assertNull("expect null plan file", options.getPlanFile());
assertNull("expect null plan file base dir", options.getPlanFileBaseDir());
assertFalse("expect no pre-install file", options.getPreInstallFiles().stream()
.anyMatch(file -> file.getAbsolutePath().equals(contentPackageJar.getAbsolutePath())));
assertFalse("expect no classpath", options.getExtendedClassPathFiles().stream()
Expand Down
24 changes: 12 additions & 12 deletions cli/src/test/java/net/adamcin/oakpal/cli/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,27 @@ public void testAdhocOpear() {
final Result<Options> defaultOptionsResult = new Options.Builder().build(console);
assertFalse("options build is successful", defaultOptionsResult.getError().isPresent());
defaultOptionsResult.forEach(options -> {
assertNull("planFromFile is null by default", options.getPlanFromFile());
assertNull("planFromFileBaseDir is null by default", options.getPlanFromFileBaseDir());
assertNull("planFromFile is null by default", options.getPlanFile());
assertNull("planFromFileBaseDir is null by default", options.getPlanFileBaseDir());
});

final Result<Options> optionsResult = new Options.Builder()
.setPlanFromFile(new File("src/test/resources/opears/adhocPlan/plan.json")).build(console);
.setPlanFile(new File("src/test/resources/opears/adhocPlan/plan.json")).build(console);
optionsResult.forEach(options -> {
assertTrue("planFromFile should be a file: " + options.getPlanFromFile().getAbsolutePath(),
!options.getPlanFromFile().isDirectory());
assertNull("planFromFileBaseDir should be null", options.getPlanFromFileBaseDir());
assertTrue("planFromFile should be a file: " + options.getPlanFile().getAbsolutePath(),
!options.getPlanFile().isDirectory());
assertNull("planFromFileBaseDir should be null", options.getPlanFileBaseDir());
});

final Result<Options> optionsWithBaseResult = new Options.Builder()
.setPlanFromFile(new File("src/test/resources/opears/adhocPlan/plan.json"))
.setPlanFromFileBaseDir(console.getCwd())
.setPlanFile(new File("src/test/resources/opears/adhocPlan/plan.json"))
.setPlanFileBaseDir(console.getCwd())
.build(console);
optionsWithBaseResult.forEach(options -> {
assertTrue("planFromFile should be a file: " + options.getPlanFromFile().getAbsolutePath(),
!options.getPlanFromFile().isDirectory());
assertTrue("planFromFileBaseDir should be a directory: " + options.getPlanFromFileBaseDir()
.getAbsolutePath(), options.getPlanFromFileBaseDir().isDirectory());
assertTrue("planFromFile should be a file: " + options.getPlanFile().getAbsolutePath(),
!options.getPlanFile().isDirectory());
assertTrue("planFromFileBaseDir should be a directory: " + options.getPlanFileBaseDir()
.getAbsolutePath(), options.getPlanFileBaseDir().isDirectory());
});
}

Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/net/adamcin/oakpal/core/opear/AdhocOpear.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Optional;
import java.util.function.Function;

import static net.adamcin.oakpal.api.Fun.compose;
import static net.adamcin.oakpal.api.Fun.result1;
Expand Down Expand Up @@ -58,12 +59,12 @@ public ClassLoader getPlanClassLoader(final @NotNull ClassLoader parent) {
}

public static Result<AdhocOpear> fromPlanFile(final @NotNull File planFile, final @Nullable File baseDir) {
final Result<URL> planFileUrlResult = compose(File::toURI, result1(URI::toURL)).apply(planFile);
final Function<File, Result<URL>> fnFileUrl = compose(File::toURI, result1(URI::toURL));
final Result<URL> planFileUrlResult = fnFileUrl.apply(planFile);
return planFileUrlResult
.flatMap(planFileUrl -> compose(File::toURI, result1(URI::toURL))
.apply(Optional.ofNullable(baseDir).orElse(planFile.getParentFile()))
.flatMap(baseUrl -> OakpalPlan
.fromJson(planFileUrl).map(plan ->
.flatMap(planFileUrl -> fnFileUrl.apply(Optional.ofNullable(baseDir).orElse(planFile.getParentFile()))
.flatMap(baseUrl ->
OakpalPlan.fromJson(planFileUrl).map(plan ->
new AdhocOpear(planFileUrl, baseUrl))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2020 Mark Adamcin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.adamcin.oakpal.core.opear;

import net.adamcin.oakpal.api.Fun;
import net.adamcin.oakpal.api.Result;
import org.junit.Test;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

import static org.junit.Assert.*;

public class AdhocOpearTest {

@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);

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(""));
}

@Test
public void testFromPlanFileWithBaseDir() {
final File targetDir = new File("target");
final File planFile = new File("src/test/resources/OpearFileTest/folders_on_classpath/plan.json");
Result<AdhocOpear> opearResult = AdhocOpear.fromPlanFile(planFile, targetDir);

assertFalse("opear should not be a failure", opearResult.isFailure());
AdhocOpear opear = opearResult.getOrDefault(null);
assertNotNull("opear is not null", opear);

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",
targetDir.getAbsolutePath(),
Fun.tryOrOptional0(() -> new File(firstUrl.toURI()).getAbsolutePath()).get().orElse(""));
}
}

0 comments on commit dc8d7aa

Please sign in to comment.