-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathObjectUtilsTest.java
111 lines (107 loc) · 2.7 KB
/
ObjectUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.spun.util;
import org.junit.jupiter.api.Test;
import org.lambda.functions.Functions;
import org.lambda.query.Queryable;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ObjectUtilsTest
{
@Test
public void testGreatestCommonDenominator() throws Exception
{
Method gcd = ObjectUtils.getGreatestCommonDenominator(new Object[]{"this", new ArrayList<Object>()},
"getClass");
assertEquals(Object.class, gcd.getDeclaringClass());
}
@Test
public void test()
{
Date d = new Date();
UseCase o1 = new UseCase(1, "2", "odd", d);
UseCase o2 = new UseCase(1, "2", "even", d);
assertTrue(ObjectUtils.isEqualForMethods(o1, o2, "getA", "getB", "getD"));
assertFalse(ObjectUtils.isEqualForMethods(o1, o2, "getA", "getB", "getC"));
}
public static class UseCase
{
private int a;
private String b;
private String c;
private Date d;
public UseCase(int a, String b, String c, Date d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public int getA()
{
return a;
}
public String getB()
{
return b;
}
public String getC()
{
return c;
}
public Date getD()
{
return d;
}
}
@Test
void throwAsErrorExample()
{
// begin-snippet: throw_as_error
try
{
methodThatMightThrowCheckedException();
methodThatMightThrowRuntimeException();
methodThatMightThrowError();
}
catch (Throwable t)
{
throw ObjectUtils.throwAsError(t);
}
// end-snippet
}
@Test
void throwLambdaExecution()
{
// begin-snippet: throw_as_error_lambda
ObjectUtils.throwAsError(() -> methodThatMightThrowCheckedException());
int i = ObjectUtils.throwAsError(() -> methodThatMightThrowCheckedExceptionWithReturnValue());
// end-snippet
}
@Test
void uncheckedLambdas()
{
Queryable<String> files = Queryable.as("1.txt", "2.txt", "3.txt");
// begin-snippet: throw_as_unchecked
Queryable<String> paths = files.select(Functions.unchecked(
// throws IOException
s -> new File(s).getCanonicalPath()));
// end-snippet
}
private int methodThatMightThrowCheckedExceptionWithReturnValue() throws Exception
{
return 1;
}
private void methodThatMightThrowError() throws Error
{
}
private void methodThatMightThrowRuntimeException() throws RuntimeException
{
}
private void methodThatMightThrowCheckedException() throws Exception
{
}
}