~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to tests/randgen/lib/GenTest/XML/Test.pm

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2008, 2010 Oracle and/or its affiliates. All rights reserved.
 
2
# Use is subject to license terms.
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; version 2 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
11
# General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 
16
# USA
 
17
 
 
18
package GenTest::XML::Test;
 
19
 
 
20
require Exporter;
 
21
@ISA = qw(GenTest);
 
22
 
 
23
use strict;
 
24
use GenTest;
 
25
 
 
26
#
 
27
# Those names are taken from Vemundo's specification for a 
 
28
# test result XML report. Not all of them will be used
 
29
#
 
30
 
 
31
use constant TEST_ID                => 0;
 
32
use constant TEST_NAME              => 1;
 
33
use constant TEST_ENVIRONMENT_ID    => 2;
 
34
use constant TEST_STARTTIME         => 3;
 
35
use constant TEST_ENDTIME           => 4;
 
36
use constant TEST_LOGDIR            => 5;
 
37
use constant TEST_RESULT            => 6;
 
38
use constant TEST_DESCRIPTION       => 7;
 
39
use constant TEST_ATTRIBUTES        => 8;   
 
40
use constant TEST_INCIDENTS         => 9;
 
41
 
 
42
1;
 
43
 
 
44
sub new {
 
45
    my $class = shift;
 
46
 
 
47
    my $test = $class->SUPER::new({
 
48
        id              => TEST_ID,
 
49
        name            => TEST_NAME,
 
50
        environment_id  => TEST_ENVIRONMENT_ID,
 
51
        starttime       => TEST_STARTTIME,
 
52
        endtime         => TEST_ENDTIME,
 
53
        logdir          => TEST_LOGDIR,
 
54
        result          => TEST_RESULT,
 
55
        description     => TEST_DESCRIPTION,
 
56
        attributes      => TEST_ATTRIBUTES,
 
57
        incidents       => TEST_INCIDENTS
 
58
    }, @_);
 
59
 
 
60
    $test->[TEST_STARTTIME] = isoTimestamp() if not defined $test->[TEST_STARTTIME];
 
61
    $test->[TEST_ENVIRONMENT_ID] = 0 if not defined $test->[TEST_ENVIRONMENT_ID];
 
62
 
 
63
    return $test;
 
64
}
 
65
 
 
66
sub end {
 
67
    my ($test, $result) = @_;
 
68
    $test->[TEST_ENDTIME] = isoTimestamp();
 
69
    $test->[TEST_RESULT] = $result;
 
70
}
 
71
 
 
72
sub xml {
 
73
    require XML::Writer;
 
74
 
 
75
    my $test = shift;
 
76
 
 
77
    $test->end() if not defined $test->[TEST_ENDTIME];
 
78
 
 
79
    my $test_xml;
 
80
    my $writer = XML::Writer->new(
 
81
        OUTPUT      => \$test_xml,
 
82
        DATA_MODE   => 1,   # this and DATA_INDENT to have line breaks and indentation after each element
 
83
        DATA_INDENT => 2,   # number of spaces used for indentation
 
84
        UNSAFE      => 1    # required for raw(), used when including incidents
 
85
    );
 
86
 
 
87
    $writer->startTag('test', id => $test->[TEST_ID]);
 
88
 
 
89
    $writer->dataElement('name',            $test->[TEST_NAME] ? $test->[TEST_NAME] : "NO_NAME");
 
90
    $writer->dataElement('environment_id',  $test->[TEST_ENVIRONMENT_ID]);
 
91
    $writer->dataElement('starttime',       $test->[TEST_STARTTIME]);
 
92
    $writer->dataElement('endtime',         $test->[TEST_ENDTIME]);
 
93
    $writer->dataElement('logdir',          $test->[TEST_LOGDIR]);
 
94
    $writer->dataElement('result',          $test->[TEST_RESULT]);
 
95
    $writer->dataElement('description',     $test->[TEST_DESCRIPTION]);
 
96
 
 
97
    if (defined $test->[TEST_ATTRIBUTES]) {
 
98
        $writer->startTag('attributes');
 
99
        while (my ($name, $value) = each %{$test->[TEST_ATTRIBUTES]}) {
 
100
            $writer->startTag('attribute');
 
101
            $writer->dataElement('name',    $name);
 
102
            $writer->dataElement('value',    $value);
 
103
            $writer->endTag('attribute');
 
104
        }
 
105
        $writer->endTag('attributes');
 
106
    }
 
107
 
 
108
    if (defined $test->[TEST_INCIDENTS]) {
 
109
        $writer->startTag('incidents');
 
110
        foreach my $incident (@{$test->[TEST_INCIDENTS]}) {
 
111
            $writer->raw($incident->xml());
 
112
        }
 
113
        $writer->endTag('incidents');
 
114
    }
 
115
 
 
116
    # TODO: <metrics> (name, value, unit, attributes, timestamp)
 
117
 
 
118
    $writer->endTag('test');
 
119
 
 
120
    $writer->end();
 
121
 
 
122
    return $test_xml;
 
123
}
 
124
 
 
125
sub setId {
 
126
    $_[0]->[TEST_ID] = $_[1];
 
127
}
 
128
 
 
129
sub addIncident {
 
130
    my ($test, $incident) = @_;
 
131
    $test->[TEST_INCIDENTS] = [] if not defined $test->[TEST_INCIDENTS];
 
132
    push @{$test->[TEST_INCIDENTS]}, $incident;
 
133
}
 
134
 
 
135
1;