Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.crunch.test;
 
 import java.io.File;
 import java.util.Set;
 
 import  org.apache.hadoop.conf.Configuration;
 import  org.apache.hadoop.fs.Path;
 
Creates a temporary directory for a test case and destroys it afterwards. This provides a temporary directory like JUnit's TemporaryFolder but geared towards Hadoop applications. Unlike TemporaryFolder, it doesn't create any files or directories except for the root directory itself. Also, getDefaultConfiguration() provides you with a configuration that overrides path properties with temporary directories. You have to specify these properties via the constructor.
 
 public final class TemporaryPath extends ExternalResource {
   private final TemporaryFolder tmp = new TemporaryFolder();
   private final Set<StringconfKeys;

  
Construct TemporaryPath.

Parameters:
confKeys Configuration keys containing directories to override
 
   public TemporaryPath(String... confKeys) {
     if (confKeys != null) {
       this. = ImmutableSet.copyOf(confKeys);
     } else {
       this. = ImmutableSet.of();
     }
   }
 
   public void create() throws Throwable {
     .create();
   }
 
   public void delete() {
     .delete();
   }
 
   @Override
   public Statement apply(Statement baseDescription description) {
     return .apply(basedescription);
   }

  
Get the root directory which will be deleted automatically.
 
   public File getRootFile() {
     return .getRoot();
   }

  
Get the root directory as a Path.
 
   public Path getRootPath() {
     return toPath(.getRoot());
   }

  
Get the root directory as an absolute file name.
 
   public String getRootFileName() {
     return .getRoot().getAbsolutePath();
   }

  
Get a File below the temporary directory.
 
   public File getFile(String fileName) {
    return new File(getRootFile(), fileName);
  }

  
Get a Path below the temporary directory.
  public Path getPath(String fileName) {
    return toPath(getFile(fileName));
  }

  
Get an absolute file name below the temporary directory.
  public String getFileName(String fileName) {
    return getFile(fileName).getAbsolutePath();
  }

  
Copy a classpath resource to File.
  public File copyResourceFile(String resourceNamethrows IOException {
    String baseName = new File(resourceName).getName();
    File dest = new File(.getRoot(), baseName);
    copy(resourceNamedest);
    return dest;
  }

  
Copy a classpath resource to a Path.
  public Path copyResourcePath(String resourceNamethrows IOException {
    return toPath(copyResourceFile(resourceName));
  }

  
Copy a classpath resource returning its absolute file name.
  public String copyResourceFileName(String resourceNamethrows IOException {
    return copyResourceFile(resourceName).getAbsolutePath();
  }
  private static void copy(String resourceNameFile destthrows IOException {
    Files.copy(Resources.newInputStreamSupplier(Resources.getResource(resourceName)), dest);
  }
  private static Path toPath(File file) {
    return new Path(file.getAbsolutePath());
  }
  public Configuration getDefaultConfiguration() {
    return overridePathProperties(new Configuration());
  }

  
Set all keys specified in the constructor to temporary directories.
  public Configuration overridePathProperties(Configuration config) {
    for (String name : ) {
      config.set(namegetFileName("tmp-" + name));
    }
    return config;
  }
New to GrepCode? Check out our FAQ X