KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > sql > schema > DatabaseSchemaTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.sql.schema;
26
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Types JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn;
32 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
33 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
34 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
35
36 /**
37  * <code>DatabaseSchema</code> test class.
38  *
39  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier</a>
40  * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema
41  */

42 public class DatabaseSchemaTest extends NoTemplate
43 {
44   /** Database schema. */
45   private DatabaseSchema s1, s2, s3, s4, s5, s6;
46
47   /** Database table. */
48   private DatabaseTable t1, t2, t3, t4, t5, t6;
49
50   /**
51    * @see junit.framework.TestCase#setUp()
52    */

53   protected void setUp()
54   {
55     t1 = new DatabaseTable("categories", 2);
56     t1.addColumn(new DatabaseColumn("id", true, Types.INTEGER));
57     t1.addColumn(new DatabaseColumn("name", false, Types.VARCHAR));
58
59     t2 = new DatabaseTable("regions", 2);
60     t2.addColumn(new DatabaseColumn("id", true, Types.INTEGER));
61     t2.addColumn(new DatabaseColumn("name", false, Types.VARCHAR));
62
63     t3 = new DatabaseTable("users", 10);
64     t3.addColumn(new DatabaseColumn("id", true, Types.INTEGER));
65     t3.addColumn(new DatabaseColumn("firstname", false, Types.VARCHAR));
66     t3.addColumn(new DatabaseColumn("lastname", false, Types.VARCHAR));
67     t3.addColumn(new DatabaseColumn("nickname", false, Types.VARCHAR));
68     t3.addColumn(new DatabaseColumn("password", false, Types.VARCHAR));
69     t3.addColumn(new DatabaseColumn("email", false, Types.VARCHAR));
70     t3.addColumn(new DatabaseColumn("rating", false, Types.INTEGER));
71     t3.addColumn(new DatabaseColumn("balance", false, Types.FLOAT));
72     t3.addColumn(new DatabaseColumn("creation_date", false, Types.TIMESTAMP));
73     t3.addColumn(new DatabaseColumn("region", false, Types.INTEGER));
74
75     t4 = new DatabaseTable("regions", 2);
76     t4.addColumn(new DatabaseColumn("id", true, Types.FLOAT));
77     t4.addColumn(new DatabaseColumn("nickname", false, Types.VARCHAR));
78
79     t5 = new DatabaseTable("regions", 2);
80     t5.addColumn(new DatabaseColumn("id", true, Types.INTEGER));
81     t5.addColumn(new DatabaseColumn("name", true, Types.VARCHAR));
82
83     t6 = new DatabaseTable("regions", 1);
84     t6.addColumn(new DatabaseColumn("country", true, Types.VARCHAR));
85
86     s1 = new DatabaseSchema();
87
88     s2 = new DatabaseSchema(2);
89     s2.addTable(t1);
90     s2.addTable(t2);
91
92     s3 = new DatabaseSchema(1);
93     s3.addTable(t1);
94
95     s4 = new DatabaseSchema(2);
96     s4.addTable(t1);
97     s4.addTable(t4);
98
99     s5 = new DatabaseSchema(3);
100     s5.addTable(t1);
101     s5.addTable(t2);
102     s5.addTable(t5);
103
104     s6 = new DatabaseSchema(1);
105     s6.addTable(t3);
106     s6.addTable(t6);
107   }
108
109   /**
110    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#addTable(DatabaseTable)
111    */

112   public void testAddTable()
113   {
114     assertTrue(s2.hasTable("categories"));
115   }
116
117   /**
118    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#removeTable(DatabaseTable)
119    */

120   public void testRemoveTable()
121   {
122     s2.removeTable(t2);
123     assertFalse(s2.hasTable("regions"));
124   }
125
126   /**
127    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#getTables()
128    */

129   public void testGetTables()
130   {
131     ArrayList JavaDoc tables;
132     tables = new ArrayList JavaDoc();
133     assertEquals(tables, s1.getTables());
134
135     tables = new ArrayList JavaDoc();
136     tables.add(t1);
137     tables.add(t2);
138     assertEquals(tables, s2.getTables());
139   }
140
141   /**
142    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#getTable(String)
143    */

144   public void testGetTable()
145   {
146     assertEquals(t1, s2.getTable("categories"));
147     assertEquals(t2, s2.getTable("regions"));
148     assertNull(s2.getTable("users"));
149     assertNull(s2.getTable(null));
150   }
151
152   /**
153    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#hasTable(String)
154    */

155   public void testHasTable()
156   {
157     assertTrue(s2.hasTable("categories"));
158     assertTrue(s2.hasTable("regions"));
159     assertFalse(s2.hasTable("users"));
160     assertFalse(s2.hasTable(null));
161   }
162
163   /**
164    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#isCompatibleSubset(DatabaseSchema)
165    */

166   public void testIsCompatibleSubset()
167   {
168     assertTrue(s1.isCompatibleSubset(s1));
169     assertTrue(s2.isCompatibleSubset(s2));
170     assertFalse(s2.isCompatibleSubset(s3));
171     assertFalse(s2.isCompatibleSubset(s4));
172     assertTrue(s2.isCompatibleSubset(s5));
173   }
174
175   /**
176    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#mergeSchema(DatabaseSchema)
177    */

178   public void testMergeSchema()
179   {
180     try
181     {
182       s2.mergeSchema(s2);
183       s2.mergeSchema(s6);
184     }
185     catch (SQLException JavaDoc e)
186     {
187       fail("Exception thrown while merging two compatible schemas: " + e);
188     }
189     assertEquals(t3, s2.getTable("users"));
190     assertEquals(new DatabaseColumn("country", true, Types.VARCHAR), s2
191         .getTable("regions").getColumn("country"));
192
193     try
194     {
195       s2.mergeSchema(s4);
196       s2.mergeSchema(s5);
197       fail("Exception not thrown while merging two not compatible schemas");
198     }
199     catch (SQLException JavaDoc ignore)
200     {
201     }
202   }
203
204   /**
205    * org.objectweb.cjdbc.common.sql..schema.DatabaseSchema#equals(Object)
206    */

207   public void testEquals()
208   {
209     assertFalse(s1.equals(s2));
210     assertFalse(s1.equals(s3));
211     assertFalse(s1.equals(s4));
212     assertFalse(s1.equals(s5));
213
214     s1.addTable(t1);
215     s1.addTable(t2);
216     s1.addTable(t3);
217     assertFalse(s1.equals(s2));
218
219     s2.addTable(t3);
220     assertTrue(s1.equals(s2));
221   }
222 }
223
Popular Tags