001: /*
002: * Copyright 2006 Le Duc Bao, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * 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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.castor.ddlgen.engine.db2;
017:
018: import java.io.ByteArrayOutputStream;
019:
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022:
023: import org.castor.ddlgen.DDLGenConfiguration;
024: import org.castor.ddlgen.DDLWriter;
025: import org.castor.ddlgen.GeneratorException;
026: import org.castor.ddlgen.KeyGeneratorRegistry;
027: import org.castor.ddlgen.test.framework.AbstractGeneratorTest;
028: import org.castor.ddlgen.test.framework.Expected;
029:
030: /**
031: * Db2 generator test.
032: *
033: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
034: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
035: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
036: * @since 1.1
037: */
038: public final class Db2GeneratorTest extends AbstractGeneratorTest {
039: public Db2GeneratorTest(final String testcase) {
040: super (testcase);
041: }
042:
043: public Db2GeneratorTest(final String testcase,
044: final boolean useDBEngine) {
045: super (testcase);
046:
047: if (useDBEngine) {
048: setEngine(Expected.ENGINE_DB2);
049: }
050: }
051:
052: public static Test suite() throws Exception {
053: TestSuite suite = new TestSuite(
054: "All org.castor.ddlgen.engine.db2 tests");
055:
056: // schema test
057: suite.addTest(new Db2GeneratorTest("testCreateSchema", true));
058:
059: // drop test
060: suite.addTest(new Db2GeneratorTest("testDropTable", false));
061:
062: // table test
063: suite.addTest(new Db2GeneratorTest("testSingleTable", false));
064: suite.addTest(new Db2GeneratorTest("testMultipleTable", false));
065: suite.addTest(new Db2GeneratorTest("testIgnoredTable", false));
066: suite.addTest(new Db2GeneratorTest("testNoTable", false));
067:
068: //field test
069: suite.addTest(new Db2GeneratorTest("testSingleField", false));
070: suite.addTest(new Db2GeneratorTest("testSingleFieldForAllDB2",
071: true));
072: suite.addTest(new Db2GeneratorTest("testSingleFieldExceptBit",
073: true));
074: suite.addTest(new Db2GeneratorTest("testIgnoredField", false));
075: suite.addTest(new Db2GeneratorTest("testNoField", false));
076: suite.addTest(new Db2GeneratorTest("testManyKeysReference",
077: false));
078: suite.addTest(new Db2GeneratorTest(
079: "testManyClassKeysReference", false));
080: suite.addTest(new Db2GeneratorTest("test2LevelsReference",
081: false));
082:
083: // primary key test
084: suite.addTest(new Db2GeneratorTest("testClassId", true));
085: suite
086: .addTest(new Db2GeneratorTest("testClassMultipleId",
087: true));
088: suite.addTest(new Db2GeneratorTest("testFieldId", true));
089: suite
090: .addTest(new Db2GeneratorTest("testFieldMultipleId",
091: true));
092: suite.addTest(new Db2GeneratorTest("testOverwriteFieldId",
093: false));
094: suite.addTest(new Db2GeneratorTest("testNoId", false));
095:
096: // foreign key test
097: suite.addTest(new Db2GeneratorTest("testOneOneRelationship",
098: false));
099: suite.addTest(new Db2GeneratorTest("testOneManyRelationship",
100: false));
101: suite.addTest(new Db2GeneratorTest("testManyManyRelationship",
102: false));
103:
104: // index test
105: suite.addTest(new Db2GeneratorTest("testCreateIndex", false));
106:
107: // key generator test
108: suite.addTest(new Db2GeneratorTest("testKeyGenIdentity", true));
109: suite.addTest(new Db2GeneratorTest("testKeyGenHighLow", false));
110: suite.addTest(new Db2GeneratorTest("testKeyGenMax", false));
111: suite.addTest(new Db2GeneratorTest("testKeyGenSequence", true));
112: suite.addTest(new Db2GeneratorTest("testKeyGenUUID", false));
113:
114: return suite;
115: }
116:
117: /**
118: * {@inheritDoc}
119: */
120: protected void setUp() throws Exception {
121: super .setUp();
122:
123: DDLGenConfiguration conf = new DDLGenConfiguration();
124: conf
125: .addProperties("org/castor/ddlgen/test/config/ddlgen.properties");
126: conf
127: .addProperties("org/castor/ddlgen/test/config/db2.properties");
128: setGenerator(new Db2Generator(conf));
129:
130: KeyGeneratorRegistry keyGenRegistry = new KeyGeneratorRegistry(
131: conf);
132: getGenerator().setKeyGenRegistry(keyGenRegistry);
133:
134: getGenerator().initialize();
135: }
136:
137: /**
138: * {@inheritDoc}
139: */
140: protected void tearDown() throws Exception {
141: super .tearDown();
142:
143: setGenerator(null);
144: }
145:
146: /**
147: * {@inheritDoc}
148: */
149: public void testSingleFieldForAllDB2() {
150: try {
151: loadData("single_field_for_all.xml");
152:
153: try {
154: ByteArrayOutputStream out = new ByteArrayOutputStream();
155: DDLWriter writer = new DDLWriter(out, getGenerator()
156: .getConfiguration());
157:
158: getGenerator().generateCreate(writer);
159:
160: writer.close();
161: out.toString();
162:
163: fail("bit type is not supported, expected an exception");
164: } catch (GeneratorException e) {
165: assertTrue(true);
166: }
167: } catch (Exception e) {
168: fail("testSingleFieldForAll: " + e.getMessage());
169: }
170: }
171:
172: /**
173: * Create a table with 23 fields represented to each data type
174: */
175: public void testSingleFieldExceptBit() {
176: try {
177: loadData("single_field_except_bit.xml");
178:
179: DDLGenConfiguration conf = getGenerator()
180: .getConfiguration();
181: Object[] params = new Object[] {
182: conf.getInteger(PARAM_PREFIX + "tinyint"
183: + PARAM_PRECISION),
184: conf.getInteger(PARAM_PREFIX + "smallint"
185: + PARAM_PRECISION),
186: conf.getInteger(PARAM_PREFIX + "integer"
187: + PARAM_PRECISION),
188: conf.getInteger(PARAM_PREFIX + "bigint"
189: + PARAM_PRECISION),
190: conf.getInteger(PARAM_PREFIX + "float"
191: + PARAM_PRECISION),
192: conf.getInteger(PARAM_PREFIX + "float"
193: + PARAM_DECIMALS),
194: conf.getInteger(PARAM_PREFIX + "double"
195: + PARAM_PRECISION),
196: conf.getInteger(PARAM_PREFIX + "double"
197: + PARAM_DECIMALS),
198: conf.getInteger(PARAM_PREFIX + "real"
199: + PARAM_PRECISION),
200: conf.getInteger(PARAM_PREFIX + "real"
201: + PARAM_DECIMALS),
202: conf.getInteger(PARAM_PREFIX + "numeric"
203: + PARAM_PRECISION),
204: conf.getInteger(PARAM_PREFIX + "numeric"
205: + PARAM_DECIMALS),
206: conf.getInteger(PARAM_PREFIX + "decimal"
207: + PARAM_PRECISION),
208: conf.getInteger(PARAM_PREFIX + "decimal"
209: + PARAM_DECIMALS),
210: conf.getInteger(PARAM_PREFIX + "char"
211: + PARAM_LENGTH),
212: conf.getInteger(PARAM_PREFIX + "varchar"
213: + PARAM_LENGTH),
214: conf.getInteger(PARAM_PREFIX + "longvarchar"
215: + PARAM_LENGTH),
216: conf.getInteger(PARAM_PREFIX + "timestamp"
217: + PARAM_PRECISION),
218: conf.getInteger(PARAM_PREFIX + "binary"
219: + PARAM_LENGTH),
220: conf.getInteger(PARAM_PREFIX + "varbinary"
221: + PARAM_LENGTH),
222: getSuffixString(conf, PARAM_PREFIX + "other"
223: + PARAM_LENGTH),
224: getSuffixString(conf, PARAM_PREFIX + "javaobject"
225: + PARAM_LENGTH),
226: getSuffixString(conf, PARAM_PREFIX + "blob"
227: + PARAM_LENGTH),
228: getSuffixString(conf, PARAM_PREFIX + "clob"
229: + PARAM_LENGTH) };
230:
231: ByteArrayOutputStream out = new ByteArrayOutputStream();
232: DDLWriter writer = new DDLWriter(out, conf);
233:
234: getGenerator().generateCreate(writer);
235:
236: writer.close();
237: String ddl = out.toString();
238:
239: boolean b = getExpected().match(getEngine(), ddl, params);
240: assertTrue("Generated DDL: " + ddl + "\n"
241: + "Expected DDL: "
242: + getExpected().getLastMatchString(), b);
243: } catch (Exception e) {
244: fail("testSingleFieldForAll: " + e.getMessage());
245: }
246: }
247:
248: private String getSuffixString(final DDLGenConfiguration conf,
249: final String key) {
250: String suffix = "";
251: int len = conf.getInteger(key).intValue();
252: if (len >= 1024) {
253: len = len / 1024;
254: suffix = "K";
255: }
256: if (len >= 1024) {
257: len = len / 1024;
258: suffix = "M";
259: }
260: if (len >= 1024) {
261: len = len / 1024;
262: suffix = "G";
263: }
264: return len + suffix;
265: }
266: }
|