<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied. See the License for the
 specific language governing permissions and limitations
 under the License.
-->
<project name="XML Schema Test" default="test" basedir=".">

  <description>XML Schema Validation Test</description>

  <property name="xml.dir" location="${basedir}/.." />
  <property name="gen.xml.dir" location="${basedir}/../gen-xml" />
  <property name="idl.xml.schema" location="${xml.dir}/thrift-idl.xsd" />

  <property name="thrift.dir" location="../../../" />
  <property name="thrift.test.dir" location="${thrift.dir}/test" />
  <property name="thrift.compiler" location="${thrift.dir}/compiler/cpp/thrift" />

  <property file="${basedir}/build.properties" />

  <target name="compiler.check">
    <fail>
      <condition>
        <not>
          <resourcecount count="1">
            <fileset id="fs" file="${thrift.compiler}"/>
          </resourcecount>
        </not>
      </condition>
      Thrift compiler is missing !
    </fail>
  </target>

  <target name="init" depends="compiler.check, mkdirs">
    <tstamp />
  </target>

  <target name="mkdirs">
    <mkdir dir="${gen.xml.dir}"/>
  </target>

  <target name="generate" depends="init">
    <generate-xml file="${thrift.test.dir}/ThriftTest.thrift"/>
    <generate-xml file="${thrift.test.dir}/Include.thrift"/>
    <generate-xml file="${thrift.test.dir}/Recursive.thrift"/>
    <generate-xml file="${thrift.test.dir}/ManyOptionals.thrift"/>
    <generate-xml file="${thrift.test.dir}/OptionalRequiredTest.thrift"/>
    <generate-xml file="${thrift.test.dir}/ConstantsDemo.thrift"/>
    <generate-xml file="${thrift.test.dir}/TypedefTest.thrift" />
    <generate-xml file="${thrift.test.dir}/AnnotationTest.thrift" />
    <generate-xml file="${thrift.test.dir}/DocTest.thrift" />
    <generate-xml file="${thrift.test.dir}/EnumTest.thrift" />
    <generate-xml file="${thrift.test.dir}/ManyTypedefs.thrift" />
  </target>

  <target name="test" description="run schema validation"
          depends="validate-generated-xml"/>
    
  <target name="validate-generated-xml" depends="init, generate">
    <validate-xml file="${gen.xml.dir}/ThriftTest.xml"/>
    <validate-xml file="${gen.xml.dir}/Include.xml"/>
    <validate-xml file="${gen.xml.dir}/Recursive.xml"/>
    <validate-xml file="${gen.xml.dir}/ManyOptionals.xml"/>
    <validate-xml file="${gen.xml.dir}/OptionalRequiredTest.xml"/>
    <validate-xml file="${gen.xml.dir}/ConstantsDemo.xml"/>
    <validate-xml file="${gen.xml.dir}/TypedefTest.xml"/>
    <validate-xml file="${gen.xml.dir}/AnnotationTest.xml"/>
    <validate-xml file="${gen.xml.dir}/DocTest.xml"/>
    <validate-xml file="${gen.xml.dir}/EnumTest.xml"/>
    <validate-xml file="${gen.xml.dir}/ManyTypedefs.xml"/>
  </target>

  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${gen.xml.dir}" />
  </target>

  <macrodef name="generate-xml">
    <attribute name="file" />
    <sequential>
      <exec executable="${thrift.compiler}" failonerror="true">
        <arg line="-gen xml:merge"/>
        <arg line="-out ${gen.xml.dir}"/>
        <arg line="@{file}"/>
      </exec>
    </sequential>
  </macrodef>

  <macrodef name="validate-xml">
    <attribute name="file" />
    <sequential>
      <echo message="validating generated XML: @{file}" />
      <schemavalidate file="@{file}">
        <schema namespace="http://thrift.apache.org/xml/idl" 
                file="${idl.xml.schema}" />
      </schemavalidate>
    </sequential>
  </macrodef>

</project>
