1 24 25 package org.objectweb.cjdbc.scenario.standalone.sql.schema; 26 27 import java.sql.SQLException ; 28 import java.sql.Types ; 29 import java.util.ArrayList ; 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 42 public class DatabaseSchemaTest extends NoTemplate 43 { 44 45 private DatabaseSchema s1, s2, s3, s4, s5, s6; 46 47 48 private DatabaseTable t1, t2, t3, t4, t5, t6; 49 50 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 112 public void testAddTable() 113 { 114 assertTrue(s2.hasTable("categories")); 115 } 116 117 120 public void testRemoveTable() 121 { 122 s2.removeTable(t2); 123 assertFalse(s2.hasTable("regions")); 124 } 125 126 129 public void testGetTables() 130 { 131 ArrayList tables; 132 tables = new ArrayList (); 133 assertEquals(tables, s1.getTables()); 134 135 tables = new ArrayList (); 136 tables.add(t1); 137 tables.add(t2); 138 assertEquals(tables, s2.getTables()); 139 } 140 141 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 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 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 178 public void testMergeSchema() 179 { 180 try 181 { 182 s2.mergeSchema(s2); 183 s2.mergeSchema(s6); 184 } 185 catch (SQLException 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 ignore) 200 { 201 } 202 } 203 204 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 |