|
| 1 | +package org.freedesktop.dbus.test; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +import org.freedesktop.dbus.annotations.DBusInterfaceName; |
| 12 | +import org.freedesktop.dbus.connections.impl.DBusConnection; |
| 13 | +import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder; |
| 14 | +import org.freedesktop.dbus.exceptions.DBusException; |
| 15 | +import org.freedesktop.dbus.interfaces.DBusInterface; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +public class ExportNestedTest extends AbstractDBusBaseTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testExportNested() throws IOException, DBusException { |
| 22 | + try (DBusConnection conn = DBusConnectionBuilder.forSessionBus().build()) { |
| 23 | + var part1 = new MyObjectPart(); |
| 24 | + part1.setVal1("ABC"); |
| 25 | + part1.setVal2("123"); |
| 26 | + |
| 27 | + var part2 = new MyObjectPart(); |
| 28 | + part2.setVal1("DEF"); |
| 29 | + part2.setVal2("456"); |
| 30 | + |
| 31 | + var myIface = new MyObject(); |
| 32 | + myIface.getParts().addAll(Arrays.asList(part1, part2)); |
| 33 | + |
| 34 | + conn.requestBusName("com.acme"); |
| 35 | + conn.exportObject(part1); |
| 36 | + conn.exportObject(part2); |
| 37 | + conn.exportObject(myIface); |
| 38 | + |
| 39 | + try (DBusConnection innerConn = DBusConnectionBuilder.forSessionBus().build()) { |
| 40 | + var myObject = innerConn.getRemoteObject("com.acme", "/com/acme/MyObject", MyInterface.class); |
| 41 | + |
| 42 | + // hello from 'parent' object |
| 43 | + assertEquals("Hello!", myObject.sayHello()); |
| 44 | + |
| 45 | + List<String> partNames = myObject.getPartNames(); |
| 46 | + assertEquals(2, partNames.size()); |
| 47 | + |
| 48 | + // all names used in child objects |
| 49 | + assertEquals(partNames.get(0), "ABC"); |
| 50 | + assertEquals(partNames.get(1), "DEF"); |
| 51 | + |
| 52 | + List<MyInterfacePart> parts = myObject.getParts(); |
| 53 | + assertEquals(2, parts.size()); |
| 54 | + // check the child objects |
| 55 | + assertEquals("123", parts.get(0).getVal2()); |
| 56 | + assertEquals("456", parts.get(1).getVal2()); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @DBusInterfaceName("com.acme.MyInterface") |
| 62 | + public interface MyInterface extends DBusInterface { |
| 63 | + |
| 64 | + String sayHello(); |
| 65 | + |
| 66 | + List<MyInterfacePart> getParts(); |
| 67 | + |
| 68 | + List<String> getPartNames(); |
| 69 | + } |
| 70 | + |
| 71 | + public static class MyObject implements MyInterface { |
| 72 | + |
| 73 | + private List<MyInterfacePart> parts = new ArrayList<>(); |
| 74 | + |
| 75 | + public String sayHello() { |
| 76 | + return "Hello!"; |
| 77 | + } |
| 78 | + |
| 79 | + public List<MyInterfacePart> getParts() { |
| 80 | + return parts; |
| 81 | + } |
| 82 | + |
| 83 | + public String getObjectPath() { |
| 84 | + return "/com/acme/MyObject"; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public List<String> getPartNames() { |
| 89 | + return parts.stream().map(i -> i.getVal1()).collect(Collectors.toList()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @DBusInterfaceName("com.acme.MyInterfacePart") |
| 94 | + public interface MyInterfacePart extends DBusInterface { |
| 95 | + |
| 96 | + String getVal1(); |
| 97 | + |
| 98 | + String getVal2(); |
| 99 | + } |
| 100 | + |
| 101 | + public static class MyObjectPart implements MyInterfacePart { |
| 102 | + |
| 103 | + private String val1; |
| 104 | + private String val2; |
| 105 | + |
| 106 | + public String getVal1() { |
| 107 | + return val1; |
| 108 | + } |
| 109 | + |
| 110 | + public void setVal1(String val1) { |
| 111 | + this.val1 = val1; |
| 112 | + } |
| 113 | + |
| 114 | + public String getVal2() { |
| 115 | + return val2; |
| 116 | + } |
| 117 | + |
| 118 | + public void setVal2(String val2) { |
| 119 | + this.val2 = val2; |
| 120 | + } |
| 121 | + |
| 122 | + public String getObjectPath() { |
| 123 | + return "/com/acme/MyPart" + val1; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments