root/honeyclient/tags/exp/DOWN1-kindlund-multi_config/t/honeyclient_util_config.t

Revision 256, 7.9 kB (checked in by kindlund, 2 years ago)

Finished initial implementation of multi-value XML configuration data.

Line 
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 $| = 1;
6
7
8
9 # =begin testing
10 {
11 # Make sure Log::Log4perl loads
12 BEGIN { use_ok('Log::Log4perl', qw(:nowarn))
13         or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path.");
14        
15         # Suppress all logging messages, since we need clean output for unit testing.
16         Log::Log4perl->init({
17             "log4perl.rootLogger"                               => "DEBUG, Buffer",
18             "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
19             "log4perl.appender.Buffer.min_level"                => "fatal",
20             "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
21             "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
22         });
23 }
24 require_ok('Log::Log4perl');
25 use Log::Log4perl qw(:easy);
26
27 # Make sure the module loads properly, with the exportable
28 # functions shared.
29 BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar setVar))
30         or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path."); }
31 require_ok('HoneyClient::Util::Config');
32 can_ok('HoneyClient::Util::Config', 'getVar');
33 can_ok('HoneyClient::Util::Config', 'setVar');
34 use HoneyClient::Util::Config qw(getVar setVar);
35
36 # Suppress all logging messages, since we need clean output for unit testing.
37 Log::Log4perl->init({
38     "log4perl.rootLogger"                               => "DEBUG, Buffer",
39     "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer",
40     "log4perl.appender.Buffer.min_level"                => "fatal",
41     "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout",
42     "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n",
43 });
44
45 # Make sure XML::XPath loads.
46 BEGIN { use_ok('XML::XPath')
47         or diag("Can't load XML::XPath package.  Check to make sure the package library is correctly listed within the path."); }
48 require_ok('XML::XPath');
49 can_ok('XML::XPath', 'findnodes');
50 use XML::XPath;
51
52 # Make sure XML::Tidy loads
53 BEGIN { use_ok('XML::Tidy')
54         or diag("Can't load XML::Tidy package. Check to make sure the package library is correctly listed within the path."); }
55 require_ok('XML::Tidy');
56 can_ok('XML::Tidy','tidy');
57 can_ok('XML::Tidy','write');
58 use XML::Tidy;
59
60 # Make sure Data::Dumper loads
61 BEGIN { use_ok('Data::Dumper')
62         or diag("Can't load Data::Dumper package. Check to make sure the package library is correctly listed within the path."); }
63 require_ok('Data::Dumper');
64 use Data::Dumper;
65 }
66
67
68
69 # =begin testing
70 {
71 my $value = getVar(name => "address", namespace => "HoneyClient::Util::Config::Test");
72 is($value, "localhost", "getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test')")
73     or diag("The getVar() call failed.  Attempted to get variable 'address' using namespace 'HoneyClient::Util::Config::Test' within the global configuration file.");
74
75 $value = getVar(name => "address", namespace => "HoneyClient::Util::Config::Test", attribute => 'default');
76 is($value, "localhost", "getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default')")
77     or diag("The getVar() call failed.  Attempted to get attribute 'default' for variable 'address' using namespace 'HoneyClient::Util::Config::Test' within the global configuration file.");
78
79 # This check tests to make sure getVar() is able to use valid output
80 # from undefined namespaces (but where some of the parent namespace is
81 # partially known).
82 $value = getVar(name => "address", namespace => "HoneyClient::Util::Config::Test::Undefined::Child", attribute => 'default');
83 is($value, "localhost", "getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Undefined::Child', attribute => 'default')")
84     or diag("The getVar() call failed.  Attempted to get attribute 'default' for variable 'address' using namespace 'HoneyClient::Util::Config::Test::Undefined::Child' within the global configuration file.");
85
86 # This check tests to make sure getVar() returns the expected hashref
87 # when getting data from a target element that contains child sub-elements.
88 $value = getVar(name => "Yok", namespace => "HoneyClient::Util::Config::Test");
89 my $expectedValue = {
90     'childA' => [ '12345678', 'ABCDEFGH' ],
91     'childB' => [ '09876543', 'ZYXVTUWG' ],
92 };
93 is_deeply($value, $expectedValue, "getVar(name => 'Yok', namespace => 'HoneyClient::Util::Config::Test')")
94     or diag("The getVar() call failed.  Attempted to get variable 'Yok' using namespace 'HoneyClient::Util::Config::Test' within the global configuration file.");
95 }
96
97
98
99 # =begin testing
100 {
101 # Test setting an existing value
102 my $oldval = getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test' );
103 setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar' );
104 my $value = getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test' );
105 is($value, 'foobar', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar' )")
106     or diag("The setVar() call failed.  Attempted to set variable 'address' using namespace 'HoneyClient::Util::Config::Test' to 'foobar' within the global configuration file.");
107 setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', value => $oldval );
108
109 # Test setting an attribute
110 $oldval = getVar(name => 'address', attribute => 'default', namespace => 'HoneyClient::Util::Config::Test' );
111 setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default', value => 'foobar' );
112 $value = getVar(name => 'address', attribute => 'default', namespace => 'HoneyClient::Util::Config::Test' );
113 is($value, 'foobar', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default', value => 'foobar' )")
114     or diag("The setVar() call failed.  Attempted to set 'default' attribute of variable 'address' using namespace 'HoneyClient::Util::Config::Test' to 'foobar' within the global configuration file.");
115 setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default', value => $oldval );
116
117 # Test creating a value
118 setVar(name => 'zingers', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar');
119 $value = getVar(name => 'zingers', namespace => 'HoneyClient::Util::Config::Test' );
120 is($value, 'foobar', "setVar(name => 'zingers', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar' )")
121     or diag("The setVar() call failed.  Attempted to create variable 'zing' using namespace 'HoneyClient::Util::Config::Test' with a value of 'foobar' within the global configuration file.");
122
123 # Test creating an attribute
124 setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'zing', value => 'foobar');
125 $value = getVar(name => 'address', attribute => 'zing', namespace => 'HoneyClient::Util::Config::Test' );
126 is($value, 'foobar', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'zing', value => 'foobar' )")
127     or diag("The setVar() call failed.  Attempted to create attribute 'zing' using namespace 'HoneyClient::Util::Config::Test' with a value of 'foobar' within the global configuration file.");
128
129 # Creating new namespaces
130 setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Foo::Bar', value => 'baz');
131 $value =  getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Foo::Bar');
132 is($value, 'baz', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Foo::Bar', value => 'baz')")
133     or diag("The setVar() call failed.  Attempted to create attribute 'address' using namespace 'HoneyClient::Util::Config::Test::Foo::Bar' with a value of 'baz' within global configuration file.");
134 }
135
136
137
138
139 1;
Note: See TracBrowser for help on using the browser.