public final class TemporaryPath
extends org.junit.rules.ExternalResource
Rule that creates a managed temporary directory for a test case and
destroys it afterwards. This works much like JUnit's TemporaryFolder but is
geared towards Hadoop applications. The temporary directory is deleted after each test
case, no matter if the test case succeeded or failed.
This is how it works:
public class TestExample {
@Rule
public TemporaryPath tmpDir = new TemporaryPath();
@Test
public void testSomething() {
Path input = tmpDir.copyResourcePath("my-test-data");
Path output = tmpDir.getPath("output");
// create and run a Hadoop job reading from input and writing to output
}
}
In some cases, the frameworks you use in your tests use temporary directories
internally. If those directories are configurable via Hadoop properties, you can
let overridePathProperties(Configuration) override them
so that they point to your managed temporary directory. You have to specify the
properties to override via the constructor.
| Constructor and Description |
|---|
TemporaryPath(String... confKeys)
Construct
TemporaryPath. |
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
Used by JUnit internally.
|
File |
copyResourceFile(String resourceName)
Copy a classpath resource to
File. |
String |
copyResourceFileName(String resourceName)
Copy a classpath resource returning its absolute file name.
|
org.apache.hadoop.fs.Path |
copyResourcePath(String resourceName)
Copy a classpath resource to a
Path. |
org.apache.hadoop.conf.Configuration |
getDefaultConfiguration()
Get a new
Configuration instance. |
File |
getFile(String fileName)
Get a
File below the temporary directory. |
String |
getFileName(String fileName)
Get an absolute file name below the temporary directory.
|
org.apache.hadoop.fs.Path |
getPath(String fileName)
Get a
Path below the temporary directory. |
File |
getRootFile()
Get the root directory which will be deleted automatically.
|
String |
getRootFileName()
Get the root directory as an absolute file name.
|
org.apache.hadoop.fs.Path |
getRootPath()
Get the root directory as a
Path. |
org.apache.hadoop.conf.Configuration |
overridePathProperties(org.apache.hadoop.conf.Configuration conf)
Set all keys specified in the constructor to temporary directories.
|
public TemporaryPath(String... confKeys)
TemporaryPath.confKeys - Configuration keys containing directories to overridepublic org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
apply in interface org.junit.rules.TestRuleapply in class org.junit.rules.ExternalResourcepublic File getRootFile()
public org.apache.hadoop.fs.Path getRootPath()
Path.public String getRootFileName()
public org.apache.hadoop.fs.Path getPath(String fileName)
Path below the temporary directory.public String getFileName(String fileName)
public File copyResourceFile(String resourceName) throws IOException
File.IOExceptionpublic org.apache.hadoop.fs.Path copyResourcePath(String resourceName) throws IOException
Path.IOExceptionpublic org.apache.hadoop.conf.Configuration getDefaultConfiguration()
Configuration instance.public org.apache.hadoop.conf.Configuration overridePathProperties(org.apache.hadoop.conf.Configuration conf)
public String copyResourceFileName(String resourceName) throws IOException
IOExceptionCopyright © 2014 The Apache Software Foundation. All Rights Reserved.