geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject [06/50] [abbrv] incubator-geode git commit: GEODE-1466 : Added TemporaryFileRule JUnit rule for tests that need to create files in a particular directory.
Date Mon, 31 Oct 2016 20:55:46 GMT
GEODE-1466 : Added TemporaryFileRule JUnit rule for tests that need to create files in a particular
directory.

* This closes #260


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/b0659935
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/b0659935
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/b0659935

Branch: refs/heads/feature/GEODE-1930
Commit: b06599353d40ae4b54282c71aba9df57411e2704
Parents: 8a08032
Author: Jared Stewart <jstewart@pivotal.io>
Authored: Thu Oct 13 16:50:35 2016 -0700
Committer: Jinmei Liao <jiliao@pivotal.io>
Committed: Fri Oct 14 12:00:03 2016 -0700

----------------------------------------------------------------------
 geode-junit/build.gradle                        |   5 +-
 .../test/junit/rules/TemporaryFileRule.java     | 111 ++++++++++++++++
 .../test/junit/rules/TemporaryFileRuleTest.java | 130 +++++++++++++++++++
 3 files changed, 245 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b0659935/geode-junit/build.gradle
----------------------------------------------------------------------
diff --git a/geode-junit/build.gradle b/geode-junit/build.gradle
index 3e4eb22..f7e5e46 100755
--- a/geode-junit/build.gradle
+++ b/geode-junit/build.gradle
@@ -17,7 +17,10 @@
 
 dependencies {
   testCompile 'commons-lang:commons-lang:' + project.'commons-lang.version'
-  compile ('junit:junit:' + project.'junit.version') {
+  testCompile 'com.google.guava:guava:' + project.'guava.version'
+  testCompile 'org.assertj:assertj-core:' + project.'assertj-core.version'
+
+  compile('junit:junit:' + project.'junit.version') {
     exclude module: 'hamcrest-core'
   }
   compile 'org.hamcrest:hamcrest-all:' + project.'hamcrest-all.version'

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b0659935/geode-junit/src/main/java/org/apache/geode/test/junit/rules/TemporaryFileRule.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/TemporaryFileRule.java
b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/TemporaryFileRule.java
new file mode 100644
index 0000000..bd2cac2
--- /dev/null
+++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/TemporaryFileRule.java
@@ -0,0 +1,111 @@
+/*
+ * 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.geode.test.junit.rules;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+import org.junit.rules.ExternalResource;
+
+
+/**
+ * A {@link org.junit.rules.TestRule} to create temporary files in a given directory that
should be
+ * deleted when the test method finishes.  This is useful in place of {@link org.junit.rules.TemporaryFolder}
when a test needs
+ * to create files in a particular directory, for example user.home or user.dir.
+ *
+ * <p>Example of usage:
+ * <pre>
+ * public static class HasTemporaryFile {
+ *  &#064;Rule
+ *  public TemporaryFileRule temporaryFileRule = TemporaryFileRule.inUserHome();
+ *
+ *  &#064;Test
+ *  public void testUsingTempFolder() throws IOException {
+ *      File createdFile= temporaryFileRule.newFile(&quot;myfile.txt&quot;);
+ *      File createdFile= temporaryFileRule.newFile(&quot;myfile2.txt&quot;);
+ *      // ...
+ *     }
+ * }
+ * </pre>
+ */
+public class TemporaryFileRule extends ExternalResource {
+
+  private final String directory;
+
+  private Set<File> files;
+
+  private TemporaryFileRule(String parentDirectory) {
+    this.directory = parentDirectory;
+  }
+
+  public static TemporaryFileRule inUserHome() {
+    return new TemporaryFileRule(System.getProperty("user.home"));
+  }
+
+  public static TemporaryFileRule inCurrentDir() {
+    return new TemporaryFileRule(System.getProperty("user.dir"));
+  }
+
+  public static TemporaryFileRule inDirectory(String directory) {
+    return new TemporaryFileRule(directory);
+  }
+
+  @Override
+  public void before() {
+    files = new HashSet<>();
+  }
+
+  @Override
+  public void after() {
+    files.stream().filter(Objects::nonNull).filter(File::exists).forEach(File::delete);
+  }
+
+  /**
+   * Creates a new file with the given name in the specified {@link #directory}.
+   *
+   * @param fileName the name of the file to create.
+   *
+   * @return the file that was created.
+   *
+   * @throws IllegalStateException if the file already exists.
+   * @throws IllegalStateException if there is an {@link IOException} while creating the
file.
+   */
+  public File newFile(String fileName) {
+    return createFile(directory, fileName);
+  }
+
+
+  private File createFile(String directory, String fileName) {
+    File file = new File(directory, fileName);
+    try {
+      if (!file.createNewFile()) {
+        throw new IllegalStateException("The specified file " + file.getAbsolutePath() +
" already exists.");
+      }
+    } catch (IOException e) {
+      throw new IllegalStateException("IOException attempting to create file " + file.getAbsolutePath()
+ ".", e);
+    }
+
+    file.deleteOnExit();
+    files.add(file);
+    return file;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b0659935/geode-junit/src/test/java/org/apache/geode/test/junit/rules/TemporaryFileRuleTest.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/test/java/org/apache/geode/test/junit/rules/TemporaryFileRuleTest.java
b/geode-junit/src/test/java/org/apache/geode/test/junit/rules/TemporaryFileRuleTest.java
new file mode 100644
index 0000000..4deb1ee
--- /dev/null
+++ b/geode-junit/src/test/java/org/apache/geode/test/junit/rules/TemporaryFileRuleTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.geode.test.junit.rules;
+
+import static org.assertj.core.api.Assertions.*;
+
+import java.io.File;
+
+import com.google.common.io.Files;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.Result;
+
+import org.apache.geode.test.junit.categories.IntegrationTest;
+
+@Category(IntegrationTest.class)
+public class TemporaryFileRuleTest {
+
+  @Test
+  public void exceptionIsThrownIfFileAlreadyExists() {
+    Result result = TestRunner.runTest(TemporaryFileRuleTest.ExceptionIsThrownIfFileAlreadyExists.class);
+
+    assertThat(result.wasSuccessful()).isTrue();
+  }
+
+  @Test
+  public void fileGetsCreatedProperly() {
+    Result result = TestRunner.runTest(TemporaryFileRuleTest.FileGetsCreatedProperly.class);
+
+    assertThat(result.wasSuccessful()).isTrue();
+  }
+
+
+  @Test
+  public void filesGetCleanedUpAfterTestMethod() {
+    Result result = TestRunner.runTest(TemporaryFileRuleTest.FilesGetCleanedUpAfterTestMethod.class);
+
+    assertThat(result.wasSuccessful()).isTrue();
+  }
+
+  /**
+   * Used by test {@link #exceptionIsThrownIfFileAlreadyExists()}
+   */
+  public static class ExceptionIsThrownIfFileAlreadyExists {
+
+    static File tempDirectory = Files.createTempDir();
+
+    @Rule
+    public TemporaryFileRule temporaryFileRule = TemporaryFileRule.inDirectory(tempDirectory.getAbsolutePath());
+
+    @Test
+    public void doTest() throws Exception {
+      String fileName = "fileThatAlreadyExists.txt";
+      File tempFile = new File(tempDirectory, fileName);
+      assertThat(tempFile.createNewFile()).isTrue();
+      assertThatThrownBy(() -> temporaryFileRule.newFile(fileName)).isInstanceOf(IllegalStateException.class);
+    }
+  }
+
+
+  /**
+   * Used by test {@link #fileGetsCreatedProperly()}
+   */
+  public static class FileGetsCreatedProperly {
+
+    static File tempDirectory = Files.createTempDir();
+
+    @Rule
+    public TemporaryFileRule temporaryFileRule = TemporaryFileRule.inDirectory(tempDirectory.getAbsolutePath());
+
+    @Test
+    public void doTest() throws Exception {
+      String fileName = "expectedFile.txt";
+      File expectedFile = new File(tempDirectory, fileName);
+      File actualFile = temporaryFileRule.newFile(fileName);
+
+      assertThat(actualFile).isEqualTo(expectedFile);
+    }
+  }
+
+  /**
+   * Used by test {@link #filesGetCleanedUpAfterTestMethod()}
+   *
+   * This test ensures that {@link TemporaryFileRule} cleans up the files it created in between
each test method.
+   */
+  public static class FilesGetCleanedUpAfterTestMethod {
+
+    private static String fileName1 = "test1.txt";
+    private static String fileName2 = "test2.txt";
+
+    static File tempDirectory = Files.createTempDir();
+
+    @Rule
+    public TemporaryFileRule temporaryFileRule = TemporaryFileRule.inDirectory(tempDirectory.getAbsolutePath());
+
+    @Test
+    public void test1() throws Exception {
+      temporaryFileRule.newFile(fileName1);
+
+      assertThat(new File(tempDirectory, fileName1)).exists();
+      assertThat(new File(tempDirectory, fileName2)).doesNotExist();
+    }
+
+    @Test
+    public void test2() throws Exception {
+      temporaryFileRule.newFile(fileName2);
+
+      assertThat(new File(tempDirectory, fileName1)).doesNotExist();
+      assertThat(new File(tempDirectory, fileName2)).exists();
+    }
+  }
+
+}


Mime
View raw message