-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
165 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/edu/hm/hafner/util/PackageDetectorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package edu.hm.hafner.util; | ||
|
||
import edu.hm.hafner.util.PackageDetector.FileSystemFacade; | ||
|
||
/** | ||
* Factory to create package detectors. | ||
* | ||
* @author Ullrich Hafner | ||
*/ | ||
public final class PackageDetectorFactory { | ||
/** | ||
* Creates a new package detector runner that uses the detectors for Java, Kotlin, and C#. | ||
* | ||
* @return the package detector runner | ||
*/ | ||
public static PackageDetectorRunner createPackageDetectors() { | ||
return createPackageDetectors(new FileSystemFacade()); | ||
} | ||
|
||
@VisibleForTesting | ||
static PackageDetectorRunner createPackageDetectors(final FileSystemFacade facade) { | ||
return new PackageDetectorRunner( | ||
new JavaPackageDetector(facade), | ||
new KotlinPackageDetector(facade), | ||
new CSharpNamespaceDetector(facade)); | ||
} | ||
|
||
private PackageDetectorFactory() { | ||
// prevents instantiation | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/edu/hm/hafner/util/PackageDetectorRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package edu.hm.hafner.util; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Detects package or namespace names of files in the file system. | ||
* | ||
* @author Ullrich Hafner | ||
*/ | ||
public class PackageDetectorRunner { | ||
private final List<PackageDetector> detectors; | ||
|
||
PackageDetectorRunner(final PackageDetector... detectors) { | ||
this.detectors = Arrays.asList(detectors); | ||
} | ||
|
||
/** | ||
* Detects the package name of the specified file based on several detector strategies. | ||
* | ||
* @param fileName | ||
* the filename of the file to scan | ||
* @param charset | ||
* the charset to use when reading the source files | ||
* | ||
* @return the detected package name or {@link Optional#empty()} if no package name could be detected | ||
*/ | ||
public Optional<String> detectPackageName(final String fileName, final Charset charset) { | ||
return detectors.stream() | ||
.filter(detector -> detector.accepts(fileName)) | ||
.map(detector -> detector.detectPackageName(fileName, charset)) | ||
.flatMap(Optional::stream) | ||
.findFirst(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
src/test/java/edu/hm/hafner/util/PackageDetectorRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package edu.hm.hafner.util; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import edu.hm.hafner.util.PackageDetector.FileSystemFacade; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Tests the class {@link PackageDetectorRunner}. | ||
* | ||
* @author Ullrich Hafner | ||
*/ | ||
class PackageDetectorRunnerTest extends ResourceTest { | ||
@ParameterizedTest(name = "{index} => file={0}, expected package={1}") | ||
@CsvSource({ | ||
"MavenJavaTest.txt.java, hudson.plugins.tasks.util", | ||
"ActionBinding.cs, Avaloq.SmartClient.Utilities", | ||
"KotlinTest.txt.kt, edu.hm.kersting", | ||
}) | ||
void shouldExtractPackageNames(final String fileName, final String expectedPackage) throws IOException { | ||
assertThat(detect(fileName)).contains(expectedPackage); | ||
} | ||
|
||
@ParameterizedTest(name = "{index} => file={0}, no package found") | ||
@ValueSource(strings = {"MavenJavaTest.txt", "relative.txt", "KotlinTest.txt"}) | ||
void shouldNotAcceptFile(final String fileName) throws IOException { | ||
assertThat(detect(fileName)).isEmpty(); | ||
} | ||
|
||
private Optional<String> detect(final String fileName) throws IOException { | ||
try (InputStream stream = asInputStream(fileName)) { | ||
var fileSystem = mock(FileSystemFacade.class); | ||
when(fileSystem.openFile(fileName)).thenReturn(stream); | ||
|
||
var detectors = PackageDetectorFactory.createPackageDetectors(fileSystem); | ||
|
||
return detectors.detectPackageName(fileName, StandardCharsets.UTF_8); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldHandleException() throws IOException { | ||
var fileSystem = mock(FileSystemFacade.class); | ||
when(fileSystem.openFile(anyString())).thenThrow(new IOException("Simulated")); | ||
|
||
assertThat(PackageDetectorFactory.createPackageDetectors(fileSystem) | ||
.detectPackageName("file.java", StandardCharsets.UTF_8)).isEmpty(); | ||
} | ||
} |
Oops, something went wrong.