| 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 java.io.File; |
| 22 |
import java.io.IOException; |
| 23 |
import java.net.JarURLConnection; |
| 24 |
import java.net.URI; |
| 25 |
import java.nio.file.FileVisitResult; |
| 26 |
import java.nio.file.FileVisitor; |
| 27 |
import java.nio.file.Files; |
| 28 |
import java.nio.file.Path; |
| 29 |
import java.nio.file.StandardCopyOption; |
| 30 |
import java.nio.file.StandardOpenOption; |
| 31 |
import java.nio.file.attribute.BasicFileAttributes; |
| 32 |
|
| 33 |
import org.junit.Assert; |
| 34 |
import org.junit.Before; |
| 35 |
import org.junit.Rule; |
| 36 |
import org.junit.Test; |
| 37 |
import org.junit.rules.TemporaryFolder; |
| 38 |
|
| 39 |
/** |
| 40 |
* Test absolute and relative paths in resource-roots' path attribute (MODULES-218) |
| 41 |
* @author Martin Simka |
| 42 |
*/ |
| 43 |
public class ResourceRootPathsTest extends AbstractModuleTestCase { |
| 44 |
|
| 45 |
@Rule |
| 46 |
public TemporaryFolder tmpDir = new TemporaryFolder(); |
| 47 |
private File repoRoot; |
| 48 |
private File testModuleRoot; |
| 49 |
private File fileResourceRoot; |
| 50 |
|
| 51 |
private static final String MODULE_XML_TEMPLATE = |
| 52 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 53 |
"<module xmlns=\"urn:jboss:module:1.5\" name=\"test.test\">\n" + |
| 54 |
" <resources>\n" + |
| 55 |
" <resource-root name=\"relativejar\" path=\"relative.jar\" />\n" + |
| 56 |
" <resource-root name=\"absolutejar\" path=\"@absolutejar@\" />\n" + |
| 57 |
" <resource-root name=\"relativedir\" path=\"relativedir\" />\n" + |
| 58 |
" <resource-root name=\"absolutedir\" path=\"@absolutedir@\" />\n" + |
| 59 |
" </resources>\n" + |
| 60 |
"</module>"; |
| 61 |
|
| 62 |
private ModuleLoader moduleLoader; |
| 63 |
|
| 64 |
@Before |
| 65 |
public void setupModuleLoader() throws Exception { |
| 66 |
repoRoot = tmpDir.newFolder("repo").getCanonicalFile(); |
| 67 |
testModuleRoot = new File(repoRoot, "test/test/main/"); |
| 68 |
|
| 69 |
// Build a jar in module |
| 70 |
copyResource("org/jboss/modules/test/TestClass.class", "test/fileresourceloader", "org/jboss/modules/test"); |
| 71 |
fileResourceRoot = getResource("test/fileresourceloader"); |
| 72 |
final File jarInModule = new File(testModuleRoot, "relative.jar"); |
| 73 |
jarInModule.getParentFile().mkdirs(); |
| 74 |
JarResourceLoaderTest.buildJar(fileResourceRoot, jarInModule); |
| 75 |
|
| 76 |
// Build a jar outside of module |
| 77 |
final File jarOutsideModule = new File(repoRoot, "relative.jar"); |
| 78 |
JarResourceLoaderTest.buildJar(fileResourceRoot, jarOutsideModule); |
| 79 |
|
| 80 |
// copy resource dir to module |
| 81 |
copyDir(fileResourceRoot, new File(testModuleRoot, "relativedir")); |
| 82 |
|
| 83 |
//create module.xml |
| 84 |
final File moduleXml = new File(testModuleRoot, "module.xml"); |
| 85 |
String moduleXmlContent = MODULE_XML_TEMPLATE.replaceAll("@absolutejar@", jarOutsideModule.getCanonicalPath().replace(File.separatorChar, '/')); |
| 86 |
moduleXmlContent = moduleXmlContent.replaceAll("@absolutedir@", fileResourceRoot.getCanonicalPath().replace(File.separatorChar, '/')); |
| 87 |
Files.write(moduleXml.toPath(), moduleXmlContent.getBytes(), StandardOpenOption.CREATE_NEW); |
| 88 |
|
| 89 |
moduleLoader = new LocalModuleLoader(new File[] {repoRoot}); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void testPaths() throws Exception { |
| 94 |
ConcreteModuleSpec moduleSpec = (ConcreteModuleSpec) moduleLoader.findModule("test.test"); |
| 95 |
ResourceLoaderSpec[] resourceLoaders = moduleSpec.getResourceLoaders(); |
| 96 |
int checkCount = 0; |
| 97 |
for (ResourceLoaderSpec r : resourceLoaders) { |
| 98 |
ResourceLoader resourceLoader = r.getResourceLoader(); |
| 99 |
if (resourceLoader instanceof JarFileResourceLoader) { |
| 100 |
final File jar = getFileFromJarUri(resourceLoader.getLocation()); |
| 101 |
|
| 102 |
// validate jar with relative path |
| 103 |
if (testModuleRoot.equals(jar.getParentFile().getCanonicalFile())) { |
| 104 |
checkCount++; |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
// validate jar with absolute path |
| 109 |
if (repoRoot.equals(jar.getParentFile().getCanonicalFile())) { |
| 110 |
checkCount++; |
| 111 |
continue; |
| 112 |
} |
| 113 |
} else if (resourceLoader instanceof PathResourceLoader) { |
| 114 |
final File dir = new File(resourceLoader.getLocation()); |
| 115 |
|
| 116 |
// validate dir with relative path |
| 117 |
if (testModuleRoot.equals(dir.getParentFile())) { |
| 118 |
checkCount++; |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
// validate dir with absolute path |
| 123 |
if (fileResourceRoot.getParentFile().equals(dir.getParentFile())) { |
| 124 |
checkCount++; |
| 125 |
continue; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
Assert.assertEquals("Test should have checked 4 ResourceLoaders", 4, checkCount); |
| 130 |
} |
| 131 |
|
| 132 |
private File getFileFromJarUri(URI uri) throws Exception { |
| 133 |
JarURLConnection connection = (JarURLConnection) uri.toURL().openConnection(); |
| 134 |
File file = new File(connection.getJarFileURL().toURI()); |
| 135 |
return file; |
| 136 |
} |
| 137 |
|
| 138 |
static void copyDir(File sourceDir, File targetDir) throws Exception { |
| 139 |
|
| 140 |
abstract class CopyFileVisitor implements FileVisitor<Path> { |
| 141 |
boolean isFirst = true; |
| 142 |
Path ptr; |
| 143 |
} |
| 144 |
|
| 145 |
CopyFileVisitor copyVisitor = new CopyFileVisitor() { |
| 146 |
|
| 147 |
@Override |
| 148 |
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 149 |
if (!isFirst) { |
| 150 |
Path target = ptr.resolve(dir.getName(dir.getNameCount() - 1)); |
| 151 |
ptr = target; |
| 152 |
} |
| 153 |
Files.copy(dir, ptr, StandardCopyOption.COPY_ATTRIBUTES); |
| 154 |
isFirst = false; |
| 155 |
return FileVisitResult.CONTINUE; |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 160 |
Path target = ptr.resolve(file.getFileName()); |
| 161 |
Files.copy(file, target, StandardCopyOption.COPY_ATTRIBUTES); |
| 162 |
return FileVisitResult.CONTINUE; |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { |
| 167 |
throw exc; |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 172 |
Path target = ptr.getParent(); |
| 173 |
ptr = target; |
| 174 |
return FileVisitResult.CONTINUE; |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
copyVisitor.ptr = targetDir.toPath(); |
| 179 |
Files.walkFileTree(sourceDir.toPath(), copyVisitor); |
| 180 |
} |
| 181 |
} |