Hi I have write the following code and Now I have been asked to write a unit 
test case for this code. But neither I have  any idea how to write a unit test 
case nor I have ever experience with it. So in this case I am asking your help.

#!/usr/bin/perl

###
## The program is called by main.pl
## dependent conf file is the property.conf located
## under the config folder in the home directory
###

use strict;
use DBI;
use File::Basename;
use Cwd qw(realpath);

my $home = dirname( realpath( $0 ));

my %config = &readConfigFile($home."/config/global.conf");
my $db = 
&createDbConnection($config{'user'},$config{'passwd'},$config{'dbalias'});

my $stmt = $db->prepare("select job,OCCURRENCE,retention_days from 
CONFIG.PROCESS_LOG");
$stmt->execute();

my %prunehash;
while(my @row = stmt->fetchrow_array) {
    $prunehash{$row[0].':~:'.$row[1]} = $row[2];
}
$stmt->finish;

foreach (keys %prunehash) {
    my ($job,$occ) = split(':~:',$_);
    my $retdays = $prunehash{$_};
    my $delstmt = $db->prepare("delete from LOG.PROCESS_LOG where days 
(CURRENT_TIMESTAMP - LOG.PROCESS_LOG.WHEN) > ".$retdays." and 
LOG.PROCESS_LOG.JOB = '".$job."' and LOG.PROCESS_LOG.OCCURRENCE = ".$occ." ");
    
    $delstmt->execute;
    $delstmt->finish;
}


$db->disconnect;

## SubRoutines.

sub config {
    my ($flname) = @_;
    my %chash;
    open(FILE, "<$flname") || return 1; # die ("cannot open");

    # loading conf from file
    while (<FILE>) {
        chop $_ and next if (/^#/ || $_ eq "\n");
        my ($name, $value)=/(.*)=(.*)/;
        next if ((!$name) || (!$value));

        $name=&trim($name);
        $value=&trim($value);

        next if (( $name eq "") ||( $value eq ""));
        $chash{$name} = $value;
    }
    close(FILE);

    return %chash;
}

sub createDbConnection {
    my ($uid,$passwd,$dbalias) = @_;
    return DBI->connect ("dbi:DB2:$dbalias", $uid, $passwd) or die 'Unable to 
connect'
    
}

sub trim {
    (my $str)=(@_);
    $str =~ s/^\s+|\s+$//g;
    return $str;
}
########################################################
global.conf  contents

user=

passwd=

dbalias=


Thans & Regards in advance
Ratul.

Reply via email to