<?xml version="1.0"?>
<!-- 
============================================================================ 
Name        : test_regex.ant.xml 
Part of     : Helium AntLib

Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
All rights reserved.
This component and the accompanying materials are made available
under the terms of the License "Eclipse Public License v1.0"
which accompanies this distribution, and is available
at the URL "http://www.eclipse.org/legal/epl-v10.html".

Initial Contributors:
Nokia Corporation - initial contribution.

Contributors:

Description:

============================================================================
-->
<project name="test-metadata-regex" xmlns:au="antlib:org.apache.ant.antunit" xmlns:hlm="http://www.nokia.com/helium">
    <description>Helium Antlib metadata unittests.</description>

    <property environment="env" />
    
    <dirname property="project.dir" file="${ant.file.test-metadata-regex}" />

    <path id="lib.path">
        <fileset dir="${project.dir}/../../../bin" includes="*.jar"/>
    </path>
    
    <taskdef resource="com/nokia/helium/metadata/ant/antlib.xml" classpathref="lib.path" uri="http://www.nokia.com/helium" />
    <taskdef name="fmpp" classname="fmpp.tools.AntTask" classpathref="lib.path" onerror="report"/>

    <property name="metadata-filterfile" value="${project.dir}/../data/common.csv"/>
    <property name="metadata-log-files" value="${project.dir}/../data/*.log"/>
    
    <hlm:metadatafilterset id="all-regex-filterfile">
        <metadatafilterset filterfile="${metadata-filterfile}" />
    </hlm:metadatafilterset>


    <!-- Check for all the regular expression defined in the filter file,
    The log file contains text for matching regular expression.
    -->
    <target name="test-all-regex-filterfile">
        <property name="metadata-read-db" value="${project.dir}/../output/log_all_filterfile.sqlite"/>
        <delete file="${metadata-read-db}" failonerror="false"/>
        <echo message="all regular expression validation" />
        <echo message="database:${metadata-read-db}" />
        <!-- do some checks that files are present and raise asserts if not present-->
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>
        
        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:sbsmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*regex*.log"/>
                </fileset>
                <metadatafilterset refid="all-regex-filterfile" />
            </hlm:sbsmetadatainput>
        </hlm:metadatarecord>

        <!-- check we have created the DB file-->
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} metadata DB file was not created"/>
    </target>

    <!-- To verify some lengthy text for recipe tag from 
    sbs metadata input.
    -->
    <target name="test-receipe-lengthy-text">
        <property name="metadata-read-db" value="${project.dir}/../output/log_lengthy_text.sqlite"/>
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <delete file="${metadata-read-db}" failonerror="false"/>
        <echo message="all regular expression validation" />
        <echo message="database:${metadata-read-db}" />
        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:sbsmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="receipe*.log"/>
                </fileset>
                <metadatafilterset refid="all-regex-filterfile" />
            </hlm:sbsmetadatainput>
        </hlm:metadatarecord>
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} metadata DB file was not created"/>
    </target>

    <!--modify the errors so regex=".*warning U4010" description="mingw error" is no longer 
    an error but a warning-->
    <hlm:metadatafilterset id="all-regex-combined">
        <metadatafilter priority="WARNING" regex=".*warning U4010" description="mingw error" />
        <metadatafilterset filterfile="${metadata-filterfile}" />
    </hlm:metadatafilterset>

    <property name="metadata-all-db" value="${project.dir}/../output/log_all_combined.sqlite"/>

    <target name="test-all-regex-combined">
        <property name="metadata-read-db" value="${project.dir}/../output/log_all_combined.sqlite"/>
        <delete file="${metadata-read-db}" failonerror="false" />
        <echo message="all regular expression validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:sbsmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*regex*.log"/>
                </fileset>
                <metadatafilterset refid="all-regex-combined" />
            </hlm:sbsmetadatainput>
        </hlm:metadatarecord>
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} DB file was not created"/>
    </target>


    <property name="metadata-error-db" value="${project.dir}/../output/log_error_combined.sqlite"/>

    <!--create an invalid priority type, so contains message invalid 
    priority and set to error as priority -->
    <hlm:metadatafilterset id="all-regex-combined-error">
        <metadatafilter priority="INCORRECT" regex=".*warning U4010" description="test error handling so incorrect priority" />
        <metadatafilterset filterfile="${metadata-filterfile}" />
    </hlm:metadatafilterset>

    <!--create an invalid priority type, which contains warning invalid priority 
    and continues with other data. 
    -->
    <target name="test-all-regex-combined-error">
        <property name="metadata-read-db" value="${project.dir}/../output/log_error_combined.sqlite"/>
        <delete file="${metadata-read-db}" failonerror="false" />
        <echo message="all regular expression validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}" >
            <hlm:sbsmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*regex*.log"/>
                </fileset>
                <metadatafilterset refid="all-regex-combined-error" />
            </hlm:sbsmetadatainput>
        </hlm:metadatarecord>
        <!-- no need to check the DB file not present as it won't be-->
    </target>

    <!--try to add a different different filter type but not add the parameter so has an error-->
    <hlm:metadatafilterset id="all-regex-combined-no-priority">
        <metadatafilter regex=".*warning U4010" description="test error handling so incorrect priority" />
        <metadatafilterset filterfile="${metadata-filterfile}" />
    </hlm:metadatafilterset>

    <!--try to add a different different filter type but not add the parameter so has an error-->
    <target name="test-all-regex-combined-no-priority">
        <property name="metadata-read-db" value="${project.dir}/../output/log_error_combined.sqlite"/>
        <delete file="${metadata-read-db}" failonerror="false" />
        <echo message="all regular expression validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:sbsmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*regex*.log"/>
                </fileset>
                <metadatafilterset refid="all-regex-combined-no-priority" />
            </hlm:sbsmetadatainput>
        </hlm:metadatarecord>
        <!-- no need to check the DB file not present as it won't be-->
    </target>

    <!--try to add a different different filter type but not add the parameter so has an error-->
    <hlm:metadatafilterset id="all-regex-combined-no-regex">
        <metadatafilter priority="INCORRECT" description="test error handling so incorrect priority" />
        <metadatafilter priority="WARNING" description="test error handling so incorrect priority" />
        <metadatafilterset filterfile="${metadata-filterfile}" />
    </hlm:metadatafilterset>

    <!--try to add a different different filter type but not add the parameter so has an error-->
    <target name="test-all-regex-combined-no-regex">
        <property name="metadata-read-db" value="${project.dir}/../output/log_error_combined.sqlite"/>
        <delete file="${metadata-read-db}" failonerror="false" />
        <echo message="all regular expression validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:sbsmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*regex*.log"/>
                </fileset>
                <metadatafilterset refid="all-regex-combined-no-regex" />
            </hlm:sbsmetadatainput>
        </hlm:metadatarecord>
        <!-- no need to check the DB file not present as it won't be-->
    </target>

    <hlm:metadatafilterset id="text_log_metadata_input">
        <metadatafilterset filterfile="${project.dir}/../data/common.csv" />
    </hlm:metadatafilterset>

    <target name="test-text_log_metadata_input">
        <property name="metadata-read-db" value="${project.dir}/../output/log_text_metadata_input.sqlite"/>
        <delete file="${metadata-read-db}" failonerror="false" />
        <echo message="text log metadata input validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:textmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*_fixslashes*.log"/>
                </fileset>
                <metadatafilterset refid="text_log_metadata_input" />
            </hlm:textmetadatainput>
        </hlm:metadatarecord>
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} DB file was not created"/>
    </target>

    <hlm:metadatafilterset id="abld_log_metadata_input">
        <metadatafilter priority="WARNING" regex=".*warning U4010" description="mingw error" />
        <metadatafilterset filterfile="${project.dir}/../data/common.csv" />
    </hlm:metadatafilterset>


    <target name="test-abld_log_metadata_input">
        <property name="metadata-read-db" value="${project.dir}/../output/log_abld_metadata_input.sqlite"/>
        <delete file="${project.dir}/../output/log_abld_metadata_input.sqlite" failonerror="false" />
        <echo message="abld log metadata input validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:abldmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*sample_abld.log"/>
                </fileset>
                <metadatafilterset refid="abld_log_metadata_input" />
            </hlm:abldmetadatainput>
        </hlm:metadatarecord>
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} DB file was not created"/>
    </target>


    <target name="test-policy_metadata_input">
        <property name="metadata-read-db" value="${project.dir}/../output/log_policy_metadata_input.sqlite"/>
        <delete file="${metadata-policy-db}" failonerror="false" />
        <echo message="policy metadata input validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:policymetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*policy*.log"/>
                </fileset>
            </hlm:policymetadatainput>
        </hlm:metadatarecord>
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} DB file was not created"/>
    </target>

    <hlm:metadatafilterset id="ant_log_metadata_input">
        <metadatafilterset filterfile="${project.dir}/../data/common.csv" />
    </hlm:metadatafilterset>


    <target name="test-ant_log_metadata_input">
        <property name="metadata-read-db" value="${project.dir}/../output/log_ant_metadata_input.sqlite"/>
        <delete file="${metadata-ant-db}" failonerror="false" />
        <echo message="ant log metadata input validation" />
        <echo message="database:${metadata-read-db}" />
        <au:assertFileExists file="${metadata-filterfile}" message="${metadata-filterfile} filter file does not exist"/>
        <mkdir dir="${project.dir}/../output"/>

        <hlm:metadatarecord database="${metadata-read-db}">
            <hlm:antmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*ant*.log"/>
                </fileset>
                <metadatafilterset refid="ant_log_metadata_input" />
            </hlm:antmetadatainput>
        </hlm:metadatarecord>
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} DB file was not created"/>
    </target>

    <property name="metadata-read-ftl" value="${project.dir}/../../src/templates/readdata.xml.ftl"/>
    <property name="metadata-read-output" value="${project.dir}/../output/readdata.html"/>

    <target name="test-readdata" depends="test-text_log_metadata_input" >
        <property name="metadata-template-dir" value="${project.dir}/../../src/templates/" />
        <property name="metadata-output-dir" value="${project.dir}/../output" />
        <property name="metadata-read-ftl" value="${metadata-template-dir}/readdata.xml.ftl"/>
        <property name="metadata-read-db" value="${metadata-output-dir}/log_text_metadata_input.sqlite"/>
        <property name="metadata-read-output" value="${metadata-output-dir}/readdata.html"/>
        <delete file="${metadata-read-output}" failonerror="false" />
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} DB file was not created"/>
        <fmpp sourceFile="${metadata-template-dir}/readdata.xml.ftl"
                     outputfile="${metadata-output-dir}/readdata.html">
            <freemarkerLinks expandProperties="yes">
                macro: ${project.dir}/../../src/templates
            </freemarkerLinks>
            <data expandProperties="yes">
                dbPath: ${metadata-read-db}
                ant: antProperties()
            </data>
        </fmpp>
        <fmpp sourceFile="${metadata-template-dir}/build_status.xml.ftl"
                     outputfile="${metadata-output-dir}/build_status.xml">
            <freemarkerLinks expandProperties="yes">
                macro: ${project.dir}/../../src/templates
            </freemarkerLinks>
            <data expandProperties="yes">
                dbPath: ${metadata-read-db}
                logfile: ido_abs_mcl_devlon70_robot_MCL.70.500_fixslashes_raptor.log
                ant: antProperties()
            </data>
        </fmpp>
        <fmpp sourceFile="${metadata-template-dir}/email_new.html.ftl"
                     outputfile="${metadata-output-dir}/build_status.html">
            <freemarkerLinks expandProperties="yes">
                macro: ${project.dir}/../../src/templates
            </freemarkerLinks>
            <data expandProperties="yes">
                dbPath: ${metadata-read-db}
                logfile: ido_abs_mcl_devlon70_robot_MCL.70.500_fixslashes_raptor.log
                ant: antProperties()
            </data>
        </fmpp>
        <au:assertFileExists file="${metadata-read-output}" message="${metadata-read-output} DB file was not created"/>
    </target>


    <target name="test-generate-summary" depends="test-text_log_metadata_input">
        <property name="metadata-summary-ftl" value="${project.dir}/../../src/templates/summary.html.ftl"/>
        <property name="metadata-summary-output" value="${project.dir}/../output/summary.html"/>
        <property name="metadata-output-dir" value="${project.dir}/../output/" />
        <delete file="${metadata-summary-output}" failonerror="false" />
        <property name="metadata-read-db" value="${metadata-output-dir}/log_text_metadata_input.sqlite"/>
        
        <fmpp sourceFile="${metadata-summary-ftl}"
                     outputfile="${metadata-summary-output}">
            <freemarkerLinks expandProperties="yes">
                macro: ${project.dir}/../../src/templates/macro
            </freemarkerLinks>
            <data expandProperties="yes">
                dbPath: ${metadata-read-db}
                loginfo: xml(${project.dir}/../data/ido_abs_mcl_devlon70_robot_MCL.70.500_info.log)
                ant: antProperties()
            </data>
        </fmpp>
        
        <!-- check we have created the files -->
        <au:assertFileExists file="${metadata-read-db}" message="${metadata-read-db} DB file was not created"/>
        <au:assertFileExists file="${metadata-summary-output}" message="${metadata-summary-output} summary output file was not created"/>
    </target>
    
    <target name="test-countseverity">
        <delete file="${project.dir}/../output/dp.sqlite" failonerror="false" />

        <hlm:metadatarecord database="${project.dir}/../output/dp.sqlite">
            <hlm:textmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include  name="*_fixslashes*.log"/>
                </fileset>
                <metadatafilterset refid="text_log_metadata_input" />
            </hlm:textmetadatainput>
        </hlm:metadatarecord>

        <hlm:metadataCountSeverity severity="error" log="_fixslashes_raptor.log" db="${project.dir}/../output/dp.sqlite" property="errors"/>
        <au:assertTrue>
            <equals arg1="${errors}" arg2="24"/>
        </au:assertTrue>
        <au:assertTrue>
            <hlm:metadataHasSeverity severity="error" log="_fixslashes_raptor.log" db="${project.dir}/../output/dp.sqlite" />
        </au:assertTrue>
        <au:assertFalse>
            <hlm:metadataHasSeverity severity="fatal" log="_fixslashes_raptor.log" db="${project.dir}/../output/dp.sqlite" />
        </au:assertFalse>
        
        <hlm:metadatarecord database="${project.dir}/../output/dp.sqlite">
            <hlm:textmetadatainput>
                <fileset dir="${project.dir}/../data/">
                    <include name="dp.log"/>
                </fileset>
                <metadatafilterset refid="text_log_metadata_input" />
            </hlm:textmetadatainput>
        </hlm:metadatarecord>
        
        <hlm:metadataCountSeverity severity="error" log="dp.log" db="${project.dir}/../output/dp.sqlite" property="errors2"/>
        <au:assertTrue>
            <equals arg1="${errors2}" arg2="1"/>
        </au:assertTrue>
        <au:assertTrue>
            <hlm:metadataHasSeverity severity="error" log="dp.log" db="${project.dir}/../output/dp.sqlite" />
        </au:assertTrue>
        <au:assertFalse>
            <hlm:metadataHasSeverity severity="fatal" log="dp.log" db="${project.dir}/../output/dp.sqlite" />
        </au:assertFalse>
    </target>
    
    <target name="test-metadata-ant" depends="test-ant_log_metadata_input, test-readdata,test-generate-summary  " />
    <target name="test-metadata-abld" depends="test-abld_log_metadata_input, test-readdata,test-generate-summary  " />
    <target name="test-metadata-policy" depends="test-policy_metadata_input, test-readdata,test-generate-summary  " />
    <target name="test-metadata-text" depends="test-text_log_metadata_input, test-readdata,test-generate-summary  " />
    <target name="test-metadata-all" depends="test-all-regex-filterfile, test-readdata,test-generate-summary  " />
    <target name="test-metadata-combine" depends="test-all-regex-combined, test-readdata,test-generate-summary  " />
           
</project>