-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathObjectUtilsTest.java
More file actions
129 lines (113 loc) · 2.82 KB
/
ObjectUtilsTest.java
File metadata and controls
129 lines (113 loc) · 2.82 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
}
@Test
void isIn()
{
Integer[] array = {1, 2, 3};
assertTrue(ObjectUtils.isIn(1, array));
}
private int methodThatMightThrowCheckedExceptionWithReturnValue() throws Exception
{
return 1;
}
private void methodThatMightThrowError() throws Error
{
}
private void methodThatMightThrowRuntimeException() throws RuntimeException
{
}
private void methodThatMightThrowCheckedException() throws Exception
{
}
}