Skip to content

Commit

Permalink
BIGTOP-4335: Add ut cases for utils classes in common module (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
xianrenzw authored Jan 29, 2025
1 parent 515aabd commit c897f1c
Show file tree
Hide file tree
Showing 8 changed files with 878 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static String getProjectBaseDir() {
}
}

private static String getProjectStoreDir() {
protected static String getProjectStoreDir() {
String path = SystemUtils.getUserHome().getPath() + File.separator + ".bigtop-manager";
Path p = Paths.get(path);
if (!Files.exists(p)) {
Expand Down
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));
}
}
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 {}
}
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);
}
}
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);
}
}
Loading

0 comments on commit c897f1c

Please sign in to comment.