Skip to content

Commit c755308

Browse files
committed
new tests for collections
1 parent f16738a commit c755308

15 files changed

Lines changed: 1053 additions & 0 deletions
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
D-Bus Java Implementation
3+
Copyright (c) 2019 Technolution BV
4+
5+
This program is free software; you can redistribute it and/or modify it
6+
under the terms of either the GNU Lesser General Public License Version 2 or the
7+
Academic Free Licence Version 2.1.
8+
9+
Full licence texts are included in the LICENSE file with this program.
10+
*/
11+
12+
package org.freedesktop.dbus.test.collections.empty;
13+
14+
import org.freedesktop.dbus.interfaces.DBusInterface;
15+
import org.freedesktop.dbus.test.collections.empty.structs.ArrayStructIntStruct;
16+
import org.freedesktop.dbus.test.collections.empty.structs.ArrayStructPrimative;
17+
import org.freedesktop.dbus.test.collections.empty.structs.DeepArrayStruct;
18+
import org.freedesktop.dbus.test.collections.empty.structs.DeepListStruct;
19+
import org.freedesktop.dbus.test.collections.empty.structs.DeepMapStruct;
20+
import org.freedesktop.dbus.test.collections.empty.structs.ListMapStruct;
21+
import org.freedesktop.dbus.test.collections.empty.structs.ListStructPrimative;
22+
import org.freedesktop.dbus.test.collections.empty.structs.ListStructStruct;
23+
import org.freedesktop.dbus.test.collections.empty.structs.MapArrayStruct;
24+
import org.freedesktop.dbus.test.collections.empty.structs.MapStructIntStruct;
25+
import org.freedesktop.dbus.test.collections.empty.structs.MapStructPrimative;
26+
27+
/**
28+
* A sample remote interface which implements same function for all of the structs
29+
*/
30+
public interface ISampleCollectionInterface extends DBusInterface {
31+
32+
String testListPrimative(ListStructPrimative param);
33+
34+
String testListIntStruct(ListStructStruct param);
35+
36+
String testDeepList(DeepListStruct param);
37+
38+
String testArrayPrimative(ArrayStructPrimative param);
39+
40+
String testArrayIntStruct(ArrayStructIntStruct param);
41+
42+
String testDeepArray(DeepArrayStruct param);
43+
44+
String testMapPrimative(MapStructPrimative param);
45+
46+
String testMapIntStruct(MapStructIntStruct param);
47+
48+
String testDeepMap(DeepMapStruct param);
49+
50+
String testMixedListMap(ListMapStruct param);
51+
52+
String testMixedMapArray(MapArrayStruct param);
53+
54+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
D-Bus Java Implementation
3+
Copyright (c) 2019 Technolution BV
4+
5+
This program is free software; you can redistribute it and/or modify it
6+
under the terms of either the GNU Lesser General Public License Version 2 or the
7+
Academic Free Licence Version 2.1.
8+
9+
Full licence texts are included in the LICENSE file with this program.
10+
*/
11+
package org.freedesktop.dbus.test.collections.empty;
12+
13+
import org.freedesktop.dbus.test.collections.empty.structs.ArrayStructIntStruct;
14+
import org.freedesktop.dbus.test.collections.empty.structs.ArrayStructPrimative;
15+
import org.freedesktop.dbus.test.collections.empty.structs.DeepArrayStruct;
16+
import org.freedesktop.dbus.test.collections.empty.structs.DeepListStruct;
17+
import org.freedesktop.dbus.test.collections.empty.structs.DeepMapStruct;
18+
import org.freedesktop.dbus.test.collections.empty.structs.IEmptyCollectionStruct;
19+
import org.freedesktop.dbus.test.collections.empty.structs.ListMapStruct;
20+
import org.freedesktop.dbus.test.collections.empty.structs.ListStructPrimative;
21+
import org.freedesktop.dbus.test.collections.empty.structs.ListStructStruct;
22+
import org.freedesktop.dbus.test.collections.empty.structs.MapArrayStruct;
23+
import org.freedesktop.dbus.test.collections.empty.structs.MapStructIntStruct;
24+
import org.freedesktop.dbus.test.collections.empty.structs.MapStructPrimative;
25+
26+
public class SampleCollectionImpl implements ISampleCollectionInterface {
27+
28+
@Override
29+
public String testListPrimative(ListStructPrimative param) {
30+
return testValue(param);
31+
}
32+
33+
@Override
34+
public String testListIntStruct(ListStructStruct param) {
35+
return testValue(param);
36+
}
37+
38+
@Override
39+
public String testDeepList(DeepListStruct param) {
40+
return testValue(param);
41+
}
42+
43+
@Override
44+
public String testArrayPrimative(ArrayStructPrimative param) {
45+
return testValue(param);
46+
47+
}
48+
49+
@Override
50+
public String testArrayIntStruct(ArrayStructIntStruct param) {
51+
return testValue(param);
52+
}
53+
54+
@Override
55+
public String testDeepArray(DeepArrayStruct param) {
56+
return testValue(param);
57+
}
58+
59+
@Override
60+
public String testMapPrimative(MapStructPrimative param) {
61+
return testValue(param);
62+
}
63+
64+
@Override
65+
public String testMapIntStruct(MapStructIntStruct param) {
66+
return testValue(param);
67+
}
68+
69+
@Override
70+
public String testDeepMap(DeepMapStruct param) {
71+
return testValue(param);
72+
}
73+
74+
@Override
75+
public String testMixedListMap(ListMapStruct param) {
76+
return testValue(param);
77+
}
78+
79+
@Override
80+
public String testMixedMapArray(MapArrayStruct param) {
81+
return testValue(param);
82+
}
83+
84+
@Override
85+
public boolean isRemote() {
86+
return false;
87+
}
88+
89+
@Override
90+
public String getObjectPath() {
91+
return "/org/dbus/test/EmptyCollections";
92+
}
93+
94+
private String testValue(IEmptyCollectionStruct<?> param) {
95+
if (param.getValue() == null) {
96+
throw new IllegalArgumentException("Incorrect param value");
97+
}
98+
return param.isEmpty() ? param.getValidationValue() : param.getStringTestValue();
99+
}
100+
101+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
D-Bus Java Implementation
3+
Copyright (c) 2019 Technolution BV
4+
5+
This program is free software; you can redistribute it and/or modify it
6+
under the terms of either the GNU Lesser General Public License Version 2 or the
7+
Academic Free Licence Version 2.1.
8+
9+
Full licence texts are included in the LICENSE file with this program.
10+
*/
11+
package org.freedesktop.dbus.test.collections.empty;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.function.BiFunction;
21+
import java.util.function.Function;
22+
import java.util.stream.Stream;
23+
24+
import org.freedesktop.dbus.connections.impl.DBusConnection;
25+
import org.freedesktop.dbus.connections.impl.DBusConnection.DBusBusType;
26+
import org.freedesktop.dbus.exceptions.DBusException;
27+
import org.freedesktop.dbus.exceptions.DBusExecutionException;
28+
import org.freedesktop.dbus.test.collections.empty.structs.ArrayStructIntStruct;
29+
import org.freedesktop.dbus.test.collections.empty.structs.ArrayStructPrimative;
30+
import org.freedesktop.dbus.test.collections.empty.structs.DeepArrayStruct;
31+
import org.freedesktop.dbus.test.collections.empty.structs.DeepListStruct;
32+
import org.freedesktop.dbus.test.collections.empty.structs.DeepMapStruct;
33+
import org.freedesktop.dbus.test.collections.empty.structs.IEmptyCollectionStruct;
34+
import org.freedesktop.dbus.test.collections.empty.structs.ListMapStruct;
35+
import org.freedesktop.dbus.test.collections.empty.structs.ListStructPrimative;
36+
import org.freedesktop.dbus.test.collections.empty.structs.ListStructStruct;
37+
import org.freedesktop.dbus.test.collections.empty.structs.MapArrayStruct;
38+
import org.freedesktop.dbus.test.collections.empty.structs.MapStructIntStruct;
39+
import org.freedesktop.dbus.test.collections.empty.structs.MapStructPrimative;
40+
import org.freedesktop.dbus.test.helper.structs.IntStruct;
41+
import org.junit.jupiter.api.AfterEach;
42+
import org.junit.jupiter.api.BeforeEach;
43+
import org.junit.jupiter.api.DisplayName;
44+
import org.junit.jupiter.params.ParameterizedTest;
45+
import org.junit.jupiter.params.provider.Arguments;
46+
import org.junit.jupiter.params.provider.MethodSource;
47+
48+
class TestEmptyCollections {
49+
50+
private DBusConnection serverconn;
51+
private DBusConnection clientconn;
52+
private ISampleCollectionInterface clientObj;
53+
54+
@BeforeEach
55+
public void setUp() throws DBusException {
56+
serverconn = DBusConnection.getConnection(DBusBusType.SESSION);
57+
clientconn = DBusConnection.getConnection(DBusBusType.SESSION);
58+
serverconn.setWeakReferences(true);
59+
clientconn.setWeakReferences(true);
60+
61+
/** This exports an instance of the test class as the object /Test. */
62+
ISampleCollectionInterface serverImpl = new SampleCollectionImpl();
63+
serverconn.exportObject(serverImpl.getObjectPath(), serverImpl);
64+
65+
clientObj = clientconn.getRemoteObject(serverconn.getUniqueName(), serverImpl.getObjectPath(),
66+
ISampleCollectionInterface.class);
67+
68+
}
69+
70+
@AfterEach
71+
public void tearDown() {
72+
DBusExecutionException dbee = serverconn.getError();
73+
if (null != dbee) {
74+
throw dbee;
75+
}
76+
dbee = clientconn.getError();
77+
if (null != dbee) {
78+
throw dbee;
79+
}
80+
clientconn.disconnect();
81+
serverconn.disconnect();
82+
}
83+
84+
/**
85+
* Parameterized test that collection will still know the next value. The Server
86+
* will throw error or return wrong string if the test fails
87+
*
88+
* @param arguments this contains the information required to build and call a
89+
* function
90+
* @param name the name is used for validation and naming purpose should
91+
* described the structure
92+
*/
93+
@DisplayName("testCollectionsEmpty")
94+
@ParameterizedTest(name = "{1}")
95+
@MethodSource("scenarios")
96+
<T extends IEmptyCollectionStruct<?>> void testEmpty(ArgumentObj<T> arguments, String name) {
97+
T object = arguments.factoryEmpty.apply(name);
98+
String result = arguments.function.apply(clientObj, object);
99+
assertEquals(object.getValidationValue(), result);
100+
}
101+
102+
/**
103+
* Parameterized test that collection will still know the next value. The Server
104+
* will throw error or return wrong string if the test fails
105+
*
106+
* @param arguments this contains the information required to build and call a
107+
* function
108+
* @param name the name is used for validation and naming purpose should
109+
* described the structure
110+
*/
111+
@DisplayName("testCollectionsNotEmpty")
112+
@ParameterizedTest(name = "{1}")
113+
@MethodSource("scenarios")
114+
<T extends IEmptyCollectionStruct<?>> void testNonEmpty(ArgumentObj<T> arguments, String name, String validationValue) {
115+
T object = arguments.factoryNonEmpty.apply(name);
116+
String result = arguments.function.apply(clientObj, object);
117+
assertEquals(validationValue, result);
118+
}
119+
120+
static Stream<Arguments> scenarios() {
121+
return Stream.of(
122+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testListPrimative,
123+
s -> new ListStructPrimative(Collections.emptyList(), s),
124+
s -> new ListStructPrimative(Arrays.asList(1, 2), s)), "ListPrimative", "1,2"),
125+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testListIntStruct,
126+
s -> new ListStructStruct(Collections.emptyList(), s),
127+
s -> new ListStructStruct(Arrays.asList(new IntStruct(5, 6)), s)), "ListStruct", "(5,6)"),
128+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testArrayPrimative,
129+
s -> new ArrayStructPrimative(new int[0], s),
130+
s -> new ArrayStructPrimative(new int[] {4,5}, s)), "ArrayPrimative", "4,5"),
131+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testArrayIntStruct,
132+
s -> new ArrayStructIntStruct(new IntStruct[0], s),
133+
s -> new ArrayStructIntStruct(new IntStruct[] { new IntStruct(9, 12)}, s)),
134+
"ArrayIntStruct", "(9,12)"),
135+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testMapPrimative,
136+
s -> new MapStructPrimative(Collections.emptyMap(), s),
137+
s -> new MapStructPrimative(getIntHashMap(), s)), "MapPrimative", "{test:8}"),
138+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testMapIntStruct,
139+
s -> new MapStructIntStruct(Collections.emptyMap(), s),
140+
s -> new MapStructIntStruct(getIntStructHashMap(), s)), "MapIntStruct", "{other:(12,17)}"),
141+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testDeepList,
142+
s -> new DeepListStruct(Collections.emptyList(), s),
143+
s -> new DeepListStruct(getDeepList(), s)), "DeepListStruct", "[[[(111,44)]]]"),
144+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testDeepArray,
145+
s -> new DeepArrayStruct(new IntStruct[0][][], s),
146+
s -> new DeepArrayStruct(getDeepArrayStruct(), s)), "DeepArrayStruct", "[[[(131,145)]]]"),
147+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testDeepMap,
148+
s -> new DeepMapStruct(Collections.emptyMap(), s),
149+
s -> new DeepMapStruct(getDeepMapStruct(), s)), "DeepMapStruct",
150+
"{complete:{inbetween:{test:(42,19)}}}"),
151+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testMixedListMap,
152+
s -> new ListMapStruct(Collections.emptyList(), s),
153+
s -> new ListMapStruct(Arrays.asList(getIntStructHashMap()), s)), "mixedListMapStruct",
154+
"[{other=(12,17)}]"),
155+
Arguments.of(new ArgumentObj<>(ISampleCollectionInterface::testMixedMapArray,
156+
s -> new MapArrayStruct(Collections.emptyMap(), s),
157+
s -> new MapArrayStruct(getMapArray(), s)), "mixedMapArrayStruct", "{other=[(99,33)]}")
158+
);
159+
}
160+
161+
private static Map<String, IntStruct[]> getMapArray() {
162+
Map<String, IntStruct[]> map = new HashMap<>();
163+
map.put("other", new IntStruct[] {new IntStruct(99, 33)});
164+
return map;
165+
}
166+
167+
private static Map<String, Map<String, Map<String, IntStruct>>> getDeepMapStruct() {
168+
Map<String, Map<String, Map<String, IntStruct>>> outerMap = new HashMap<>();
169+
Map<String, Map<String, IntStruct>> midlleMap = new HashMap<>();
170+
Map<String, IntStruct> innerMap = new HashMap<>();
171+
innerMap.put("test", new IntStruct(42, 19));
172+
midlleMap.put("inbetween", innerMap);
173+
outerMap.put("complete", midlleMap);
174+
return outerMap;
175+
}
176+
177+
private static IntStruct[][][] getDeepArrayStruct() {
178+
IntStruct[][][] array = new IntStruct[1][1][1];
179+
array[0][0][0] = new IntStruct(131, 145);
180+
return array;
181+
}
182+
183+
private static List<List<List<IntStruct>>> getDeepList() {
184+
return Arrays.asList(Arrays.asList(Arrays.asList(new IntStruct(111, 44))));
185+
}
186+
187+
private static Map<String, IntStruct> getIntStructHashMap() {
188+
Map<String, IntStruct> map = new HashMap<>();
189+
map.put("other", new IntStruct(12, 17));
190+
return map;
191+
}
192+
193+
private static Map<String, Integer> getIntHashMap() {
194+
Map<String, Integer> map = new HashMap<>();
195+
map.put("test", 8);
196+
return map;
197+
}
198+
199+
/**
200+
* Wrapper object for first three arguments
201+
*/
202+
private final static class ArgumentObj<T> {
203+
private final BiFunction<ISampleCollectionInterface, T, String> function;
204+
private final Function<String, T> factoryEmpty;
205+
private Function<String, T> factoryNonEmpty;
206+
207+
public ArgumentObj(BiFunction<ISampleCollectionInterface, T, String> function, Function<String, T> factoryEmpty,
208+
Function<String, T> factoryNonEmpty) {
209+
this.function = function;
210+
this.factoryEmpty = factoryEmpty;
211+
this.factoryNonEmpty = factoryNonEmpty;
212+
213+
}
214+
}
215+
216+
}

0 commit comments

Comments
 (0)