From 1d22cde7e67d4c31db4ab1f64487a53382ae9390 Mon Sep 17 00:00:00 2001 From: zzzk1 Date: Sun, 28 Jan 2024 18:01:23 +0800 Subject: [PATCH] Improve add Utils test case --- .../common/fs/HdfsOperatorTest.scala | 3 +- .../common/util/DateUtilsTest.scala | 74 +++++++++++++++++++ .../common/util/FileUtilsTest.scala | 58 +++++++++++++++ .../streampark/common/util/UtilsTest.scala | 68 +++++++++++++++++ 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 streampark-common/src/test/scala/org/apache/streampark/common/util/DateUtilsTest.scala create mode 100644 streampark-common/src/test/scala/org/apache/streampark/common/util/FileUtilsTest.scala create mode 100644 streampark-common/src/test/scala/org/apache/streampark/common/util/UtilsTest.scala diff --git a/streampark-common/src/test/scala/org/apache/streampark/common/fs/HdfsOperatorTest.scala b/streampark-common/src/test/scala/org/apache/streampark/common/fs/HdfsOperatorTest.scala index 79479bc9c7..3a426200be 100644 --- a/streampark-common/src/test/scala/org/apache/streampark/common/fs/HdfsOperatorTest.scala +++ b/streampark-common/src/test/scala/org/apache/streampark/common/fs/HdfsOperatorTest.scala @@ -17,9 +17,10 @@ package org.apache.streampark.common.fs +import org.apache.streampark.common.fs.HdfsOperatorTest.withTempDir + import org.apache.commons.codec.digest.DigestUtils import org.apache.commons.io.{FileUtils, IOUtils} -import org.apache.streampark.common.fs.HdfsOperatorTest.withTempDir import org.junit.jupiter.api.Assertions.{assertDoesNotThrow, assertEquals, assertFalse, assertNotEquals, assertThrows, assertTrue} import org.junit.jupiter.api.Test diff --git a/streampark-common/src/test/scala/org/apache/streampark/common/util/DateUtilsTest.scala b/streampark-common/src/test/scala/org/apache/streampark/common/util/DateUtilsTest.scala new file mode 100644 index 0000000000..05d9607c52 --- /dev/null +++ b/streampark-common/src/test/scala/org/apache/streampark/common/util/DateUtilsTest.scala @@ -0,0 +1,74 @@ +/* + * 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.streampark.common.util + +import org.junit.jupiter.api.Assertions._ +import org.junit.jupiter.api.Test + +import java.text.SimpleDateFormat +import java.time.LocalDateTime +import java.util.concurrent.TimeUnit + +class DateUtilsTest { + val dateTestCase = "2000-01-01 00:00:01" + val timeStampTestCase: Long = 946656001000L; + + @Test def stringToDateTest(): Unit = { + val sdf: SimpleDateFormat = new SimpleDateFormat(DateUtils.fullFormat) + val date = sdf.parse(dateTestCase); + + assertEquals(date, DateUtils.stringToDate(dateTestCase)) + } + + @Test def milliSecond2DateTest(): Unit = { + val sdf: SimpleDateFormat = new SimpleDateFormat(DateUtils.fullFormat) + val date = sdf.parse(dateTestCase) + + assertEquals(date, DateUtils.milliSecond2Date(timeStampTestCase)) + } + + @Test def getTimeTest(): Unit = { + assertEquals(timeStampTestCase, DateUtils.getTime(dateTestCase)) + } + + @Test def toDurationTest(): Unit = { + val oneSecondInMillis: Long = TimeUnit.SECONDS.toMillis(1) + val oneMinutesInMillis: Long = TimeUnit.MINUTES.toMillis(1) + val oneHourInMillis: Long = TimeUnit.HOURS.toMillis(1) + val oneDayInMillis: Long = TimeUnit.DAYS.toMillis(1) + val allConditionInOne = + oneSecondInMillis + oneMinutesInMillis + oneHourInMillis + oneDayInMillis + + assertEquals("1 days 1 hours 1 minutes 1 seconds ", DateUtils.toDuration(allConditionInOne)) + assertEquals("0 hours 0 minutes 1 seconds ", DateUtils.toDuration(oneSecondInMillis)) + assertEquals("0 hours 1 minutes ", DateUtils.toDuration(oneMinutesInMillis)) + assertEquals("1 hours ", DateUtils.toDuration(oneHourInMillis)) + assertEquals("1 days ", DateUtils.toDuration(oneDayInMillis)) + } + + @Test def getTimeUnitTest(): Unit = { + assertEquals((5, TimeUnit.SECONDS), DateUtils.getTimeUnit("")) + assertEquals((5, TimeUnit.SECONDS), DateUtils.getTimeUnit("5s")) + assertEquals((4, TimeUnit.MINUTES), DateUtils.getTimeUnit("4m")) + assertEquals((3, TimeUnit.HOURS), DateUtils.getTimeUnit("3h")) + assertEquals((2, TimeUnit.DAYS), DateUtils.getTimeUnit("2d")) + + assertThrows(classOf[IllegalArgumentException], () => DateUtils.getTimeUnit("5s4m3h2d")) + assertThrows(classOf[IllegalArgumentException], () => DateUtils.getTimeUnit("invalid")) + } +} diff --git a/streampark-common/src/test/scala/org/apache/streampark/common/util/FileUtilsTest.scala b/streampark-common/src/test/scala/org/apache/streampark/common/util/FileUtilsTest.scala new file mode 100644 index 0000000000..4d07630a5c --- /dev/null +++ b/streampark-common/src/test/scala/org/apache/streampark/common/util/FileUtilsTest.scala @@ -0,0 +1,58 @@ +/* + * 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.streampark.common.util + +import org.junit.jupiter.api.Assertions.{assertEquals, assertFalse, assertThrows, assertTrue} +import org.junit.jupiter.api.Test + +import java.io.{File, IOException} + +class FileUtilsTest { + + @Test def isJarFileTypeTest(): Unit = { + assertThrows(classOf[RuntimeException], () => FileUtils.isJarFileType(null: File)) + + val file: File = File.createTempFile("test", "1") + assertFalse(FileUtils.isJarFileType(file: File)) + file.delete() + assertThrows(classOf[RuntimeException], () => FileUtils.isJarFileType(file: File)) + } + + @Test def mkdirTest(): Unit = { + assertThrows(classOf[NullPointerException], () => FileUtils.mkdir(null)) + val file: File = File.createTempFile("test", "1") + + assertThrows(classOf[IOException], () => FileUtils.mkdir(file)) + } + + @Test def getPathFromEnvTest(): Unit = { + assertThrows(classOf[NullPointerException], () => FileUtils.getPathFromEnv(null)) + assertThrows(classOf[IllegalArgumentException], () => FileUtils.getPathFromEnv("null")) + FileUtils.getPathFromEnv("null") + } + + @Test def existsTest(): Unit = { + assertFalse(FileUtils.exists(null)) + + val file: File = File.createTempFile("test", "1") + assertTrue(FileUtils.exists(file)) + file.delete() + assertFalse(FileUtils.exists(file)) + } + +} diff --git a/streampark-common/src/test/scala/org/apache/streampark/common/util/UtilsTest.scala b/streampark-common/src/test/scala/org/apache/streampark/common/util/UtilsTest.scala new file mode 100644 index 0000000000..ceb8c8c666 --- /dev/null +++ b/streampark-common/src/test/scala/org/apache/streampark/common/util/UtilsTest.scala @@ -0,0 +1,68 @@ +/* + * 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.streampark.common.util + + +import org.junit.jupiter.api.Assertions.{assertDoesNotThrow, assertEquals, assertFalse, assertThrows, assertTrue} +import org.junit.jupiter.api.{Assertions, Test} + +import java.io.IOException +import java.net.URL +import java.util + +class UtilsTest { + @Test def requiredNotNullTest(): Unit = { + val nullPointerException = assertThrows( + classOf[NullPointerException], + () => Utils.requireNotNull(null, "object can't be null")) + assertEquals("object can't be null", nullPointerException.getMessage) + } + + @Test def requireNotEmpty(): Unit = { + assertFalse(Utils.requireNotEmpty(null)) + assertTrue(Utils.requireNotEmpty(new Array[Int](1))) + assertTrue(Utils.requireNotEmpty("string")) + assertTrue(Utils.requireNotEmpty(Traversable.canBuildFrom("Traversable"))) + assertTrue(Utils.requireNotEmpty(Iterable.canBuildFrom("Iterable"))) + + val arrayList = new util.ArrayList[String](16) + arrayList.add("arrayList") + assertTrue(Utils.requireNotEmpty(arrayList)) + + val hashMap = new util.HashMap[String, String](16) + hashMap.put("hash", "map") + assertTrue(Utils.requireNotEmpty(hashMap)) + + assertTrue(Utils.requireNotEmpty()) + } + + @Test def requiredTest(): Unit = { + assertThrows(classOf[IllegalArgumentException], ()=>Utils.required(false)) + } + + @Test def requireCheckJarFileTest(): Unit = { + val jar :URL = new URL("http", "host", "file") + val ioException = assertThrows(classOf[IOException], ()=>Utils.requireCheckJarFile(jar)) + assertEquals("JAR file path is invalid " + jar.toString, ioException.getMessage) + } + + @Test def checkHttpURLTest(): Unit = { + val url = "http://localhost" + assertFalse(Utils.checkHttpURL(url)) + } +}