/**
 * Copyright 2013 Cloudera Inc.
 *
 * Licensed 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 com.cloudera.cdk.data.hcatalog;

import com.cloudera.cdk.data.Dataset;
import com.cloudera.cdk.data.DatasetDescriptor;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.cloudera.cdk.data.filesystem.DatasetTestUtilities.*;

public class TestHCatalogDatasetRepository {

  private static final Logger logger = LoggerFactory
      .getLogger(TestHCatalogDatasetRepository.class);

  private static final String TABLE_NAME = "test1";

  private FileSystem fileSystem;
  private Path testDirectory;
  private HCatalogDatasetRepository repo;

  @Before
  public void setUp() throws IOException {
    fileSystem = FileSystem.get(new Configuration());
    testDirectory = new Path(Files.createTempDir().getAbsolutePath());
  }

  @Test
  public void testManagedTable() throws IOException {
    File tableDir = new File("/tmp/test/user/hive/warehouse/" + TABLE_NAME);
    Assert.assertFalse("Data directory should not exist before test", tableDir.exists());
    repo = new HCatalogDatasetRepository();

    Dataset ds = repo.create(TABLE_NAME,
        new DatasetDescriptor.Builder().schema(USER_SCHEMA_URL).get());
    Assert.assertTrue("Data directory should exist after dataset creation",
        tableDir.exists());

    writeTestUsers(ds, 10);
    checkTestUsers(ds, 10);
    Assert.assertTrue("Data directory should exist after writing", tableDir.exists());

    repo.drop(TABLE_NAME);
    Assert.assertFalse("Data directory should not exist after dropping",
        tableDir.exists());
  }

  @Test
  public void testExternalTable() throws IOException {
    File tableDir = new File(testDirectory.toString(), TABLE_NAME);
    Assert.assertFalse("Data directory should not exist before test", tableDir.exists());
    repo = new HCatalogDatasetRepository(fileSystem, testDirectory);

    Dataset ds = repo.create(TABLE_NAME,
        new DatasetDescriptor.Builder().schema(USER_SCHEMA_URL).get());
    Assert.assertTrue("Data directory should exist after dataset creation",
        tableDir.exists());

    writeTestUsers(ds, 10);
    checkTestUsers(ds, 10);
    Assert.assertTrue("Data directory should exist after writing", tableDir.exists());

    repo.drop(TABLE_NAME);
    Assert.assertFalse("Data directory should not exist after dropping",
        tableDir.exists());
  }

}

@@ -883,16 +883,54 @@
 System;%0A
+import org.apache.hadoop.fs.FileUtil;%0A
 import o
@@ -955,16 +955,16 @@
 s.Path;%0A
-
 import o
@@ -1583,79 +1583,8 @@
 ));%0A
-    testDirectory = new Path(Files.createTempDir().getAbsolutePath());%0A
   %7D%0A
@@ -1715,32 +1715,111 @@
  + TABLE_NAME);%0A
+    FileUtil.fullyDelete(tableDir); // clear out possible previous failed runs%0A
     Assert.asser
@@ -2432,32 +2432,32 @@
 );%0A  %7D%0A%0A  @Test%0A
-
   public void te
@@ -2487,32 +2487,108 @@
 s IOException %7B%0A
+    Path testDirectory = new Path(Files.createTempDir().getAbsolutePath());%0A
     File tableDi

Fix bug in test where Hive warehouse table directory not getting deleted causes test to fail repeatedly.