001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.dbmaintainer.clean.impl;
017:
018: import static org.junit.Assert.fail;
019: import static org.unitils.core.dbsupport.DbSupportFactory.getDefaultDbSupport;
020: import static org.unitils.dbmaintainer.clean.impl.DefaultDBClearer.PROPKEY_PRESERVE_SCHEMAS;
021: import static org.unitils.dbmaintainer.clean.impl.DefaultDBClearer.PROPKEY_PRESERVE_SEQUENCES;
022: import static org.unitils.dbmaintainer.clean.impl.DefaultDBClearer.PROPKEY_PRESERVE_SYNONYMS;
023: import static org.unitils.dbmaintainer.clean.impl.DefaultDBClearer.PROPKEY_PRESERVE_TABLES;
024: import static org.unitils.dbmaintainer.clean.impl.DefaultDBClearer.PROPKEY_PRESERVE_VIEWS;
025:
026: import java.util.Properties;
027:
028: import javax.sql.DataSource;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.junit.Before;
033: import org.junit.Test;
034: import org.unitils.UnitilsJUnit4;
035: import org.unitils.core.ConfigurationLoader;
036: import org.unitils.core.UnitilsException;
037: import org.unitils.core.dbsupport.SQLHandler;
038: import org.unitils.database.annotations.TestDataSource;
039: import org.unitils.dbmaintainer.clean.DBClearer;
040:
041: /**
042: * Test class for the {@link DBClearer} with preserve items configured, but some items do not exist.
043: *
044: * @author Tim Ducheyne
045: * @author Filip Neven
046: */
047: public class DefaultDBClearerPreserveDoesNotExistTest extends
048: UnitilsJUnit4 {
049:
050: /* The logger instance for this class */
051: private static Log logger = LogFactory
052: .getLog(DefaultDBClearerPreserveDoesNotExistTest.class);
053:
054: /* DataSource for the test database, is injected */
055: @TestDataSource
056: private DataSource dataSource = null;
057:
058: /* Tested object */
059: private DefaultDBClearer defaultDbClearer;
060:
061: /* The unitils configuration */
062: private Properties configuration;
063:
064: /* The sql statement handler */
065: private SQLHandler sqlHandler;
066:
067: /**
068: * Configures the tested object.
069: *
070: * todo Test_trigger_Preserve Test_CASE_Trigger_Preserve
071: */
072: @Before
073: public void setUp() throws Exception {
074: configuration = new ConfigurationLoader().loadConfiguration();
075: sqlHandler = new SQLHandler(dataSource);
076: defaultDbClearer = new DefaultDBClearer();
077: }
078:
079: /**
080: * Test for schemas to preserve that do not exist.
081: */
082: @Test(expected=UnitilsException.class)
083: public void testClearSchemas_schemasToPreserveDoNotExist()
084: throws Exception {
085: configuration.setProperty(PROPKEY_PRESERVE_SCHEMAS,
086: "unexisting_schema1, unexisting_schema2");
087: defaultDbClearer.init(configuration, sqlHandler);
088: }
089:
090: /**
091: * Test for tables to preserve that do not exist.
092: */
093: @Test(expected=UnitilsException.class)
094: public void testClearSchemas_tablesToPreserveDoNotExist()
095: throws Exception {
096: configuration.setProperty(PROPKEY_PRESERVE_TABLES,
097: "unexisting_table1, unexisting_table2");
098: defaultDbClearer.init(configuration, sqlHandler);
099: }
100:
101: /**
102: * Test for views to preserve that do not exist.
103: */
104: @Test(expected=UnitilsException.class)
105: public void testClearSchemas_viewsToPreserveDoNotExist()
106: throws Exception {
107: configuration.setProperty(PROPKEY_PRESERVE_VIEWS,
108: "unexisting_view1, unexisting_view2");
109: defaultDbClearer.init(configuration, sqlHandler);
110: }
111:
112: /**
113: * Test for sequences to preserve that do not exist.
114: */
115: @Test
116: public void testClearSchemas_sequencesToPreserveDoNotExist()
117: throws Exception {
118: if (!getDefaultDbSupport(configuration, sqlHandler)
119: .supportsSequences()) {
120: logger
121: .warn("Current dialect does not support sequences. Skipping test.");
122: return;
123: }
124: try {
125: configuration.setProperty(PROPKEY_PRESERVE_SEQUENCES,
126: "unexisting_sequence1, unexisting_sequence2");
127: defaultDbClearer.init(configuration, sqlHandler);
128: fail("UnitilsException expected.");
129: } catch (UnitilsException e) {
130: // expected
131: }
132: }
133:
134: /**
135: * Test for synonyms to preserve that do not exist.
136: */
137: @Test
138: public void testClearSchemas_synonymsToPreserveDoNotExist()
139: throws Exception {
140: if (!getDefaultDbSupport(configuration, sqlHandler)
141: .supportsSynonyms()) {
142: logger
143: .warn("Current dialect does not support synonyms. Skipping test.");
144: return;
145: }
146: try {
147: configuration.setProperty(PROPKEY_PRESERVE_SYNONYMS,
148: "unexisting_synonym1, unexisting_synonym2");
149: defaultDbClearer.init(configuration, sqlHandler);
150: fail("UnitilsException expected.");
151: } catch (UnitilsException e) {
152: // expected
153: }
154:
155: }
156: }
|