View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // TestHelper.java
20  // Since: 2010/10/20
21  //
22  //--------------------------------------
23  package org.utgenome.util;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import org.xerial.util.FileResource;
29  import org.xerial.util.FileUtil;
30  
31  /**
32   * Utilities for writing JUnit code
33   * 
34   * @author leo
35   * 
36   */
37  public class TestHelper {
38  
39  	/**
40  	 * Create a temporary file
41  	 * 
42  	 * @param <T>
43  	 * @param referenceClass
44  	 * @param srcFileName
45  	 * @return
46  	 * @throws IOException
47  	 */
48  	public static <T> File createTempFileFrom(Class<T> referenceClass, String srcFileName) throws IOException {
49  		File tmp = FileUtil.createTempFile(new File("target"), "temp", srcFileName);
50  		FileUtil.copy(FileResource.openByteStream(referenceClass, srcFileName), tmp);
51  		tmp.deleteOnExit();
52  		return tmp;
53  	}
54  
55  	public static <T> File createTempFileFrom(Class<T> referenceClass, String srcFileName, File output) throws IOException {
56  		FileUtil.copy(FileResource.openByteStream(referenceClass, srcFileName), output);
57  		output.deleteOnExit();
58  		return output;
59  	}
60  
61  	public static <T> File createTempDir() throws IOException {
62  		File tmp = FileUtil.createTempDir(new File("target"), "tempdir");
63  		return tmp;
64  	}
65  
66  }