Logo Search packages:      
Sourcecode: dbus-java version File versions  Download package

static void org::freedesktop::dbus::test::test::main ( String[]  args  )  [inline, static]

This gets a remote object matching our bus name and exported object path.

This registers an instance of the test class as the signal handler for the TestSignal class.

This exports an instance of the test class as the object /Test.

This creates an instance of the Test Signal, with the given object path, signal name and parameters, and broadcasts in on the Bus.

This gets a remote object matching our bus name and exported object path.

Get introspection data

Get introspection data

Call ping.

This gets a remote object matching our bus name and exported object path.

Call the remote object and get a response.

call something that throws

Try and call an invalid remote object

Try and call an invalid remote object

Try and call an explicitly unexported object

Try and call an implicitly unexported object

This gets a remote object matching our bus name and exported object path.

Call the remote object and get a response.

This creates an instance of the Test Signal, with the given object path, signal name and parameters, and broadcasts in on the Bus.

Pause while we wait for the DBus messages to go back and forth.

Disconnect from the bus.

Definition at line 419 of file test.java.

References org::freedesktop::dbus::AbstractConnection::addFallback(), org::freedesktop::dbus::DBusConnection::addSigHandler(), org::freedesktop::dbus::AbstractConnection::callMethodAsync(), org::freedesktop::dbus::AbstractConnection::callWithCallback(), org::freedesktop::dbus::DBusConnection::disconnect(), org::freedesktop::dbus::AbstractConnection::exportObject(), org::freedesktop::dbus::DBusConnection::getConnection(), org::freedesktop::dbus::AbstractConnection::getError(), org::freedesktop::dbus::test::TestNewInterface::getName(), org::freedesktop::dbus::test::TestRemoteInterface::getName(), org::freedesktop::dbus::DBusConnection::getPeerRemoteObject(), org::freedesktop::dbus::DBusConnection::getRemoteObject(), org::freedesktop::dbus::DBusConnection::getUniqueName(), org::freedesktop::DBus::Introspectable::Introspect(), org::freedesktop::DBus::Peer::Ping(), org::freedesktop::dbus::DBusConnection::removeSigHandler(), org::freedesktop::dbus::DBusConnection::requestBusName(), org::freedesktop::dbus::AbstractConnection::sendSignal(), org::freedesktop::dbus::AbstractConnection::setWeakReferences(), org::freedesktop::dbus::test::TestRemoteInterface::testfloat(), and org::freedesktop::dbus::AbstractConnection::unExportObject().

   { try {
      System.out.println("Creating Connection");
      serverconn = DBusConnection.getConnection(DBusConnection.SESSION);
      clientconn = DBusConnection.getConnection(DBusConnection.SESSION);
      serverconn.setWeakReferences(true);
      clientconn.setWeakReferences(true);
      
      System.out.println("Registering Name");
      serverconn.requestBusName("foo.bar.Test");
      
      /** This gets a remote object matching our bus name and exported object path. */
      Peer peer = clientconn.getRemoteObject("foo.bar.Test", "/Test", Peer.class);
      DBus dbus = clientconn.getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class);

      System.out.print("Listening for signals...");
      try {
         /** This registers an instance of the test class as the signal handler for the TestSignal class. */
         clientconn.addSigHandler(TestSignalInterface.TestSignal.class, new signalhandler());
         String source = dbus.GetNameOwner("foo.bar.Test");
         clientconn.addSigHandler(TestSignalInterface.TestArraySignal.class, source, peer, new arraysignalhandler());
         clientconn.addSigHandler(TestSignalInterface.TestObjectSignal.class, new objectsignalhandler());
         badarraysignalhandler<TestSignalInterface.TestSignal> bash = new badarraysignalhandler<TestSignalInterface.TestSignal>();
         clientconn.addSigHandler(TestSignalInterface.TestSignal.class, bash);
         clientconn.removeSigHandler(TestSignalInterface.TestSignal.class, bash);
         System.out.println("done");
      } catch (MatchRuleInvalid MRI) {
         test.fail("Failed to add handlers: "+MRI.getMessage());
      } catch (DBusException DBe) {
         test.fail("Failed to add handlers: "+DBe.getMessage());
      }
      
      System.out.println("Listening for Method Calls");
      testclass tclass = new testclass(serverconn);
      testclass tclass2 = new testclass(serverconn);
      /** This exports an instance of the test class as the object /Test. */
      serverconn.exportObject("/Test", tclass);
      serverconn.exportObject("/BadTest", tclass);
      serverconn.exportObject("/BadTest2", tclass2);
      serverconn.addFallback("/FallbackTest", tclass);

      // explicitly unexport object
      serverconn.unExportObject("/BadTest");
      // implicitly unexport object
      tclass2 = null;
      System.gc();
      System.runFinalization();
      System.gc();
      System.runFinalization();
      System.gc();
      System.runFinalization();
      
      System.out.println("Sending Signal");
      /** This creates an instance of the Test Signal, with the given object path, signal name and parameters, and broadcasts in on the Bus. */
      serverconn.sendSignal(new TestSignalInterface.TestSignal("/foo/bar/Wibble", "Bar", new UInt32(42)));

      System.out.println("These things are on the bus:");
      String[] names = dbus.ListNames();
      for (String name: names)
         System.out.println("\t"+name);
      
      System.out.println("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);
      /** Get introspection data */
      String data = intro.Introspect();
      if (null == data || !data.startsWith("<!DOCTYPE"))
         fail("Introspection data invalid");
      System.out.println("Got Introspection Data: \n"+data);
      intro = clientconn.getRemoteObject("foo.bar.Test", "/Test", Introspectable.class);
      /** Get introspection data */
      data = intro.Introspect();
      if (null == data || !data.startsWith("<!DOCTYPE"))
         fail("Introspection data invalid");
      System.out.println("Got Introspection Data: \n"+data);
      
      System.out.println("Pinging ourselves");
      /** Call ping. */
      for (int i = 0; i < 10; i++) {
         long then = System.currentTimeMillis();
         peer.Ping();
         long now = System.currentTimeMillis();
         System.out.println("Ping returned in "+(now-then)+"ms.");
      }
      
      System.out.println("Calling Method0/1");
      /** This gets a remote object matching our bus name and exported object path. */
      TestRemoteInterface tri = (TestRemoteInterface) clientconn.getPeerRemoteObject("foo.bar.Test", "/Test");
      System.out.println("Got Remote Object: "+tri);
      /** Call the remote object and get a response. */
      String rname = tri.getName();
      System.out.println("Got Remote Name: "+rname);
      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");
      System.out.println("sending it to sleep");
      tri.waitawhile();
      System.out.println("testing floats");
      if (17.093f != tri.testfloat(new float[] { 17.093f, -23f, 0.0f, 31.42f }))
         fail("testfloat returned the wrong thing");
      System.out.println("Structs of Structs");
      List<List<Integer>> lli = new Vector<List<Integer>>();
      List<Integer> li = new Vector<Integer>();
      li.add(1);
      li.add(2);
      li.add(3);
      lli.add(li);
      lli.add(li);
      lli.add(li);
      TestStruct3 ts3 = new TestStruct3(new TestStruct2(new Vector<String>(), new Variant<Integer>(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));

      System.out.println("frobnicating");
      List<Long> ls = new Vector<Long>();
      ls.add(2L);
      ls.add(5L);
      ls.add(71L);
      Map<UInt16,Short> mus = new HashMap<UInt16,Short>();
      mus.put(new UInt16(4), (short) 5);
      mus.put(new UInt16(5), (short) 6);
      mus.put(new UInt16(6), (short) 7);
      Map<String,Map<UInt16,Short>> msmus = new HashMap<String,Map<UInt16,Short>>();
      msmus.put("stuff", mus);
      int rint = tri.frobnicate(ls, msmus, 13);
      if (-5 != rint)
         fail("frobnicate return value incorrect");
 
      System.out.println("Doing stuff asynchronously with callback");
      clientconn.callWithCallback(tri, "getName", new callbackhandler());

      /** call something that throws */
      try {
         System.out.println("Throwing stuff");
         tri.throwme();
         test.fail("Method Execution should have failed");
      } catch (TestException Te) {
         System.out.println("Remote Method Failed with: "+Te.getClass().getName()+" "+Te.getMessage());
         if (!Te.getMessage().equals("test"))
            test.fail("Error message was not correct");
      }

      /* Test type signatures */
      Vector<Type> ts = new Vector<Type>();
      Marshalling.getJavaType("ya{si}", ts, -1);
      tri.sig(ts.toArray(new Type[0]));

      tri.newpathtest(new Path("/new/path/test"));
     
      /** Try and call an invalid remote object */
      try {
         System.out.println("Calling Method2");
         tri = clientconn.getRemoteObject("foo.bar.NotATest", "/Moofle", TestRemoteInterface.class);
         System.out.println("Got Remote Name: "+tri.getName());
         test.fail("Method Execution should have failed");
      } catch (ServiceUnknown SU) {
         System.out.println("Remote Method Failed with: "+SU.getClass().getName()+" "+SU.getMessage());
      }
      
      /** Try and call an invalid remote object */
      try {
         System.out.println("Calling Method3");
         tri = clientconn.getRemoteObject("foo.bar.Test", "/Moofle", TestRemoteInterface.class);
         System.out.println("Got Remote Name: "+tri.getName());
         test.fail("Method Execution should have failed");
      } catch (UnknownObject UO) {
         System.out.println("Remote Method Failed with: "+UO.getClass().getName()+" "+UO.getMessage());
      }
      
      /** Try and call an explicitly unexported object */
      try {
         System.out.println("Calling Method4");
         tri = clientconn.getRemoteObject("foo.bar.Test", "/BadTest", TestRemoteInterface.class);
         System.out.println("Got Remote Name: "+tri.getName());
         test.fail("Method Execution should have failed");
      } catch (UnknownObject UO) {
         System.out.println("Remote Method Failed with: "+UO.getClass().getName()+" "+UO.getMessage());
      }
 
      /** Try and call an implicitly unexported object */
      try {
         System.out.println("Calling Method5");
         tri = clientconn.getRemoteObject("foo.bar.Test", "/BadTest2", TestRemoteInterface.class);
         System.out.println("Got Remote Name: "+tri.getName());
         test.fail("Method Execution should have failed");
      } catch (UnknownObject UO) {
         System.out.println("Remote Method Failed with: "+UO.getClass().getName()+" "+UO.getMessage());
      }

      System.out.println("Calling Method6");
      tri = clientconn.getRemoteObject("foo.bar.Test", "/FallbackTest/0/1", TestRemoteInterface.class);
      intro = clientconn.getRemoteObject("foo.bar.Test", "/FallbackTest/0/4", Introspectable.class);
      System.out.println("Got Fallback Name: "+tri.getName());
      System.out.println("Fallback Introspection Data: \n"+intro.Introspect());

      System.out.println("Calling Method7--9");
      /** This gets a remote object matching our bus name and exported object path. */
      TestRemoteInterface2 tri2 = clientconn.getRemoteObject("foo.bar.Test", "/Test", TestRemoteInterface2.class);
      System.out.print("Calling the other introspect method: ");
      String intro2 = tri2.Introspect();
      System.out.println(intro2);
      if (0 != col.compare("Not XML", intro2))
         fail("Introspect return value incorrect");

      /** Call the remote object and get a response. */
      TestTuple<String,List<Integer>,Boolean> rv = tri2.show(234);
      System.out.println("Show returned: "+rv);
      if (!serverconn.getUniqueName().equals(rv.a) ||
            1 != rv.b.size() ||
            1953 != rv.b.get(0) ||
            true != rv.c.booleanValue())
         fail("show return value incorrect ("+rv.a+","+rv.b+","+rv.c+")");
      
      System.out.println("Doing stuff asynchronously");
      DBusAsyncReply<Boolean> stuffreply = (DBusAsyncReply<Boolean>) clientconn.callMethodAsync(tri2, "dostuff", new TestStruct("bar", new UInt32(52), new Variant<Boolean>(new Boolean(true))));

      System.out.println("Checking bools");
      if (tri2.check()) fail("bools are broken");
         
      List<String> l = new Vector<String>();
      l.add("hi");
      l.add("hello");
      l.add("hej");
      l.add("hey");
      l.add("aloha");
      System.out.println("Sampling Arrays:");
      List<Integer> is = tri2.sampleArray(l, new Integer[] { 1, 5, 7, 9 }, new long[] { 2, 6, 8, 12 });
      System.out.println("sampleArray returned an array:");
      for (Integer i: is)
         System.out.println("--"+i);
      if (is.size() != 5 ||
            is.get(0).intValue() != -1 ||
            is.get(1).intValue() != -5 ||
            is.get(2).intValue() != -7 ||
            is.get(3).intValue() != -12 ||
            is.get(4).intValue() != -18)
         fail("sampleArray return value incorrect");

      System.out.println("Get This");
      if (!tclass.equals(tri2.getThis(tri2)))
         fail("Didn't get the correct this");
      
      Boolean b = stuffreply.getReply();
      System.out.println("Do stuff replied "+b);
      if (true != b.booleanValue())
         fail("dostuff return value incorrect");
      
      System.out.print("Sending Array Signal...");
      /** This creates an instance of the Test Signal, with the given object path, signal name and parameters, and broadcasts in on the Bus. */
      List<TestStruct2> tsl = new Vector<TestStruct2>();
      tsl.add(new TestStruct2(l, new Variant<UInt64>(new UInt64(567))));
      serverconn.sendSignal(new TestSignalInterface.TestArraySignal("/Test", tsl));
      
      System.out.println("done");

      System.out.print("testing custom serialization...");
      Vector<Integer> v = new Vector<Integer>();
      v.add(1);
      v.add(2);
      v.add(3);
      TestSerializable<String> s = new TestSerializable<String>(1, "woo", v);
      s = tri2.testSerializable((byte) 12, s, 13);
      System.out.print("returned: "+s);
      if (s.getInt() != 1 || 
          ! s.getString().equals("woo") ||
          s.getVector().size() != 3 ||
          s.getVector().get(0) != 1 ||
          s.getVector().get(1) != 2 ||
          s.getVector().get(2) != 3)
         fail("Didn't get back the same TestSerializable");
      
      System.out.println("done");

      System.out.print("testing complex variants...");
      Map m = new HashMap();
      m.put("cow", "moo");
      tri2.complexv(new Variant(m, "a{ss}"));
      System.out.println("done");
      
      System.out.print("testing recursion...");
      
      if (0 != col.compare("This Is A UTF-8 Name: ﺱ !!",tri2.recursionTest())) fail("recursion test failed");
      
      System.out.println("done");

      System.out.print("testing method overloading...");
      tri = clientconn.getRemoteObject("foo.bar.Test", "/Test", TestRemoteInterface.class);
      if (1 != tri2.overload("foo")) test.fail("wrong overloaded method called");
      if (2 != tri2.overload((byte) 0)) test.fail("wrong overloaded method called");
      if (3 != tri2.overload()) test.fail("wrong overloaded method called");
      if (4 != tri.overload()) test.fail("wrong overloaded method called");
      System.out.println("done");

      System.out.print("Testing nested lists...");
      lli = new Vector<List<Integer>>();
      li = new Vector<Integer>();
      li.add(1);
      lli.add(li);
      List<List<Integer>> reti = tri2.checklist(lli);
      if (reti.size() != 1 ||
          reti.get(0).size() != 1 ||
          reti.get(0).get(0) != 1)
         test.fail("Failed to check nested lists");
      System.out.println("done");

      System.out.print("Testing dynamic object creation...");
      TestNewInterface tni = tri2.getNew();
      System.out.print(tni.getName()+" ");
      System.out.println("done");

      /* send an object in a signal */
      serverconn.sendSignal(new TestSignalInterface.TestObjectSignal("/foo/bar/Wibble", tclass));

      /** Pause while we wait for the DBus messages to go back and forth. */
      Thread.sleep(1000);

      System.out.println("Checking for outstanding errors");
      DBusExecutionException DBEe = serverconn.getError();
      if (null != DBEe) throw DBEe;
      DBEe = clientconn.getError();
      if (null != DBEe) throw DBEe;
    
      System.out.println("Disconnecting");
      /** Disconnect from the bus. */
      clientconn.disconnect();
      clientconn = null;
      serverconn.disconnect();
      serverconn = null;

      if (!done1) fail("Signal handler 1 failed to be run");
      if (!done2) fail("Signal handler 2 failed to be run");
      if (!done3) fail("Signal handler 3 failed to be run");
      if (!done4) fail("Callback handler failed to be run");
      
   } catch (Exception e) {
      e.printStackTrace();
      fail("Unexpected Exception Occurred");
   }}


Generated by  Doxygen 1.6.0   Back to index