home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Test / MockObject.pm < prev   
Encoding:
Perl POD Document  |  2008-02-20  |  7.9 KB  |  319 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Test::MockObject - Fake an object from the bus for unit testing
  24.  
  25. =head1 SYNOPSIS
  26.  
  27.   use Net::DBus;
  28.   use Net::DBus::Test::MockObject;
  29.  
  30.   my $bus = Net::DBus->test
  31.  
  32.   # Lets fake presence of HAL...
  33.  
  34.   # First we need to define the service 
  35.   my $service = $bus->export_service("org.freedesktop.Hal");
  36.  
  37.   # Then create a mock object
  38.   my $object = Net::DBus::Test::MockObject->new($service,
  39.                                                 "/org/freedesktop/Hal/Manager");
  40.  
  41.   # Fake the 'GetAllDevices' method
  42.   $object->seed_action("org.freedesktop.Hal.Manager", 
  43.                        "GetAllDevices",
  44.                        reply => {
  45.                          return => [ "/org/freedesktop/Hal/devices/computer_i8042_Aux_Port",
  46.                                      "/org/freedesktop/Hal/devices/computer_i8042_Aux_Port_logicaldev_input",
  47.                                      "/org/freedesktop/Hal/devices/computer_i8042_Kbd_Port",
  48.                                      "/org/freedesktop/Hal/devices/computer_i8042_Kbd_Port_logicaldev_input"
  49.                          ],
  50.                        });
  51.  
  52.  
  53.   # Now can test any class which calls out to 'GetAllDevices' in HAL
  54.   ....test stuff....
  55.  
  56. =head1 DESCRIPTION
  57.  
  58. This provides an alternate for L<Net::DBus::Object> to enable bus 
  59. objects to be quickly mocked up, thus facilitating creation of unit 
  60. tests for services which may need to call out to objects provided
  61. by 3rd party services on the bus. It is typically used as a companion
  62. to the L<Net::DBus::MockBus> object, to enable complex services to
  63. be tested without actually starting a real bus.
  64.  
  65. !!!!! WARNING !!!
  66.  
  67. This object & its APIs should be considered very experimental at
  68. this point in time, and no guarentees about future API compatability
  69. are provided what-so-ever. Comments & suggestions on how to evolve
  70. this framework are, however, welcome & encouraged.
  71.  
  72. =head1 METHODS
  73.  
  74. =over 4
  75.  
  76. =cut
  77.  
  78. package Net::DBus::Test::MockObject;
  79.  
  80. use strict;
  81. use warnings;
  82.  
  83. =item my $object = Net::DBus::Test::MockObject->new($service, $path, $interface);
  84.  
  85. Create a new mock object, attaching to the service defined by the C<$service>
  86. parameter. This would be an instance of the L<Net::DBus::Service> object. The
  87. C<$path> parameter defines the object path at which to attach this mock object,
  88. and C<$interface> defines the interface it will support.
  89.  
  90. =cut
  91.  
  92. sub new {
  93.     my $class = shift;
  94.     my $self = {};
  95.  
  96.     $self->{service} = shift;
  97.     $self->{object_path} = shift;
  98.     $self->{interface} = shift;
  99.     $self->{actions} = {};
  100.     $self->{message} = shift;
  101.  
  102.     bless $self, $class;
  103.    
  104.     $self->get_service->_register_object($self);
  105.  
  106.     return $self;
  107. }
  108.  
  109.  
  110. sub _get_sub_nodes {
  111.     my $self = shift;
  112.     return [];
  113. }
  114.  
  115. =item my $service = $object->get_service
  116.  
  117. Retrieves the L<Net::DBus::Service> object within which this
  118. object is exported.
  119.  
  120. =cut
  121.  
  122. sub get_service {
  123.     my $self = shift;
  124.     return $self->{service};
  125. }
  126.  
  127. =item my $path = $object->get_object_path
  128.  
  129. Retrieves the path under which this object is exported
  130.  
  131. =cut
  132.  
  133. sub get_object_path {
  134.     my $self = shift;
  135.     return $self->{object_path};
  136. }
  137.  
  138.  
  139. =item my $msg = $object->get_last_message
  140.  
  141. Retrieves the last message processed by this object. The returned
  142. object is an instance of L<Net::DBus::Binding::Message>
  143.  
  144. =cut
  145.  
  146. sub get_last_message {
  147.     my $self = shift;
  148.     return $self->{message};
  149. }
  150.  
  151. =item my $sig = $object->get_last_message_signature
  152.  
  153. Retrieves the type signature of the last processed message.
  154.  
  155. =cut
  156.  
  157. sub get_last_message_signature {
  158.     my $self = shift;
  159.     return $self->{message}->get_signature;
  160. }
  161.  
  162. =item my $value = $object->get_last_message_param
  163.  
  164. Returns the first value supplied as an argument to the last
  165. processed message.
  166.  
  167. =cut
  168.  
  169. sub get_last_message_param {
  170.     my $self = shift;
  171.     my @args = $self->{message}->get_args_list;
  172.     return $args[0];
  173. }
  174.  
  175. =item my @values = $object->get_last_message_param_list
  176.  
  177. Returns a list of all the values supplied as arguments to 
  178. the last processed message.
  179.  
  180. =cut
  181.  
  182. sub get_last_message_param_list {
  183.     my $self = shift;
  184.     my @args = $self->{message}->get_args_list;
  185.     return \@args;
  186. }
  187.  
  188. =item $object->seed_action($interface, $method, %action);
  189.  
  190. Registers an action to be performed when a message corresponding
  191. to the method C<$method> within the interface C<$interface> is
  192. received. The C<%action> parameter can have a number of possible
  193. keys set:
  194.  
  195. =over 4
  196.  
  197. =item signals
  198.  
  199. Causes a signal to be emitted when the method is invoked. The 
  200. value associated with this key should be an instance of the
  201. L<Net::DBus::Binding::Message::Signal> class.
  202.  
  203. =item error
  204.  
  205. Causes an error to be generated when the method is invoked. The
  206. value associated with this key should be a hash reference, with
  207. two elements. The first, C<name>, giving the error name, and the
  208. second, C<description>, providing the descriptive text.
  209.  
  210. =item reply
  211.  
  212. Causes a normal method return to be generated. The value associated
  213. with this key should be an array reference, whose elements are the
  214. values to be returned by the method.
  215.  
  216. =back
  217.  
  218. =cut
  219.  
  220. sub seed_action {
  221.     my $self = shift;
  222.     my $interface = shift;
  223.     my $method = shift;
  224.     my %action = @_;
  225.     
  226.     $self->{actions}->{$method} = {} unless exists $self->{actions}->{$method};
  227.     $self->{actions}->{$method}->{$interface} = \%action;
  228. }
  229.  
  230. sub _dispatch {
  231.     my $self = shift;
  232.     my $connection = shift;
  233.     my $message = shift;
  234.     
  235.     my $interface = $message->get_interface;
  236.     my $method = $message->get_member;
  237.  
  238.     my $con = $self->get_service->get_bus->get_connection;
  239.  
  240.     if (!exists $self->{actions}->{$method}) {
  241.     my $error = $con->make_error_message($message,
  242.                          "org.freedesktop.DBus.Failed",
  243.                          "no action seeded for method " . $message->get_member);
  244.     $con->send($error);
  245.     return;
  246.     }
  247.     
  248.     my $action;
  249.     if ($interface) {
  250.     if (!exists $self->{actions}->{$method}->{$interface}) {
  251.         my $error = $con->make_error_message($message,
  252.                          "org.freedesktop.DBus.Failed",
  253.                          "no action with correct interface seeded for method " . $message->get_member);
  254.         $con->send($error);
  255.         return;
  256.     }
  257.     $action = $self->{actions}->{$method}->{$interface};
  258.     } else {
  259.     my @interfaces = keys %{$self->{actions}->{$method}};
  260.     if ($#interfaces > 0) {
  261.         my $error = $con->make_error_message($message,
  262.                          "org.freedesktop.DBus.Failed",
  263.                          "too many actions seeded for method " . $message->get_member);
  264.         $con->send($error);
  265.         return;
  266.     }
  267.     $action = $self->{actions}->{$method}->{$interfaces[0]};
  268.     }
  269.  
  270.     if (exists $action->{signals}) {
  271.     my $sigs = $action->{signals};
  272.     if (ref($sigs) ne "ARRAY") {
  273.         $sigs = [ $sigs ];
  274.     }
  275.     foreach my $sig (@{$sigs}) {
  276.         $self->get_service->get_bus->get_connection->send($sig);
  277.     }
  278.     }
  279.  
  280.     $self->{message} = $message;
  281.     
  282.     if (exists $action->{error}) {
  283.     my $error = $con->make_error_message($message,
  284.                          $action->{error}->{name},
  285.                          $action->{error}->{description});
  286.     $con->send($error);
  287.     } elsif (exists $action->{reply}) {
  288.     my $reply = $con->make_method_return_message($message);
  289.     my $iter = $reply->iterator(1);
  290.     foreach my $value (@{$action->{reply}->{return}}) {
  291.         $iter->append($value);
  292.     }
  293.     $con->send($reply);
  294.     }
  295. }
  296.  
  297.  
  298. 1;
  299.  
  300. =pod
  301.  
  302. =back
  303.  
  304. =head1 BUGS
  305.  
  306. It doesn't completely replicate the API of L<Net::DBus::Binding::Object>, 
  307. merely enough to make the high level bindings work in a test scenario.
  308.  
  309. =head1 SEE ALSO
  310.  
  311. L<Net::DBus>, L<Net::DBus::Object>, L<Net::DBus::Test::MockConnection>,
  312. L<http://www.mockobjects.com/Faq.html>
  313.  
  314. =head1 COPYRIGHT
  315.  
  316. Copyright 2005 Daniel Berrange <dan@berrange.com>
  317.  
  318. =cut
  319.