Add CopyFileTestRule.
authorGary Gregory <garydgregory@gmail.com>
Tue, 6 Feb 2018 01:00:54 +0000 (18:00 -0700)
committerGary Gregory <garydgregory@gmail.com>
Tue, 6 Feb 2018 01:00:54 +0000 (18:00 -0700)
commons-testing-generic/src/main/java/org/apache/commons/testing/net/AvailableServerPortFinder.java
commons-testing-generic/src/test/java/org/apache/commons/testing/CloserTest.java
commons-testing-junit4/pom.xml
commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/io/CopyFileTestRule.java [new file with mode: 0644]
commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/io/CopyFileTestRuleTest.java [new file with mode: 0644]
commons-testing-junit4/src/test/resources/test.txt [new file with mode: 0644]
pom.xml

index 9a276e9..656a97a 100644 (file)
@@ -14,6 +14,7 @@
  * See the license for the specific language governing permissions and\r
  * limitations under the license.\r
  */\r
+\r
 package org.apache.commons.testing.net;\r
 \r
 import java.io.IOException;\r
index b812292..ccb939d 100644 (file)
@@ -14,6 +14,7 @@
  * See the license for the specific language governing permissions and\r
  * limitations under the license.\r
  */\r
+\r
 package org.apache.commons.testing;\r
 \r
 import org.junit.Assert;\r
index 836c935..5c27ac3 100644 (file)
       <groupId>org.apache.commons</groupId>\r
       <artifactId>commons-lang3</artifactId>\r
     </dependency>\r
+    <dependency>\r
+      <groupId>commons-io</groupId>\r
+      <artifactId>commons-io</artifactId>\r
+    </dependency>\r
   </dependencies>\r
 \r
   <reporting>\r
diff --git a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/io/CopyFileTestRule.java b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/io/CopyFileTestRule.java
new file mode 100644 (file)
index 0000000..38792c0
--- /dev/null
@@ -0,0 +1,88 @@
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache license, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the license for the specific language governing permissions and\r
+ * limitations under the license.\r
+ */\r
+\r
+package org.apache.commons.testing.junit4.io;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.Objects;\r
+\r
+import org.apache.commons.io.FileUtils;\r
+import org.junit.Assert;\r
+import org.junit.rules.ExternalResource;\r
+import org.junit.rules.TemporaryFolder;\r
+\r
+/**\r
+ * Copies a file as a JUnit TestRule.\r
+ * \r
+ * @since 1.0.0\r
+ */\r
+public class CopyFileTestRule extends ExternalResource {\r
+\r
+    /**\r
+     * Creates a test rule that will copy a file to a given temporary folder.\r
+     * \r
+     * @param sourceFilePath\r
+     *            the file to copy.\r
+     * @param targetTemporaryFolder\r
+     *            the destination folder.\r
+     * @param targetFilePath\r
+     * @return a new test rule.\r
+     */\r
+    public static CopyFileTestRule create(final String sourceFilePath, final TemporaryFolder targetTemporaryFolder,\r
+            final String targetFilePath) {\r
+        return new CopyFileTestRule(sourceFilePath, targetTemporaryFolder, targetFilePath);\r
+    }\r
+\r
+    private final TemporaryFolder targetTemporaryFolder;\r
+    private final String targetFileName;\r
+    private final String sourceFilePath;\r
+\r
+    CopyFileTestRule(final String sourceFilePath, final TemporaryFolder targetTemporaryFolder,\r
+            final String targetFilePath) {\r
+        super();\r
+        this.sourceFilePath = Objects.requireNonNull(sourceFilePath, "sourceFilePath");\r
+        this.targetTemporaryFolder = Objects.requireNonNull(targetTemporaryFolder, "targetTemporaryFolder");\r
+        this.targetFileName = Objects.requireNonNull(targetFilePath, "targetFileName");\r
+    }\r
+\r
+    /*\r
+     * (non-Javadoc)\r
+     *\r
+     * @see org.junit.rules.ExternalResource#before()\r
+     */\r
+    @Override\r
+    protected void before() throws Throwable {\r
+        copyFile();\r
+    }\r
+\r
+    private void copyFile() throws IOException {\r
+        final File fromFile = getSourceFile();\r
+        final File toFile = getTargetFile();\r
+        FileUtils.copyFile(fromFile, toFile);\r
+        Assert.assertTrue(toFile.exists());\r
+    }\r
+\r
+    public File getSourceFile() {\r
+        return new File(sourceFilePath);\r
+    }\r
+\r
+    public File getTargetFile() {\r
+        return new File(targetTemporaryFolder.getRoot(), targetFileName);\r
+    }\r
+\r
+}\r
diff --git a/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/io/CopyFileTestRuleTest.java b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/io/CopyFileTestRuleTest.java
new file mode 100644 (file)
index 0000000..022995f
--- /dev/null
@@ -0,0 +1,47 @@
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache license, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the license for the specific language governing permissions and\r
+ * limitations under the license.\r
+ */\r
+\r
+package org.apache.commons.testing.junit4.io;\r
+\r
+import java.io.IOException;\r
+import java.nio.charset.StandardCharsets;\r
+\r
+import org.apache.commons.io.FileUtils;\r
+import org.apache.commons.testing.junit4.RuleChainFactory;\r
+import org.junit.Assert;\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.junit.rules.RuleChain;\r
+import org.junit.rules.TemporaryFolder;\r
+\r
+public class CopyFileTestRuleTest {\r
+\r
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();\r
+\r
+    public CopyFileTestRule copyFileTestRule = CopyFileTestRule.create("src/test/resources/test.txt", temporaryFolder,\r
+            "test-dest.txt");\r
+\r
+    @Rule\r
+    public RuleChain ruleChain = RuleChainFactory.create(temporaryFolder, copyFileTestRule);\r
+\r
+    @Test\r
+    public void test() throws IOException {\r
+        Assert.assertEquals("test",\r
+                FileUtils.readFileToString(copyFileTestRule.getTargetFile(), StandardCharsets.UTF_8).trim());\r
+\r
+    }\r
+}\r
diff --git a/commons-testing-junit4/src/test/resources/test.txt b/commons-testing-junit4/src/test/resources/test.txt
new file mode 100644 (file)
index 0000000..77356c3
--- /dev/null
@@ -0,0 +1 @@
+test\r
diff --git a/pom.xml b/pom.xml
index 909ad78..d784071 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <version>${junit4.version}</version>\r
       </dependency>\r
       <dependency>\r
+        <groupId>commons-io</groupId>\r
+        <artifactId>commons-io</artifactId>\r
+        <version>${commons-io.version}</version>\r
+      </dependency>\r
+      <dependency>\r
         <groupId>org.apache.commons</groupId>\r
         <artifactId>commons-lang3</artifactId>\r
         <version>${commons-lang.version}</version>\r
     <mongodb2.version>2.14.3</mongodb2.version>\r
     <mongodb3.version>3.6.1</mongodb3.version>\r
     <junit4.version>4.12</junit4.version>\r
+    <commons-io.version>2.6</commons-io.version>\r
     <commons-lang.version>3.7</commons-lang.version>\r
   </properties>\r
 \r