01: /*
02: * ObjectScripterTest.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2007, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db;
13:
14: import java.util.ArrayList;
15: import java.util.List;
16: import junit.framework.TestCase;
17: import workbench.TestUtil;
18:
19: /**
20: * @author support@sql-workbench.net
21: */
22: public class ObjectScripterTest extends TestCase {
23: public ObjectScripterTest(String testName) {
24: super (testName);
25: }
26:
27: private void setupDatabase(WbConnection conn) {
28: try {
29: String script = "create sequence test_sequence;\n"
30: + "create table person (id integer primary key, firstname varchar(50), lastname varchar(50)); \n"
31: + "create table address (id integer primary key, person_id integer not null, address_info varchar(500)); \n"
32: + "alter table address add constraint fk_adr_per foreign key (person_id) references person(id); \n"
33: + "create view v_person as select * from person\n;";
34:
35: TestUtil.executeScript(conn, script);
36: } catch (Exception e) {
37: e.printStackTrace();
38: fail(e.getMessage());
39: }
40: }
41:
42: public void testGenerateScript() {
43:
44: try {
45: TestUtil util = new TestUtil("objectSripterTest");
46:
47: WbConnection con = util.getConnection();
48: setupDatabase(con);
49: List<DbObject> objects = new ArrayList<DbObject>();
50:
51: objects.add(new SequenceDefinition(null, "TEST_SEQUENCE"));
52: TableIdentifier tbl = new TableIdentifier("PERSON");
53: tbl.setType("TABLE");
54: objects.add(tbl);
55:
56: tbl = new TableIdentifier("ADDRESS");
57: tbl.setType("TABLE");
58: objects.add(tbl);
59:
60: objects.add(new ViewDefinition("V_PERSON"));
61: ObjectScripter scripter = new ObjectScripter(objects, con);
62: String script = scripter.getScript();
63: System.out.println(script);
64:
65: int personPos = script.indexOf("CREATE TABLE PERSON");
66: assertTrue(personPos > -1);
67:
68: int addressPos = script.indexOf("CREATE TABLE ADDRESS");
69: assertTrue(addressPos > -1);
70:
71: int seqPos = script.indexOf("CREATE SEQUENCE TEST_");
72: assertTrue(seqPos > -1);
73:
74: assertTrue(personPos > seqPos);
75: assertTrue(addressPos > seqPos);
76:
77: int fkPos = script.indexOf("CONSTRAINT FK_ADR_PER");
78: assertTrue(fkPos > personPos);
79: assertTrue(fkPos > addressPos);
80:
81: int viewPos = script.indexOf("CREATE FORCE VIEW");
82: assertTrue(viewPos > -1);
83: assertTrue(viewPos > personPos);
84: assertTrue(viewPos > addressPos);
85: } catch (Exception e) {
86: e.printStackTrace();
87: fail(e.getMessage());
88: }
89: }
90: }
|