1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import com.healthmarketscience.jackcess.Database.FileFormat;
26 import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
27 import com.healthmarketscience.jackcess.impl.DatabaseImpl;
28 import com.healthmarketscience.jackcess.impl.TableImpl;
29 import junit.framework.TestCase;
30 import static com.healthmarketscience.jackcess.TestUtil.*;
31 import static com.healthmarketscience.jackcess.DatabaseBuilder.*;
32
33
34
35
36
37 public class TableUpdaterTest extends TestCase
38 {
39
40 public TableUpdaterTest(String name) throws Exception {
41 super(name);
42 }
43
44 public void testTableUpdating() throws Exception {
45 for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
46 Database db = create(fileFormat);
47
48 doTestUpdating(db, false, true, null);
49
50 db.close();
51 }
52 }
53
54 public void testTableUpdatingOneToOne() throws Exception {
55 for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
56 Database db = create(fileFormat);
57
58 doTestUpdating(db, true, true, null);
59
60 db.close();
61 }
62 }
63
64 public void testTableUpdatingNoEnforce() throws Exception {
65 for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
66 Database db = create(fileFormat);
67
68 doTestUpdating(db, false, false, null);
69
70 db.close();
71 }
72 }
73
74 public void testTableUpdatingNamedRelationship() throws Exception {
75 for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
76 Database db = create(fileFormat);
77
78 doTestUpdating(db, false, true, "FKnun3jvv47l9kyl74h85y8a0if");
79
80 db.close();
81 }
82 }
83
84 private void doTestUpdating(Database db, boolean oneToOne, boolean enforce, String relationshipName)
85 throws Exception
86 {
87 Table t1 = newTable("TestTable")
88 .addColumn(newColumn("id", DataType.LONG))
89 .toTable(db);
90
91 Table t2 = newTable("TestTable2")
92 .addColumn(newColumn("id2", DataType.LONG))
93 .toTable(db);
94
95 int t1idxs = 1;
96 newPrimaryKey("id")
97 .addToTable(t1);
98 newColumn("data", DataType.TEXT)
99 .addToTable(t1);
100 newColumn("bigdata", DataType.MEMO)
101 .addToTable(t1);
102
103 newColumn("data2", DataType.TEXT)
104 .addToTable(t2);
105 newColumn("bigdata2", DataType.MEMO)
106 .addToTable(t2);
107
108 int t2idxs = 0;
109 if(oneToOne) {
110 ++t2idxs;
111 newPrimaryKey("id2")
112 .addToTable(t2);
113 }
114
115 RelationshipBuilder rb = newRelationship("TestTable", "TestTable2")
116 .addColumns("id", "id2");
117 if(enforce) {
118 ++t1idxs;
119 ++t2idxs;
120 rb.setReferentialIntegrity()
121 .setCascadeDeletes();
122 }
123
124 if (relationshipName != null) {
125 rb.setName(relationshipName);
126 }
127
128 Relationship rel = rb.toRelationship(db);
129
130 if (relationshipName == null) {
131 assertEquals("TestTableTestTable2", rel.getName());
132 } else {
133 assertEquals(relationshipName, rel.getName());
134 }
135 assertSame(t1, rel.getFromTable());
136 assertEquals(Arrays.asList(t1.getColumn("id")), rel.getFromColumns());
137 assertSame(t2, rel.getToTable());
138 assertEquals(Arrays.asList(t2.getColumn("id2")), rel.getToColumns());
139 assertEquals(oneToOne, rel.isOneToOne());
140 assertEquals(enforce, rel.hasReferentialIntegrity());
141 assertEquals(enforce, rel.cascadeDeletes());
142 assertFalse(rel.cascadeUpdates());
143 assertEquals(Relationship.JoinType.INNER, rel.getJoinType());
144
145 assertEquals(t1idxs, t1.getIndexes().size());
146 assertEquals(1, ((TableImpl)t1).getIndexDatas().size());
147
148 assertEquals(t2idxs, t2.getIndexes().size());
149 assertEquals((t2idxs > 0 ? 1 : 0), ((TableImpl)t2).getIndexDatas().size());
150
151 ((DatabaseImpl)db).getPageChannel().startWrite();
152 try {
153
154 for(int i = 0; i < 10; ++i) {
155 t1.addRow(i, "row" + i, "row-data" + i);
156 }
157
158 for(int i = 0; i < 10; ++i) {
159 t2.addRow(i, "row2_" + i, "row-data2_" + i);
160 }
161
162 } finally {
163 ((DatabaseImpl)db).getPageChannel().finishWrite();
164 }
165
166 try {
167 t2.addRow(10, "row10", "row-data10");
168 if(enforce) {
169 fail("ConstraintViolationException should have been thrown");
170 }
171 } catch(ConstraintViolationException cv) {
172
173 if(!enforce) { throw cv; }
174 }
175
176 Row r1 = CursorBuilder.findRowByPrimaryKey(t1, 5);
177 t1.deleteRow(r1);
178
179 int id = 0;
180 for(Row r : t1) {
181 assertEquals(id, r.get("id"));
182 ++id;
183 if(id == 5) {
184 ++id;
185 }
186 }
187
188 id = 0;
189 for(Row r : t2) {
190 assertEquals(id, r.get("id2"));
191 ++id;
192 if(enforce && (id == 5)) {
193 ++id;
194 }
195 }
196 }
197
198 public void testInvalidUpdate() throws Exception
199 {
200 for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
201 Database db = create(fileFormat);
202
203 Table t1 = newTable("TestTable")
204 .addColumn(newColumn("id", DataType.LONG))
205 .toTable(db);
206
207 try {
208 newColumn("ID", DataType.TEXT)
209 .addToTable(t1);
210 fail("created table with no columns?");
211 } catch(IllegalArgumentException e) {
212
213 }
214
215 Table t2 = newTable("TestTable2")
216 .addColumn(newColumn("id2", DataType.LONG))
217 .toTable(db);
218
219 try {
220 newRelationship(t1, t2)
221 .toRelationship(db);
222 fail("created rel with no columns?");
223 } catch(IllegalArgumentException e) {
224
225 }
226
227 try {
228 newRelationship("TestTable", "TestTable2")
229 .addColumns("id", "id")
230 .toRelationship(db);
231 fail("created rel with wrong columns?");
232 } catch(IllegalArgumentException e) {
233
234 }
235
236 db.close();
237 }
238 }
239
240 public void testUpdateLargeTableDef() throws Exception
241 {
242 for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
243 Database db = create(fileFormat);
244
245 final int numColumns = 89;
246
247 Table t = newTable("test")
248 .addColumn(newColumn("first", DataType.TEXT))
249 .toTable(db);
250
251 List<String> colNames = new ArrayList<String>();
252 colNames.add("first");
253 for(int i = 0; i < numColumns; ++i) {
254 String colName = "MyColumnName" + i;
255 colNames.add(colName);
256 DataType type = (((i % 3) == 0) ? DataType.MEMO : DataType.TEXT);
257 newColumn(colName, type)
258 .addToTable(t);
259 }
260
261 List<String> row = new ArrayList<String>();
262 Map<String,Object> expectedRowData = new LinkedHashMap<String, Object>();
263 for(int i = 0; i < colNames.size(); ++i) {
264 String value = "" + i + " some row data";
265 row.add(value);
266 expectedRowData.put(colNames.get(i), value);
267 }
268
269 t.addRow(row.toArray());
270
271 t.reset();
272 assertEquals(expectedRowData, t.getNextRow());
273
274 db.close();
275 }
276 }
277 }