| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* Copyright 2014 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 |
|
| 24 |
import java.io.BufferedInputStream; |
| 25 |
import java.io.File; |
| 26 |
import java.io.FileInputStream; |
| 27 |
import java.io.FileOutputStream; |
| 28 |
import java.io.IOException; |
| 29 |
import java.util.jar.JarEntry; |
| 30 |
import java.util.jar.JarFile; |
| 31 |
import java.util.jar.JarOutputStream; |
| 32 |
|
| 33 |
/** |
| 34 |
* Test the functionality of the JarResourceLoader. |
| 35 |
* |
| 36 |
* @author John Bailey |
| 37 |
*/ |
| 38 |
public class JarResourceLoaderTest extends AbstractResourceLoaderTestCase { |
| 39 |
|
| 40 |
private JarFile jarFile; |
| 41 |
|
| 42 |
protected ResourceLoader createLoader(final PathFilter exportFilter) throws Exception { |
| 43 |
File fileResourceRoot = getResource("test/fileresourceloader"); |
| 44 |
// Copy the classfile over |
| 45 |
copyResource("org/jboss/modules/test/TestClass.class", "test/fileresourceloader", "org/jboss/modules/test"); |
| 46 |
|
| 47 |
// Build a jar to match the fileresource loader |
| 48 |
final File outputFile = new File(getResource("test"), "jarresourceloader/test.jar"); |
| 49 |
outputFile.getParentFile().mkdirs(); |
| 50 |
buildJar(fileResourceRoot, outputFile); |
| 51 |
// Create the jar file and resource loader |
| 52 |
jarFile = new JarFile(outputFile, true); |
| 53 |
return new JarFileResourceLoader("test-root", jarFile); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected void assertResource(Resource resource, String fileName) { |
| 58 |
final JarEntry entry = jarFile.getJarEntry(fileName); |
| 59 |
Assert.assertEquals(entry.getSize(), resource.getSize()); |
| 60 |
} |
| 61 |
|
| 62 |
static void buildJar(final File source, final File targetFile) throws IOException { |
| 63 |
final JarOutputStream target = new JarOutputStream(new FileOutputStream(targetFile)); |
| 64 |
final String sourceBase = source.getPath(); |
| 65 |
add(sourceBase, source, target); |
| 66 |
target.close(); |
| 67 |
} |
| 68 |
|
| 69 |
private static void add(final String sourceBase, final File source, final JarOutputStream target) throws IOException { |
| 70 |
BufferedInputStream in = null; |
| 71 |
String entryName = source.getPath().replace(sourceBase, "").replace("\\", "/"); |
| 72 |
if(entryName.startsWith("/")) |
| 73 |
entryName = entryName.substring(1); |
| 74 |
try { |
| 75 |
if (source.isDirectory()) { |
| 76 |
if (!entryName.isEmpty()) { |
| 77 |
if (!entryName.endsWith("/")) |
| 78 |
entryName += "/"; |
| 79 |
final JarEntry entry = new JarEntry(entryName); |
| 80 |
target.putNextEntry(entry); |
| 81 |
target.closeEntry(); |
| 82 |
} |
| 83 |
for (File nestedFile : source.listFiles()) |
| 84 |
add(sourceBase, nestedFile, target); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
final JarEntry entry = new JarEntry(entryName); |
| 89 |
target.putNextEntry(entry); |
| 90 |
in = new BufferedInputStream(new FileInputStream(source)); |
| 91 |
|
| 92 |
byte[] buffer = new byte[1024]; |
| 93 |
int count = 0; |
| 94 |
while ((count = in.read(buffer)) != -1) { |
| 95 |
target.write(buffer, 0, count); |
| 96 |
} |
| 97 |
target.closeEntry(); |
| 98 |
} |
| 99 |
finally { |
| 100 |
if (in != null) in.close(); |
| 101 |
} |
| 102 |
} |
| 103 |
} |