KickJava   Java API By Example, From Geeks To Geeks.

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


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.DatabaseTable;
33 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
34
35 /**
36  * <code>DatabaseTable</code> test class.
37  *
38  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier</a>
39  * org.objectweb.cjdbc.common.sql..schema.DatabaseTable
40  */

41 public class DatabaseTableTest extends NoTemplate
42 {
43   /** Database table. */
44   private DatabaseTable t1, t2, t3, t4, t5, t6;
45
46   /** Database column. */
47   private DatabaseColumn c1, c2, c3, c4, c5, c6, c7, c8;
48
49   /**
50    * @see junit.framework.TestCase#setUp()
51    */

52   protected void setUp()
53   {
54     c1 = new DatabaseColumn("id", true, Types.INTEGER);
55     c2 = new DatabaseColumn("name", false, Types.VARCHAR);
56     c3 = new DatabaseColumn("rating", false, Types.INTEGER);
57     c4 = new DatabaseColumn("balance", false, Types.FLOAT);
58     c5 = new DatabaseColumn("creation_date", false, Types.TIMESTAMP);
59
60     c6 = new DatabaseColumn("nickname", false, Types.VARCHAR);
61     c7 = new DatabaseColumn("comment", false, Types.VARCHAR);
62     c8 = new DatabaseColumn("name", true, Types.VARCHAR);
63
64     t1 = new DatabaseTable("users", 5);
65     t1.addColumn(c1);
66     t1.addColumn(c2);
67     t1.addColumn(c3);
68     t1.addColumn(c4);
69     t1.addColumn(c5);
70
71     t2 = new DatabaseTable("users-merge-compatible", 2);
72     t2.addColumn(c6);
73     t2.addColumn(c7);
74
75     t3 = new DatabaseTable("users-merge-incompatible", 3);
76     t3.addColumn(c6);
77     t3.addColumn(c7);
78     t3.addColumn(c8);
79
80     // same as users table
81
t4 = new DatabaseTable("users", 5);
82     t4.addColumn(c1);
83     t4.addColumn(c2);
84     t4.addColumn(c3);
85     t4.addColumn(c4);
86     t4.addColumn(c5);
87
88     // same as users table but name is unique
89
t5 = new DatabaseTable("users", 5);
90     t5.addColumn(c1);
91     t5.addColumn(c2);
92     t5.addColumn(c3);
93     t5.addColumn(c8);
94     t5.addColumn(c5);
95
96     t6 = new DatabaseTable("users", 1);
97     t6.addColumn(c6);
98   }
99
100   /**
101    * org.objectweb.cjdbc.common.sql..schema.DatabaseTable#mergeColumns(DatabaseTable)
102    */

103   public void testMergeColumns()
104   {
105     try
106     {
107       t1.mergeColumns(t3);
108       fail("SQLException not thrown with two incompatible tables");
109     }
110     catch (SQLException JavaDoc ignore)
111     {
112     }
113
114     try
115     {
116       t1.mergeColumns(t2);
117       t1.mergeColumns(t1);
118     }
119     catch (SQLException JavaDoc e)
120     {
121       fail("SQLException thrown with two compatible tables (" + e + ")");
122     }
123
124     assertTrue(t1.getColumn("nickname").equals(c6));
125     assertTrue(t1.getColumn("comment").equals(c7));
126   }
127
128   /**
129    * org.objectweb.cjdbc.common.sql..schema.DatabaseTable#getColumns()
130    */

131   public void testGetColumns()
132   {
133     ArrayList JavaDoc columns = new ArrayList JavaDoc();
134     columns.add(c1);
135     columns.add(c2);
136     columns.add(c3);
137     columns.add(c4);
138     columns.add(c5);
139
140     assertEquals(columns, t1.getColumns());
141   }
142
143   /**
144    * org.objectweb.cjdbc.common.sql..schema.DatabaseTable#getColumn(String)
145    */

146   public void testGetColumn()
147   {
148     assertEquals(t1.getColumn("id"), c1);
149     assertEquals(t1.getColumn("ID"), c1);
150     assertNull(t1.getColumn("not_found"));
151   }
152
153   /**
154    * org.objectweb.cjdbc.common.sql..schema.DatabaseTable#getName()
155    */

156   public void testGetName()
157   {
158     assertEquals("users", t1.getName());
159   }
160
161   /**
162    * org.objectweb.cjdbc.common.sql..schema.DatabaseTable#equals(Object)
163    */

164   public void testEquals()
165   {
166     assertTrue(t1.equals(t1));
167     assertFalse(t1.equals(t2));
168     assertFalse(t1.equals(t3));
169     assertTrue(t1.equals(t4));
170     assertFalse(t1.equals(t5));
171     assertFalse(t1.equals(t6));
172   }
173
174   /**
175    * org.objectweb.cjdbc.common.sql..schema.DatabaseTable#equalsIgnoreType(Object)
176    */

177   public void testEqualsIgnoreType()
178   {
179     assertTrue(t1.equalsIgnoreType(t1));
180     assertFalse(t1.equalsIgnoreType(t2));
181     assertFalse(t1.equalsIgnoreType(t3));
182     assertTrue(t1.equalsIgnoreType(t4));
183     assertFalse(t1.equalsIgnoreType(t5));
184     assertFalse(t1.equalsIgnoreType(t6));
185   }
186 }
187
Popular Tags