|
| 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