<?xml version="1.0"?>
<!--

    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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>drill-contrib-parent</artifactId>
    <groupId>org.apache.drill.contrib</groupId>
    <version>1.15.0.0-mapr</version>
  </parent>

  <artifactId>drill-jdbc-storage</artifactId>

  <name>contrib/jdbc-storage-plugin</name>

  <properties>
    <mysql.connector.version>8.0.13</mysql.connector.version>
    <derby.database.name>drill_derby_test</derby.database.name>
    <mysql.database.name>drill_mysql_test</mysql.database.name>
    <mysql.scriptFile.name>mysql-test-data.sql</mysql.scriptFile.name>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.drill.exec</groupId>
      <artifactId>drill-java-exec</artifactId>
      <version>${project.version}</version>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
      <groupId>org.apache.drill.exec</groupId>
      <artifactId>drill-java-exec</artifactId>
      <classifier>tests</classifier>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.drill</groupId>
      <artifactId>drill-common</artifactId>
      <classifier>tests</classifier>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derbyclient</artifactId>
      <version>10.14.2.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derbynet</artifactId>
      <version>10.14.2.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.connector.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <profiles>
    <profile>
      <id>linux</id>
      <properties>
        <!-- Reg expr includes both mysql-test-data.sql and mysql-test-data-linux.sql script files -->
        <mysql.scriptFile.name>mysql-test-data*.sql</mysql.scriptFile.name>
      </properties>
      <activation>
        <os>
          <family>linux</family>
        </os>
      </activation>
    </profile>
  </profiles>

  <build>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
      </testResource>
    </testResources>
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <!--
          Forking multiple processes can cause race conditions with the initialization of
          the test databases.
          -->
          <forkCount combine.self="override">1</forkCount>
          <excludes>
            <exclude>**/*</exclude>
          </excludes>
        </configuration>
      </plugin>
      <plugin>
        <!-- Because the JDBC tests are somewhat heavyweight, we only run them in the 'verify' phase -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <forkCount combine.self="override">1</forkCount>
          <systemPropertyVariables>
            <derby.port>${derby.reserved.port}</derby.port>
            <mysql.port>${mysql.reserved.port}</mysql.port>
            <mysql.name>${mysql.database.name}</mysql.name>
          </systemPropertyVariables>
          <skipITs>${skipTests}</skipITs>
          <includes>
            <include>**/*IT.java</include>
          </includes>
          <useSystemClassLoader>false</useSystemClassLoader>
        </configuration>
        <executions>
          <execution>
            <id>run-IT-Tests</id>
            <phase>integration-test</phase>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
          <execution>
            <phase>verify</phase>
            <goals>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <!-- Allows us to reserve ports for external servers that we will launch  -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>reserve-network-port</id>
            <goals>
              <goal>reserve-network-port</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
              <portNames>
                <portName>derby.reserved.port</portName>
                <portName>mysql.reserved.port</portName>
              </portNames>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <skip>${skipTests}</skip>
              <artifactItems>
                <artifactItem>
                  <!-- This will download a MySQL distribution and use it to run tests against -->
                  <groupId>com.jcabi</groupId>
                  <artifactId>mysql-dist</artifactId>
                  <version>5.6.14</version>
                  <classifier>${mysql.classifier}</classifier>
                  <type>zip</type>
                  <overWrite>false</overWrite>
                  <outputDirectory>${project.build.directory}/mysql-dist</outputDirectory>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <!-- Allows us to run tests against an in-memory Derby database -->
        <groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
        <artifactId>inmemdb-maven-plugin</artifactId>
        <version>1.4.3</version>
        <configuration>
          <monitorKey>inmemdb</monitorKey>
          <monitorPort>11527</monitorPort>
          <skip>${skipTests}</skip>
        </configuration>
        <executions>
          <execution>
            <id>run</id>
            <goals>
              <goal>run</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
              <daemon>true</daemon>
              <type>derby</type>
              <database>${derby.database.name}</database>
              <username>root</username>
              <password>root</password>
              <port>${derby.reserved.port}</port>
              <sources>
                <script>
                  <sourceFile>${basedir}/src/test/resources/derby-test-data.sql</sourceFile>
                </script>
              </sources>
            </configuration>
          </execution>
          <execution>
            <id>stop</id>
            <goals>
              <goal>stop</goal>
            </goals>
            <phase>post-integration-test</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-mysql-maven-plugin</artifactId>
        <version>0.9</version>
        <dependencies>
          <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.0.Final</version>
            <scope>runtime</scope>
          </dependency>
          <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>mysql-test</id>
            <goals>
              <goal>classify</goal>
              <goal>start</goal>
              <goal>stop</goal>
            </goals>
            <configuration>
              <skip>${skipTests}</skip>
              <port>${mysql.reserved.port}</port>
              <data>${project.build.directory}/mysql-data</data>
              <clearexistingdata>true</clearexistingdata>
              <dbname>${mysql.database.name}</dbname>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
          </dependency>
        </dependencies>
        <configuration>
          <skip>${skipTests}</skip>
          <driver>com.mysql.cj.jdbc.Driver</driver>
          <username>root</username>
          <password>root</password>
          <url>jdbc:mysql://localhost:${mysql.reserved.port}/${mysql.database.name}</url>
        </configuration>
        <executions>
          <execution>
            <id>create-tables</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <orderFile>ascending</orderFile>
              <fileset>
                <basedir>${basedir}/src/test/resources</basedir>
                <includes>
                  <include>${mysql.scriptFile.name}</include>
                </includes>
              </fileset>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>
