You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by jo...@apache.org on 2019/12/17 02:35:33 UTC
[incubator-dolphinscheduler] branch dev updated: Add
FileUtilsTest.java , the unit test for FileUtils (#1493)
This is an automated email from the ASF dual-hosted git repository.
journey pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new 4cf8458 Add FileUtilsTest.java , the unit test for FileUtils (#1493)
4cf8458 is described below
commit 4cf8458719a0601f70e78e9ecc89089df887b8ec
Author: zhukai <bo...@qq.com>
AuthorDate: Tue Dec 17 10:35:24 2019 +0800
Add FileUtilsTest.java , the unit test for FileUtils (#1493)
---
.../dolphinscheduler/api/utils/FileUtilsTest.java | 109 +++++++++++++++++++++
pom.xml | 1 +
2 files changed, 110 insertions(+)
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/FileUtilsTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/FileUtilsTest.java
new file mode 100644
index 0000000..f205d39
--- /dev/null
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/FileUtilsTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.dolphinscheduler.api.utils;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.mockito.Mockito;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.io.Resource;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.junit.Assert.*;
+
+public class FileUtilsTest {
+
+ private static final Logger logger = LoggerFactory.getLogger(FileUtilsTest.class);
+
+ @Rule
+ public TemporaryFolder folder = null;
+
+ private String rootPath = null;
+
+ @Before
+ public void setUp() throws Exception {
+
+ folder = new TemporaryFolder();
+ folder.create();
+
+ rootPath = folder.getRoot().getAbsolutePath();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+
+ folder.delete();
+ }
+
+ /**
+ * Use mock to test copyFile
+ * @throws IOException
+ */
+ @Test
+ public void testCopyFile() throws IOException {
+
+ //Define dest file path
+ String destFilename = rootPath + System.getProperty("file.separator") + "data.txt";
+ logger.info("destFilename: "+destFilename);
+
+ //Define InputStream for MultipartFile
+ String data = "data text";
+ InputStream targetStream = new ByteArrayInputStream(data.getBytes());
+
+ //Use Mockito to mock MultipartFile
+ MultipartFile file = Mockito.mock(MultipartFile.class);
+ Mockito.when(file.getInputStream()).thenReturn(targetStream);
+
+ //Invoke copyFile
+ FileUtils.copyFile(file,destFilename);
+
+ //Test file exists
+ File destFile = new File(destFilename);
+ assertTrue(destFile.exists());
+
+ }
+
+ @Test
+ public void testFile2Resource() throws IOException {
+
+ //Define dest file path
+ String destFilename = rootPath + System.getProperty("file.separator") + "data.txt";
+ logger.info("destFilename: "+destFilename);
+
+ //Define test resource
+ File file = folder.newFile("resource.txt");
+
+ //Invoke file2Resource and test not null
+ Resource resource = FileUtils.file2Resource(file.getAbsolutePath());
+ assertNotNull(resource);
+
+ //Invoke file2Resource and test null
+ Resource resource1 = FileUtils.file2Resource(file.getAbsolutePath()+"abc");
+ assertNull(resource1);
+
+ }
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index ed118a5..61c2297 100644
--- a/pom.xml
+++ b/pom.xml
@@ -613,6 +613,7 @@
<configuration>
<includes>
<include>**/api/utils/CheckUtilsTest.java</include>
+ <include>**/api/utils/FileUtilsTest.java</include>
<include>**/common/graph/*.java</include>
<include>**/*CollectionUtilsTest.java</include><!--run test classes-->
</includes>