Repository: commons-testing Updated Branches: refs/heads/master a5c619156 -> 945be5fa9
Add CopyFileTestRule. Project: http://git-wip-us.apache.org/repos/asf/commons-testing/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-testing/commit/945be5fa Tree: http://git-wip-us.apache.org/repos/asf/commons-testing/tree/945be5fa Diff: http://git-wip-us.apache.org/repos/asf/commons-testing/diff/945be5fa Branch: refs/heads/master Commit: 945be5fa9bd75b591a9f9d46b740ccdade3cf1bf Parents: a5c6191 Author: Gary Gregory <garydgreg...@gmail.com> Authored: Mon Feb 5 18:00:54 2018 -0700 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Mon Feb 5 18:00:54 2018 -0700 ---------------------------------------------------------------------- .../testing/net/AvailableServerPortFinder.java | 1 + .../org/apache/commons/testing/CloserTest.java | 1 + commons-testing-junit4/pom.xml | 4 + .../testing/junit4/io/CopyFileTestRule.java | 88 ++++++++++++++++++++ .../testing/junit4/io/CopyFileTestRuleTest.java | 47 +++++++++++ .../src/test/resources/test.txt | 1 + pom.xml | 6 ++ 7 files changed, 148 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-testing/blob/945be5fa/commons-testing-generic/src/main/java/org/apache/commons/testing/net/AvailableServerPortFinder.java ---------------------------------------------------------------------- diff --git a/commons-testing-generic/src/main/java/org/apache/commons/testing/net/AvailableServerPortFinder.java b/commons-testing-generic/src/main/java/org/apache/commons/testing/net/AvailableServerPortFinder.java index 9a276e9..656a97a 100644 --- a/commons-testing-generic/src/main/java/org/apache/commons/testing/net/AvailableServerPortFinder.java +++ b/commons-testing-generic/src/main/java/org/apache/commons/testing/net/AvailableServerPortFinder.java @@ -14,6 +14,7 @@ * See the license for the specific language governing permissions and * limitations under the license. */ + package org.apache.commons.testing.net; import java.io.IOException; http://git-wip-us.apache.org/repos/asf/commons-testing/blob/945be5fa/commons-testing-generic/src/test/java/org/apache/commons/testing/CloserTest.java ---------------------------------------------------------------------- diff --git a/commons-testing-generic/src/test/java/org/apache/commons/testing/CloserTest.java b/commons-testing-generic/src/test/java/org/apache/commons/testing/CloserTest.java index b812292..ccb939d 100644 --- a/commons-testing-generic/src/test/java/org/apache/commons/testing/CloserTest.java +++ b/commons-testing-generic/src/test/java/org/apache/commons/testing/CloserTest.java @@ -14,6 +14,7 @@ * See the license for the specific language governing permissions and * limitations under the license. */ + package org.apache.commons.testing; import org.junit.Assert; http://git-wip-us.apache.org/repos/asf/commons-testing/blob/945be5fa/commons-testing-junit4/pom.xml ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/pom.xml b/commons-testing-junit4/pom.xml index 836c935..5c27ac3 100644 --- a/commons-testing-junit4/pom.xml +++ b/commons-testing-junit4/pom.xml @@ -76,6 +76,10 @@ <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> </dependencies> <reporting> http://git-wip-us.apache.org/repos/asf/commons-testing/blob/945be5fa/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/io/CopyFileTestRule.java ---------------------------------------------------------------------- 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 index 0000000..38792c0 --- /dev/null +++ b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/io/CopyFileTestRule.java @@ -0,0 +1,88 @@ +/* + * 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.commons.testing.junit4.io; + +import java.io.File; +import java.io.IOException; +import java.util.Objects; + +import org.apache.commons.io.FileUtils; +import org.junit.Assert; +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; + +/** + * Copies a file as a JUnit TestRule. + * + * @since 1.0.0 + */ +public class CopyFileTestRule extends ExternalResource { + + /** + * Creates a test rule that will copy a file to a given temporary folder. + * + * @param sourceFilePath + * the file to copy. + * @param targetTemporaryFolder + * the destination folder. + * @param targetFilePath + * @return a new test rule. + */ + public static CopyFileTestRule create(final String sourceFilePath, final TemporaryFolder targetTemporaryFolder, + final String targetFilePath) { + return new CopyFileTestRule(sourceFilePath, targetTemporaryFolder, targetFilePath); + } + + private final TemporaryFolder targetTemporaryFolder; + private final String targetFileName; + private final String sourceFilePath; + + CopyFileTestRule(final String sourceFilePath, final TemporaryFolder targetTemporaryFolder, + final String targetFilePath) { + super(); + this.sourceFilePath = Objects.requireNonNull(sourceFilePath, "sourceFilePath"); + this.targetTemporaryFolder = Objects.requireNonNull(targetTemporaryFolder, "targetTemporaryFolder"); + this.targetFileName = Objects.requireNonNull(targetFilePath, "targetFileName"); + } + + /* + * (non-Javadoc) + * + * @see org.junit.rules.ExternalResource#before() + */ + @Override + protected void before() throws Throwable { + copyFile(); + } + + private void copyFile() throws IOException { + final File fromFile = getSourceFile(); + final File toFile = getTargetFile(); + FileUtils.copyFile(fromFile, toFile); + Assert.assertTrue(toFile.exists()); + } + + public File getSourceFile() { + return new File(sourceFilePath); + } + + public File getTargetFile() { + return new File(targetTemporaryFolder.getRoot(), targetFileName); + } + +} http://git-wip-us.apache.org/repos/asf/commons-testing/blob/945be5fa/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/io/CopyFileTestRuleTest.java ---------------------------------------------------------------------- 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 index 0000000..022995f --- /dev/null +++ b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/io/CopyFileTestRuleTest.java @@ -0,0 +1,47 @@ +/* + * 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.commons.testing.junit4.io; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.testing.junit4.RuleChainFactory; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import org.junit.rules.TemporaryFolder; + +public class CopyFileTestRuleTest { + + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + public CopyFileTestRule copyFileTestRule = CopyFileTestRule.create("src/test/resources/test.txt", temporaryFolder, + "test-dest.txt"); + + @Rule + public RuleChain ruleChain = RuleChainFactory.create(temporaryFolder, copyFileTestRule); + + @Test + public void test() throws IOException { + Assert.assertEquals("test", + FileUtils.readFileToString(copyFileTestRule.getTargetFile(), StandardCharsets.UTF_8).trim()); + + } +} http://git-wip-us.apache.org/repos/asf/commons-testing/blob/945be5fa/commons-testing-junit4/src/test/resources/test.txt ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/test/resources/test.txt b/commons-testing-junit4/src/test/resources/test.txt new file mode 100644 index 0000000..77356c3 --- /dev/null +++ b/commons-testing-junit4/src/test/resources/test.txt @@ -0,0 +1 @@ +test http://git-wip-us.apache.org/repos/asf/commons-testing/blob/945be5fa/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 909ad78..d784071 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,11 @@ <version>${junit4.version}</version> </dependency> <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>${commons-io.version}</version> + </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang.version}</version> @@ -139,6 +144,7 @@ <mongodb2.version>2.14.3</mongodb2.version> <mongodb3.version>3.6.1</mongodb3.version> <junit4.version>4.12</junit4.version> + <commons-io.version>2.6</commons-io.version> <commons-lang.version>3.7</commons-lang.version> </properties>