diff --git a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java index ddb14149b91..6f4861161a7 100644 --- a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java +++ b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java @@ -204,13 +204,13 @@ public void run() { try { sanitizedOutputDirectory = outputDirectory.getCanonicalFile(); } catch (IOException e) { - throw new RuntimeException("Could not get canonical file for " + outputDirectory, e); + throw new CRDGeneratorCliException("Could not get canonical file for " + outputDirectory, e); } try { Files.createDirectories(sanitizedOutputDirectory.toPath()); } catch (IOException e) { - throw new RuntimeException( + throw new CRDGeneratorCliException( "Could not create output directory at " + sanitizedOutputDirectory, e); } @@ -247,12 +247,12 @@ String getDiagText() { filesToScan.forEach(f -> sb.append(" ").append(f.getPath()).append("\n")); } - List classpathElements = getClasspathElements(); - if (classpathElements.isEmpty()) { + List allClasspathElements = getClasspathElements(); + if (allClasspathElements.isEmpty()) { sb.append("Classpath: []\n"); } else { sb.append("\nClasspath:\n"); - classpathElements.forEach(cpe -> sb.append(" ").append(cpe).append("\n")); + allClasspathElements.forEach(cpe -> sb.append(" ").append(cpe).append("\n")); } sb.append("\n"); return sb.toString(); @@ -262,7 +262,7 @@ private void setCustomResourceClassNames(List source) { source.stream() .filter(s -> s instanceof SourceParameter.CustomResourceClass) .map(SourceParameter.CustomResourceClass.class::cast) - .map(SourceParameter.CustomResourceClass::getCustomResourceClass) + .map(SourceParameter.CustomResourceClass::getValue) .forEach(customResourceClassNames::add); } @@ -270,14 +270,14 @@ private void setFilesToScan(List source) { source.stream() .filter(s -> s instanceof SourceParameter.FileToScan) .map(SourceParameter.FileToScan.class::cast) - .map(SourceParameter.FileToScan::getFileToScan) + .map(SourceParameter.FileToScan::getValue) .forEach(filesToScan::add); } private List getClasspathElements() { List allClasspathElements = new LinkedList<>(classpathElements); - // Add files to classpath elements to improve UX of the CLI: - // A scan target must be always in the classpath. + // Add files to classpath elements for better usability: + // If a scan target contains a custom resource class, it must be also in the classpath. filesToScan.stream() .map(File::getPath) .forEach(allClasspathElements::add); @@ -318,10 +318,20 @@ static CommandLine createCommandLine(CRDGeneratorCLI crdGeneratorCLI) { * Exception to indicate that no custom resource classes * have been retained after scanning and filtering. */ - static class CustomResourceClassNotFoundException extends RuntimeException { + static class CustomResourceClassNotFoundException extends CRDGeneratorCliException { CustomResourceClassNotFoundException() { super("No Custom Resource class retained after filtering"); } } + private static class CRDGeneratorCliException extends RuntimeException { + CRDGeneratorCliException(String message) { + super(message); + } + + CRDGeneratorCliException(String message, Throwable cause) { + super(message, cause); + } + } + } diff --git a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameter.java b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameter.java index ba4fb6905e0..fa61d171e4c 100644 --- a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameter.java +++ b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameter.java @@ -24,26 +24,26 @@ interface SourceParameter { class FileToScan implements SourceParameter { - private final File fileToScan; + private final File value; - FileToScan(File fileToScan) { - this.fileToScan = fileToScan; + FileToScan(File value) { + this.value = value; } - public File getFileToScan() { - return fileToScan; + public File getValue() { + return value; } } class CustomResourceClass implements SourceParameter { - private final String customResourceClass; + private final String value; - CustomResourceClass(String customResourceClass) { - this.customResourceClass = customResourceClass; + CustomResourceClass(String value) { + this.value = value; } - public String getCustomResourceClass() { - return customResourceClass; + public String getValue() { + return value; } } } diff --git a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameterTypeConverter.java b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameterTypeConverter.java index ff12a631f6e..e612627db9f 100644 --- a/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameterTypeConverter.java +++ b/crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/SourceParameterTypeConverter.java @@ -28,21 +28,23 @@ class SourceParameterTypeConverter "\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*"); private static final Pattern FQCN_PATTERN = Pattern.compile( - "(" + VALID_JAVA_IDENTIFIER + "\\.)*" + VALID_JAVA_IDENTIFIER); - + "(" + VALID_JAVA_IDENTIFIER + "\\.)*+" + VALID_JAVA_IDENTIFIER); + + /** + * Checks, if the value is a valid full qualifying class name (FQCN). + * + * @param value the input value + * @return true, if valid. + */ static boolean isFQCN(String value) { return FQCN_PATTERN.matcher(value).matches(); } @Override - public SourceParameter convert(String value) { + public SourceParameter convert(String value) throws IOException { File f = new File(value); if (f.exists()) { - try { - return new SourceParameter.FileToScan(f.getCanonicalFile()); - } catch (IOException e) { - throw new RuntimeException(e); - } + return new SourceParameter.FileToScan(f.getCanonicalFile()); } if (isFQCN(value)) { diff --git a/crd-generator/cli/src/test/java/io/fabric8/crd/generator/cli/SourceArgumentConverterTest.java b/crd-generator/cli/src/test/java/io/fabric8/crd/generator/cli/SourceArgumentConverterTest.java index ee95e94aadb..ead4620d845 100644 --- a/crd-generator/cli/src/test/java/io/fabric8/crd/generator/cli/SourceArgumentConverterTest.java +++ b/crd-generator/cli/src/test/java/io/fabric8/crd/generator/cli/SourceArgumentConverterTest.java @@ -32,12 +32,13 @@ class SourceArgumentConverterTest { @Test - void givenValidCustomResourceClassArgument_thenConvertToCustomResourceClassArgument() { + void givenValidCustomResourceClassArgument_thenConvertToCustomResourceClassArgument() + throws IOException { SourceParameterTypeConverter converter = new SourceParameterTypeConverter(); SourceParameter sourceParameter = converter.convert("com.example.MyValidClassName"); assertInstanceOf(SourceParameter.CustomResourceClass.class, sourceParameter); SourceParameter.CustomResourceClass customResourceClassArg = (SourceParameter.CustomResourceClass) sourceParameter; - assertEquals("com.example.MyValidClassName", customResourceClassArg.getCustomResourceClass()); + assertEquals("com.example.MyValidClassName", customResourceClassArg.getValue()); } @Test @@ -49,7 +50,7 @@ void givenExistingFile_thenConvertToFileToScanArgument(@TempDir File tempDir) th SourceParameter sourceParameter = converter.convert(exampleFile.getAbsolutePath()); assertInstanceOf(SourceParameter.FileToScan.class, sourceParameter); SourceParameter.FileToScan fileToScanArg = (SourceParameter.FileToScan) sourceParameter; - assertEquals(exampleFile, fileToScanArg.getFileToScan()); + assertEquals(exampleFile, fileToScanArg.getValue()); } @Test