Skip to content

Commit

Permalink
Fix code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo42 committed Sep 11, 2024
1 parent 1f75348 commit 37839c2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -247,12 +247,12 @@ String getDiagText() {
filesToScan.forEach(f -> sb.append(" ").append(f.getPath()).append("\n"));
}

List<String> classpathElements = getClasspathElements();
if (classpathElements.isEmpty()) {
List<String> 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();
Expand All @@ -262,22 +262,22 @@ private void setCustomResourceClassNames(List<SourceParameter> 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);
}

private void setFilesToScan(List<SourceParameter> 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<String> getClasspathElements() {
List<String> 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);
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>true</code>, 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 37839c2

Please sign in to comment.