| 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 Sys::Syslog loads |
|---|
| 61 |
BEGIN { use_ok('Sys::Syslog') |
|---|
| 62 |
or diag("Can't load Sys::Syslog package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 63 |
require_ok('Sys::Syslog'); |
|---|
| 64 |
use Sys::Syslog; |
|---|
| 65 |
|
|---|
| 66 |
# Make sure Data::Dumper loads |
|---|
| 67 |
BEGIN { use_ok('Data::Dumper') |
|---|
| 68 |
or diag("Can't load Data::Dumper package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 69 |
require_ok('Data::Dumper'); |
|---|
| 70 |
use Data::Dumper; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
# =begin testing |
|---|
| 76 |
{ |
|---|
| 77 |
my $value = getVar(name => "address", namespace => "HoneyClient::Util::Config::Test"); |
|---|
| 78 |
is($value, "localhost", "getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test')") |
|---|
| 79 |
or diag("The getVar() call failed. Attempted to get variable 'address' using namespace 'HoneyClient::Util::Config::Test' within the global configuration file."); |
|---|
| 80 |
|
|---|
| 81 |
$value = getVar(name => "address", namespace => "HoneyClient::Util::Config::Test", attribute => 'default'); |
|---|
| 82 |
is($value, "localhost", "getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default')") |
|---|
| 83 |
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."); |
|---|
| 84 |
|
|---|
| 85 |
# This check tests to make sure getVar() is able to use valid output |
|---|
| 86 |
# from undefined namespaces (but where some of the parent namespace is |
|---|
| 87 |
# partially known). |
|---|
| 88 |
$value = getVar(name => "address", namespace => "HoneyClient::Util::Config::Test::Undefined::Child", attribute => 'default'); |
|---|
| 89 |
is($value, "localhost", "getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Undefined::Child', attribute => 'default')") |
|---|
| 90 |
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."); |
|---|
| 91 |
|
|---|
| 92 |
# This check tests to make sure getVar() returns the expected hashref |
|---|
| 93 |
# when getting data from a target element that contains child sub-elements. |
|---|
| 94 |
$value = getVar(name => "Yok", namespace => "HoneyClient::Util::Config::Test"); |
|---|
| 95 |
my $expectedValue = { |
|---|
| 96 |
'childA' => [ '12345678', 'ABCDEFGH' ], |
|---|
| 97 |
'childB' => [ '09876543', 'ZYXVTUWG' ], |
|---|
| 98 |
}; |
|---|
| 99 |
is_deeply($value, $expectedValue, "getVar(name => 'Yok', namespace => 'HoneyClient::Util::Config::Test')") |
|---|
| 100 |
or diag("The getVar() call failed. Attempted to get variable 'Yok' using namespace 'HoneyClient::Util::Config::Test' within the global configuration file."); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
# =begin testing |
|---|
| 106 |
{ |
|---|
| 107 |
# Test setting an existing value |
|---|
| 108 |
my $oldval = getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test' ); |
|---|
| 109 |
setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar' ); |
|---|
| 110 |
my $value = getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test' ); |
|---|
| 111 |
is($value, 'foobar', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar' )") |
|---|
| 112 |
or diag("The setVar() call failed. Attempted to set variable 'address' using namespace 'HoneyClient::Util::Config::Test' to 'foobar' within the global configuration file."); |
|---|
| 113 |
setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', value => $oldval ); |
|---|
| 114 |
|
|---|
| 115 |
# Test setting an attribute |
|---|
| 116 |
$oldval = getVar(name => 'address', attribute => 'default', namespace => 'HoneyClient::Util::Config::Test' ); |
|---|
| 117 |
setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default', value => 'foobar' ); |
|---|
| 118 |
$value = getVar(name => 'address', attribute => 'default', namespace => 'HoneyClient::Util::Config::Test' ); |
|---|
| 119 |
is($value, 'foobar', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default', value => 'foobar' )") |
|---|
| 120 |
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."); |
|---|
| 121 |
setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'default', value => $oldval ); |
|---|
| 122 |
|
|---|
| 123 |
# Test creating a value |
|---|
| 124 |
setVar(name => 'zingers', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar'); |
|---|
| 125 |
$value = getVar(name => 'zingers', namespace => 'HoneyClient::Util::Config::Test' ); |
|---|
| 126 |
is($value, 'foobar', "setVar(name => 'zingers', namespace => 'HoneyClient::Util::Config::Test', value => 'foobar' )") |
|---|
| 127 |
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."); |
|---|
| 128 |
|
|---|
| 129 |
# Test creating an attribute |
|---|
| 130 |
setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'zing', value => 'foobar'); |
|---|
| 131 |
$value = getVar(name => 'address', attribute => 'zing', namespace => 'HoneyClient::Util::Config::Test' ); |
|---|
| 132 |
is($value, 'foobar', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test', attribute => 'zing', value => 'foobar' )") |
|---|
| 133 |
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."); |
|---|
| 134 |
|
|---|
| 135 |
# Creating new namespaces |
|---|
| 136 |
setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Foo::Bar', value => 'baz'); |
|---|
| 137 |
$value = getVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Foo::Bar'); |
|---|
| 138 |
is($value, 'baz', "setVar(name => 'address', namespace => 'HoneyClient::Util::Config::Test::Foo::Bar', value => 'baz')") |
|---|
| 139 |
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."); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
1; |
|---|