-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue/9: improve unit test coverage for skippy/skippy-core (#12)
- Loading branch information
Showing
17 changed files
with
375 additions
and
71 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
71 changes: 71 additions & 0 deletions
71
skippy-core/src/main/java/io/skippy/core/AnalyzedFileList.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,71 @@ | ||
package io.skippy.core; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
/** | ||
* A list of {@link AnalyzedFile}s with a couple of utility methods that operates on this list. | ||
*/ | ||
class AnalyzedFileList { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(AnalyzedFileList.class); | ||
|
||
static final AnalyzedFileList UNAVAILABLE = new AnalyzedFileList(emptyList()); | ||
|
||
private final List<AnalyzedFile> analyzedFiles; | ||
|
||
/** | ||
* C'tor. | ||
* | ||
* @param analyzedFiles a list of {@link AnalyzedFile}s | ||
*/ | ||
private AnalyzedFileList(List<AnalyzedFile> analyzedFiles) { | ||
this.analyzedFiles = analyzedFiles; | ||
} | ||
|
||
static AnalyzedFileList parse(Path skippyAnalysisFile) { | ||
if (!skippyAnalysisFile.toFile().exists()) { | ||
return UNAVAILABLE; | ||
} | ||
try { | ||
var result = new ArrayList<AnalyzedFile>(); | ||
for (var line : Files.readAllLines(skippyAnalysisFile, Charset.forName("UTF8"))) { | ||
String[] split = line.split(":"); | ||
result.add(new AnalyzedFile(new FullyQualifiedClassName(split[0]), Path.of(split[1]), Path.of(split[2]), split[3], split[4])); | ||
} | ||
return new AnalyzedFileList(result); | ||
} catch (Exception e) { | ||
LOGGER.error("Parsing of file '%s' failed: '%s'".formatted(skippyAnalysisFile, e.getMessage()), e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
List<FullyQualifiedClassName> getClasses() { | ||
return analyzedFiles.stream() | ||
.map(s -> s.getFullyQualifiedClassName()) | ||
.toList(); | ||
} | ||
|
||
List<FullyQualifiedClassName> getClassesWithSourceChanges() { | ||
return analyzedFiles.stream() | ||
.filter(s -> s.sourceFileHasChanged()) | ||
.map(s -> s.getFullyQualifiedClassName()) | ||
.toList(); | ||
} | ||
|
||
List<FullyQualifiedClassName> getClassesWithBytecodeChanges() { | ||
return analyzedFiles.stream() | ||
.filter(s -> s.classFileHasChanged()) | ||
.map(s -> s.getFullyQualifiedClassName()) | ||
.toList(); | ||
} | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
skippy-core/src/test/java/io/skippy/core/AnalyzedFileListTest.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,24 @@ | ||
package io.skippy.core; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class AnalyzedFileListTest { | ||
|
||
@Test | ||
void testParse() throws URISyntaxException { | ||
var analyzedFilesTxt = Path.of(getClass().getResource("analyzedfiles/analyzedFiles.txt").toURI()); | ||
var analyzedFiles = AnalyzedFileList.parse(analyzedFilesTxt); | ||
|
||
assertEquals(asList( | ||
new FullyQualifiedClassName("com.example.Foo"), | ||
new FullyQualifiedClassName("com.example.FooTest") | ||
), analyzedFiles.getClasses()); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
skippy-core/src/test/java/io/skippy/core/AnalyzedFileTest.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,65 @@ | ||
package io.skippy.core; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class AnalyzedFileTest { | ||
|
||
@Test | ||
void testSourceAndClassFileHaveNotChanged() throws URISyntaxException { | ||
var foo = new AnalyzedFile( | ||
new FullyQualifiedClassName("com.example.Foo"), | ||
Path.of(getClass().getResource("analyzedfile/Foo.java").toURI()), | ||
Path.of(getClass().getResource("analyzedfile/Foo.class").toURI()), | ||
"cGN5C7g/BdD4rxxFBgZ7pw==", | ||
"54Uq2W8MWDOi6dCDnWoLVQ==" | ||
); | ||
|
||
assertEquals(false, foo.sourceFileHasChanged()); | ||
assertEquals(false, foo.classFileHasChanged()); | ||
} | ||
|
||
@Test | ||
void testSourceFileHasChanged() throws URISyntaxException { | ||
var fooNew = new AnalyzedFile( | ||
new FullyQualifiedClassName("com.example.Foo"), | ||
Path.of(getClass().getResource("analyzedfile/Foo.java").toURI()), | ||
Path.of(getClass().getResource("analyzedfile/Foo.class").toURI()), | ||
"NEW-SOURCE-FILE-HASH", | ||
"54Uq2W8MWDOi6dCDnWoLVQ==" | ||
); | ||
assertEquals(true, fooNew.sourceFileHasChanged()); | ||
assertEquals(false, fooNew.classFileHasChanged()); | ||
} | ||
|
||
@Test | ||
void testClassFileHasChanged() throws URISyntaxException { | ||
var fooNew = new AnalyzedFile( | ||
new FullyQualifiedClassName("com.example.Foo"), | ||
Path.of(getClass().getResource("analyzedfile/Foo.java").toURI()), | ||
Path.of(getClass().getResource("analyzedfile/Foo.class").toURI()), | ||
"cGN5C7g/BdD4rxxFBgZ7pw==", | ||
"NEW-CLASS-FILE-HASH" | ||
); | ||
assertEquals(false, fooNew.sourceFileHasChanged()); | ||
assertEquals(true, fooNew.classFileHasChanged()); | ||
} | ||
|
||
@Test | ||
void testSourceAndClassFileHaveChanged() throws URISyntaxException { | ||
var fooNew = new AnalyzedFile( | ||
new FullyQualifiedClassName("com.example.Foo"), | ||
Path.of(getClass().getResource("analyzedfile/Foo.java").toURI()), | ||
Path.of(getClass().getResource("analyzedfile/Foo.class").toURI()), | ||
"NEW-SOURCE-FILE-HASH", | ||
"NEW-CLASS-FILE-HASH" | ||
); | ||
assertEquals(true, fooNew.sourceFileHasChanged()); | ||
assertEquals(true, fooNew.classFileHasChanged()); | ||
} | ||
|
||
} |
Oops, something went wrong.