unit test case for this perl code
am 02.09.2011 10:46:30 von Anirban Adhikary--0-2074727828-1314953190=:37105
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hi I have write the following code and Now I have been asked to write a uni=
t test case for this code. But neither I have=A0 any idea how to write a un=
it test case nor I have ever experience with it. So in this case I am askin=
g your help.
#!/usr/bin/perl
###=0A## The program is called by ma=
in.pl=0A## dependent conf file is the property.conf located=0A## under the =
config folder in the home directory=0A###
use strict;=0Ause DBI;=0Ause=
File::Basename;=0Ause Cwd qw(realpath);
my $home =3D dirname( realpat=
h( $0 ));
my %config =3D &readConfigFile($home."/config/global.conf");=
=0Amy $db =3D &createDbConnection($config{'user'},$config{'passwd'},$confi g=
{'dbalias'});
my $stmt =3D $db->prepare("select job,OCCURRENCE,retenti=
on_days from CONFIG.PROCESS_LOG");=0A$stmt->execute();
my %prunehash;=
=0Awhile(my @row =3D stmt->fetchrow_array) {
$prunehash{$row[0]=
..':~:'.$row[1]} =3D $row[2];=0A}=0A$stmt->finish;
foreach (keys %prune=
hash) {
my ($job,$occ) =3D split(':~:',$_);
my $ret=
days =3D $prunehash{$_};
my $delstmt =3D $db->prepare("delete f=
rom LOG.PROCESS_LOG where days (CURRENT_TIMESTAMP - LOG.PROCESS_LOG.WHEN) >=
".$retdays." and LOG.PROCESS_LOG.JOB =3D '".$job."' and LOG.PROCESS_LOG.OC=
CURRENCE =3D ".$occ." ");
$delstmt->execute;
=
$delstmt->finish;=0A}
=0A$db->disconnect;
## SubRoutines.=
sub config {
my ($flname) =3D @_;
my %chash;=
open(FILE, "<$flname") || return 1; # die ("cannot open");=0A=
# loading conf from file
while (
=A0 =A0 chop $_ and next if (/^#/ || $_ eq "\n");
=
=A0 my ($name, $value)=3D/(.*)=3D(.*)/;
=A0 next if ((!$n=
ame) || (!$value));
=A0 =A0 $name=3D&trim($name);
=A0=
=A0 =A0 $value=3D&trim($value);
=A0 =A0 next if (( $=
name eq "") ||( $value eq ""));
=A0 $chash{$name} =3D $va=
lue;
}
close(FILE);
=A0 return %chash;=
=0A}
sub createDbConnection {
my ($uid,$passwd,$dbalias) =
=3D @_;
return DBI->connect ("dbi:DB2:$dbalias", $uid, $passwd)=
or die 'Unable to connect'
=0A}
sub trim {
(m=
y $str)=3D(@_);
$str =3D~ s/^\s+|\s+$//g;
return $s=
tr;=0A}=0A################################################## ######=0Aglobal=
..conf=A0 contents
user=
=0Apasswd=
=0Adbalias=
=0A=0 AThans=
& Regards in advance=0ARatul.
--0-2074727828-1314953190=:37105--