aboutsummaryrefslogtreecommitdiffstats
path: root/test/src/java/com/healthmarketscience/jackcess/ComplexColumnTest.java
blob: a0664c46dcd7e11c748e67f8703ad7d6f6ff8cf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) 2011 Boomi, Inc.

package com.healthmarketscience.jackcess;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.healthmarketscience.jackcess.JetFormatTest.*;
import static com.healthmarketscience.jackcess.DatabaseTest.*;
import com.healthmarketscience.jackcess.complex.Attachment;
import com.healthmarketscience.jackcess.complex.ComplexDataType;
import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey;
import com.healthmarketscience.jackcess.complex.SingleValue;
import com.healthmarketscience.jackcess.complex.Version;
import junit.framework.TestCase;


/**
 *
 * @author James Ahlborn
 */
public class ComplexColumnTest extends TestCase 
{

  public ComplexColumnTest(String name) {
    super(name);
  }

  public void testVersions() throws Exception
  {
    for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) {
      
      Database db = openCopy(testDB);
      db.setTimeZone(TEST_TZ);

      Table t1 = db.getTable("Table1");
      Column col = t1.getColumn("append-memo-data");
      assertTrue(col.isAppendOnly());
      Column verCol = col.getVersionHistoryColumn();
      assertNotNull(verCol);
      assertEquals(ComplexDataType.VERSION_HISTORY,
                   verCol.getComplexInfo().getType());

      for(Map<String,Object> row : t1) {
        String rowId = (String)row.get("id");
        ComplexValueForeignKey complexValueFk =
          (ComplexValueForeignKey)verCol.getRowValue(row);

        String curValue = (String)col.getRowValue(row);
      
        if(rowId.equals("row1")) {
          checkVersions(1, complexValueFk, curValue);
        } else if(rowId.equals("row2")) {
          checkVersions(2, complexValueFk, curValue,
                        "row2-memo", new Date(1315876862334L));
        } else if(rowId.equals("row3")) {
          checkVersions(3, complexValueFk, curValue,
                        "row3-memo-again", new Date(1315876965382L),
                        "row3-memo-revised", new Date(1315876953077L),
                        "row3-memo", new Date(1315876879126L));
        } else if(rowId.equals("row4")) {
          checkVersions(4, complexValueFk, curValue,
                        "row4-memo", new Date(1315876945758L));
        } else {
          assertTrue(false);
        }
      }

      Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo",
                       Column.AUTO_NUMBER, Column.AUTO_NUMBER};
      t1.addRow(row8);

      ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey)
        verCol.getRowValue(row8);
      Date upTime = new Date();
      row8ValFk.addVersion("row8-memo", upTime);
      checkVersions(8, row8ValFk, "row8-memo",
                    "row8-memo", upTime);    

      Cursor cursor = Cursor.createCursor(t1);
      assertTrue(cursor.findRow(t1.getColumn("id"), "row3"));
      ComplexValueForeignKey row3ValFk = (ComplexValueForeignKey)
        cursor.getCurrentRowValue(verCol);
      cursor.setCurrentRowValue(col, "new-value");
      Version v = row3ValFk.addVersion("new-value", upTime);
      checkVersions(3, row3ValFk, "new-value",
                    "new-value", upTime,
                    "row3-memo-again", new Date(1315876965382L),
                    "row3-memo-revised", new Date(1315876953077L),
                    "row3-memo", new Date(1315876879126L));

      try {
        v.update();
        fail("UnsupportedOperationException should have been thrown");
      } catch(UnsupportedOperationException expected) {
        // success
      }
    
      db.close();
    }
  }

  public void testAttachments() throws Exception
  {
    for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) {
      
      Database db = openCopy(testDB);
      db.setTimeZone(TEST_TZ);

      Table t1 = db.getTable("Table1");
      Column col = t1.getColumn("attach-data");
      assertEquals(ComplexDataType.ATTACHMENT,
                   col.getComplexInfo().getType());

      for(Map<String,Object> row : t1) {
        String rowId = (String)row.get("id");
        ComplexValueForeignKey complexValueFk =
          (ComplexValueForeignKey)col.getRowValue(row);

        if(rowId.equals("row1")) {
          checkAttachments(1, complexValueFk);
        } else if(rowId.equals("row2")) {
          checkAttachments(2, complexValueFk, "test_data.txt", "test_data2.txt");
        } else if(rowId.equals("row3")) {
          checkAttachments(3, complexValueFk);
        } else if(rowId.equals("row4")) {
          checkAttachments(4, complexValueFk, "test_data2.txt");
        } else {
          assertTrue(false);
        }
      }

      Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo",
                       Column.AUTO_NUMBER, Column.AUTO_NUMBER};
      t1.addRow(row8);

      ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey)
        col.getRowValue(row8);
      row8ValFk.addAttachment(null, "test_data.txt", "txt",
                              getFileBytes("test_data.txt"), null, null);
      checkAttachments(8, row8ValFk, "test_data.txt");

      Cursor cursor = Cursor.createCursor(t1);
      assertTrue(cursor.findRow(t1.getColumn("id"), "row4"));
      ComplexValueForeignKey row4ValFk = (ComplexValueForeignKey)
        cursor.getCurrentRowValue(col);
      Attachment a = row4ValFk.addAttachment(null, "test_data.txt", "txt",
                                             getFileBytes("test_data.txt"), null,
                                             null);
      checkAttachments(4, row4ValFk, "test_data2.txt", "test_data.txt");

      a.setFileType("xml");
      a.setFileName("some_data.xml");
      byte[] newBytes = "this is not xml".getBytes("US-ASCII");
      a.setFileData(newBytes);
      a.update();

      Attachment updated = row4ValFk.getAttachments().get(1);
      assertNotSame(updated, a);
      assertEquals("xml", updated.getFileType());
      assertEquals("some_data.xml", updated.getFileName());
      assertTrue(Arrays.equals(newBytes, updated.getFileData()));
    
      db.close();
    }
  }

  public void testMultiValues() throws Exception
  {
    for(final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMPLEX)) {
      
      Database db = openCopy(testDB);
      db.setTimeZone(TEST_TZ);

      Table t1 = db.getTable("Table1");
      Column col = t1.getColumn("multi-value-data");
      assertEquals(ComplexDataType.MULTI_VALUE,
                   col.getComplexInfo().getType());

      for(Map<String,Object> row : t1) {
        String rowId = (String)row.get("id");
        ComplexValueForeignKey complexValueFk =
          (ComplexValueForeignKey)col.getRowValue(row);

        if(rowId.equals("row1")) {
          checkMultiValues(1, complexValueFk);
        } else if(rowId.equals("row2")) {
          checkMultiValues(2, complexValueFk, "value1", "value4");
        } else if(rowId.equals("row3")) {
          checkMultiValues(3, complexValueFk,
                           "value1", "value2", "value3", "value4");
        } else if(rowId.equals("row4")) {
          checkMultiValues(4, complexValueFk);
        } else {
          assertTrue(false);
        }
      }     

      Object[] row8 = {"row8", Column.AUTO_NUMBER, "some-data", "row8-memo",
                       Column.AUTO_NUMBER, Column.AUTO_NUMBER};
      t1.addRow(row8);

      ComplexValueForeignKey row8ValFk = (ComplexValueForeignKey)
        col.getRowValue(row8);
      row8ValFk.addMultiValue("value1");
      row8ValFk.addMultiValue("value2");
      checkMultiValues(8, row8ValFk, "value1", "value2");

      Cursor cursor = Cursor.createCursor(t1);
      assertTrue(cursor.findRow(t1.getColumn("id"), "row2"));
      ComplexValueForeignKey row2ValFk = (ComplexValueForeignKey)
        cursor.getCurrentRowValue(col);
      SingleValue v = row2ValFk.addMultiValue("value2");
      row2ValFk.addMultiValue("value3");
      checkMultiValues(2, row2ValFk, "value1", "value4", "value2", "value3");

      v.set("value5");
      v.update();
      checkMultiValues(2, row2ValFk, "value1", "value4", "value5", "value3");
    
      db.close();
    }
  }
  
  private static void checkVersions(
      int cValId, ComplexValueForeignKey complexValueFk,
      String curValue, Object... versionInfos)
    throws Exception
  {
    assertEquals(cValId, complexValueFk.get());

    List<Version> versions = complexValueFk.getVersions();
    if(versionInfos.length == 0) {
      assertTrue(versions.isEmpty());
      assertNull(curValue);
    } else {
      assertEquals(versionInfos.length / 2, versions.size());
      assertEquals(curValue, versions.get(0).getValue());
      for(int i = 0; i < versionInfos.length; i+=2) {
        String value = (String)versionInfos[i];
        Date modDate = (Date)versionInfos[i+1];
        Version v = versions.get(i/2);
        assertEquals(value, v.getValue());
        assertSameDate(modDate, v.getModifiedDate());
      }
    }
  }

  private static void checkAttachments(
      int cValId, ComplexValueForeignKey complexValueFk,
      String... fileNames)
    throws Exception
  {
    assertEquals(cValId, complexValueFk.get());
    
    List<Attachment> attachments = complexValueFk.getAttachments();
    if(fileNames.length == 0) {
      assertTrue(attachments.isEmpty());
    } else {
      assertEquals(fileNames.length, attachments.size());
      for(int i = 0; i < fileNames.length; ++i) {
        String fname = fileNames[i];
        byte[] dataBytes = getFileBytes(fname);
        Attachment a = attachments.get(i);
        assertEquals(fname, a.getFileName());
        assertEquals("txt", a.getFileType());
        assertTrue(Arrays.equals(dataBytes, a.getFileData()));
      }
    }
  }
  
  private static void checkMultiValues(
      int cValId, ComplexValueForeignKey complexValueFk,
      Object... expectedValues)
    throws Exception
  {
    assertEquals(cValId, complexValueFk.get());

    List<SingleValue> values = complexValueFk.getMultiValues();
    if(expectedValues.length == 0) {
      assertTrue(values.isEmpty());
    } else {
      assertEquals(expectedValues.length, values.size());
      for(int i = 0; i < expectedValues.length; ++i) {
        Object value = expectedValues[i];
        SingleValue v = values.get(i);
        assertEquals(value, v.get());
      }
    }    
  }

  private static byte[] getFileBytes(String fname) throws Exception
  {
    if("test_data.txt".equals(fname)) {
      return TEST_BYTES;
    }
    if("test_data2.txt".equals(fname)) {
      return TEST2_BYTES;
    }
    throw new RuntimeException("unexpected bytes");
  }
  
  private static byte b(int i) { return (byte)i; }
  
  private static final byte[] TEST_BYTES = new byte[] {
    b(0x01),b(0x00),b(0x00),b(0x00),b(0x3A),b(0x00),b(0x00),b(0x00),b(0x78),b(0x5E),b(0x13),b(0x61),b(0x60),b(0x60),b(0x60),b(0x04),b(0x62),b(0x16),b(0x20),b(0x2E),b(0x61),b(0xA8),b(0x00),b(0x62),
    b(0x20),b(0x9D),b(0x91),b(0x59),b(0xAC),b(0x00),b(0x44),b(0xC5),b(0xF9),b(0xB9),b(0xA9),b(0x0A),b(0x25),b(0xA9),b(0xC5),b(0x25),b(0x0A),b(0x29),b(0x89),b(0x25),b(0x89),b(0x0A),b(0x69),b(0xF9),
    b(0x45),b(0x0A),b(0x89),b(0x25),b(0x25),b(0x89),b(0xC9),b(0x19),b(0xB9),b(0xA9),b(0x79),b(0x25),b(0x7A),b(0x00),b(0x52),b(0xA9),b(0x0F),b(0x7A)
  };
  
  private static final byte[] TEST2_BYTES = new byte[] {
    b(0x01),b(0x00),b(0x00),b(0x00),b(0x3F),b(0x00),b(0x00),b(0x00),b(0x78),b(0x5E),b(0x13),b(0x61),b(0x60),b(0x60),b(0x60),b(0x04),b(0x62),b(0x16),b(0x20),b(0x2E),b(0x61),b(0xA8),b(0x00),b(0x62),
    b(0x20),b(0x9D),b(0x91),b(0x59),b(0xAC),b(0x00),b(0x44),b(0xC5),b(0xF9),b(0xB9),b(0xA9),b(0x0A),b(0xB9),b(0xF9),b(0x45),b(0xA9),b(0x0A),b(0x25),b(0xA9),b(0xC5),b(0x25),b(0x0A),b(0x29),b(0x89),
    b(0x25),b(0x89),b(0x0A),b(0x69),b(0xF9),b(0x45),b(0x0A),b(0x89),b(0x25),b(0x25),b(0x89),b(0xC9),b(0x19),b(0xB9),b(0xA9),b(0x79),b(0x25),b(0x7A),b(0x00),b(0xA5),b(0x0B),b(0x11),b(0x4D)
  };
}