Skip to content

Commit f6664f8

Browse files
authored
Merge pull request #131 from dblevins/annotation-symmetry
Annotation symmetry tests
2 parents ae36bf7 + e91eff0 commit f6664f8

File tree

52 files changed

+4891
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+4891
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.johnzon.jsonb.symmetry;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
public class Calls {
23+
24+
private final List<String> calls = new ArrayList<>();
25+
26+
public String called() {
27+
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
28+
29+
if (stackTrace.length > 2) {
30+
final StackTraceElement caller = stackTrace[2];
31+
final String className = caller.getClassName();
32+
final String methodName = caller.getMethodName();
33+
34+
final int lastDot = className.lastIndexOf('.');
35+
final int lastDollar = className.lastIndexOf('$');
36+
final String simpleName;
37+
if (lastDollar == 0 && lastDot == 0) {
38+
simpleName = className;
39+
} else {
40+
final int start = Math.max(lastDollar, lastDot) + 1;
41+
simpleName = className.substring(start);
42+
}
43+
44+
final String result = simpleName + "." + methodName;
45+
calls.add(result);
46+
return result;
47+
}
48+
return null;
49+
}
50+
51+
public String called(final Object instance) {
52+
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
53+
54+
if (stackTrace.length > 2) {
55+
final StackTraceElement caller = stackTrace[2];
56+
final String simpleName = instance.getClass().getSimpleName();
57+
final String methodName = caller.getMethodName();
58+
59+
60+
final String result = simpleName + "." + methodName;
61+
calls.add(result);
62+
return result;
63+
}
64+
return null;
65+
}
66+
67+
public List<String> list() {
68+
return new ArrayList<>(calls);
69+
}
70+
71+
public String get() {
72+
final String result = String.join("\n", calls);
73+
calls.clear();
74+
return result;
75+
}
76+
77+
public void reset() {
78+
calls.clear();
79+
}
80+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.johnzon.jsonb.symmetry;
18+
19+
import jakarta.json.bind.Jsonb;
20+
import org.junit.FixMethodOrder;
21+
import org.junit.Test;
22+
import org.junit.runners.MethodSorters;
23+
24+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
25+
public abstract class SymmetryTest {
26+
27+
public abstract Jsonb jsonb();
28+
29+
public abstract void assertWrite(final Jsonb jsonb);
30+
31+
public abstract void assertRead(final Jsonb jsonb);
32+
33+
/**
34+
* Assert a simple write operation
35+
*/
36+
@Test
37+
public void write() throws Exception {
38+
try (final Jsonb jsonb = jsonb()) {
39+
assertWrite(jsonb);
40+
}
41+
}
42+
43+
/**
44+
* Assert a simple read operation
45+
*/
46+
@Test
47+
public void read() throws Exception {
48+
try (final Jsonb jsonb = jsonb()) {
49+
assertRead(jsonb);
50+
}
51+
}
52+
53+
/**
54+
* Validate any caching done from a write operation
55+
* leads to a consistent result on any future
56+
* write operations
57+
*/
58+
@Test
59+
public void writeAfterWrite() throws Exception {
60+
try (final Jsonb jsonb = jsonb()) {
61+
assertWrite(jsonb);
62+
assertWrite(jsonb);
63+
assertWrite(jsonb);
64+
assertWrite(jsonb);
65+
}
66+
}
67+
68+
/**
69+
* Validate any caching done from a read operation
70+
* leads to a consistent result on any future
71+
* read operations
72+
*/
73+
@Test
74+
public void readAfterRead() throws Exception {
75+
try (final Jsonb jsonb = jsonb()) {
76+
assertRead(jsonb);
77+
assertRead(jsonb);
78+
assertRead(jsonb);
79+
assertRead(jsonb);
80+
}
81+
}
82+
83+
/**
84+
* Validate any caching done from a read operation
85+
* does not alter the expected behavior of a write
86+
* operation
87+
*/
88+
@Test
89+
public void writeAfterRead() throws Exception {
90+
try (final Jsonb jsonb = jsonb()) {
91+
assertRead(jsonb);
92+
assertWrite(jsonb);
93+
assertRead(jsonb);
94+
assertWrite(jsonb);
95+
}
96+
}
97+
98+
/**
99+
* Validate any caching done from a write operation
100+
* does not alter the expected behavior of a read
101+
* operation
102+
*/
103+
@Test
104+
public void readAfterWrite() throws Exception {
105+
try (final Jsonb jsonb = jsonb()) {
106+
assertWrite(jsonb);
107+
assertRead(jsonb);
108+
assertWrite(jsonb);
109+
assertRead(jsonb);
110+
}
111+
}
112+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.johnzon.jsonb.symmetry.adapter.array;
18+
19+
import jakarta.json.bind.Jsonb;
20+
import jakarta.json.bind.JsonbBuilder;
21+
import org.junit.Ignore;
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
public class ArrayAdapterOnClassDirectTest extends ArrayAdapterOnClassTest {
27+
28+
public Jsonb jsonb() {
29+
return JsonbBuilder.create();
30+
}
31+
32+
public void assertWrite(final Jsonb jsonb) {
33+
final Email email = new Email("test", "domain.com");
34+
final String json = jsonb.toJson(email);
35+
assertEquals("[\"test\",\"domain.com\",\"EmailClass.adaptToJson\"]", json);
36+
assertEquals("EmailClass.adaptToJson", calls());
37+
}
38+
39+
public void assertRead(final Jsonb jsonb) {
40+
final String json = "[\"test\",\"domain.com\"]";
41+
final Email email = jsonb.fromJson(json, Email.class);
42+
assertEquals("test@domain.com:EmailClass.adaptFromJson", email.toString());
43+
assertEquals("EmailClass.adaptFromJson", calls());
44+
}
45+
46+
/**
47+
* Fails as the adapter is not found
48+
*/
49+
@Test
50+
@Ignore()
51+
@Override
52+
public void read() throws Exception {
53+
super.read();
54+
}
55+
56+
/**
57+
* Fails as the adapter is not found
58+
*/
59+
@Test
60+
@Ignore()
61+
@Override
62+
public void readAfterRead() throws Exception {
63+
super.readAfterRead();
64+
}
65+
66+
/**
67+
* Fails as the adapter is not found on the first read
68+
*/
69+
@Test
70+
@Ignore()
71+
@Override
72+
public void writeAfterRead() throws Exception {
73+
super.writeAfterRead();
74+
}
75+
76+
@Test
77+
@Ignore()
78+
@Override
79+
public void readAfterWrite() throws Exception {
80+
super.readAfterWrite();
81+
}
82+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.johnzon.jsonb.symmetry.adapter.array;
18+
19+
import jakarta.json.bind.Jsonb;
20+
import jakarta.json.bind.JsonbBuilder;
21+
22+
import static org.junit.Assert.assertEquals;
23+
24+
public class ArrayAdapterOnClassSimpleTest extends ArrayAdapterOnClassTest {
25+
26+
public Jsonb jsonb() {
27+
return JsonbBuilder.create();
28+
}
29+
30+
@Override
31+
public void assertRead(final Jsonb jsonb) {
32+
final String json = "{\"email\":[\"test\",\"domain.com\"]}";
33+
final ArrayAdapterPrecedenceConfigClassTest.Contact actual = jsonb.fromJson(json, ArrayAdapterPrecedenceConfigClassTest.Contact.class);
34+
assertEquals("Contact{email=test@domain.com:EmailClass.adaptFromJson}", actual.toString());
35+
assertEquals("Contact.<init>\n" +
36+
"EmailClass.adaptFromJson\n" +
37+
"Contact.setEmail", calls());
38+
}
39+
40+
@Override
41+
public void assertWrite(final Jsonb jsonb) {
42+
final Email email = new Email("test", "domain.com");
43+
final ArrayAdapterPrecedenceConfigClassTest.Contact contact = new ArrayAdapterPrecedenceConfigClassTest.Contact();
44+
contact.setEmail(email);
45+
reset();
46+
47+
final String json = jsonb.toJson(contact);
48+
assertEquals("{\"email\":[\"test\",\"domain.com\",\"EmailClass.adaptToJson\"]}", json);
49+
assertEquals("Contact.getEmail\n" +
50+
"EmailClass.adaptToJson", calls());
51+
}
52+
53+
public static class Contact {
54+
55+
private Email email;
56+
57+
public Contact() {
58+
CALLS.called();
59+
}
60+
61+
public Email getEmail() {
62+
CALLS.called();
63+
return email;
64+
}
65+
66+
public void setEmail(final Email email) {
67+
CALLS.called();
68+
this.email = email;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return String.format("Contact{email=%s}", email);
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)