Skip to content

Commit 0dc3327

Browse files
authored
Add some tests for language elements (#32)
* Add test for method mechanisms * Add test for enumerations * Add test for packages in Java * Add test for optional
1 parent 26e3b6f commit 0dc3327

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package pl.mperor.lab.java.lang.element;
2+
3+
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
public class EnumTest {
10+
11+
@Test
12+
public void testEnumeration() {
13+
Assertions.assertEquals(0, Season.SPRING.ordinal());
14+
var hottestSeasons = Season.SUMMER;
15+
Assertions.assertTrue(hottestSeasons == Season.SUMMER);
16+
Assertions.assertEquals("FALL", Season.FALL.name());
17+
Assertions.assertEquals(Season.WINTER, Season.valueOf("WINTER"));
18+
Assertions.assertArrayEquals(new Month[]{Month.DECEMBER, Month.JANUARY, Month.FEBRUARY},
19+
Season.WINTER.getMonths());
20+
Assertions.assertEquals(4, Season.values().length);
21+
}
22+
23+
enum Season {
24+
SPRING(Month.MARCH, Month.APRIL, Month.MAY),
25+
SUMMER(Month.JUNE, Month.JULY, Month.AUGUST),
26+
FALL(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER),
27+
WINTER(Month.DECEMBER, Month.JANUARY, Month.FEBRUARY);
28+
29+
private final Month[] months;
30+
31+
Season(Month... months) {
32+
this.months = months;
33+
}
34+
35+
Month[] getMonths() {
36+
return Arrays.copyOf(months, months.length);
37+
}
38+
}
39+
40+
enum Month {
41+
JANUARY,
42+
FEBRUARY,
43+
MARCH,
44+
APRIL,
45+
MAY,
46+
JUNE,
47+
JULY,
48+
AUGUST,
49+
SEPTEMBER,
50+
OCTOBER,
51+
NOVEMBER,
52+
DECEMBER
53+
}
54+
55+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package pl.mperor.lab.java.lang.element;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Arrays;
7+
8+
public class MethodTest {
9+
10+
@Test
11+
public void testOverloading() {
12+
byte b = 1;
13+
Assertions.assertEquals(1, Overloading.countArgs(b), "Type Promotion");
14+
Assertions.assertEquals(1, Overloading.countArgs(1.0), "Different Parameter Types");
15+
Assertions.assertEquals(2, Overloading.countArgs(1, 'a'), "Different Number of Parameters");
16+
Assertions.assertEquals(3, Overloading.countArgs("one", "two", "three"), "Varargs");
17+
Assertions.assertEquals(0, Overloading.countArgs(), "Varargs");
18+
Assertions.assertEquals(1, new Overloading().countArgs('c'), "Not only static!");
19+
}
20+
21+
static class Overloading {
22+
static int countArgs(long l) {
23+
return 1;
24+
}
25+
26+
static int countArgs(double d) {
27+
return 1;
28+
}
29+
30+
static <T> int countArgs(long l, T t) {
31+
return 2;
32+
}
33+
34+
static <T> int countArgs(T... t) {
35+
return t != null ? t.length : 0;
36+
}
37+
38+
int countArgs(char c) {
39+
return 1;
40+
}
41+
}
42+
43+
@Test
44+
public void testParameterPassingMechanism() {
45+
int primitive = 0;
46+
ParameterPassing.primitiveByValue(primitive);
47+
Assertions.assertEquals(0, primitive);
48+
49+
String string = "original";
50+
ParameterPassing.immutableObjectByValue(string);
51+
Assertions.assertEquals("original", string);
52+
53+
int[] array = {1, 2, 3};
54+
ParameterPassing.mutableObjectByValue(array);
55+
Assertions.assertArrayEquals(new int[]{0, 0, 0}, array);
56+
}
57+
58+
static class ParameterPassing {
59+
static void primitiveByValue(int primitive) {
60+
primitive = 1;
61+
}
62+
63+
static void immutableObjectByValue(String immutable) {
64+
immutable = "";
65+
}
66+
67+
static void mutableObjectByValue(int[] array) {
68+
Arrays.fill(array, 0);
69+
}
70+
}
71+
72+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package pl.mperor.lab.java.lang.element;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Optional;
7+
8+
public class OptionalTest {
9+
10+
@Test
11+
public void testOptionalImperatively() {
12+
Assertions.assertFalse(SomeAPI.getNameById(0).isPresent());
13+
// Please for(get), do not use get, instead use orElseThrow() / orElse("default")
14+
Assertions.assertEquals("default", SomeAPI.getNameById(0).orElse("default"));
15+
Assertions.assertEquals("some name", SomeAPI.getNameById(1).orElseThrow());
16+
}
17+
18+
@Test
19+
public void testOptionalFunctionally() {
20+
SomeAPI.getNameById(1).ifPresentOrElse(
21+
name -> Assertions.assertEquals("some name", name),
22+
() -> Assertions.fail()
23+
);
24+
}
25+
26+
static class SomeAPI {
27+
28+
// 1. Return a reference if the value will always exist
29+
// 2. Return an Optional<T> if the value may or may not exist
30+
public static Optional<String> getNameById(long id) {
31+
if (id == 0) {
32+
return Optional.empty();
33+
}
34+
35+
return Optional.of("some name");
36+
}
37+
38+
// 3. Do not use Optional<T> for fields - overhead with little benefit
39+
private String config;
40+
41+
// 4. Do not user Optional<T> for method parameters - user overloading instead
42+
public void setConfig(String config) {
43+
this.config = config;
44+
}
45+
46+
public void setConfig() {
47+
setConfig("default");
48+
}
49+
}
50+
51+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pl.mperor.lab.java.lang.element;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.stream.Collectors;
8+
9+
public class PackageTest {
10+
11+
@Test
12+
public void testPackage() {
13+
Assertions.assertEquals("pl.mperor.lab.java.lang.element",
14+
PackageTest.class.getPackageName(),
15+
"Package is a box for classes");
16+
17+
Assertions.assertEquals("pl.mperor.lab.java.lang.element.PackageTest",
18+
PackageTest.class.getName(),
19+
"Package + class name => unique address");
20+
21+
Assertions.assertEquals("pl.mperor",
22+
Arrays.stream("mperor.pl".split("\\."))
23+
.sorted((_, _) -> -1)
24+
.collect(Collectors.joining(".")),
25+
"Packages use the reversed domain name");
26+
}
27+
}

0 commit comments

Comments
 (0)