Skip to content

Commit

Permalink
Improve add Utils test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkezhou1 committed Jan 28, 2024
1 parent b0c565b commit 1d22cde
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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"))
}
}
Original file line number Diff line number Diff line change
@@ -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))
}

}
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 1d22cde

Please sign in to comment.