# HG changeset patch
# User Augusto Mauch <augusto.mauch@openbravo.com>
# Date 1449662230 -3600
#      Wed Dec 09 12:57:10 2015 +0100
# Node ID 0c8377991884c991dc23a2fb2a5a1a7117140021
# Parent  f8106033817e99f604fed308eb20ba383704dfe7
Related with issue 31672: Enables using a custom exclude filter in DbsmTest

diff --git a/src-test/src/org/openbravo/dbsm/test/base/DbsmTest.java b/src-test/src/org/openbravo/dbsm/test/base/DbsmTest.java
--- a/src-test/src/org/openbravo/dbsm/test/base/DbsmTest.java
+++ b/src-test/src/org/openbravo/dbsm/test/base/DbsmTest.java
@@ -94,6 +94,7 @@
   private Rdbms rdbms;
   private Platform platform;
   private SQLBatchEvaluator evaluator;
+  private ExcludeFilter excludeFilter;
   protected RecreationMode recreationMode = RecreationMode.standard;
 
   public enum Rdbms {
@@ -199,7 +200,14 @@
   }
 
   protected ExcludeFilter getExcludeFilter() {
-    return new OpenbravoExcludeFilter();
+    if (excludeFilter == null) {
+      excludeFilter = new OpenbravoExcludeFilter();
+    }
+    return excludeFilter;
+  }
+
+  public void setExcludeFilter(ExcludeFilter excludeFilter) {
+    this.excludeFilter = excludeFilter;
   }
 
   protected BasicDataSource getDataSource() {
# HG changeset patch
# User Augusto Mauch <augusto.mauch@openbravo.com>
# Date 1449673211 -3600
#      Wed Dec 09 16:00:11 2015 +0100
# Node ID 06fa38a1654c2937a529b0d0a6e6b3dace012a21
# Parent  0c8377991884c991dc23a2fb2a5a1a7117140021
Fixes issue 31672: Adds tests to check the exclusion filter in dbsourcemanager

The new tests check that the exclude filter works properly for functions, tables, triggers and views. The tests check that the excluded database objects are indeed in the database, but that are not exported.

diff --git a/src-test/model/excludeFilter/BASE_MODEL_WITH_FUNCTION.xml b/src-test/model/excludeFilter/BASE_MODEL_WITH_FUNCTION.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/BASE_MODEL_WITH_FUNCTION.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+  <database name="TABLE TEST">
+    <function name="NOT_EXCLUDED_FUNCTION" type="NUMERIC">
+      <body><![CDATA[BEGIN
+  RETURN(0) ;
+END NOT_EXCLUDED_FUNCTION
+]]></body>
+    </function>
+    <function name="EXCLUDED_FUNCTION" type="NUMERIC">
+      <body><![CDATA[BEGIN
+  RETURN(0) ;
+END EXCLUDED_FUNCTION
+]]></body>
+    </function>    
+  </database>
diff --git a/src-test/model/excludeFilter/BASE_MODEL_WITH_TABLE.xml b/src-test/model/excludeFilter/BASE_MODEL_WITH_TABLE.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/BASE_MODEL_WITH_TABLE.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+  <database name="TABLE TEST">
+    <table name="NOT_EXCLUDED_TABLE" primaryKey="NOT_EXCLUDED_TEST_ID">
+      <column name="TEST_ID" primaryKey="true" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+      <column name="COL1" primaryKey="false" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+    </table>
+    <table name="EXCLUDED_TABLE" primaryKey="EXCLUDED_TEST_ID">
+      <column name="TEST_ID" primaryKey="true" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+      <column name="COL1" primaryKey="false" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+    </table>
+  </database>
diff --git a/src-test/model/excludeFilter/BASE_MODEL_WITH_TRIGGER.xml b/src-test/model/excludeFilter/BASE_MODEL_WITH_TRIGGER.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/BASE_MODEL_WITH_TRIGGER.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0"?>
+  <database name="TABLE TEST">
+    <table name="NOT_EXCLUDED_TABLE" primaryKey="TEST_ID">
+      <column name="TEST_ID" primaryKey="true" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+      <column name="COL1" primaryKey="false" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+    </table>
+    
+    <trigger name="NOT_EXCLUDED_TRIGGER" table="NOT_EXCLUDED_TABLE" fires="before" insert="true" update="true" delete="true" foreach="row">
+      <body><![CDATA[
+BEGIN
+END NOT_EXCLUDED_TRIGGER
+]]></body>
+    </trigger>
+    <trigger name="EXCLUDED_TRIGGER" table="NOT_EXCLUDED_TABLE" fires="before" insert="true" update="true" delete="true" foreach="row">
+      <body><![CDATA[
+BEGIN
+END EXCLUDED_TRIGGER
+]]></body>
+    </trigger>    
+  </database>
diff --git a/src-test/model/excludeFilter/BASE_MODEL_WITH_VIEW.xml b/src-test/model/excludeFilter/BASE_MODEL_WITH_VIEW.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/BASE_MODEL_WITH_VIEW.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+  <database name="TABLE TEST">
+    <table name="NOT_EXCLUDED_TABLE" primaryKey="TEST_ID">
+      <column name="TEST_ID" primaryKey="true" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+      <column name="COL1" primaryKey="false" required="true" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
+    </table>
+    <view name="NOT_EXCLUDED_VIEW"><![CDATA[SELECT col1 FROM not_excluded_table]]></view>
+    <view name="EXCLUDED_VIEW"><![CDATA[SELECT col1 FROM not_excluded_table]]></view>
+  </database>
diff --git a/src-test/model/excludeFilter/excludeFunction.xml b/src-test/model/excludeFilter/excludeFunction.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/excludeFunction.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+  <vector>
+    <excludedFunction name="EXCLUDED_FUNCTION"/>
+  </vector>
\ No newline at end of file
diff --git a/src-test/model/excludeFilter/excludeTable.xml b/src-test/model/excludeFilter/excludeTable.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/excludeTable.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0"?>
+  <vector>
+    <excludedTable name="EXCLUDED_TABLE"/>
+  </vector>
+
diff --git a/src-test/model/excludeFilter/excludeTrigger.xml b/src-test/model/excludeFilter/excludeTrigger.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/excludeTrigger.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0"?>
+  <vector>
+    <excludedTrigger name="EXCLUDED_TRIGGER"/>
+  </vector>
+
diff --git a/src-test/model/excludeFilter/excludeView.xml b/src-test/model/excludeFilter/excludeView.xml
new file mode 100644
--- /dev/null
+++ b/src-test/model/excludeFilter/excludeView.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0"?>
+  <vector>
+    <excludedView name="EXCLUDED_VIEW"/>
+  </vector>
+
diff --git a/src-test/src/org/openbravo/dbsm/test/model/CheckExcludeFilter.java b/src-test/src/org/openbravo/dbsm/test/model/CheckExcludeFilter.java
new file mode 100644
--- /dev/null
+++ b/src-test/src/org/openbravo/dbsm/test/model/CheckExcludeFilter.java
@@ -0,0 +1,141 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2015 Openbravo S.L.U.
+ * Licensed under the Apache Software License version 2.0
+ * 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.
+ ************************************************************************************
+ */
+
+package org.openbravo.dbsm.test.model;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.model.Database;
+import org.apache.ddlutils.model.Function;
+import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.model.Trigger;
+import org.apache.ddlutils.model.View;
+import org.apache.ddlutils.platform.ExcludeFilter;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.openbravo.dbsm.test.base.DbsmTest;
+
+/**
+ * Test cases to test the function based indexes support
+ * 
+ * @author AugustoMauch
+ *
+ */
+@RunWith(Parameterized.class)
+public class CheckExcludeFilter extends DbsmTest {
+
+  private static final String EXPORT_DIR = "/tmp/export-test";
+
+  public CheckExcludeFilter(String rdbms, String driver, String url, String sid, String user,
+      String password, String name) throws FileNotFoundException, IOException {
+    super(rdbms, driver, url, sid, user, password, name);
+  }
+
+  @Test
+  // Tests that tables in the exclude filter are not exported
+  public void tableIsExcluded() throws IOException {
+    resetDB();
+    updateDatabase("excludeFilter/BASE_MODEL_WITH_TABLE.xml");
+    ExcludeFilter excludeFilter = new ExcludeFilter();
+    excludeFilter.fillFromFile(new File("model/excludeFilter/excludeTable.xml"));
+    setExcludeFilter(excludeFilter);
+    exportDatabase(EXPORT_DIR);
+    File notExcludedFile = new File(EXPORT_DIR + "/tables/NOT_EXCLUDED_TABLE.xml");
+    File excludedFile = new File(EXPORT_DIR + "/tables/EXCLUDED_TABLE.xml");
+    assertThat(excludedTableExistsInDb(), equalTo(true));
+    assertThat(notExcludedFile.exists(), equalTo(true));
+    assertThat(excludedFile.exists(), equalTo(false));
+  }
+
+  @Test
+  // Tests that tables in the exclude filter are not exported
+  public void viewIsExcluded() throws IOException {
+    resetDB();
+    updateDatabase("excludeFilter/BASE_MODEL_WITH_VIEW.xml");
+    ExcludeFilter excludeFilter = new ExcludeFilter();
+    excludeFilter.fillFromFile(new File("model/excludeFilter/excludeView.xml"));
+    setExcludeFilter(excludeFilter);
+    exportDatabase(EXPORT_DIR);
+    File notExcludedFile = new File(EXPORT_DIR + "/views/NOT_EXCLUDED_VIEW.xml");
+    File excludedFile = new File(EXPORT_DIR + "/views/EXCLUDED_VIEW.xml");
+    assertThat(excludedViewExistsInDb(), equalTo(true));
+    assertThat(notExcludedFile.exists(), equalTo(true));
+    assertThat(excludedFile.exists(), equalTo(false));
+  }
+
+  @Test
+  // Tests that tables in the exclude filter are not exported
+  public void triggerIsExcluded() throws IOException {
+    resetDB();
+    updateDatabase("excludeFilter/BASE_MODEL_WITH_TRIGGER.xml");
+    ExcludeFilter excludeFilter = new ExcludeFilter();
+    excludeFilter.fillFromFile(new File("model/excludeFilter/excludeTrigger.xml"));
+    setExcludeFilter(excludeFilter);
+    exportDatabase(EXPORT_DIR);
+    File notExcludedFile = new File(EXPORT_DIR + "/triggers/NOT_EXCLUDED_TRIGGER.xml");
+    File excludedFile = new File(EXPORT_DIR + "/triggers/EXCLUDED_TRIGGER.xml");
+    assertThat(excludedTriggerExistsInDb(), equalTo(true));
+    assertThat(notExcludedFile.exists(), equalTo(true));
+    assertThat(excludedFile.exists(), equalTo(false));
+  }
+
+  @Test
+  // Tests that tables in the exclude filter are not exported
+  public void functionIsExcluded() throws IOException {
+    resetDB();
+    updateDatabase("excludeFilter/BASE_MODEL_WITH_FUNCTION.xml");
+    ExcludeFilter excludeFilter = new ExcludeFilter();
+    excludeFilter.fillFromFile(new File("model/excludeFilter/excludeFunction.xml"));
+    setExcludeFilter(excludeFilter);
+    exportDatabase(EXPORT_DIR);
+    File notExcludedFile = new File(EXPORT_DIR + "/functions/NOT_EXCLUDED_FUNCTION.xml");
+    File excludedFile = new File(EXPORT_DIR + "/functions/EXCLUDED_FUNCTION.xml");
+    assertThat(excludedFunctionExistsInDb(), equalTo(true));
+    assertThat(notExcludedFile.exists(), equalTo(true));
+    assertThat(excludedFile.exists(), equalTo(false));
+  }
+
+  private boolean excludedFunctionExistsInDb() {
+    Platform platform = getPlatform();
+    Database database = platform.loadModelFromDatabase(new ExcludeFilter());
+    Function function = database.findFunction("EXCLUDED_FUNCTION");
+    return (function != null);
+  }
+
+  private boolean excludedTableExistsInDb() {
+    Platform platform = getPlatform();
+    Database database = platform.loadModelFromDatabase(new ExcludeFilter());
+    Table table = database.findTable("EXCLUDED_TABLE");
+    return (table != null);
+  }
+
+  private boolean excludedViewExistsInDb() {
+    Platform platform = getPlatform();
+    Database database = platform.loadModelFromDatabase(new ExcludeFilter());
+    View view = database.findView("EXCLUDED_VIEW");
+    return (view != null);
+  }
+
+  private boolean excludedTriggerExistsInDb() {
+    Platform platform = getPlatform();
+    Database database = platform.loadModelFromDatabase(new ExcludeFilter());
+    Trigger trigger = database.findTrigger("EXCLUDED_TRIGGER");
+    return (trigger != null);
+  }
+}
diff --git a/src-test/src/org/openbravo/dbsm/test/model/ModelSuite.java b/src-test/src/org/openbravo/dbsm/test/model/ModelSuite.java
--- a/src-test/src/org/openbravo/dbsm/test/model/ModelSuite.java
+++ b/src-test/src/org/openbravo/dbsm/test/model/ModelSuite.java
@@ -36,6 +36,7 @@
     CreateDefault.class, //
     FunctionBasedIndexes.class, //
     OperatorClassIndexes.class, //
+    CheckExcludeFilter.class, //
     Functions.class })
 public class ModelSuite {
 
