-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decision to use io.github.classgraph #751
and stop fighting all the weird jar and classpath loading issues, also should help us with java modules and future versions etc, over the next few commits will remove the current resource abstraction and clean up the file-utils
- Loading branch information
Showing
7 changed files
with
386 additions
and
0 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
84 changes: 84 additions & 0 deletions
84
karate-core/src/main/java/com/intuit/karate/resource/FileResource.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,84 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2020 Intuit Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.intuit.karate.resource; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* | ||
* @author pthomas3 | ||
*/ | ||
public class FileResource implements Resource { | ||
|
||
private final File file; | ||
private final boolean classPath; | ||
private final String relativePath; | ||
|
||
public FileResource(File file) { | ||
this(file, false, file.getPath().replace('\\', '/')); | ||
} | ||
|
||
public FileResource(File file, boolean classPath, String relativePath) { | ||
this.file = file; | ||
this.classPath = classPath; | ||
this.relativePath = relativePath; | ||
} | ||
|
||
@Override | ||
public boolean isFile() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public File getFile() { | ||
return file; | ||
} | ||
|
||
@Override | ||
public boolean isClassPath() { | ||
return classPath; | ||
} | ||
|
||
@Override | ||
public String getRelativePath() { | ||
return relativePath; | ||
} | ||
|
||
@Override | ||
public InputStream getStream() { | ||
try { | ||
return new FileInputStream(file); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getPrefixedPath(); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
karate-core/src/main/java/com/intuit/karate/resource/JarResource.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,74 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2020 Intuit Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.intuit.karate.resource; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* | ||
* @author pthomas3 | ||
*/ | ||
public class JarResource implements Resource { | ||
|
||
private final byte[] bytes; | ||
private final String relativePath; | ||
|
||
public JarResource(byte[] bytes, String relativePath) { | ||
this.bytes = bytes; | ||
this.relativePath = relativePath; | ||
} | ||
|
||
@Override | ||
public boolean isFile() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isClassPath() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public File getFile() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getRelativePath() { | ||
return relativePath; | ||
} | ||
|
||
@Override | ||
public InputStream getStream() { | ||
return new ByteArrayInputStream(bytes); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getPrefixedPath(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
karate-core/src/main/java/com/intuit/karate/resource/Resource.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,49 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2020 Intuit Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.intuit.karate.resource; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* | ||
* @author pthomas3 | ||
*/ | ||
public interface Resource { | ||
|
||
boolean isFile(); | ||
|
||
boolean isClassPath(); | ||
|
||
File getFile(); | ||
|
||
String getRelativePath(); | ||
|
||
default String getPrefixedPath() { | ||
return isClassPath() ? "classpath:" + getRelativePath() : getRelativePath(); | ||
} | ||
|
||
InputStream getStream(); | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
karate-core/src/main/java/com/intuit/karate/resource/ResourceUtils.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,95 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2020 Intuit Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.intuit.karate.resource; | ||
|
||
import io.github.classgraph.ClassGraph; | ||
import io.github.classgraph.ResourceList; | ||
import io.github.classgraph.ScanResult; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* | ||
* @author pthomas3 | ||
*/ | ||
public class ResourceUtils { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ResourceUtils.class); | ||
|
||
private ResourceUtils() { | ||
// only static methods | ||
} | ||
|
||
public static List<Resource> findResourcesByExtension(String extension, String... paths) { | ||
List<Resource> list = new ArrayList(); | ||
try (ScanResult scanResult = new ClassGraph().acceptPaths(paths).scan()) { | ||
ResourceList rl = scanResult.getResourcesWithExtension(extension); | ||
rl.forEachByteArrayIgnoringIOException((res, bytes) -> { | ||
URI uri = res.getURI(); | ||
if ("file".equals(uri.getScheme())) { | ||
File file = Paths.get(uri).toFile(); | ||
list.add(new FileResource(file, true, res.getPath())); | ||
} else { | ||
list.add(new JarResource(bytes, res.getPath())); | ||
} | ||
}); | ||
} | ||
return list; | ||
} | ||
|
||
public static List<Resource> findFilesByExtension(String extension, File... files) { | ||
Set<File> results = new HashSet(); | ||
for (File base : files) { | ||
Path searchPath = base.toPath(); | ||
Stream<Path> stream; | ||
try { | ||
stream = Files.walk(searchPath); | ||
for (Iterator<Path> paths = stream.iterator(); paths.hasNext();) { | ||
Path path = paths.next(); | ||
String fileName = path.getFileName().toString(); | ||
if (fileName.endsWith("." + extension)) { | ||
results.add(path.toFile()); | ||
} | ||
} | ||
} catch (IOException e) { // NoSuchFileException | ||
logger.trace("unable to walk path: {} - {}", searchPath, e.getMessage()); | ||
} | ||
} | ||
return results.stream().map(f -> new FileResource(f)).collect(Collectors.toList()); | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
karate-core/src/test/java/com/intuit/karate/resource/ResourceUtilsTest.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,78 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2020 Intuit Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.intuit.karate.resource; | ||
|
||
import com.intuit.karate.FileUtils; | ||
import java.io.File; | ||
import java.util.List; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* | ||
* @author pthomas3 | ||
*/ | ||
class ResourceUtilsTest { | ||
|
||
static final Logger logger = LoggerFactory.getLogger(ResourceUtilsTest.class); | ||
|
||
@Test | ||
void testFindFilesByExtension() { | ||
List<Resource> list = ResourceUtils.findFilesByExtension("txt", new File("src/test/java/com/intuit/karate/resource")); | ||
assertEquals(1, list.size()); | ||
Resource resource = list.iterator().next(); | ||
assertTrue(resource.isFile()); | ||
assertFalse(resource.isClassPath()); | ||
assertEquals("src/test/java/com/intuit/karate/resource/test1.txt", resource.getRelativePath()); | ||
assertEquals("src/test/java/com/intuit/karate/resource/test1.txt", resource.getPrefixedPath()); | ||
assertEquals("foo", FileUtils.toString(resource.getStream())); | ||
} | ||
|
||
@Test | ||
void testFindJarFilesByExtension() { | ||
List<Resource> list = ResourceUtils.findResourcesByExtension("properties", "cucumber"); | ||
assertEquals(1, list.size()); | ||
Resource resource = list.iterator().next(); | ||
assertFalse(resource.isFile()); | ||
assertTrue(resource.isClassPath()); | ||
assertEquals("cucumber/version.properties", resource.getRelativePath()); | ||
assertEquals("classpath:cucumber/version.properties", resource.getPrefixedPath()); | ||
assertEquals("cucumber-jvm.version=1.2.5", FileUtils.toString(resource.getStream())); | ||
} | ||
|
||
@Test | ||
void testFindClassPathFilesByExtension() { | ||
List<Resource> list = ResourceUtils.findResourcesByExtension("txt", "com/intuit/karate/resource"); | ||
assertEquals(1, list.size()); | ||
Resource resource = list.iterator().next(); | ||
assertTrue(resource.isFile()); | ||
assertTrue(resource.isClassPath()); | ||
assertEquals("com/intuit/karate/resource/test1.txt", resource.getRelativePath()); | ||
assertEquals("classpath:com/intuit/karate/resource/test1.txt", resource.getPrefixedPath()); | ||
assertEquals("foo", FileUtils.toString(resource.getStream())); | ||
} | ||
|
||
} |
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 @@ | ||
foo |