diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/AbstractDBusBaseTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/AbstractDBusBaseTest.java index 1d67433e..61d80f15 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/AbstractDBusBaseTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/AbstractDBusBaseTest.java @@ -1,75 +1,60 @@ package org.freedesktop.dbus.test; -import org.freedesktop.dbus.bin.EmbeddedDBusDaemon; -import org.freedesktop.dbus.config.DBusSysProps; -import org.freedesktop.dbus.connections.BusAddress; -import org.freedesktop.dbus.connections.transports.TransportBuilder; +import org.freedesktop.dbus.connections.impl.DBusConnection; +import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; import org.freedesktop.dbus.exceptions.DBusException; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; +import org.freedesktop.dbus.exceptions.DBusExecutionException; +import org.freedesktop.dbus.test.helper.SampleClass; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; /** - * Base test which will start a embedded DBus daemon if no UNIX transport is found. - * - * @author hypfvieh - * @since v4.0.0 - 2021-09-14 + * Base test providing server and client connection and some default exports. */ -public abstract class AbstractDBusBaseTest extends AbstractBaseTest { - - //CHECKSTYLE:OFF - protected static EmbeddedDBusDaemon edbus; - //CHECKSTYLE:ON - - /** - * Wait 500 ms if the current test uses TCP transport. - * - * @throws InterruptedException on interruption - */ - protected static void waitIfTcp() throws InterruptedException { - if (!TransportBuilder.getRegisteredBusTypes().contains("UNIX")) { - Thread.sleep(500L); - } +public abstract class AbstractDBusBaseTest extends AbstractDBusDaemonBaseTest { + // CHECKSTYLE:OFF + protected DBusConnection serverconn = null; + protected DBusConnection clientconn = null; + protected SampleClass tclass; + // CHECKSTYLE:ON + + @BeforeEach + public void setUp() throws DBusException { + serverconn = DBusConnectionBuilder.forSessionBus().withShared(false).withWeakReferences(true).build(); + clientconn = DBusConnectionBuilder.forSessionBus().withShared(false).withWeakReferences(true).build(); + serverconn.requestBusName(getTestBusName()); + + tclass = new SampleClass(serverconn); + + /** This exports an instance of the test class as the object /Test. */ + serverconn.exportObject(getTestObjectPath(), tclass); + serverconn.addFallback(getTestObjectPath() + "FallbackTest", tclass); } - /** - * Start an embedded Dbus daemon (in background) if the test uses TCP transport. - * - * @throws DBusException if start of daemon failed - * @throws InterruptedException on interruption - */ - @BeforeAll - public static void beforeAll() throws DBusException, InterruptedException { - Logger logger = LoggerFactory.getLogger(AbstractDBusBaseTest.class); - if (!TransportBuilder.getRegisteredBusTypes().contains("UNIX")) { - String busType = TransportBuilder.getRegisteredBusTypes().get(0); - String addr = TransportBuilder.createDynamicSession(busType, true); - BusAddress address = BusAddress.of(addr); - - logger.info("Creating {} based DBus daemon on address {}", busType, addr); - edbus = new EmbeddedDBusDaemon(addr); - edbus.startInBackgroundAndWait(MAX_WAIT); + @AfterEach + public void tearDown() throws Exception { + logger.debug("Checking for outstanding errors"); + DBusExecutionException dbee = serverconn.getError(); + if (null != dbee) { + throw dbee; + } + dbee = clientconn.getError(); + if (null != dbee) { + throw dbee; + } - if (address.isBusType("TCP")) { - String addrStr = address.removeParameter("listen").toString(); - System.setProperty(DBusSysProps.DBUS_SESSION_BUS_ADDRESS, addrStr); - } + logger.debug("Disconnecting"); + /** Disconnect from the bus. */ + clientconn.disconnect(); + serverconn.releaseBusName(getTestBusName()); + serverconn.disconnect(); + } - } + protected String getTestObjectPath() { + return "/" + getClass().getSimpleName(); } - /** - * Shutdown embedded Dbus daemon after test (if any). - * - * @throws IOException shutdown failed - */ - @AfterAll - public static void afterAll() throws IOException { - if (edbus != null) { - edbus.close(); - } + protected String getTestBusName() { + return getClass().getName(); } } diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/AbstractDBusDaemonBaseTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/AbstractDBusDaemonBaseTest.java new file mode 100644 index 00000000..2155adc8 --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/AbstractDBusDaemonBaseTest.java @@ -0,0 +1,75 @@ +package org.freedesktop.dbus.test; + +import org.freedesktop.dbus.bin.EmbeddedDBusDaemon; +import org.freedesktop.dbus.config.DBusSysProps; +import org.freedesktop.dbus.connections.BusAddress; +import org.freedesktop.dbus.connections.transports.TransportBuilder; +import org.freedesktop.dbus.exceptions.DBusException; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +/** + * Base test which will start a embedded DBus daemon if no UNIX transport is found. + * + * @author hypfvieh + * @since v4.0.0 - 2021-09-14 + */ +public abstract class AbstractDBusDaemonBaseTest extends AbstractBaseTest { + + //CHECKSTYLE:OFF + protected static EmbeddedDBusDaemon edbus; + //CHECKSTYLE:ON + + /** + * Wait 500 ms if the current test uses TCP transport. + * + * @throws InterruptedException on interruption + */ + protected static void waitIfTcp() throws InterruptedException { + if (!TransportBuilder.getRegisteredBusTypes().contains("UNIX")) { + Thread.sleep(500L); + } + } + + /** + * Start an embedded Dbus daemon (in background) if the test uses TCP transport. + * + * @throws DBusException if start of daemon failed + * @throws InterruptedException on interruption + */ + @BeforeAll + public static void beforeAll() throws DBusException, InterruptedException { + Logger logger = LoggerFactory.getLogger(AbstractDBusDaemonBaseTest.class); + if (!TransportBuilder.getRegisteredBusTypes().contains("UNIX")) { + String busType = TransportBuilder.getRegisteredBusTypes().get(0); + String addr = TransportBuilder.createDynamicSession(busType, true); + BusAddress address = BusAddress.of(addr); + + logger.info("Creating {} based DBus daemon on address {}", busType, addr); + edbus = new EmbeddedDBusDaemon(addr); + edbus.startInBackgroundAndWait(MAX_WAIT); + + if (address.isBusType("TCP")) { + String addrStr = address.removeParameter("listen").toString(); + System.setProperty(DBusSysProps.DBUS_SESSION_BUS_ADDRESS, addrStr); + } + + } + } + + /** + * Shutdown embedded Dbus daemon after test (if any). + * + * @throws IOException shutdown failed + */ + @AfterAll + public static void afterAll() throws IOException { + if (edbus != null) { + edbus.close(); + } + } +} diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/BaseFunctionsTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/BaseFunctionsTest.java new file mode 100644 index 00000000..11dd85a7 --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/BaseFunctionsTest.java @@ -0,0 +1,170 @@ +package org.freedesktop.dbus.test; + +import org.freedesktop.dbus.DBusPath; +import org.freedesktop.dbus.errors.ServiceUnknown; +import org.freedesktop.dbus.errors.UnknownObject; +import org.freedesktop.dbus.exceptions.DBusException; +import org.freedesktop.dbus.interfaces.DBus; +import org.freedesktop.dbus.interfaces.Introspectable; +import org.freedesktop.dbus.interfaces.Peer; +import org.freedesktop.dbus.interfaces.Properties; +import org.freedesktop.dbus.test.helper.SampleException; +import org.freedesktop.dbus.test.helper.SampleSerializable; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface2; +import org.freedesktop.dbus.utils.TimeMeasure; +import org.junit.jupiter.api.Test; + +import java.text.Collator; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class BaseFunctionsTest extends AbstractDBusBaseTest { + + @Test + public void testPing() throws DBusException { + logger.debug("Pinging ourselves"); + Peer peer = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), Peer.class); + + TimeMeasure timeMeasure = new TimeMeasure(); + assertDoesNotThrow(() -> { + for (int i = 0; i < 10; i++) { + timeMeasure.reset(); + peer.Ping(); + logger.debug("Ping returned in " + timeMeasure.getElapsed() + "ms."); + } + }); + + } + + @Test + public void testDbusNames() throws DBusException, InterruptedException { + DBus dbus = clientconn.getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class); + + String[] names = dbus.ListNames(); + logger.debug("Names on bus: {}", Arrays.toString(names)); + assertTrue(Arrays.asList(names).contains(getTestBusName())); + } + + @Test + public void testSerialization() throws DBusException { + SampleRemoteInterface2 tri2 = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + List v = new ArrayList<>(); + v.add(1); + v.add(2); + v.add(3); + SampleSerializable s = new SampleSerializable<>(1, "woo", v); + s = tri2.testSerializable((byte) 12, s, 13); + logger.debug("returned: " + s); + if (s.getInt() != 1 || !s.getString().equals("woo") || s.getList().size() != 3 || s.getList().get(0) != 1 + || s.getList().get(1) != 2 || s.getList().get(2) != 3) { + fail("Didn't get back the same TestSerializable"); + } + } + + @Test + public void testIntrospection() throws DBusException { + logger.debug("Getting our introspection data"); + /** This gets a remote object matching our bus name and exported object path. */ + Introspectable intro = clientconn.getRemoteObject(getTestBusName(), "/", Introspectable.class); + intro = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), Introspectable.class); + /** Get introspection data */ + String data = intro.Introspect(); + assertNotNull(data); + assertTrue(data.startsWith("> lli = new ArrayList<>(); + List li = new ArrayList<>(); + li.add(57); + lli.add(li); + + @SuppressWarnings("unchecked") + DBusAsyncReply>> checklistReply = (DBusAsyncReply>>) clientconn.callMethodAsync(tri2, "checklist", + lli); + + // wait a bit to allow the async call to complete + Thread.sleep(500L); + + assertIterableEquals(lli, checklistReply.getReply(), "did not get back the same as sent in async"); + assertIterableEquals(li, checklistReply.getReply().get(0)); + } + + @Test + public void testNestedListsCallback() throws DBusException, InterruptedException { + SampleRemoteInterface2 tri2 = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + List> lli = new ArrayList<>(); + List li = new ArrayList<>(); + li.add(25); + lli.add(li); + + NestedListCallbackHandler cbHandle = new NestedListCallbackHandler(); + + clientconn.callWithCallback(tri2, "checklist", cbHandle, lli); + + // wait a bit to allow the async call to complete + Thread.sleep(500L); + + assertIterableEquals(lli, cbHandle.getRetval(), "did not get back the same as sent in async"); + assertIterableEquals(li, cbHandle.getRetval().get(0)); + } + + @Test + public void testNestedLists() throws DBusException { + SampleRemoteInterface2 tri2 = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + List> lli = new ArrayList<>(); + List li = new ArrayList<>(); + li.add(1); + lli.add(li); + + List> reti = tri2.checklist(lli); + if (reti.size() != 1 || reti.get(0).size() != 1 || reti.get(0).get(0) != 1) { + fail("Failed to check nested lists"); + } + } + + @Test + public void testListOfStruct() throws DBusException { + SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + + IntStruct elem1 = new IntStruct(3, 7); + IntStruct elem2 = new IntStruct(9, 14); + List list = Arrays.asList(elem1, elem2); + SampleStruct4 param = new SampleStruct4(list); + int[][] out = tri.testListstruct(param); + if (out.length != 2) { + fail("teststructstruct returned the wrong thing: " + Arrays.deepToString(out)); + } + assertEquals(elem1.getValue1(), out[0][0]); + assertEquals(elem1.getValue2(), out[0][1]); + assertEquals(elem2.getValue1(), out[1][0]); + assertEquals(elem2.getValue2(), out[1][1]); + } +} diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/ComplexTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/ComplexTest.java new file mode 100644 index 00000000..4a4572c4 --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/ComplexTest.java @@ -0,0 +1,139 @@ +package org.freedesktop.dbus.test; + +import org.freedesktop.dbus.DBusAsyncReply; +import org.freedesktop.dbus.errors.UnknownMethod; +import org.freedesktop.dbus.exceptions.DBusException; +import org.freedesktop.dbus.test.helper.SampleNewInterfaceClass; +import org.freedesktop.dbus.test.helper.interfaces.SampleNewInterface; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface2; +import org.freedesktop.dbus.test.helper.structs.SampleStruct; +import org.freedesktop.dbus.test.helper.structs.SampleTuple; +import org.freedesktop.dbus.types.UInt16; +import org.freedesktop.dbus.types.UInt32; +import org.freedesktop.dbus.types.Variant; +import org.junit.jupiter.api.Test; + +import java.text.Collator; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ComplexTest extends AbstractDBusBaseTest { + + @Test + public void testDbusIgnore() throws DBusException { + SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + assertThrowsExactly(UnknownMethod.class, () -> { + tri.thisShouldBeIgnored(); + }); + } + + public void testFrob() throws DBusException { + final SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + logger.debug("frobnicating"); + List ls = new ArrayList<>(); + ls.add(2L); + ls.add(5L); + ls.add(71L); + Map mus = new HashMap<>(); + mus.put(new UInt16(4), (short) 5); + mus.put(new UInt16(5), (short) 6); + mus.put(new UInt16(6), (short) 7); + Map> msmus = new HashMap<>(); + msmus.put("stuff", mus); + int rint = tri.frobnicate(ls, msmus, 13); + if (-5 != rint) { + fail("frobnicate return value incorrect"); + } + + } + + @Test + public void testResponse() throws DBusException, InterruptedException { + SampleRemoteInterface2 tri2 = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + + logger.debug(tri2.Introspect()); + /** Call the remote object and get a response. */ + SampleTuple, Boolean> rv = tri2.show(234); + logger.debug("Show returned: " + rv); + if (!clientconn.getUniqueName().equals(rv.getFirstValue()) || 1 != rv.getSecondValue().size() || 1953 != rv.getSecondValue().get(0) + || !rv.getThirdValue().booleanValue()) { + fail("show return value incorrect (" + rv.getFirstValue() + "," + rv.getSecondValue() + "," + rv.getThirdValue() + ")"); + } + + logger.debug("Doing stuff asynchronously"); + @SuppressWarnings("unchecked") + DBusAsyncReply stuffreply = (DBusAsyncReply) clientconn.callMethodAsync(tri2, "dostuff", + new SampleStruct("bar", new UInt32(52), new Variant<>(Boolean.TRUE))); + + // wait a bit to allow the async call to complete + Thread.sleep(500L); + + assertFalse(tri2.check(), "bools are broken"); + + assertTrue(stuffreply.getReply(), "dostuff return value incorrect"); + + } + + @Test + public void testComplex() throws DBusException { + SampleRemoteInterface2 tri2 = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + Collator col = Collator.getInstance(); + col.setDecomposition(Collator.FULL_DECOMPOSITION); + col.setStrength(Collator.PRIMARY); + + Map m = new HashMap<>(); + m.put("cow", "moo"); + tri2.complexv(new Variant<>(m, "a{ss}")); + logger.debug("done"); + + logger.debug("testing recursion..."); + + if (0 != col.compare("This Is A UTF-8 Name: ﺱ !!", tri2.recursionTest(getTestBusName(), getTestObjectPath()))) { + fail("recursion test failed"); + } + + logger.debug("done"); + + logger.debug("testing method overloading..."); + SampleRemoteInterface tri = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface.class); + if (1 != tri2.overload("foo")) { + fail("wrong overloaded method called"); + } + if (2 != tri2.overload((byte) 0)) { + fail("wrong overloaded method called"); + } + if (3 != tri2.overload()) { + fail("wrong overloaded method called"); + } + if (4 != tri.overload()) { + fail("wrong overloaded method called"); + } + } + + @Test + public void testRegression13291() throws DBusException { + SampleRemoteInterface tri = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface.class); + + logger.debug("reg13291..."); + byte[] as = new byte[10]; + for (int i = 0; i < 10; i++) { + as[i] = (byte) (100 - i); + } + + tri.reg13291(as, as); + logger.debug("done"); + } + + @Test + public void testDynamicObjectCreation() throws DBusException { + SampleRemoteInterface2 tri2 = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + + SampleNewInterface tni = tri2.getNew(); + + assertEquals(tni.getName(), SampleNewInterfaceClass.class.getSimpleName()); + } + +} diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/DBusConnectionTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/DBusConnectionTest.java index 9b6908d9..e2878239 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/DBusConnectionTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/DBusConnectionTest.java @@ -8,7 +8,7 @@ /** * */ -public class DBusConnectionTest extends AbstractDBusBaseTest { +public class DBusConnectionTest extends AbstractDBusDaemonBaseTest { @Test public void testBusnamesShouldBeAutoReleasedOnCloseOfNonSharedConnection() throws Exception { diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/ExportNestedTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/ExportNestedTest.java index f357d1a7..bd06a60c 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/ExportNestedTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/ExportNestedTest.java @@ -13,7 +13,7 @@ import java.util.List; import java.util.stream.Collectors; -public class ExportNestedTest extends AbstractDBusBaseTest { +public class ExportNestedTest extends AbstractDBusDaemonBaseTest { @Test public void testExportNested() throws IOException, DBusException { diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java index 3389c1c8..df02fe18 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java @@ -17,7 +17,7 @@ import java.util.stream.Stream; @EnabledIf(value = "isFileDescriptorSupported", disabledReason = "file descriptors not supported with the current transport") -public class FileDescriptorsTest extends AbstractDBusBaseTest { +public class FileDescriptorsTest extends AbstractDBusDaemonBaseTest { public static final String TEST_OBJECT_PATH = "/FileDescriptorsTest"; private DBusConnection serverconn = null, clientconn = null; diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/HandlerTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/HandlerTest.java new file mode 100644 index 00000000..a3ee19da --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/HandlerTest.java @@ -0,0 +1,203 @@ +package org.freedesktop.dbus.test; + +import org.freedesktop.dbus.DBusMatchRule; +import org.freedesktop.dbus.DBusPath; +import org.freedesktop.dbus.exceptions.DBusException; +import org.freedesktop.dbus.interfaces.DBus; +import org.freedesktop.dbus.interfaces.Introspectable; +import org.freedesktop.dbus.messages.DBusSignal; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterfaceEnum.TestEnum; +import org.freedesktop.dbus.test.helper.signals.SampleSignals; +import org.freedesktop.dbus.test.helper.signals.SampleSignals.*; +import org.freedesktop.dbus.test.helper.signals.handler.*; +import org.freedesktop.dbus.test.helper.structs.SampleStruct2; +import org.freedesktop.dbus.types.UInt32; +import org.freedesktop.dbus.types.UInt64; +import org.freedesktop.dbus.types.Variant; +import org.junit.jupiter.api.Test; + +import java.util.*; + +public class HandlerTest extends AbstractDBusBaseTest { + @Test + public void testSignalHandlers() throws DBusException, InterruptedException { + SignalHandler sigh = new SignalHandler(1, new UInt32(42), "Bar"); + RenamedSignalHandler rsh = new RenamedSignalHandler(1, new UInt32(42), "Bar"); + EmptySignalHandler esh = new EmptySignalHandler(1); + ArraySignalHandler ash = new ArraySignalHandler(1); + final ObjectSignalHandler osh = new ObjectSignalHandler(1); + final PathSignalHandler psh = new PathSignalHandler(1); + final EnumSignalHandler ensh = new EnumSignalHandler(1); + + /** This gets a remote object matching our bus name and exported object path. */ + SampleRemoteInterface peer = (SampleRemoteInterface) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + + DBus dbus = clientconn.getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class); + + logger.debug("Listening for signals..."); + /** This registers an instance of the test class as the signal handler for the TestSignal class. */ + + clientconn.addSigHandler(SampleSignals.TestEmptySignal.class, esh); + clientconn.addSigHandler(SampleSignals.TestSignal.class, sigh); + clientconn.addSigHandler(SampleSignals.TestRenamedSignal.class, rsh); + + String source = dbus.GetNameOwner(getTestBusName()); + + clientconn.addSigHandler(SampleSignals.TestArraySignal.class, source, peer, ash); + clientconn.addSigHandler(SampleSignals.TestObjectSignal.class, osh); + clientconn.addSigHandler(SampleSignals.TestPathSignal.class, psh); + clientconn.addSigHandler(SampleSignals.TestEnumSignal.class, ensh); + + BadArraySignalHandler bash = new BadArraySignalHandler<>(1); + clientconn.addSigHandler(TestSignal.class, bash); + clientconn.removeSigHandler(TestSignal.class, bash); + logger.debug("done"); + + logger.debug("Sending Signals"); + + /** + * This creates an instance of the Test Signal, with the given object path, signal name and parameters, and + * broadcasts in on the Bus. + */ + serverconn.sendMessage(new TestSignal("/foo/bar/Wibble", "Bar", new UInt32(42))); + serverconn.sendMessage(new TestEmptySignal("/foo/bar/Wibble")); + serverconn.sendMessage(new TestRenamedSignal("/foo/bar/Wibble", "Bar", new UInt32(42))); + + logger.debug("Sending Path Signal..."); + DBusPath path = new DBusPath("/nonexistantwooooooo"); + DBusPath p = peer.pathrv(path); + logger.debug(path.toString() + " => " + p.toString()); + assertEquals(path, p, "pathrv incorrect"); + List paths = new ArrayList<>(); + paths.add(path); + List ps = peer.pathlistrv(paths); + logger.debug(paths.toString() + " => " + ps.toString()); + Map pathm = new HashMap<>(); + pathm.put(path, path); + serverconn.sendMessage(new TestPathSignal(getTestObjectPath(), path, paths, pathm)); + + logger.debug("Sending Array Signal..."); + final List tsl = new ArrayList<>(); + List l = new ArrayList<>(); + l.add("hi"); + l.add("hello"); + l.add("hej"); + l.add("hey"); + l.add("aloha"); + tsl.add(new SampleStruct2(l, new Variant<>(new UInt64(567)))); + Map tsm = new HashMap<>(); + tsm.put(new UInt32(1), new SampleStruct2(l, new Variant<>(new UInt64(678)))); + tsm.put(new UInt32(42), new SampleStruct2(l, new Variant<>(new UInt64(789)))); + serverconn.sendMessage(new TestArraySignal(getTestObjectPath(), tsl, tsm)); + + logger.debug("Sending Object Signal..."); + serverconn.sendMessage(new TestObjectSignal(getTestObjectPath(), tclass)); + + logger.debug("Sending Enum Signal..."); + serverconn.sendMessage(new TestEnumSignal(getTestObjectPath(), TestEnum.TESTVAL1, Arrays.asList(TestEnum.TESTVAL2, TestEnum.TESTVAL3))); + + // wait some time to receive signals + Thread.sleep(1000L); + + // ensure callback has been fired at least once + assertEquals(1, sigh.getActualTestRuns(), "SignalHandler should have been called"); + assertEquals(1, esh.getActualTestRuns(), "EmptySignalHandler should have been called"); + assertEquals(1, rsh.getActualTestRuns(), "RenamedSignalHandler should have been called"); + assertEquals(1, ash.getActualTestRuns(), "ArraySignalHandler should have been called"); + assertEquals(1, ensh.getActualTestRuns(), "EnumSignalHandler should have been called"); + assertEquals(1, psh.getActualTestRuns(), "PathSignalHandler should have been called"); + assertEquals(1, osh.getActualTestRuns(), "ObjectSignalHandler should have been called"); + + assertDoesNotThrow(() -> sigh.throwAssertionError()); + assertDoesNotThrow(() -> esh.throwAssertionError()); + assertDoesNotThrow(() -> rsh.throwAssertionError()); + assertDoesNotThrow(() -> ash.throwAssertionError()); + assertDoesNotThrow(() -> ensh.throwAssertionError()); + assertDoesNotThrow(() -> psh.throwAssertionError()); + assertDoesNotThrow(() -> osh.throwAssertionError()); + + /** Remove sig handler */ + clientconn.removeSigHandler(SampleSignals.TestSignal.class, sigh); + clientconn.removeSigHandler(SampleSignals.TestEmptySignal.class, esh); + clientconn.removeSigHandler(SampleSignals.TestRenamedSignal.class, rsh); + clientconn.removeSigHandler(SampleSignals.TestArraySignal.class, ash); + clientconn.removeSigHandler(SampleSignals.TestObjectSignal.class, osh); + clientconn.removeSigHandler(SampleSignals.TestPathSignal.class, psh); + clientconn.removeSigHandler(SampleSignals.TestEnumSignal.class, ensh); + + } + + @Test + public void testGenericSignalHandler() throws DBusException, InterruptedException { + GenericSignalHandler genericHandler = new GenericSignalHandler(); + DBusMatchRule signalRule = new DBusMatchRule("signal", "org.foo", "methodnoarg", "/"); + + clientconn.addGenericSigHandler(signalRule, genericHandler); + + DBusSignal signalToSend = clientconn.getMessageFactory().createSignal(null, "/", "org.foo", "methodnoarg", null); + + serverconn.sendMessage(signalToSend); + + // wait some time to receive signals + Thread.sleep(1000L); + + // ensure callback has been fired at least once + assertEquals(1, genericHandler.getActualTestRuns(), "GenericHandler should have been called"); + + clientconn.removeGenericSigHandler(signalRule, genericHandler); + } + + @Test + public void testGenericDecodeSignalHandler() throws DBusException, InterruptedException { + GenericHandlerWithDecode genericDecode = new GenericHandlerWithDecode(new UInt32(42), "SampleString"); + DBusMatchRule signalRule = new DBusMatchRule("signal", "org.foo", "methodarg", "/"); + + clientconn.addGenericSigHandler(signalRule, genericDecode); + + DBusSignal signalToSend = serverconn.getMessageFactory().createSignal(null, "/", "org.foo", "methodarg", "us", new UInt32(42), "SampleString"); + + serverconn.sendMessage(signalToSend); + + // wait some time to receive signals + Thread.sleep(1000L); + + assertDoesNotThrow(() -> { + genericDecode.incomingSameAsExpected(); + clientconn.removeGenericSigHandler(signalRule, genericDecode); + }); + + assertNull(genericDecode.getAssertionError()); + } + + @Test + public void testGenericHandlerWithNoInterface() throws DBusException, InterruptedException { + GenericHandlerWithDecode genericDecode = new GenericHandlerWithDecode(new UInt32(42), "SampleString"); + DBusMatchRule signalRule = new DBusMatchRule("signal", null, "methodargNoIface", "/"); + + clientconn.addGenericSigHandler(signalRule, genericDecode); + DBusSignal signalToSend = clientconn.getMessageFactory().createSignal(null, "/", "org.foo", "methodargNoIface", "us", new UInt32(42), "SampleString"); + + serverconn.sendMessage(signalToSend); + + // wait some time to receive signals + Thread.sleep(1000L); + + assertDoesNotThrow(() -> { + genericDecode.incomingSameAsExpected(); + clientconn.removeGenericSigHandler(signalRule, genericDecode); + }); + + assertNull(genericDecode.getAssertionError()); + } + + @Test + public void testFallbackHandler() throws DBusException { + SampleRemoteInterface tri = + clientconn.getRemoteObject(getTestBusName(), getTestObjectPath() + "FallbackTest/0/1", SampleRemoteInterface.class); + Introspectable intro = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath() + "FallbackTest/0/4", Introspectable.class); + + assertEquals("This Is A UTF-8 Name: س !!", tri.getName()); + assertTrue(intro.Introspect().startsWith(" ts = new ArrayList<>(); + Marshalling.getJavaType("ya{si}", ts, -1); + tri.sig(ts.toArray(new Type[0])); + + DBusPath path = new DBusPath("/nonexistantwooooooo"); + DBusPath p = tri.pathrv(path); + logger.debug(path.toString() + " => " + p.toString()); + assertEquals(path, p, "pathrv incorrect"); + + List paths = new ArrayList<>(); + paths.add(path); + + List ps = tri.pathlistrv(paths); + logger.debug(paths.toString() + " => " + ps.toString()); + + assertEquals(paths, ps, "pathlistrv incorrect"); + + Map pathm = new HashMap<>(); + pathm.put(path, path); + Map pm = tri.pathmaprv(pathm); + + logger.debug(pathm.toString() + " => " + pm.toString()); + logger.debug(pm.containsKey(path) + " " + pm.get(path) + " " + path.equals(pm.get(path))); + logger.debug(pm.containsKey(p) + " " + pm.get(p) + " " + p.equals(pm.get(p))); + + assertTrue(pm.containsKey(path), "pathmaprv incorrect"); + assertEquals(path, pm.get(path), "pathmaprv incorrect"); + } + + @Test + public void testCallWithCallback() throws DBusException, InterruptedException { + final SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getRemoteObject(getTestBusName(), getTestObjectPath()); + + logger.debug("Doing stuff asynchronously with callback"); + CallbackHandlerImpl cbWhichWorks = new CallbackHandlerImpl(); + clientconn.callWithCallback(tri, "getName", cbWhichWorks); + + logger.debug("Doing stuff asynchronously with callback, which throws an error"); + CallbackHandlerImpl cbWhichThrows = new CallbackHandlerImpl(); + clientconn.callWithCallback(tri, "getNameAndThrow", cbWhichThrows); + + /** call something that throws */ + try { + logger.debug("Throwing stuff"); + tri.throwme(); + fail("Method Execution should have failed"); + } catch (SampleException _ex) { + logger.debug("Remote Method Failed with: " + _ex.getClass().getName() + " " + _ex.getMessage()); + if (!_ex.getMessage().equals("test")) { + fail("Error message was not correct"); + } + } + + Thread.sleep(500L); // wait some time to let the callbacks do their work + + // we do not expect any test failures + assertNull(cbWhichThrows.getLastError()); + assertNull(cbWhichWorks.getLastError()); + + assertEquals(1, cbWhichWorks.getTestHandleCalls()); + assertEquals(0, cbWhichThrows.getTestHandleCalls()); + + assertEquals(0, cbWhichWorks.getTestErrorCalls()); + assertEquals(1, cbWhichThrows.getTestErrorCalls()); + } +} diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/StressTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/StressTest.java index af79a2e0..7faa9fb2 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/StressTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/StressTest.java @@ -15,7 +15,7 @@ /** * */ -public class StressTest extends AbstractDBusBaseTest { +public class StressTest extends AbstractDBusDaemonBaseTest { private static final String OBJECT_PATH = "/org/freedesktop/dbus/test/RemoteObjectImpl"; diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestAll.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestAll.java deleted file mode 100644 index 983cd08e..00000000 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestAll.java +++ /dev/null @@ -1,812 +0,0 @@ -package org.freedesktop.dbus.test; - -import org.freedesktop.dbus.DBusAsyncReply; -import org.freedesktop.dbus.DBusMatchRule; -import org.freedesktop.dbus.DBusPath; -import org.freedesktop.dbus.Marshalling; -import org.freedesktop.dbus.connections.impl.DBusConnection; -import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; -import org.freedesktop.dbus.errors.ServiceUnknown; -import org.freedesktop.dbus.errors.UnknownMethod; -import org.freedesktop.dbus.errors.UnknownObject; -import org.freedesktop.dbus.exceptions.DBusException; -import org.freedesktop.dbus.exceptions.DBusExecutionException; -import org.freedesktop.dbus.interfaces.CallbackHandler; -import org.freedesktop.dbus.interfaces.DBus; -import org.freedesktop.dbus.interfaces.Introspectable; -import org.freedesktop.dbus.interfaces.Peer; -import org.freedesktop.dbus.interfaces.Properties; -import org.freedesktop.dbus.messages.DBusSignal; -import org.freedesktop.dbus.test.helper.SampleClass; -import org.freedesktop.dbus.test.helper.SampleException; -import org.freedesktop.dbus.test.helper.SampleNewInterfaceClass; -import org.freedesktop.dbus.test.helper.SampleSerializable; -import org.freedesktop.dbus.test.helper.callbacks.handler.CallbackHandlerImpl; -import org.freedesktop.dbus.test.helper.interfaces.SampleNewInterface; -import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface; -import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface2; -import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterfaceEnum; -import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterfaceEnum.TestEnum; -import org.freedesktop.dbus.test.helper.signals.SampleSignals; -import org.freedesktop.dbus.test.helper.signals.SampleSignals.*; -import org.freedesktop.dbus.test.helper.signals.handler.*; -import org.freedesktop.dbus.test.helper.structs.*; -import org.freedesktop.dbus.types.UInt16; -import org.freedesktop.dbus.types.UInt32; -import org.freedesktop.dbus.types.UInt64; -import org.freedesktop.dbus.types.Variant; -import org.freedesktop.dbus.utils.TimeMeasure; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.lang.reflect.Type; -import java.text.Collator; -import java.util.*; - -/** - * This is a test program which sends and recieves a signal, implements, exports and calls a remote method. - */ -// CHECKSTYLE:OFF -public class TestAll extends AbstractDBusBaseTest { - - public static final String TEST_OBJECT_PATH = "/TestAll"; - - // CHECKSTYLE:OFF - private DBusConnection serverconn = null; - private DBusConnection clientconn = null; - private SampleClass tclass; - // CHECKSTYLE:ON - - @BeforeEach - public void setUp() throws DBusException { - serverconn = DBusConnectionBuilder.forSessionBus().withShared(false).withWeakReferences(true).build(); - clientconn = DBusConnectionBuilder.forSessionBus().withShared(false).withWeakReferences(true).build(); - serverconn.requestBusName("foo.bar.Test"); - - tclass = new SampleClass(serverconn); - - /** This exports an instance of the test class as the object /Test. */ - serverconn.exportObject(TEST_OBJECT_PATH, tclass); - serverconn.addFallback("/FallbackTest", tclass); - } - - @AfterEach - public void tearDown() throws Exception { - logger.debug("Checking for outstanding errors"); - DBusExecutionException dbee = serverconn.getError(); - if (null != dbee) { - throw dbee; - } - dbee = clientconn.getError(); - if (null != dbee) { - throw dbee; - } - - logger.debug("Disconnecting"); - /** Disconnect from the bus. */ - clientconn.disconnect(); - serverconn.releaseBusName("foo.bar.Test"); - serverconn.disconnect(); - } - - @Test - public void testSignalHandlers() throws DBusException, InterruptedException { - SignalHandler sigh = new SignalHandler(1, new UInt32(42), "Bar"); - RenamedSignalHandler rsh = new RenamedSignalHandler(1, new UInt32(42), "Bar"); - EmptySignalHandler esh = new EmptySignalHandler(1); - ArraySignalHandler ash = new ArraySignalHandler(1); - final ObjectSignalHandler osh = new ObjectSignalHandler(1); - final PathSignalHandler psh = new PathSignalHandler(1); - final EnumSignalHandler ensh = new EnumSignalHandler(1); - - /** This gets a remote object matching our bus name and exported object path. */ - SampleRemoteInterface peer = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - DBus dbus = clientconn.getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class); - - logger.debug("Listening for signals..."); - /** This registers an instance of the test class as the signal handler for the TestSignal class. */ - - clientconn.addSigHandler(SampleSignals.TestEmptySignal.class, esh); - clientconn.addSigHandler(SampleSignals.TestSignal.class, sigh); - clientconn.addSigHandler(SampleSignals.TestRenamedSignal.class, rsh); - - String source = dbus.GetNameOwner("foo.bar.Test"); - - clientconn.addSigHandler(SampleSignals.TestArraySignal.class, source, peer, ash); - clientconn.addSigHandler(SampleSignals.TestObjectSignal.class, osh); - clientconn.addSigHandler(SampleSignals.TestPathSignal.class, psh); - clientconn.addSigHandler(SampleSignals.TestEnumSignal.class, ensh); - - BadArraySignalHandler bash = new BadArraySignalHandler<>(1); - clientconn.addSigHandler(TestSignal.class, bash); - clientconn.removeSigHandler(TestSignal.class, bash); - logger.debug("done"); - - logger.debug("Sending Signals"); - - /** - * This creates an instance of the Test Signal, with the given object path, signal name and parameters, and - * broadcasts in on the Bus. - */ - serverconn.sendMessage(new TestSignal("/foo/bar/Wibble", "Bar", new UInt32(42))); - serverconn.sendMessage(new TestEmptySignal("/foo/bar/Wibble")); - serverconn.sendMessage(new TestRenamedSignal("/foo/bar/Wibble", "Bar", new UInt32(42))); - - logger.debug("Sending Path Signal..."); - DBusPath path = new DBusPath("/nonexistantwooooooo"); - DBusPath p = peer.pathrv(path); - logger.debug(path.toString() + " => " + p.toString()); - assertEquals(path, p, "pathrv incorrect"); - List paths = new ArrayList<>(); - paths.add(path); - List ps = peer.pathlistrv(paths); - logger.debug(paths.toString() + " => " + ps.toString()); - Map pathm = new HashMap<>(); - pathm.put(path, path); - serverconn.sendMessage(new TestPathSignal(TEST_OBJECT_PATH, path, paths, pathm)); - - logger.debug("Sending Array Signal..."); - final List tsl = new ArrayList<>(); - List l = new ArrayList<>(); - l.add("hi"); - l.add("hello"); - l.add("hej"); - l.add("hey"); - l.add("aloha"); - tsl.add(new SampleStruct2(l, new Variant<>(new UInt64(567)))); - Map tsm = new HashMap<>(); - tsm.put(new UInt32(1), new SampleStruct2(l, new Variant<>(new UInt64(678)))); - tsm.put(new UInt32(42), new SampleStruct2(l, new Variant<>(new UInt64(789)))); - serverconn.sendMessage(new TestArraySignal(TEST_OBJECT_PATH, tsl, tsm)); - - logger.debug("Sending Object Signal..."); - serverconn.sendMessage(new TestObjectSignal(TEST_OBJECT_PATH, tclass)); - - logger.debug("Sending Enum Signal..."); - serverconn.sendMessage(new TestEnumSignal(TEST_OBJECT_PATH, TestEnum.TESTVAL1, Arrays.asList(TestEnum.TESTVAL2, TestEnum.TESTVAL3))); - - // wait some time to receive signals - Thread.sleep(1000L); - - // ensure callback has been fired at least once - assertEquals(1, sigh.getActualTestRuns(), "SignalHandler should have been called"); - assertEquals(1, esh.getActualTestRuns(), "EmptySignalHandler should have been called"); - assertEquals(1, rsh.getActualTestRuns(), "RenamedSignalHandler should have been called"); - assertEquals(1, ash.getActualTestRuns(), "ArraySignalHandler should have been called"); - assertEquals(1, ensh.getActualTestRuns(), "EnumSignalHandler should have been called"); - assertEquals(1, psh.getActualTestRuns(), "PathSignalHandler should have been called"); - assertEquals(1, osh.getActualTestRuns(), "ObjectSignalHandler should have been called"); - - assertDoesNotThrow(() -> sigh.throwAssertionError()); - assertDoesNotThrow(() -> esh.throwAssertionError()); - assertDoesNotThrow(() -> rsh.throwAssertionError()); - assertDoesNotThrow(() -> ash.throwAssertionError()); - assertDoesNotThrow(() -> ensh.throwAssertionError()); - assertDoesNotThrow(() -> psh.throwAssertionError()); - assertDoesNotThrow(() -> osh.throwAssertionError()); - - /** Remove sig handler */ - clientconn.removeSigHandler(SampleSignals.TestSignal.class, sigh); - clientconn.removeSigHandler(SampleSignals.TestEmptySignal.class, esh); - clientconn.removeSigHandler(SampleSignals.TestRenamedSignal.class, rsh); - clientconn.removeSigHandler(SampleSignals.TestArraySignal.class, ash); - clientconn.removeSigHandler(SampleSignals.TestObjectSignal.class, osh); - clientconn.removeSigHandler(SampleSignals.TestPathSignal.class, psh); - clientconn.removeSigHandler(SampleSignals.TestEnumSignal.class, ensh); - - } - - @Test - public void testGenericSignalHandler() throws DBusException, InterruptedException { - GenericSignalHandler genericHandler = new GenericSignalHandler(); - DBusMatchRule signalRule = new DBusMatchRule("signal", "org.foo", "methodnoarg", "/"); - - clientconn.addGenericSigHandler(signalRule, genericHandler); - - DBusSignal signalToSend = clientconn.getMessageFactory().createSignal(null, "/", "org.foo", "methodnoarg", null); - - serverconn.sendMessage(signalToSend); - - // wait some time to receive signals - Thread.sleep(1000L); - - // ensure callback has been fired at least once - assertEquals(1, genericHandler.getActualTestRuns(), "GenericHandler should have been called"); - - clientconn.removeGenericSigHandler(signalRule, genericHandler); - } - - @Test - public void testGenericDecodeSignalHandler() throws DBusException, InterruptedException { - GenericHandlerWithDecode genericDecode = new GenericHandlerWithDecode(new UInt32(42), "SampleString"); - DBusMatchRule signalRule = new DBusMatchRule("signal", "org.foo", "methodarg", "/"); - - clientconn.addGenericSigHandler(signalRule, genericDecode); - - DBusSignal signalToSend = serverconn.getMessageFactory().createSignal(null, "/", "org.foo", "methodarg", "us", new UInt32(42), "SampleString"); - - serverconn.sendMessage(signalToSend); - - // wait some time to receive signals - Thread.sleep(1000L); - - assertDoesNotThrow(() -> { - genericDecode.incomingSameAsExpected(); - clientconn.removeGenericSigHandler(signalRule, genericDecode); - }); - - assertNull(genericDecode.getAssertionError()); - } - - @Test - public void testGenericHandlerWithNoInterface() throws DBusException, InterruptedException { - GenericHandlerWithDecode genericDecode = new GenericHandlerWithDecode(new UInt32(42), "SampleString"); - DBusMatchRule signalRule = new DBusMatchRule("signal", null, "methodargNoIface", "/"); - - clientconn.addGenericSigHandler(signalRule, genericDecode); - DBusSignal signalToSend = clientconn.getMessageFactory().createSignal(null, "/", "org.foo", "methodargNoIface", "us", new UInt32(42), "SampleString"); - - serverconn.sendMessage(signalToSend); - - // wait some time to receive signals - Thread.sleep(1000L); - - assertDoesNotThrow(() -> { - genericDecode.incomingSameAsExpected(); - clientconn.removeGenericSigHandler(signalRule, genericDecode); - }); - - assertNull(genericDecode.getAssertionError()); - } - - @Test - public void testPing() throws DBusException { - logger.debug("Pinging ourselves"); - Peer peer = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, Peer.class); - - TimeMeasure timeMeasure = new TimeMeasure(); - assertDoesNotThrow(() -> { - for (int i = 0; i < 10; i++) { - timeMeasure.reset(); - peer.Ping(); - logger.debug("Ping returned in " + timeMeasure.getElapsed() + "ms."); - } - }); - - } - - public void testDbusNames() throws DBusException { - logger.debug("These things are on the bus:"); - - logger.debug("Listening for Method Calls"); - SampleClass mtclass = new SampleClass(serverconn); - SampleClass tclass2 = new SampleClass(serverconn); - /** This exports an instance of the test class as the object /Test. */ - serverconn.exportObject("/TestClassToFindOnBus", mtclass); - serverconn.exportObject("/SecondTestClassToFindOnBus", tclass2); - - DBus dbus = clientconn.getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class); - - String[] names = dbus.ListNames(); - assertTrue(Arrays.asList(names).contains("/TestClassToFindOnBus")); - assertTrue(Arrays.asList(names).contains("/SecondTestClassToFindOnBus")); - - serverconn.unExportObject("/SecondTestClassToFindOnBus"); - serverconn.unExportObject("/TestClassToFindOnBus"); - } - - @Test - public void testIntrospection() throws DBusException { - logger.debug("Getting our introspection data"); - /** This gets a remote object matching our bus name and exported object path. */ - Introspectable intro = clientconn.getRemoteObject("foo.bar.Test", "/", Introspectable.class); - intro = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, Introspectable.class); - /** Get introspection data */ - String data = intro.Introspect(); - assertNotNull(data); - assertTrue(data.startsWith(" ts = new ArrayList<>(); - Marshalling.getJavaType("ya{si}", ts, -1); - tri.sig(ts.toArray(new Type[0])); - - DBusPath path = new DBusPath("/nonexistantwooooooo"); - DBusPath p = tri.pathrv(path); - logger.debug(path.toString() + " => " + p.toString()); - assertEquals(path, p, "pathrv incorrect"); - - List paths = new ArrayList<>(); - paths.add(path); - - List ps = tri.pathlistrv(paths); - logger.debug(paths.toString() + " => " + ps.toString()); - - assertEquals(paths, ps, "pathlistrv incorrect"); - - Map pathm = new HashMap<>(); - pathm.put(path, path); - Map pm = tri.pathmaprv(pathm); - - logger.debug(pathm.toString() + " => " + pm.toString()); - logger.debug(pm.containsKey(path) + " " + pm.get(path) + " " + path.equals(pm.get(path))); - logger.debug(pm.containsKey(p) + " " + pm.get(p) + " " + p.equals(pm.get(p))); - - assertTrue(pm.containsKey(path), "pathmaprv incorrect"); - assertEquals(path, pm.get(path), "pathmaprv incorrect"); - } - - @Test - public void testCallGetUtf8String() throws DBusException { - SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - /** Call the remote object and get a response. */ - String rname = tri.getName(); - - Collator col = Collator.getInstance(); - col.setDecomposition(Collator.FULL_DECOMPOSITION); - col.setStrength(Collator.PRIMARY); - - if (0 != col.compare("This Is A UTF-8 Name: ﺱ !!", rname)) { - fail("getName return value incorrect"); - } - } - - @Test - public void testFloats() throws DBusException { - SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - logger.debug("sending it to sleep"); - tri.waitawhile(); - logger.debug("testing floats"); - if (17.093f != tri.testfloat(new float[] { - 17.093f, -23f, 0.0f, 31.42f - })) { - fail("testfloat returned the wrong thing"); - } - } - - @Test - public void testStruct() throws DBusException { - final SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - List> lli = new ArrayList<>(); - List li = new ArrayList<>(); - li.add(1); - li.add(2); - li.add(3); - lli.add(li); - lli.add(li); - lli.add(li); - SampleStruct3 ts3 = new SampleStruct3(new SampleStruct2(new ArrayList<>(), new Variant<>(0)), lli); - int[][] out = tri.teststructstruct(ts3); - if (out.length != 3) { - fail("teststructstruct returned the wrong thing: " + Arrays.deepToString(out)); - } - for (int[] o : out) { - if (o.length != 3 || o[0] != 1 || o[1] != 2 || o[2] != 3) { - fail("teststructstruct returned the wrong thing: " + Arrays.deepToString(out)); - } - } - } - - @Test - public void testListOfStruct() throws DBusException { - SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - IntStruct elem1 = new IntStruct(3, 7); - IntStruct elem2 = new IntStruct(9, 14); - List list = Arrays.asList(elem1, elem2); - SampleStruct4 param = new SampleStruct4(list); - int[][] out = tri.testListstruct(param); - if (out.length != 2) { - fail("teststructstruct returned the wrong thing: " + Arrays.deepToString(out)); - } - assertEquals(elem1.getValue1(), out[0][0]); - assertEquals(elem1.getValue2(), out[0][1]); - assertEquals(elem2.getValue1(), out[1][0]); - assertEquals(elem2.getValue2(), out[1][1]); - } - - @Test - public void testEnum() throws DBusException { - SampleRemoteInterfaceEnum tri = (SampleRemoteInterfaceEnum) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - assertEquals(TestEnum.TESTVAL2, tri.getEnumValue()); - } - - @Test - public void testDbusIgnore() throws DBusException { - SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - assertThrowsExactly(UnknownMethod.class, () -> { - tri.thisShouldBeIgnored(); - }); - } - - public void testFrob() throws DBusException { - final SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - logger.debug("frobnicating"); - List ls = new ArrayList<>(); - ls.add(2L); - ls.add(5L); - ls.add(71L); - Map mus = new HashMap<>(); - mus.put(new UInt16(4), (short) 5); - mus.put(new UInt16(5), (short) 6); - mus.put(new UInt16(6), (short) 7); - Map> msmus = new HashMap<>(); - msmus.put("stuff", mus); - int rint = tri.frobnicate(ls, msmus, 13); - if (-5 != rint) { - fail("frobnicate return value incorrect"); - } - - } - - @Test - public void testCallWithCallback() throws DBusException, InterruptedException { - final SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - logger.debug("Doing stuff asynchronously with callback"); - CallbackHandlerImpl cbWhichWorks = new CallbackHandlerImpl(); - clientconn.callWithCallback(tri, "getName", cbWhichWorks); - - logger.debug("Doing stuff asynchronously with callback, which throws an error"); - CallbackHandlerImpl cbWhichThrows = new CallbackHandlerImpl(); - clientconn.callWithCallback(tri, "getNameAndThrow", cbWhichThrows); - - /** call something that throws */ - try { - logger.debug("Throwing stuff"); - tri.throwme(); - fail("Method Execution should have failed"); - } catch (SampleException _ex) { - logger.debug("Remote Method Failed with: " + _ex.getClass().getName() + " " + _ex.getMessage()); - if (!_ex.getMessage().equals("test")) { - fail("Error message was not correct"); - } - } - - Thread.sleep(500L); // wait some time to let the callbacks do their work - - // we do not expect any test failures - assertNull(cbWhichThrows.getLastError()); - assertNull(cbWhichWorks.getLastError()); - - assertEquals(1, cbWhichWorks.getTestHandleCalls()); - assertEquals(0, cbWhichThrows.getTestHandleCalls()); - - assertEquals(0, cbWhichWorks.getTestErrorCalls()); - assertEquals(1, cbWhichThrows.getTestErrorCalls()); - } - - @Test - public void testException() throws DBusException { - SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - /** call something that throws */ - try { - logger.debug("Throwing stuff"); - tri.throwme(); - fail("Method Execution should have failed"); - } catch (SampleException _ex) { - logger.debug("Remote Method Failed with: " + _ex.getClass().getName() + " " + _ex.getMessage()); - if (!_ex.getMessage().equals("test")) { - fail("Error message was not correct"); - } - } - - } - - @Test - public void testFails() throws DBusException { - SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", TEST_OBJECT_PATH); - - /** Try and call an invalid remote object */ - try { - logger.debug("Calling Method2"); - tri = clientconn.getRemoteObject("foo.bar.NotATest", "/Moofle", SampleRemoteInterface.class); - logger.debug("Got Remote Name: " + tri.getName()); - fail("Method Execution should have failed"); - } catch (ServiceUnknown _ex) { - logger.debug("Remote Method Failed with: " + _ex.getClass().getName() + " " + _ex.getMessage()); - } - - /** Try and call an invalid remote object */ - try { - logger.debug("Calling Method3"); - tri = clientconn.getRemoteObject("foo.bar.Test", "/Moofle", SampleRemoteInterface.class); - logger.debug("Got Remote Name: " + tri.getName()); - fail("Method Execution should have failed"); - } catch (UnknownObject _ex) { - logger.debug("Remote Method Failed with: " + _ex.getClass().getName() + " " + _ex.getMessage()); - } - - /** Try and call an explicitly unexported object */ - try { - logger.debug("Calling Method4"); - tri = clientconn.getRemoteObject("foo.bar.Test", "/BadTest", SampleRemoteInterface.class); - logger.debug("Got Remote Name: " + tri.getName()); - fail("Method Execution should have failed"); - } catch (UnknownObject _ex) { - logger.debug("Remote Method Failed with: " + _ex.getClass().getName() + " " + _ex.getMessage()); - } - - } - - @Test - public void testFallback() throws DBusException { - SampleRemoteInterface tri = - clientconn.getRemoteObject("foo.bar.Test", "/FallbackTest/0/1", SampleRemoteInterface.class); - Introspectable intro = clientconn.getRemoteObject("foo.bar.Test", "/FallbackTest/0/4", Introspectable.class); - - assertEquals("This Is A UTF-8 Name: س !!", tri.getName()); - assertTrue(intro.Introspect().startsWith(", Boolean> rv = tri2.show(234); - logger.debug("Show returned: " + rv); - if (!clientconn.getUniqueName().equals(rv.getFirstValue()) || 1 != rv.getSecondValue().size() || 1953 != rv.getSecondValue().get(0) - || !rv.getThirdValue().booleanValue()) { - fail("show return value incorrect (" + rv.getFirstValue() + "," + rv.getSecondValue() + "," + rv.getThirdValue() + ")"); - } - - logger.debug("Doing stuff asynchronously"); - @SuppressWarnings("unchecked") - DBusAsyncReply stuffreply = (DBusAsyncReply) clientconn.callMethodAsync(tri2, "dostuff", - new SampleStruct("bar", new UInt32(52), new Variant<>(Boolean.TRUE))); - - // wait a bit to allow the async call to complete - Thread.sleep(500L); - - assertFalse(tri2.check(), "bools are broken"); - - assertTrue(stuffreply.getReply(), "dostuff return value incorrect"); - - } - - @Test - public void testArrays() throws DBusException { - final SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - - List l = new ArrayList<>(); - l.add("hi"); - l.add("hello"); - l.add("hej"); - l.add("hey"); - l.add("aloha"); - logger.debug("Sampling Arrays:"); - List is = tri2.sampleArray(l, new Integer[] { - 1, 5, 7, 9 - }, new long[] { - 2, 6, 8, 12 - }); - logger.debug("sampleArray returned an array:"); - for (Integer i : is) { - logger.debug("--" + i); - } - - assertEquals(5, is.size()); - assertEquals(-1, is.get(0).intValue()); - assertEquals(-5, is.get(1).intValue()); - assertEquals(-7, is.get(2).intValue()); - assertEquals(-12, is.get(3).intValue()); - assertEquals(-18, is.get(4).intValue()); - } - - public void testSerialization() throws DBusException { - SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - List v = new ArrayList<>(); - v.add(1); - v.add(2); - v.add(3); - SampleSerializable s = new SampleSerializable<>(1, "woo", v); - s = tri2.testSerializable((byte) 12, s, 13); - logger.debug("returned: " + s); - if (s.getInt() != 1 || !s.getString().equals("woo") || s.getList().size() != 3 || s.getList().get(0) != 1 - || s.getList().get(1) != 2 || s.getList().get(2) != 3) { - fail("Didn't get back the same TestSerializable"); - } - } - - @Test - public void testComplex() throws DBusException { - SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - Collator col = Collator.getInstance(); - col.setDecomposition(Collator.FULL_DECOMPOSITION); - col.setStrength(Collator.PRIMARY); - - Map m = new HashMap<>(); - m.put("cow", "moo"); - tri2.complexv(new Variant<>(m, "a{ss}")); - logger.debug("done"); - - logger.debug("testing recursion..."); - - if (0 != col.compare("This Is A UTF-8 Name: ﺱ !!", tri2.recursionTest())) { - fail("recursion test failed"); - } - - logger.debug("done"); - - logger.debug("testing method overloading..."); - SampleRemoteInterface tri = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface.class); - if (1 != tri2.overload("foo")) { - fail("wrong overloaded method called"); - } - if (2 != tri2.overload((byte) 0)) { - fail("wrong overloaded method called"); - } - if (3 != tri2.overload()) { - fail("wrong overloaded method called"); - } - if (4 != tri.overload()) { - fail("wrong overloaded method called"); - } - } - - @Test - public void testOverload() throws DBusException { - logger.debug("testing method overloading..."); - SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - SampleRemoteInterface tri = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface.class); - - assertEquals(1, tri2.overload("foo")); - assertEquals(2, tri2.overload((byte) 0)); - assertEquals(3, tri2.overload()); - assertEquals(4, tri.overload()); - } - - @Test - public void testRegression13291() throws DBusException { - SampleRemoteInterface tri = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface.class); - - logger.debug("reg13291..."); - byte[] as = new byte[10]; - for (int i = 0; i < 10; i++) { - as[i] = (byte) (100 - i); - } - - tri.reg13291(as, as); - logger.debug("done"); - } - - @Test - public void testNestedLists() throws DBusException { - SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - List> lli = new ArrayList<>(); - List li = new ArrayList<>(); - li.add(1); - lli.add(li); - - List> reti = tri2.checklist(lli); - if (reti.size() != 1 || reti.get(0).size() != 1 || reti.get(0).get(0) != 1) { - fail("Failed to check nested lists"); - } - } - - @Test - public void testDynamicObjectCreation() throws DBusException { - SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - - SampleNewInterface tni = tri2.getNew(); - - assertEquals(tni.getName(), SampleNewInterfaceClass.class.getSimpleName()); - } - - @Test - public void testNestedListsAsync() throws DBusException, InterruptedException { - SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - List> lli = new ArrayList<>(); - List li = new ArrayList<>(); - li.add(57); - lli.add(li); - - @SuppressWarnings("unchecked") - DBusAsyncReply>> checklistReply = (DBusAsyncReply>>) clientconn.callMethodAsync(tri2, "checklist", - lli); - - // wait a bit to allow the async call to complete - Thread.sleep(500L); - - assertIterableEquals(lli, checklistReply.getReply(), "did not get back the same as sent in async"); - assertIterableEquals(li, checklistReply.getReply().get(0)); - } - - @Test - public void testNestedListsCallback() throws DBusException, InterruptedException { - SampleRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - List> lli = new ArrayList<>(); - List li = new ArrayList<>(); - li.add(25); - lli.add(li); - - NestedListCallbackHandler cbHandle = new NestedListCallbackHandler(); - - clientconn.callWithCallback(tri2, "checklist", cbHandle, lli); - - // wait a bit to allow the async call to complete - Thread.sleep(500L); - - assertIterableEquals(lli, cbHandle.getRetval(), "did not get back the same as sent in async"); - assertIterableEquals(li, cbHandle.getRetval().get(0)); - } - - @Test - public void testStructAsync() throws DBusException, InterruptedException { - SampleRemoteInterface2 tri2 = - clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, SampleRemoteInterface2.class); - SampleStruct struct = new SampleStruct("fizbuzz", new UInt32(5248), new Variant<>(2234)); - - @SuppressWarnings("unchecked") - DBusAsyncReply structReply = (DBusAsyncReply) clientconn.callMethodAsync(tri2, "returnSamplestruct", - struct); - - // wait a bit to allow the async call to complete - Thread.sleep(500L); - - assertEquals(struct, structReply.getReply(), "struct did not match"); - } - - private final class NestedListCallbackHandler implements CallbackHandler>> { - private List> retval; - - @Override - public void handle(List> _r) { - retval = _r; - } - - @Override - public void handleError(DBusExecutionException _e) { - } - - List> getRetval() { - return retval; - } - } - -} diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestCross.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestCross.java index 0cae334a..4c78a136 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestCross.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestCross.java @@ -17,7 +17,7 @@ import java.util.List; import java.util.Map.Entry; -public class TestCross extends AbstractDBusBaseTest { +public class TestCross extends AbstractDBusDaemonBaseTest { private ServerThread serverThread; private CrossTestServer cts; diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectCallback.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectCallback.java index 3f0e4018..43098986 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectCallback.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectCallback.java @@ -12,7 +12,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; -public class TestDisconnectCallback extends AbstractDBusBaseTest { +public class TestDisconnectCallback extends AbstractDBusDaemonBaseTest { @Test public void testDisconnectCallback() throws DBusException, InterruptedException { diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectStuff.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectStuff.java index 334df3ba..b54e1ec4 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectStuff.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestDisconnectStuff.java @@ -8,7 +8,7 @@ import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface; import org.junit.jupiter.api.Test; -public class TestDisconnectStuff extends AbstractDBusBaseTest { +public class TestDisconnectStuff extends AbstractDBusDaemonBaseTest { @Test public void testStuffAfterDisconnect() throws DBusException, InterruptedException { diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestShared.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestShared.java index e8043407..219d8264 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestShared.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestShared.java @@ -11,7 +11,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class TestShared extends AbstractDBusBaseTest { +public class TestShared extends AbstractDBusDaemonBaseTest { public static final String TEST_OBJECT_PATH = "/TestAll"; diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestTwoPart.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestTwoPart.java index 596c44c4..58112b24 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestTwoPart.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TestTwoPart.java @@ -11,7 +11,7 @@ import java.io.IOException; -public class TestTwoPart extends AbstractDBusBaseTest { +public class TestTwoPart extends AbstractDBusDaemonBaseTest { private volatile boolean serverReady = false; diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TypeTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TypeTest.java new file mode 100644 index 00000000..10495315 --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/TypeTest.java @@ -0,0 +1,126 @@ +package org.freedesktop.dbus.test; + +import org.freedesktop.dbus.DBusAsyncReply; +import org.freedesktop.dbus.exceptions.DBusException; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface2; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterfaceEnum; +import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterfaceEnum.TestEnum; +import org.freedesktop.dbus.test.helper.structs.SampleStruct; +import org.freedesktop.dbus.test.helper.structs.SampleStruct2; +import org.freedesktop.dbus.test.helper.structs.SampleStruct3; +import org.freedesktop.dbus.types.UInt32; +import org.freedesktop.dbus.types.Variant; +import org.junit.jupiter.api.Test; + +import java.text.Collator; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class TypeTest extends AbstractDBusBaseTest { + @Test + public void testEnum() throws DBusException { + SampleRemoteInterfaceEnum tri = (SampleRemoteInterfaceEnum) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + + assertEquals(TestEnum.TESTVAL2, tri.getEnumValue()); + } + + @Test + public void testArrays() throws DBusException { + final SampleRemoteInterface2 tri2 = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + + List l = new ArrayList<>(); + l.add("hi"); + l.add("hello"); + l.add("hej"); + l.add("hey"); + l.add("aloha"); + logger.debug("Sampling Arrays:"); + List is = tri2.sampleArray(l, new Integer[] { + 1, 5, 7, 9 + }, new long[] { + 2, 6, 8, 12 + }); + logger.debug("sampleArray returned an array:"); + for (Integer i : is) { + logger.debug("--" + i); + } + + assertEquals(5, is.size()); + assertEquals(-1, is.get(0).intValue()); + assertEquals(-5, is.get(1).intValue()); + assertEquals(-7, is.get(2).intValue()); + assertEquals(-12, is.get(3).intValue()); + assertEquals(-18, is.get(4).intValue()); + } + + @Test + public void testFloats() throws DBusException { + SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + + logger.debug("sending it to sleep"); + tri.waitawhile(); + logger.debug("testing floats"); + if (17.093f != tri.testfloat(new float[] { + 17.093f, -23f, 0.0f, 31.42f + })) { + fail("testfloat returned the wrong thing"); + } + } + + @Test + public void testStruct() throws DBusException { + final SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + + List> lli = new ArrayList<>(); + List li = new ArrayList<>(); + li.add(1); + li.add(2); + li.add(3); + lli.add(li); + lli.add(li); + lli.add(li); + SampleStruct3 ts3 = new SampleStruct3(new SampleStruct2(new ArrayList<>(), new Variant<>(0)), lli); + int[][] out = tri.teststructstruct(ts3); + if (out.length != 3) { + fail("teststructstruct returned the wrong thing: " + Arrays.deepToString(out)); + } + for (int[] o : out) { + if (o.length != 3 || o[0] != 1 || o[1] != 2 || o[2] != 3) { + fail("teststructstruct returned the wrong thing: " + Arrays.deepToString(out)); + } + } + } + + @Test + public void testStructAsync() throws DBusException, InterruptedException { + SampleRemoteInterface2 tri2 = + clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), SampleRemoteInterface2.class); + SampleStruct struct = new SampleStruct("fizbuzz", new UInt32(5248), new Variant<>(2234)); + + @SuppressWarnings("unchecked") + DBusAsyncReply structReply = (DBusAsyncReply) clientconn.callMethodAsync(tri2, "returnSamplestruct", + struct); + + // wait a bit to allow the async call to complete + Thread.sleep(500L); + + assertEquals(struct, structReply.getReply(), "struct did not match"); + } + + @Test + public void testCallGetUtf8String() throws DBusException { + SampleRemoteInterface tri = (SampleRemoteInterface) clientconn.getPeerRemoteObject(getTestBusName(), getTestObjectPath()); + /** Call the remote object and get a response. */ + String rname = tri.getName(); + + Collator col = Collator.getInstance(); + col.setDecomposition(Collator.FULL_DECOMPOSITION); + col.setStrength(Collator.PRIMARY); + + if (0 != col.compare("This Is A UTF-8 Name: ﺱ !!", rname)) { + fail("getName return value incorrect"); + } + } +} diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/collections/empty/TestEmptyCollections.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/collections/empty/TestEmptyCollections.java index ae97b89d..c25bd46a 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/collections/empty/TestEmptyCollections.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/collections/empty/TestEmptyCollections.java @@ -4,7 +4,7 @@ import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusExecutionException; -import org.freedesktop.dbus.test.AbstractDBusBaseTest; +import org.freedesktop.dbus.test.AbstractDBusDaemonBaseTest; import org.freedesktop.dbus.test.collections.empty.structs.*; import org.freedesktop.dbus.test.helper.structs.IntStruct; import org.freedesktop.dbus.utils.Util; @@ -40,7 +40,7 @@ * used to determine whether (de)serialization of non empty collection is executed correctly. * */ -class TestEmptyCollections extends AbstractDBusBaseTest { +class TestEmptyCollections extends AbstractDBusDaemonBaseTest { private DBusConnection serverconn; private DBusConnection clientconn; diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/helper/NestedListCallbackHandler.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/helper/NestedListCallbackHandler.java new file mode 100644 index 00000000..38b02ee6 --- /dev/null +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/helper/NestedListCallbackHandler.java @@ -0,0 +1,23 @@ +package org.freedesktop.dbus.test.helper; + +import org.freedesktop.dbus.exceptions.DBusExecutionException; +import org.freedesktop.dbus.interfaces.CallbackHandler; + +import java.util.List; + +public final class NestedListCallbackHandler implements CallbackHandler>> { + private List> retval; + + @Override + public void handle(List> _r) { + retval = _r; + } + + @Override + public void handleError(DBusExecutionException _e) { + } + + public List> getRetval() { + return retval; + } +} diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/helper/SampleClass.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/helper/SampleClass.java index 597c8414..84e86c28 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/helper/SampleClass.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/helper/SampleClass.java @@ -10,16 +10,11 @@ import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.interfaces.DBusInterface; import org.freedesktop.dbus.interfaces.Properties; -import org.freedesktop.dbus.test.TestAll; import org.freedesktop.dbus.test.helper.interfaces.SampleNewInterface; import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface; import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterface2; import org.freedesktop.dbus.test.helper.interfaces.SampleRemoteInterfaceEnum; -import org.freedesktop.dbus.test.helper.structs.IntStruct; -import org.freedesktop.dbus.test.helper.structs.SampleStruct; -import org.freedesktop.dbus.test.helper.structs.SampleStruct3; -import org.freedesktop.dbus.test.helper.structs.SampleStruct4; -import org.freedesktop.dbus.test.helper.structs.SampleTuple; +import org.freedesktop.dbus.test.helper.structs.*; import org.freedesktop.dbus.types.UInt16; import org.freedesktop.dbus.types.UInt32; import org.freedesktop.dbus.types.Variant; @@ -28,11 +23,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @SuppressWarnings({"checkstyle:methodname"}) public class SampleClass implements SampleRemoteInterface, SampleRemoteInterface2, SampleRemoteInterfaceEnum, Properties { @@ -257,9 +248,9 @@ public SampleSerializable testSerializable(byte _b, SampleSerializable testSerializable(byte _b, SampleSerializable _s, int _i); @IntrospectionDescription("Call another method on itself from within a call") - String recursionTest(); + String recursionTest(String _dbusName, String _path); @IntrospectionDescription("Parameter-overloaded method (string)") int overload(String _s);