forked from GraphWalker/graphwalker-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While also: 1. Upgrade reflection library due to ronmamo/reflections#273 2. Memoized list of classloaders as it's getting called on each processed file. 3. Added tests for SimpleCache 4. Added tests CodeGenerator fix 5. Minor adjustments
- Loading branch information
Showing
9 changed files
with
126 additions
and
20 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
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
70 changes: 70 additions & 0 deletions
70
graphwalker-java/src/test/java/org/graphwalker/java/source/CodeGeneratorTest.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,70 @@ | ||
package org.graphwalker.java.source; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.*; | ||
import java.nio.file.attribute.FileTime; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class CodeGeneratorTest { | ||
|
||
@Test | ||
public void testOnlyRegenerateWhenSourceIsModified() throws IOException, URISyntaxException { | ||
String tmpdir = System.getProperty("java.io.tmpdir"); | ||
Path inputDir = Files.createTempDirectory(Paths.get(tmpdir), "input").toAbsolutePath(); | ||
Path outputDir = Files.createTempDirectory(Paths.get(tmpdir), "output").toAbsolutePath(); | ||
|
||
// sample file | ||
URL resource = getClass().getClassLoader().getResource("org/graphwalker/java/path with space/MyModel.graphml"); | ||
File testFile = new File(resource.toURI()); | ||
|
||
Files.copy(testFile.toPath(), inputDir.resolve(testFile.getName()), StandardCopyOption.REPLACE_EXISTING); | ||
FileTime sourceLastModified = Files.getLastModifiedTime(inputDir.resolve(testFile.getName())); | ||
|
||
File[] inputFiles = Arrays.stream(inputDir.toFile().listFiles()) | ||
.toArray(File[]::new); | ||
|
||
assertEquals(1, inputFiles.length); | ||
|
||
// run codegenerator for each input files in inputDir | ||
CodeGenerator.generate(inputDir, outputDir); | ||
|
||
// source modified time should not be altered | ||
FileTime sourceLastModifiedDelta = Files.getLastModifiedTime(inputDir.resolve(testFile.getName())); | ||
assertEquals(sourceLastModified.toMillis(), sourceLastModifiedDelta.toMillis()); | ||
|
||
// glob the output files, find the one with the same name as the input file | ||
File[] outputFiles = Arrays.stream(outputDir.toFile().listFiles()) | ||
.filter(f -> f.getName().equals(testFile.getName().replace(".graphml", ".java"))) | ||
.toArray(File[]::new); | ||
|
||
assertEquals(1, outputFiles.length); | ||
assertEquals("MyModel.java", outputFiles[0].getName()); | ||
|
||
// note lst modified time is in milliseconds | ||
long lastModified = outputFiles[0].lastModified(); | ||
|
||
// sleep 5 seconds to make sure the last modified time is different | ||
try { | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// regenerate the file | ||
CodeGenerator.generate(inputDir, outputDir); | ||
|
||
// check that the file was not regenerated | ||
outputFiles = Arrays.stream(outputDir.toFile().listFiles()) | ||
.filter(f -> f.getName().equals(testFile.getName().replace(".graphml", ".java"))) | ||
.toArray(File[]::new); | ||
assertEquals(1, outputFiles.length); | ||
assertEquals(lastModified, outputFiles[0].lastModified()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
graphwalker-java/src/test/java/org/graphwalker/java/source/cache/SimpleCacheTest.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,30 @@ | ||
package org.graphwalker.java.source.cache; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class SimpleCacheTest { | ||
|
||
@Test | ||
public void get() throws IOException { | ||
SimpleCache cache = new SimpleCache(Paths.get("./")); | ||
// create temporary file | ||
File foo = File.createTempFile("prefix", "suffix"); | ||
foo.deleteOnExit(); | ||
Path key = foo.toPath(); | ||
long lastModified = foo.lastModified(); | ||
cache.add(key, new CacheEntry(lastModified, true)); | ||
CacheEntry cached = cache.get(key); | ||
assertEquals(lastModified, cached.getLastModifiedTime().toMillis()); | ||
assertEquals(true, cached.isGenerated()); | ||
|
||
assertTrue(cache.contains(key)); | ||
} | ||
} |
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