| 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.log.StreamModuleLogger; |
| 22 |
import org.jboss.modules.util.Util; |
| 23 |
import org.junit.BeforeClass; |
| 24 |
|
| 25 |
import java.io.Closeable; |
| 26 |
import java.io.File; |
| 27 |
import java.io.FileInputStream; |
| 28 |
import java.io.FileOutputStream; |
| 29 |
import java.io.IOException; |
| 30 |
import java.io.InputStream; |
| 31 |
import java.io.OutputStream; |
| 32 |
|
| 33 |
/** |
| 34 |
* Abstract Test Case used as a base for all module tests. |
| 35 |
* |
| 36 |
* @author John Bailey |
| 37 |
*/ |
| 38 |
public abstract class AbstractModuleTestCase { |
| 39 |
|
| 40 |
@BeforeClass |
| 41 |
public static void initUrlHandler() { |
| 42 |
// this also kicks off Module's static init |
| 43 |
Module.setModuleLogger(new StreamModuleLogger(System.err)); |
| 44 |
} |
| 45 |
|
| 46 |
protected File getResource(final String path) throws Exception { |
| 47 |
return Util.getResourceFile(getClass(), path); |
| 48 |
} |
| 49 |
|
| 50 |
protected void copyResource(final String inputResource, final String outputBase, final String outputPath) throws Exception { |
| 51 |
final File resource = getResource(inputResource); |
| 52 |
final File outputDirectory = new File(getResource(outputBase), outputPath); |
| 53 |
|
| 54 |
if(!resource.exists()) |
| 55 |
throw new IllegalArgumentException("Resource does not exist"); |
| 56 |
if (outputDirectory.exists() && outputDirectory.isFile()) |
| 57 |
throw new IllegalArgumentException("OutputDirectory must be a directory"); |
| 58 |
if (!outputDirectory.exists()) { |
| 59 |
if (!outputDirectory.mkdirs()) |
| 60 |
throw new RuntimeException("Failed to create output directory"); |
| 61 |
} |
| 62 |
final File outputFile = new File(outputDirectory, resource.getName()); |
| 63 |
final InputStream in = new FileInputStream(resource); |
| 64 |
try { |
| 65 |
final OutputStream out = new FileOutputStream(outputFile); |
| 66 |
try { |
| 67 |
final byte[] b = new byte[8192]; |
| 68 |
int c; |
| 69 |
while ((c = in.read(b)) != -1) { |
| 70 |
out.write(b, 0, c); |
| 71 |
} |
| 72 |
out.close(); |
| 73 |
in.close(); |
| 74 |
} finally { |
| 75 |
safeClose(out); |
| 76 |
} |
| 77 |
} finally { |
| 78 |
safeClose(in); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private static void safeClose(final Closeable closeable) { |
| 83 |
if (closeable != null) try { |
| 84 |
closeable.close(); |
| 85 |
} catch (IOException e) { |
| 86 |
// meh |
| 87 |
} |
| 88 |
} |
| 89 |
} |