View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.schema.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.junit.Test;
24  import org.kuali.common.impex.model.Table;
25  import org.kuali.common.impex.schema.impl.oracle.OracleTableSqlProducer;
26  
27  import static junit.framework.Assert.assertEquals;
28  import static junit.framework.Assert.fail;
29  
30  public class TestOracleTableSqlProducer {
31  
32      private final static String[] EXPECTED_SIMPLE_SQL = {"DECLARE temp NUMBER;\n" +
33              "BEGIN\n" +
34              "\tSELECT COUNT(*) INTO temp FROM user_tables WHERE table_name = 'FOO_T';\n" +
35              "\tIF temp > 0 THEN EXECUTE IMMEDIATE 'DROP TABLE FOO_T CASCADE CONSTRAINTS PURGE'; END IF;\n" +
36              "END;\n",
37  
38              "CREATE TABLE FOO_T\n" +
39              "(\n" +
40              "\tID VARCHAR2(36),\n" +
41              "\tCREATETIME TIMESTAMP NOT NULL,\n" +
42              "\tFOO_COUNT NUMBER(10),\n" +
43              "\tNAME VARCHAR2(255),\n" +
44              "\tCONSTRAINT FOO_U1_NAME UNIQUE (NAME)\n" +
45              ")\n",
46  
47              "ALTER TABLE FOO_T\n" +
48              "\tADD CONSTRAINT FOO_TP1\n" +
49              "PRIMARY KEY (ID)\n"
50      };
51  
52  
53  
54      @Test
55      public void simpleTableTest() {
56  
57          OracleTableSqlProducer producer = new OracleTableSqlProducer();
58  
59          producer.setMappingProvider(new NoOpProvider());
60  
61          Table table = MockDataUtil.buildSimpleTable();
62  
63          List<String> results = producer.getTablesSql(Collections.singletonList(table));
64  
65          assertEquals(EXPECTED_SIMPLE_SQL.length, results.size());
66  
67          List<String> expected = Arrays.asList(EXPECTED_SIMPLE_SQL);
68  
69          List<String> foundExpected = new ArrayList<String>(expected);
70          for (String e : expected) {
71              if(results.contains(e)) {
72                  foundExpected.remove(e);
73              }
74              else {
75                  fail("Expected sql statment **" + e + "** not found in generated statements.");
76              }
77          }
78  
79          if(!foundExpected.isEmpty()) {
80              fail("Following expected sql statements not found in generated statements: \n" + foundExpected.toString());
81          }
82  
83      }
84  
85  }