| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* Copyright 2016 Red Hat, Inc., and individual contributors |
| 4 |
* as indicated by the @author tags. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.jboss.modules; |
| 20 |
|
| 21 |
import org.jboss.modules.filter.PathFilter; |
| 22 |
import org.junit.Assert; |
| 23 |
import org.junit.runner.RunWith; |
| 24 |
import org.junit.runners.Parameterized; |
| 25 |
|
| 26 |
import java.io.File; |
| 27 |
import java.io.IOException; |
| 28 |
import java.nio.file.FileSystem; |
| 29 |
import java.nio.file.FileSystems; |
| 30 |
import java.nio.file.Files; |
| 31 |
import java.nio.file.Path; |
| 32 |
import java.util.Arrays; |
| 33 |
import java.util.Collection; |
| 34 |
|
| 35 |
/** |
| 36 |
* Test the functionality of the PathResourceLoader |
| 37 |
* |
| 38 |
* @author Sergei Egorov |
| 39 |
*/ |
| 40 |
@RunWith(Parameterized.class) |
| 41 |
public class PathResourceLoaderTest extends AbstractResourceLoaderTestCase { |
| 42 |
|
| 43 |
@Parameterized.Parameters(name = "{0}") |
| 44 |
public static Collection<Object[]> data() { |
| 45 |
return Arrays.asList(new Object[][]{ |
| 46 |
{TestMode.FOLDER}, |
| 47 |
{TestMode.JAR} |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
@Parameterized.Parameter |
| 52 |
public TestMode testMode; |
| 53 |
|
| 54 |
private Path resourceRoot; |
| 55 |
|
| 56 |
@Override |
| 57 |
protected ResourceLoader createLoader(PathFilter exportFilter) throws Exception { |
| 58 |
resourceRoot = testMode.getResourceRoot(this); |
| 59 |
|
| 60 |
return ResourceLoaders.createPathResourceLoader(resourceRoot); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected void assertResource(Resource resource, String fileName) throws IOException { |
| 65 |
final Path resourceFile = resourceRoot.resolve(fileName); |
| 66 |
|
| 67 |
Assert.assertEquals(Files.size(resourceFile), resource.getSize()); |
| 68 |
Assert.assertEquals(resourceFile.toUri().toURL(), resource.getURL()); |
| 69 |
} |
| 70 |
|
| 71 |
private enum TestMode { |
| 72 |
FOLDER, |
| 73 |
JAR { |
| 74 |
@Override |
| 75 |
Path getResourceRoot(PathResourceLoaderTest test) throws Exception { |
| 76 |
// Build a jar to match the fileresource loader |
| 77 |
final File outputFile = new File(test.getResource("test"), "jarresourceloader/test.jar"); |
| 78 |
outputFile.getParentFile().mkdirs(); |
| 79 |
JarResourceLoaderTest.buildJar(super.getResourceRoot(test).toFile(), outputFile); |
| 80 |
|
| 81 |
FileSystem fileSystem = FileSystems.newFileSystem(outputFile.toPath(), (ClassLoader) null); |
| 82 |
return fileSystem.getRootDirectories().iterator().next(); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
Path getResourceRoot(PathResourceLoaderTest test) throws Exception { |
| 87 |
Path resourceRoot = test.getResource("test/fileresourceloader").toPath().toAbsolutePath(); |
| 88 |
// Copy the classfile over |
| 89 |
test.copyResource("org/jboss/modules/test/TestClass.class", "test/fileresourceloader", "org/jboss/modules/test"); |
| 90 |
|
| 91 |
return resourceRoot; |
| 92 |
} |
| 93 |
} |
| 94 |
} |