602
|
1 |
#!bin\perl
|
|
2 |
# Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
# Unit test for CConfig
|
|
16 |
#
|
|
17 |
#
|
|
18 |
|
|
19 |
package unit_CConfig;
|
|
20 |
|
|
21 |
use lib qw(../);
|
|
22 |
use lib qw(../stages);
|
|
23 |
use FindBin;
|
|
24 |
use Cwd;
|
|
25 |
use lib $FindBin::Bin."\\..";
|
|
26 |
use CConfig;
|
|
27 |
use CTestScore;
|
|
28 |
|
|
29 |
sub RunTest($)
|
|
30 |
{
|
|
31 |
my ($testscore) = @_;
|
|
32 |
|
|
33 |
print "> *** Testing CConfig ***\n";
|
|
34 |
|
|
35 |
if (!defined($testscore))
|
|
36 |
{
|
|
37 |
$testscore = New CTestScore();
|
|
38 |
}
|
|
39 |
|
|
40 |
if (!defined($ENV{TEMP}))
|
|
41 |
{
|
|
42 |
print STDOUT "TEMP environment variable must be defined before testing of CConfig is run.\n";
|
|
43 |
return $testscore;
|
|
44 |
}
|
|
45 |
|
|
46 |
my $testFilename = $ENV{TEMP}."\\CConfig.tst";
|
|
47 |
|
|
48 |
if (-e $testFilename)
|
|
49 |
{
|
|
50 |
print STDOUT "File '$testFilename' already exists.\nPlease delete this file to enable testing of CConfig.\n";
|
|
51 |
return $testscore;
|
|
52 |
}
|
|
53 |
|
|
54 |
open(TESTFILE,">$testFilename");
|
|
55 |
print TESTFILE "Key:value\n";
|
|
56 |
close(TESTFILE);
|
|
57 |
|
|
58 |
my $testConfig=New CConfig($testFilename);
|
|
59 |
|
|
60 |
$testscore->Test($testConfig->Get("Key") eq "value", "Loaded in file");
|
|
61 |
$testscore->Test($testConfig->Get("kEy") eq "value", "Tested key case insensitivity");
|
|
62 |
$testscore->Test($testConfig->Set("key:2", "value2"), "Set extra key");
|
|
63 |
$testscore->Test( ($testConfig->Get("Key") eq "value") && ($testConfig->Get("key:2") eq "value2"), "Returned all key values");
|
|
64 |
$testscore->Test(!defined($testConfig->Get("nokey")), "Getting invalid key is undefined");
|
|
65 |
$testscore->Test($testConfig->Set("key3", ["value3","value4"]), "Set list value");
|
|
66 |
my $listref=$testConfig->Get("key3");
|
|
67 |
$testscore->Test( (ref($listref) eq "ARRAY") && (($listref->[0]) eq "value3") && (($listref->[1]) eq "value4"), "Returned list value");
|
|
68 |
$testscore->Test($testConfig->Save($testFilename), "Saved file");
|
|
69 |
$testConfig->Set("key4", "value5");
|
|
70 |
$testscore->Test($testConfig->Reload($testFilename), "Loaded file");
|
|
71 |
$listref=$testConfig->Get("key3");
|
|
72 |
$testscore->Test( ($testConfig->Get("Key") eq "value") && ($testConfig->Get("key:2") eq "value2") && (ref($listref) eq "ARRAY") && (($listref->[0]) eq "value3") && (($listref->[1]) eq "value4") && (!defined( $testConfig->Get("key4") )), "Returned all key values");
|
|
73 |
$testscore->Test(!($testConfig->Set(undef, "val")), "Can't set undefined key");
|
|
74 |
$testscore->Test(!($testConfig->Set("key5", undef)), "Can't set undefined value");
|
|
75 |
$testscore->Test($testConfig->Set("key6", ""), "Can set empty value");
|
|
76 |
$testscore->Test($testConfig->Set("", "val"), "Can set empty key");
|
|
77 |
$testscore->Test(!($testConfig->Set("key7", {"hashkey"=>"hashval"})), "Can't set hash value");
|
|
78 |
|
|
79 |
# Support for additional behaviour to handle Error message, scanlog format etc.
|
|
80 |
my $logFilename = "CConfig_scanlog.log";
|
|
81 |
TestConfigStatus($testConfig, $testscore);
|
|
82 |
TestLogFile($testFilename, $logFilename, $testscore);
|
|
83 |
ValidateLogFile($logFilename, $testscore);
|
|
84 |
unlink $logFilename;
|
|
85 |
TestDieConditions($testFilename, $testscore);
|
|
86 |
unlink $testFilename;
|
|
87 |
|
|
88 |
return $testscore;
|
|
89 |
}
|
|
90 |
|
|
91 |
sub TestConfigStatus($$)
|
|
92 |
{
|
|
93 |
my $testConfig = shift;
|
|
94 |
my $testScore = shift;
|
|
95 |
# First check the initial internal state of CConfig
|
|
96 |
$testScore->Test($testConfig->{iPhaseErrorCount} == 0, "Initial error count is zero");
|
|
97 |
$testScore->Test(!defined ($testConfig->{iPhase}), "Initial undefined initial phase");
|
|
98 |
}
|
|
99 |
|
|
100 |
# This writes out a log file
|
|
101 |
sub TestLogFile($$)
|
|
102 |
{
|
|
103 |
my $testFilename = shift;
|
|
104 |
my $logFilename = shift;
|
|
105 |
my $testScore = shift;
|
|
106 |
my $testConfig = New CConfig($testFilename);
|
|
107 |
# This puts a logfile in the current working directory
|
|
108 |
# i.e. test/CConfig.log
|
|
109 |
#my $logFilename = "CConfig_scanlog.log";
|
|
110 |
$testConfig->SetLog($logFilename);
|
|
111 |
#$testConfig->Command("Command");
|
|
112 |
$testConfig->PhaseStart("Phase_1");
|
|
113 |
$testScore->Test($testConfig->{iPhase} eq "Phase_1", "Phase_1 defined");
|
|
114 |
$testConfig->Component("Component_1");
|
|
115 |
$testConfig->Error("Phase_1 error message");
|
|
116 |
$testConfig->Warning("Phase_1 warning message");
|
|
117 |
my $phaseErrors = $testConfig->PhaseEnd();
|
|
118 |
$testScore->Test($phaseErrors == 1, "Phase_1 reported 1 error");
|
|
119 |
$testConfig->PhaseStart("Phase_2");
|
|
120 |
$testConfig->Warning("Phase_2 warning message");
|
|
121 |
$testConfig->Print("Opening Phase_3 should finish Phase_2");
|
|
122 |
$testConfig->PhaseStart("Phase_3");
|
|
123 |
$testScore->Test(1, "Phase switching didn't die");
|
|
124 |
$phaseErrors = $testConfig->PhaseEnd();
|
|
125 |
$testScore->Test($phaseErrors == 0, "Phase_3 reported no errors");
|
|
126 |
#my $logPath = cwd()."/".$logFilename;
|
|
127 |
#print "> *** Please check CConfig log file at $logPath ***\n";
|
|
128 |
$testScore->Test(1, "Logfile \"$logFilename\" written");
|
|
129 |
}
|
|
130 |
|
|
131 |
sub ValidateLogFile($$)
|
|
132 |
{
|
|
133 |
# This is the expected log file output - without the date/time stamp
|
|
134 |
my $logFilename = shift;
|
|
135 |
my $testScore = shift;
|
|
136 |
my $logFileExpected = "===-------------------------------------------------
|
|
137 |
=== Phase_1
|
|
138 |
===-------------------------------------------------
|
|
139 |
=== Phase_1 started
|
|
140 |
=== Phase_1 == Component_1
|
|
141 |
ERROR: Phase_1 error message
|
|
142 |
WARNING: Phase_1 warning message
|
|
143 |
=== Phase_1 finished
|
|
144 |
===-------------------------------------------------
|
|
145 |
=== Phase_2
|
|
146 |
===-------------------------------------------------
|
|
147 |
=== Phase_2 started
|
|
148 |
WARNING: Phase_2 warning message
|
|
149 |
Opening Phase_3 should finish Phase_2
|
|
150 |
=== Phase_2 finished
|
|
151 |
===-------------------------------------------------
|
|
152 |
=== Phase_3
|
|
153 |
===-------------------------------------------------
|
|
154 |
=== Phase_3 started
|
|
155 |
=== Phase_3 finished
|
|
156 |
";
|
|
157 |
$testScore->Test($logFileExpected eq ReadTimelessLogFile($logFilename), "Logfile \"$logFilename\" matches");
|
|
158 |
}
|
|
159 |
|
|
160 |
# Read a scanlog file and strip the time/date stamp
|
|
161 |
sub ReadTimelessLogFile($)
|
|
162 |
{
|
|
163 |
my $filename = shift;
|
|
164 |
my $retVal;
|
|
165 |
open(LOGFILE,"$filename");
|
|
166 |
while (defined (my $line = <LOGFILE>))
|
|
167 |
{
|
|
168 |
if ($line =~ m/.+started .+/)
|
|
169 |
{
|
|
170 |
$line =~ s/(.+started ).+/$1/;
|
|
171 |
}
|
|
172 |
elsif ($line =~ m/.+finished .+/)
|
|
173 |
{
|
|
174 |
$line =~ s/(.+finished ).+/$1/;
|
|
175 |
}
|
|
176 |
$retVal .= $line;
|
|
177 |
}
|
|
178 |
#print "\nParsed:\n";
|
|
179 |
#print $retVal;
|
|
180 |
return $retVal;
|
|
181 |
}
|
|
182 |
|
|
183 |
sub TestDieConditions($$)
|
|
184 |
{
|
|
185 |
my $testFilename = shift;
|
|
186 |
my $testScore = shift;
|
|
187 |
my $testConfig;
|
|
188 |
my $logFilename = "CConfig_scrap.log";
|
|
189 |
# Try a direct Die() call
|
|
190 |
$testConfig = New CConfig($testFilename);
|
|
191 |
eval
|
|
192 |
{
|
|
193 |
$testConfig->Die("Die message.");
|
|
194 |
};
|
|
195 |
if ($@) {
|
|
196 |
$testScore->Test(1, "Died on Die() command");
|
|
197 |
} else {
|
|
198 |
$testScore->Test(0, "Failed to die on Die() command");
|
|
199 |
}
|
|
200 |
# Try setting SetErrorDie() then calling Error()
|
|
201 |
$testConfig = New CConfig($testFilename);
|
|
202 |
$testConfig->SetLog($logFilename);
|
|
203 |
# Try PhaseEnd() before any PhaseStart() has been called
|
|
204 |
$testConfig = New CConfig($testFilename);
|
|
205 |
$testConfig->SetLog($logFilename);
|
|
206 |
eval
|
|
207 |
{
|
|
208 |
# No PhaseStart() has been made so a PhaseEnd() should fail
|
|
209 |
$testConfig->PhaseEnd();
|
|
210 |
};
|
|
211 |
if ($@) {
|
|
212 |
$testScore->Test(1, "Died on PhaseEnd() command when there was no prior PhaseStart()");
|
|
213 |
} else {
|
|
214 |
$testScore->Test(0, "Failed to die on PhaseEnd() command");
|
|
215 |
}
|
|
216 |
# Try setting provoking PhaseEnd() when phase has errors
|
|
217 |
$testConfig = New CConfig($testFilename);
|
|
218 |
$testConfig->SetLog($logFilename);
|
|
219 |
$testConfig->PhaseStart("Error_Phase_1");
|
|
220 |
$testConfig->Error("Error message.");
|
|
221 |
eval
|
|
222 |
{
|
|
223 |
# Here is the killer line that should cause it to fail.
|
|
224 |
# Phase 1 has an error but the phase has not been collected
|
|
225 |
# before starting False_Phase_2
|
|
226 |
$testConfig->PhaseStart("False_phase_2");
|
|
227 |
};
|
|
228 |
if ($@) {
|
|
229 |
$testScore->Test(1, "Died on PhaseStart() command when previous phase had Error() calls");
|
|
230 |
} else {
|
|
231 |
$testScore->Test(0, "Failed to die on Error() command");
|
|
232 |
}
|
|
233 |
unlink $logFilename;
|
|
234 |
}
|
|
235 |
|
|
236 |
1;
|