Skip to content

Commit 82c9ff1

Browse files
time operations (#177)
* date operation * time utils * junit test
1 parent eef7b46 commit 82c9ff1

20 files changed

+239
-9
lines changed

src/main/java/com/examplehub/basics/MathExample.java renamed to src/main/java/com/examplehub/basics/math/MathExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.examplehub.basics;
1+
package com.examplehub.basics.math;
22

33
public class MathExample {
44
public static void main(String[] args) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.system;
2+
3+
public class FileSeparatorTest {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.time;
2+
3+
public class CalendarExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.time;
2+
3+
public class DateExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.time;
2+
3+
public class GetTime {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.time;
2+
3+
public class InstantExample {
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.examplehub.basics.time;
2+
3+
public class LocalDateTimeExample {
4+
5+
}

src/main/java/com/examplehub/utils/StringUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ public static String toString(byte[] bytes, int offset, int len) {
1616
}
1717
return builder.toString();
1818
}
19+
20+
public static String capitalize(String origin) {
21+
if (origin == null) {
22+
throw new NullPointerException();
23+
}
24+
if (origin.length() == 0) {
25+
return "";
26+
}
27+
return Character.toUpperCase(origin.charAt(0)) + origin.substring(1).toLowerCase();
28+
}
1929
}

src/test/java/com/examplehub/basics/file/CreateFolderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import org.junit.jupiter.api.Disabled;
56
import org.junit.jupiter.api.Test;
67

78
class CreateFolderTest {
9+
10+
@Disabled
811
@Test
912
void test() {
1013
String path = "example_folder";

src/test/java/com/examplehub/basics/floats/FloatOperationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class FloatOperationTest {
99
void testCompare() {
1010
assertEquals(0.1, 1.0 / 10);
1111
assertTrue(0.1 > 1 - 9.0 / 10);
12+
assertEquals(0.1, 1 - 9 / 10.0, 0.00000001);
1213
}
1314

1415
@Test

src/test/java/com/examplehub/basics/ints/IntOperationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.*;
44

55
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.function.Executable;
67

78
class IntOperationTest {
89
@Test
@@ -18,6 +19,10 @@ void testDivide() {
1819
assertEquals(5, 10 / 2);
1920
assertEquals(2, 7 / 3);
2021
assertEquals(164, 12345 / 75);
22+
23+
assertThrows(ArithmeticException.class, () -> {
24+
int division = 10 / 0;
25+
});
2126
}
2227

2328
@Test
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.examplehub.basics.system;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.condition.EnabledOnOs;
5+
import org.junit.jupiter.api.condition.OS;
6+
7+
import java.io.File;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class FileSeparatorTestTest {
12+
@Test
13+
@EnabledOnOs({OS.LINUX, OS.MAC, OS.MAC})
14+
void testOnUnixLike() {
15+
assertEquals("/", File.separator);
16+
}
17+
@Test
18+
@EnabledOnOs(OS.WINDOWS)
19+
void testOnWindows() {
20+
assertEquals("\\", File.separator);
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.examplehub.basics.time;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.text.SimpleDateFormat;
6+
import java.util.Calendar;
7+
8+
class CalendarExampleTest {
9+
@Test
10+
void test() {
11+
Calendar calendar = Calendar.getInstance();
12+
System.out.println("year: " + calendar.get(Calendar.YEAR));
13+
System.out.println("month: " + calendar.get(Calendar.MONTH) + 1);
14+
System.out.println("day of month: " + calendar.get(Calendar.DAY_OF_MONTH));
15+
System.out.println("day of week: " + calendar.get(Calendar.DAY_OF_WEEK));
16+
System.out.println("day of year: " + calendar.get(Calendar.DAY_OF_YEAR));
17+
System.out.println("hour of day: " + calendar.get(Calendar.HOUR));
18+
System.out.println("hour of day: " + calendar.get(Calendar.HOUR_OF_DAY));
19+
System.out.println("minute: " + calendar.get(Calendar.MINUTE));
20+
System.out.println("second: " + calendar.get(Calendar.SECOND));
21+
System.out.println("million seconds: " + calendar.get(Calendar.MILLISECOND));
22+
}
23+
24+
@Test
25+
void testSetCustomTime() {
26+
Calendar calendar = Calendar.getInstance();
27+
calendar.clear();
28+
calendar.set(Calendar.YEAR, 2088);
29+
calendar.set(Calendar.MONTH, 8);
30+
calendar.set(Calendar.DATE, 25);
31+
calendar.set(Calendar.HOUR_OF_DAY, 16);
32+
calendar.set(Calendar.MINUTE, 33);
33+
calendar.set(Calendar.SECOND, 59);
34+
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.examplehub.basics.time;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
8+
class DateExampleTest {
9+
@Test
10+
void test() {
11+
Date date = new Date();
12+
System.out.println(date.getYear() + 1900);
13+
System.out.println(date.getMonth() + 1);
14+
System.out.println(date.getDate());
15+
System.out.println(date);
16+
System.out.println(date.toGMTString());
17+
System.out.println(date.toLocaleString());
18+
}
19+
20+
@Test
21+
void testSimpleDateForMate() {
22+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
23+
System.out.println(format.format(new Date()));
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.examplehub.basics.time;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class GetTimeTest {
6+
@Test
7+
void test() {
8+
System.out.println(System.currentTimeMillis());
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.basics.time;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.time.Instant;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class InstantExampleTest {
10+
@Test
11+
void test() {
12+
Instant now = Instant.now();
13+
System.out.println(now);
14+
System.out.println(now.toEpochMilli());
15+
System.out.println(now.getEpochSecond());
16+
}
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.examplehub.basics.time;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
7+
import java.time.LocalTime;
8+
9+
class LocalDateTimeExampleTest {
10+
@Test
11+
void test() {
12+
LocalDate localDate = LocalDate.now();
13+
LocalTime localTime = LocalTime.now();
14+
LocalDateTime localDateTime = LocalDateTime.now();
15+
System.out.println("localDate: " + localDate);
16+
System.out.println("localTime: " + localTime);
17+
System.out.println("localDateTime: " + localDateTime);
18+
}
19+
20+
@Test
21+
void test2() {
22+
LocalDateTime localDateTime = LocalDateTime.now();
23+
LocalDate localDate = localDateTime.toLocalDate();
24+
LocalTime localTime = localDateTime.toLocalTime();
25+
System.out.println("localDate: " + localDate);
26+
System.out.println("localTime: " + localTime);
27+
System.out.println("localDateTime: " + localDateTime);
28+
}
29+
30+
@Test
31+
void test3() {
32+
LocalDate d2 = LocalDate.of(2019, 11, 30);
33+
LocalTime t2 = LocalTime.of(15, 16, 17);
34+
LocalDateTime dt2 = LocalDateTime.of(2019, 11, 30, 15, 16, 17);
35+
LocalDateTime dt3 = LocalDateTime.of(d2, t2);
36+
System.out.println(d2);
37+
System.out.println(t2);
38+
System.out.println(dt2);
39+
System.out.println(dt3);
40+
}
41+
}

src/test/java/com/examplehub/basics/utils/MathUtilsExampleTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
import static org.junit.jupiter.api.Assertions.*;
44

55
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.ArgumentsSource;
8+
import org.junit.jupiter.params.provider.ValueSource;
69

710
class MathUtilsExampleTest {
8-
@Test
9-
void testAbs() {
10-
assertEquals(100, Math.abs(-100));
11-
assertEquals(100, Math.abs(100));
12-
assertEquals(7.8, Math.abs(-7.8));
11+
12+
@ParameterizedTest
13+
@ValueSource(ints = {0, 1, 10, 99, 1000, 9999999})
14+
void testAbs(int num) {
15+
assertEquals(num, Math.abs(num));
16+
}
17+
18+
@ParameterizedTest
19+
@ValueSource(ints = {-10, -5, -1000})
20+
void testNegativeAbs(int num) {
21+
assertEquals(-num, Math.abs(num));
1322
}
1423

1524
@Test

src/test/java/com/examplehub/sorts/BubbleSortTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.examplehub.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertTrue;
4-
53
import com.examplehub.domain.Student;
64
import com.examplehub.utils.RandomUtils;
75
import com.examplehub.utils.SortUtils;
8-
import java.util.Arrays;
96
import org.junit.jupiter.api.BeforeEach;
107
import org.junit.jupiter.api.Test;
118

9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
1213
class BubbleSortTest {
1314

1415
private Sort sort;

src/test/java/com/examplehub/utils/StringUtilsTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import org.junit.jupiter.api.Disabled;
56
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.CsvSource;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
import java.util.List;
613

714
class StringUtilsTest {
815
@Test
@@ -16,4 +23,22 @@ void testCharsToString() {
1623
char[] bytes = {'1', '2', '3', '4', '\0', '\0', '\0'};
1724
assertEquals("234", StringUtils.toString(bytes, 1, 3));
1825
}
26+
27+
@ParameterizedTest
28+
@MethodSource
29+
void testCapitalize(String input, String excepted) {
30+
assertEquals(excepted, StringUtils.capitalize(input));
31+
}
32+
33+
static List<Arguments> testCapitalize() {
34+
return List.of(Arguments.arguments("abc", "Abc"), //
35+
Arguments.arguments("APPLE", "Apple"), //
36+
Arguments.arguments("gooD", "Good"));
37+
}
38+
39+
@ParameterizedTest
40+
@CsvSource({"abc, Abc", "APPLE, Apple", "gooD, Good"})
41+
void testCapitalizeCvsSource(String input, String excepted) {
42+
assertEquals(excepted, StringUtils.capitalize(input));
43+
}
1944
}

0 commit comments

Comments
 (0)