Github user ilooner commented on a diff in the pull request: https://github.com/apache/drill/pull/984#discussion_r148144235 --- Diff: exec/java-exec/src/test/java/org/apache/drill/test/BaseDirTestWatcher.java --- @@ -0,0 +1,184 @@ +/* + * 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.drill.test; + +import com.google.common.base.Charsets; +import org.apache.commons.io.FileUtils; +import org.apache.drill.common.util.TestTools; +import org.apache.drill.exec.util.TestUtilities; +import org.junit.runner.Description; + +import java.io.File; +import java.io.IOException; + +public class BaseDirTestWatcher extends DirTestWatcher { + public enum DirType { + ROOT, + TEST_TMP; + } + + private File tmpDir; + private File storeDir; + private File dfsTestTmpParentDir; + private File dfsTestTmpDir; + private File rootDir; + + public BaseDirTestWatcher() { + super(); + } + + @Override + protected void starting(Description description) { + super.starting(description); + + rootDir = makeSubDir("root"); + tmpDir = new File(rootDir, "tmp"); + storeDir = new File(rootDir, "store"); + dfsTestTmpParentDir = new File(rootDir, "dfsTestTmp"); + + tmpDir.mkdirs(); + storeDir.mkdirs(); + dfsTestTmpParentDir.mkdirs(); + } + + public File getTmpDir() { + return tmpDir; + } + + public File getStoreDir() { + return storeDir; + } + + public File getDfsTestTmpParentDir() { + return dfsTestTmpParentDir; + } + + public File getDfsTestTmpDir() { + return dfsTestTmpDir; + } + + public String getDfsTestTmpDirPath() { + return dfsTestTmpDir.getAbsolutePath().replaceAll("/\\./", "/"); + } + + public File getRootDir() { + return rootDir; + } + + public String getRootDirPath() { + return rootDir.getAbsolutePath().replaceAll("/\\./", "/"); + } + + public void newDfsTestTmpDir() { + dfsTestTmpDir = TestUtilities.createTempDir(BaseTestQuery.dirTestWatcher.getDfsTestTmpParentDir()); + } + + private File getDir(DirType type) { + switch (type) { + case ROOT: + return rootDir; + case TEST_TMP: + return dfsTestTmpDir; + default: + throw new IllegalArgumentException(String.format("Unsupported type %s", type)); + } + } + + public File makeRootSubDir(String relPath) { + return makeSubDir(relPath, DirType.ROOT); + } + + public File makeTestTmpSubDir(String relPath) { + return makeSubDir(relPath, DirType.TEST_TMP); + } + + private File makeSubDir(String relPath, DirType type) { + File subDir = new File(getDir(type), relPath); + subDir.mkdirs(); + return subDir; + } + + /** + * This preserves the relative path of the directory in root + * @param relPath + * @return + */ + public File copyResourceToRoot(String relPath) { + return copyTo(relPath, relPath, TestTools.DataType.RESOURCE, DirType.ROOT); + } + + public File copyFileToRoot(String relPath) { + return copyTo(relPath, relPath, TestTools.DataType.PROJECT, DirType.ROOT); + } + + public File copyResourceToRoot(String relPath, String destPath) { + return copyTo(relPath, destPath, TestTools.DataType.RESOURCE, DirType.ROOT); + } + + public File copyResourceToTestTmp(String relPath, String destPath) { + return copyTo(relPath, destPath, TestTools.DataType.RESOURCE, DirType.TEST_TMP); + } + + private File copyTo(String relPath, String destPath, TestTools.DataType dataType, DirType dirType) { + File file = TestTools.getFile(relPath, dataType); + + if (file.isDirectory()) { + File subDir = makeSubDir(destPath, dirType); + TestTools.copyDirToDest(relPath, subDir, dataType); + return subDir; + } else { + File baseDir = getDir(dirType); + + destPath = destPath.startsWith("/") ? destPath.substring(1): destPath; + baseDir.toPath() + .resolve(destPath) + .getParent() + .toFile() + .mkdirs(); + + File destFile = baseDir.toPath() + .resolve(destPath) + .toFile(); + + try { + destFile.createNewFile(); + FileUtils.copyFile(file, destFile); + } catch (IOException e) { + throw new RuntimeException("This should not happen", e); + } + + return destFile; + } + } + + public void replaceMetaDataContents(File metaDataFile, String replacePath, String customStringReplacement) { --- End diff -- done
---