-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathInterfaceSatisfactionTest.java
More file actions
138 lines (105 loc) · 5.25 KB
/
InterfaceSatisfactionTest.java
File metadata and controls
138 lines (105 loc) · 5.25 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
130
131
132
133
134
135
136
137
138
package com.theoryinpractise.halbuilder.json;
import com.theoryinpractise.halbuilder.api.Contract;
import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
import com.theoryinpractise.halbuilder.api.RepresentationException;
import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.impl.bytecode.InterfaceContract;
import java.io.InputStreamReader;
import java.util.Map.Entry;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.fail;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class InterfaceSatisfactionTest {
private RepresentationFactory representationFactory = new JsonRepresentationFactory();
public static interface IPerson {
Integer getAge();
Boolean getExpired();
Integer getId();
String getName();
}
public static interface INamed {
String name();
}
public static interface IJob {
Integer getJobId();
}
public static interface ISimpleJob {
Integer jobId();
}
public static interface INullprop {
String nullprop();
}
@DataProvider
public Object[][] providerSatisfactionData() {
return new Object[][]{
{IPerson.class, true},
{INamed.class, true},
{IJob.class, false},
{ISimpleJob.class, false},
};
}
@DataProvider
public Object[][] provideSatisfactionResources() {
return new Object[][]{
{representationFactory.readRepresentation(new InputStreamReader(InterfaceSatisfactionTest.class.getResourceAsStream("/example.json"))), representationFactory.readRepresentation(new InputStreamReader(InterfaceSatisfactionTest.class.getResourceAsStream("/exampleWithNullProperty.json")))}};
}
@Test(dataProvider = "providerSatisfactionData")
public void testSimpleInterfaceSatisfaction(Class<?> aClass, boolean shouldBeSatisfied) {
ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(InterfaceSatisfactionTest.class.getResourceAsStream("/example.json")));
assertThat(representation.isSatisfiedBy(InterfaceContract.newInterfaceContract(aClass))).isEqualTo(shouldBeSatisfied);
}
@Test(dataProvider = "provideSatisfactionResources")
public void testAnonymousInnerContractSatisfaction(ReadableRepresentation representation, ReadableRepresentation nullPropertyRepresentation) {
Contract contractHasName = new Contract() {
public boolean isSatisfiedBy(ReadableRepresentation resource) {
return resource.getProperties().containsKey("name");
}
};
Contract contractHasOptional = new Contract() {
public boolean isSatisfiedBy(ReadableRepresentation resource) {
return resource.getProperties().containsKey("optional");
}
};
Contract contractHasOptionalFalse = new Contract() {
public boolean isSatisfiedBy(ReadableRepresentation resource) {
return resource.getProperties().containsKey("optional") && resource.getProperties().get("optional").equals("false");
}
};
Contract contractHasNullProperty = new Contract() {
public boolean isSatisfiedBy(ReadableRepresentation resource) {
return resource.getProperties().containsKey("nullprop") && resource.getProperties().get("nullprop") == null;
}
};
assertThat(representation.isSatisfiedBy(contractHasName)).isEqualTo(true);
assertThat(representation.isSatisfiedBy(contractHasOptional)).isEqualTo(true);
assertThat(representation.isSatisfiedBy(contractHasOptionalFalse)).isEqualTo(false);
assertThat(representation.isSatisfiedBy(contractHasNullProperty)).isEqualTo(false);
assertThat(nullPropertyRepresentation.isSatisfiedBy(contractHasNullProperty)).isEqualTo(true);
}
@Test
public void testClassRendering() {
ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(InterfaceSatisfactionTest.class.getResourceAsStream("/example.json")));
assertThat(representation.toClass(INamed.class).name()).isEqualTo("Example Resource");
assertThat(representation.toClass(IPerson.class).getName()).isEqualTo("Example Resource");
try {
representation.toClass(ISimpleJob.class);
fail("RepresentationException expected");
} catch (RepresentationException e) {
//
}
try {
representation.toClass(IJob.class);
fail("RepresentationException expected");
} catch (RepresentationException e) {
//
}
}
@Test
public void testNullPropertyClassRendering() {
ReadableRepresentation representation = representationFactory.readRepresentation(new InputStreamReader(InterfaceSatisfactionTest.class.getResourceAsStream("/exampleWithNullProperty.json")));
assertThat(representation.toClass(INullprop.class)).isNotNull();
assertThat(representation.toClass(INullprop.class).nullprop() == null);
}
}