Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export file system facade to be used in tests #1285

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/quality-monitor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ jobs:
"tools": [
{
"id": "revapi",
"sourcePath": "src/main/java"
"sourcePath": "src/main/java",
"pattern": "**/target/revapi-result.json"
}
]
},
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,16 @@
<attachments>
<vetted>ok</vetted>
</attachments>
<differences>
<item>
<ignore>true</ignore>
<regex>true</regex>
<code>java.method.visibilityIncreased</code>
<classSimpleName>PackageDetectorFactory</classSimpleName>
<methodName>createPackageDetectors</methodName>
<justification>Not used yet</justification>
</item>
</differences>
</revapi.differences>
<revapi.versions>
<enabled>true</enabled>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/hm/hafner/util/CSharpNamespaceDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.regex.Pattern;

import edu.hm.hafner.util.PackageDetectorFactory.FileSystemFacade;

/**
* Detects the namespace of a C# workspace file.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/hm/hafner/util/JavaPackageDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.regex.Pattern;

import edu.hm.hafner.util.PackageDetectorFactory.FileSystemFacade;

/**
* Detects the package name of a Java file.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/hm/hafner/util/KotlinPackageDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.regex.Pattern;

import edu.hm.hafner.util.PackageDetectorFactory.FileSystemFacade;

/**
* Detects the package name of a Kotlin file.
*
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/edu/hm/hafner/util/PackageDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.apache.commons.io.input.BOMInputStream;

import com.google.errorprone.annotations.MustBeClosed;
import edu.hm.hafner.util.PackageDetectorFactory.FileSystemFacade;

/**
* Base class for package detectors.
Expand Down Expand Up @@ -96,15 +94,4 @@ private Optional<String> detectPackageName(final Stream<String> lines) {
* @return {@code true} if the classifier accepts the specified file for processing.
*/
abstract boolean accepts(String fileName);

/**
* Facade for file system operations. May be replaced by stubs in test cases.
*/
@VisibleForTesting
static class FileSystemFacade {
@MustBeClosed
InputStream openFile(final String fileName) throws IOException, InvalidPathException {
return Files.newInputStream(Paths.get(fileName));
}
}
}
41 changes: 39 additions & 2 deletions src/main/java/edu/hm/hafner/util/PackageDetectorFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package edu.hm.hafner.util;

import edu.hm.hafner.util.PackageDetector.FileSystemFacade;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;

import com.google.errorprone.annotations.MustBeClosed;

/**
* Factory to create package detectors.
Expand All @@ -17,8 +23,16 @@ public static PackageDetectorRunner createPackageDetectors() {
return createPackageDetectors(new FileSystemFacade());
}

/**
* Creates a new package detector runner that uses the detectors for Java, Kotlin, and C#.
*
* @param facade
* the file system facade to use
*
* @return the package detector runner
*/
@VisibleForTesting
static PackageDetectorRunner createPackageDetectors(final FileSystemFacade facade) {
public static PackageDetectorRunner createPackageDetectors(final FileSystemFacade facade) {
return new PackageDetectorRunner(
new JavaPackageDetector(facade),
new KotlinPackageDetector(facade),
Expand All @@ -28,4 +42,27 @@ static PackageDetectorRunner createPackageDetectors(final FileSystemFacade facad
private PackageDetectorFactory() {
// prevents instantiation
}

/**
* Facade for file system operations. May be replaced by stubs in test cases.
*/
@VisibleForTesting
public static class FileSystemFacade {
/**
* Opens the specified file.
*
* @param fileName
* the name of the file to open
*
* @return the input stream to read the file
* @throws IOException
* if the file could not be opened
* @throws InvalidPathException
* the file name is invalid
*/
@MustBeClosed
public InputStream openFile(final String fileName) throws IOException, InvalidPathException {
return Files.newInputStream(Paths.get(fileName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import edu.hm.hafner.util.PackageDetector.FileSystemFacade;
import edu.hm.hafner.util.PackageDetectorFactory.FileSystemFacade;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
Expand Down
Loading