-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BIGTOP-4335: Add ut cases for utils classes in common module (#155)
- Loading branch information
Showing
8 changed files
with
878 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...op-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/CaseUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.bigtop.manager.common.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
public class CaseUtilsTest { | ||
|
||
@Test | ||
public void testToLowerCase() { | ||
// Normal case | ||
assertEquals("hello world", CaseUtils.toLowerCase("Hello World")); | ||
// Input is an empty string | ||
assertEquals("", CaseUtils.toLowerCase("")); | ||
// Input is null | ||
assertNull(CaseUtils.toLowerCase(null)); | ||
} | ||
|
||
@Test | ||
public void testToUpperCase() { | ||
// Normal case | ||
assertEquals("HELLO WORLD", CaseUtils.toUpperCase("Hello World")); | ||
// Input is an empty string | ||
assertEquals("", CaseUtils.toUpperCase("")); | ||
// Input is null | ||
assertNull(CaseUtils.toUpperCase(null)); | ||
} | ||
|
||
@Test | ||
public void testToCamelCase() { | ||
// Normal case | ||
assertEquals("HelloWorld", CaseUtils.toCamelCase("hello-world")); | ||
// Using underscore separator | ||
assertEquals("HelloWorld", CaseUtils.toCamelCase("hello_world", CaseUtils.SEPARATOR_UNDERSCORE)); | ||
// First letter lowercase | ||
assertEquals("helloWorld", CaseUtils.toCamelCase("hello-world", CaseUtils.SEPARATOR_HYPHEN, false)); | ||
// Input is an empty string | ||
assertEquals("", CaseUtils.toCamelCase("")); | ||
// Input is null | ||
assertNull(CaseUtils.toCamelCase(null)); | ||
} | ||
|
||
@Test | ||
public void testToHyphenCase() { | ||
// Normal case | ||
assertEquals("hello-world", CaseUtils.toHyphenCase("HelloWorld")); | ||
// Input is an empty string | ||
assertEquals("", CaseUtils.toHyphenCase("")); | ||
// Input is null | ||
assertNull(CaseUtils.toHyphenCase(null)); | ||
} | ||
|
||
@Test | ||
public void testToUnderScoreCase() { | ||
// Normal case | ||
assertEquals("hello_world", CaseUtils.toUnderScoreCase("HelloWorld")); | ||
// Input is an empty string | ||
assertEquals("", CaseUtils.toUnderScoreCase("")); | ||
// Input is null | ||
assertNull(CaseUtils.toUnderScoreCase(null)); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...p-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/ClassUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.bigtop.manager.common.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class ClassUtilsTest { | ||
|
||
// Test case for a normal class | ||
@Test | ||
public void testGetFieldsNormalClass() { | ||
List<Field> fields = ClassUtils.getFields(TestClass.class); | ||
assertEquals(3, fields.size()); | ||
} | ||
|
||
// Test case for a class with no fields | ||
@Test | ||
public void testGetFieldsNoFieldsClass() { | ||
List<Field> fields = ClassUtils.getFields(NoFieldsClass.class); | ||
assertEquals(0, fields.size()); | ||
} | ||
|
||
// Test case for an inherited class | ||
@Test | ||
public void testGetFieldsInheritedClass() { | ||
List<Field> fields = ClassUtils.getFields(InheritedClass.class); | ||
assertEquals(5, fields.size()); | ||
} | ||
|
||
// Test case for an interface | ||
@Test | ||
public void testGetFieldsInterface() { | ||
List<Field> fields = ClassUtils.getFields(TestInterface.class); | ||
assertEquals(0, fields.size()); | ||
} | ||
|
||
// Test case for null input | ||
@Test | ||
public void testGetFieldsNullInput() { | ||
List<Field> fields = ClassUtils.getFields(null); | ||
assertEquals(0, fields.size()); | ||
} | ||
|
||
// Test case for a primitive type | ||
@Test | ||
public void testGetFieldsPrimitiveType() { | ||
List<Field> fields = ClassUtils.getFields(int.class); | ||
assertEquals(0, fields.size()); // int is a primitive type, which has no fields | ||
} | ||
|
||
// Test case for a wrapper type | ||
@Test | ||
public void testGetFieldsWrapperType() { | ||
List<Field> fields = ClassUtils.getFields(Integer.class); | ||
assertTrue(fields.size() > 0); | ||
} | ||
|
||
// Helper test class | ||
private static class TestClass { | ||
private int field1; | ||
private String field2; | ||
private double field3; | ||
} | ||
|
||
// Helper test class with no fields | ||
private static class NoFieldsClass {} | ||
|
||
// Helper test class that inherits from TestClass | ||
private static class InheritedClass extends TestClass { | ||
private boolean field4; | ||
private char field5; | ||
} | ||
|
||
// Helper test interface | ||
private static interface TestInterface {} | ||
} |
87 changes: 87 additions & 0 deletions
87
...op-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/DateUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.bigtop.manager.common.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.Timestamp; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class DateUtilsTest { | ||
|
||
@Test | ||
public void testFormatTimestamp() { | ||
Timestamp timestamp = Timestamp.valueOf("2023-01-01 12:00:00"); | ||
|
||
String formatted = DateUtils.format(timestamp); | ||
assertEquals("2023-01-01 12:00:00", formatted); | ||
|
||
String customFormatted = DateUtils.format(timestamp, "yyyy-MM-dd"); | ||
assertEquals("2023-01-01", customFormatted); | ||
} | ||
|
||
@Test | ||
public void testFormatDate() { | ||
Date date = new Date(1672545600000L); | ||
TimeZone systemTimeZone = TimeZone.getDefault(); | ||
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
sdf.setTimeZone(systemTimeZone); | ||
String expectedFormatted = sdf.format(date); | ||
String formatted = DateUtils.format(date); | ||
assertEquals(expectedFormatted, formatted); | ||
|
||
SimpleDateFormat sdfSystem = new SimpleDateFormat("yyyy-MM-dd"); | ||
sdfSystem.setTimeZone(systemTimeZone); | ||
String expectedCustomFormatted = sdfSystem.format(date); | ||
String customFormatted = DateUtils.format(date, "yyyy-MM-dd"); | ||
assertEquals(expectedCustomFormatted, customFormatted); | ||
} | ||
|
||
@Test | ||
public void testFormatNullTimestamp() { | ||
String formatted = DateUtils.format((Timestamp) null); | ||
assertEquals("", formatted); | ||
} | ||
|
||
@Test | ||
public void testFormatNullDate() { | ||
String formatted = DateUtils.format((Date) null); | ||
assertEquals("", formatted); | ||
} | ||
|
||
@Test | ||
public void testFormatWithNullPattern() { | ||
Date date = new Date(1672531200000L); | ||
assertThrows(NullPointerException.class, () -> { | ||
DateUtils.format(date, null); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testFormatWithEmptyStringWhenDateIsNull() { | ||
String result = DateUtils.format((Date) null, "yyyy-MM-dd HH:mm:ss"); | ||
assertEquals("", result); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...op-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/FileUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.bigtop.manager.common.utils; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class FileUtilsTest { | ||
private File testFile; | ||
|
||
@BeforeEach | ||
void setUp() throws IOException { | ||
// Create a test file | ||
testFile = new File("testFile.txt"); | ||
try (FileWriter writer = new FileWriter(testFile, StandardCharsets.UTF_8)) { | ||
writer.write("test file content"); | ||
} | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
// Delete test file | ||
if (testFile.exists()) { | ||
testFile.delete(); | ||
} | ||
} | ||
|
||
@Test | ||
void readFile2StrNormalCase() { | ||
// Test normal case | ||
String result = FileUtils.readFile2Str(testFile); | ||
assertEquals("test file content", result); | ||
} | ||
|
||
@Test | ||
void readFile2StrEmptyFile() throws IOException { | ||
// Test empty file | ||
try (FileWriter writer = new FileWriter(testFile, StandardCharsets.UTF_8)) { | ||
writer.write(""); | ||
} | ||
String result = FileUtils.readFile2Str(testFile); | ||
assertEquals("", result); | ||
} | ||
|
||
@Test | ||
void readFile2StrFileDoesNotExist() { | ||
// Test file does not exist case | ||
File nonExistentFile = new File("nonExistentFile.txt"); | ||
Exception exception = assertThrows(RuntimeException.class, () -> { | ||
FileUtils.readFile2Str(nonExistentFile); | ||
}); | ||
assertNotNull(exception); | ||
assertTrue(exception.getMessage().contains("nonExistentFile.txt")); | ||
} | ||
|
||
@Test | ||
void readFile2StrFileContainsSpecialCharacters() throws IOException { | ||
// Test file containing special characters | ||
try (FileWriter writer = new FileWriter(testFile, StandardCharsets.UTF_8)) { | ||
writer.write("test file content\nwith newline\tand tab"); | ||
} | ||
String result = FileUtils.readFile2Str(testFile); | ||
assertEquals("test file content\nwith newline\tand tab", result); | ||
} | ||
|
||
@Test | ||
void readFile2StrFileContainsChineseCharacters() throws IOException { | ||
// Test file containing Chinese characters | ||
try (FileWriter writer = new FileWriter(testFile, StandardCharsets.UTF_8)) { | ||
writer.write("测试文件内容"); | ||
} | ||
String result = FileUtils.readFile2Str(testFile); | ||
assertEquals("测试文件内容", result); | ||
} | ||
} |
Oops, something went wrong.