View Javadoc
1   /*
2   Copyright (c) 2015 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl;
18  
19  import java.io.File;
20  import java.nio.ByteBuffer;
21  import java.sql.Types;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import com.healthmarketscience.jackcess.ColumnBuilder;
29  import com.healthmarketscience.jackcess.DataType;
30  import com.healthmarketscience.jackcess.Database;
31  import static com.healthmarketscience.jackcess.Database.*;
32  import com.healthmarketscience.jackcess.Row;
33  import com.healthmarketscience.jackcess.Table;
34  import com.healthmarketscience.jackcess.TableBuilder;
35  import static com.healthmarketscience.jackcess.TestUtil.*;
36  import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
37  import junit.framework.TestCase;
38  
39  /**
40   *
41   * @author James Ahlborn
42   */
43  public class LongValueTest extends TestCase
44  {
45  
46    public LongValueTest(String name) throws Exception {
47      super(name);
48    }
49  
50    public void testReadLongValue() throws Exception {
51  
52      for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.TEST2, true)) {
53        Database db = openMem(testDB);
54        Table table = db.getTable("MSP_PROJECTS");
55        Row row = table.getNextRow();
56        assertEquals("Jon Iles this is a a vawesrasoih aksdkl fas dlkjflkasjd flkjaslkdjflkajlksj dfl lkasjdf lkjaskldfj lkas dlk lkjsjdfkl; aslkdf lkasjkldjf lka skldf lka sdkjfl;kasjd falksjdfljaslkdjf laskjdfk jalskjd flkj aslkdjflkjkjasljdflkjas jf;lkasjd fjkas dasdf asd fasdf asdf asdmhf lksaiyudfoi jasodfj902384jsdf9 aw90se fisajldkfj lkasj dlkfslkd jflksjadf as", row.get("PROJ_PROP_AUTHOR"));
57        assertEquals("T", row.get("PROJ_PROP_COMPANY"));
58        assertEquals("Standard", row.get("PROJ_INFO_CAL_NAME"));
59        assertEquals("Project1", row.get("PROJ_PROP_TITLE"));
60        byte[] foundBinaryData = row.getBytes("RESERVED_BINARY_DATA");
61        byte[] expectedBinaryData =
62          toByteArray(new File("src/test/data/test2BinData.dat"));
63        assertTrue(Arrays.equals(expectedBinaryData, foundBinaryData));
64  
65        db.close();
66      }
67    }
68  
69    public void testWriteLongValue() throws Exception {
70  
71      for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
72        Database db = createMem(fileFormat);
73  
74        Table table =
75        new TableBuilder("test")
76          .addColumn(new ColumnBuilder("A", DataType.TEXT))
77          .addColumn(new ColumnBuilder("B", DataType.MEMO))
78          .addColumn(new ColumnBuilder("C", DataType.OLE))
79          .toTable(db);
80  
81        String testStr = "This is a test";
82        String longMemo = createString(2030);
83        byte[] oleValue = toByteArray(new File("src/test/data/test2BinData.dat"));
84  
85  
86        table.addRow(testStr, testStr, null);
87        table.addRow(testStr, longMemo, oleValue);
88        table.addRow("", "", new byte[0]);
89        table.addRow(null, null, null);
90  
91        table.reset();
92  
93        Row row = table.getNextRow();
94  
95        assertEquals(testStr, row.get("A"));
96        assertEquals(testStr, row.get("B"));
97        assertNull(row.get("C"));
98  
99        row = table.getNextRow();
100 
101       assertEquals(testStr, row.get("A"));
102       assertEquals(longMemo, row.get("B"));
103       assertTrue(Arrays.equals(oleValue, row.getBytes("C")));
104 
105       row = table.getNextRow();
106 
107       assertEquals("", row.get("A"));
108       assertEquals("", row.get("B"));
109       assertTrue(Arrays.equals(new byte[0], row.getBytes("C")));
110 
111       row = table.getNextRow();
112 
113       assertNull(row.get("A"));
114       assertNull(row.get("B"));
115       assertNull(row.getBytes("C"));
116 
117       db.close();
118     }    
119   }
120 
121   public void testManyMemos() throws Exception {
122     for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
123       Database db = createMem(fileFormat);
124       final int numColumns = 126;
125       TableBuilder bigTableBuilder = new TableBuilder("test");
126 
127       for (int i = 0; i < numColumns; i++)
128       {
129         bigTableBuilder.addColumn(new ColumnBuilder("column_" + i, DataType.MEMO));
130       }
131 
132       Table bigTable = bigTableBuilder.toTable(db);
133 
134       List<Object[]> expectedRows = new ArrayList<Object[]>();
135 
136       for (int j = 0; j < 3; j++)
137       {
138         Object[] rowData = new String[numColumns];
139         for (int i = 0; i < numColumns; i++)
140         {
141           rowData[i] = "v_" + i + ";" + (j + 999);
142         }
143         expectedRows.add(rowData);
144         bigTable.addRow(rowData);
145       }
146 
147       String extra1 = createString(100);
148       String extra2 = createString(2050);
149 
150       for (int j = 0; j < 1; j++)
151       {
152         Object[] rowData = new String[numColumns];
153         for (int i = 0; i < numColumns; i++)
154         {
155           rowData[i] = "v_" + i + ";" + (j + 999) + extra2;
156         }
157         expectedRows.add(rowData);
158         bigTable.addRow(rowData);
159       }
160 
161       for (int j = 0; j < 2; j++)
162       {
163         Object[] rowData = new String[numColumns];
164         for (int i = 0; i < numColumns; i++)
165         {
166           String tmp = "v_" + i + ";" + (j + 999);
167           if((i % 3) == 0) {
168             tmp += extra1;
169           } else if((i % 7) == 0) {
170             tmp += extra2;
171           }
172           rowData[i] = tmp;
173         }
174         expectedRows.add(rowData);
175         bigTable.addRow(rowData);
176       }
177 
178       bigTable.reset();
179       Iterator<Object[]> expIter = expectedRows.iterator();
180       for(Map<?,?> row : bigTable) {
181         Object[] expectedRow = expIter.next();
182         assertEquals(Arrays.asList(expectedRow),
183                      new ArrayList<Object>(row.values()));
184       }
185 
186       db.close();
187     }
188   }
189 
190   public void testLongValueAsMiddleColumn() throws Exception
191   {
192     for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
193       Database db = createMem(fileFormat);
194       Table newTable = new TableBuilder("NewTable")
195         .addColumn(new ColumnBuilder("a").setSQLType(Types.INTEGER))
196         .addColumn(new ColumnBuilder("b").setSQLType(Types.LONGVARCHAR))
197         .addColumn(new ColumnBuilder("c").setSQLType(Types.VARCHAR))
198         .toTable(db);
199 
200       String lval = createString(2000); // "--2000 chars long text--";
201       String tval = createString(40); // "--40chars long text--";
202       newTable.addRow(new Integer(1), lval, tval);
203 
204       newTable = db.getTable("NewTable");
205       Map<String, Object> readRow = newTable.getNextRow();
206       assertEquals(new Integer(1), readRow.get("a"));
207       assertEquals(lval, readRow.get("b"));
208       assertEquals(tval, readRow.get("c"));
209 
210       db.close();
211     }
212   }
213 
214   public void testUnicodeCompression() throws Exception
215   {
216     File dbFile = new File("src/test/data/V2003/testUnicodeCompV2003.mdb");
217     Database db = open(Database.FileFormat.V2003, new File("src/test/data/V2003/testUnicodeCompV2003.mdb"), true);
218 
219     StringBuilder sb = new StringBuilder(127);
220     for(int i = 1; i <= 0xFF; ++i) {
221       sb.append((char)i);
222     }
223     String longStr = sb.toString();
224 
225     String[] expectedStrs = {
226       "only ascii chars",
227       "\u00E4\u00E4kk\u00F6si\u00E4",
228       "\u041C\u0438\u0440",
229       "\u03F0\u03B1\u1F76 \u03C4\u1F79\u03C4' \u1F10\u03B3\u1F7C \u039A\u1F7B\u03F0\u03BB\u03C9\u03C0\u03B1",
230       "\u6F22\u5B57\u4EEE\u540D\u4EA4\u3058\u308A\u6587",
231       "3L9\u001D52\u0002_AB(\u00A5\u0005!!V",
232       "\u00FCmlaut",
233       longStr
234     };
235 
236     Table t = db.getTable("Table");
237     for(Row row : t) {
238       int id = (Integer)row.get("ID");
239       String str = (String)row.get("Unicode");
240       assertEquals(expectedStrs[id-1], str);
241     }
242 
243 
244     ColumnImpl col = (ColumnImpl)t.getColumn("Unicode");
245 
246     ByteBuffer bb = col.write(longStr, 1000);
247 
248     assertEquals(longStr.length() + 2, bb.remaining());
249 
250     byte[] bytes = new byte[bb.remaining()];
251     bb.get(bytes);
252     assertEquals(longStr, col.read(bytes));
253 
254 
255     longStr = longStr.replace('a', '\u0440');
256 
257     bb = col.write(longStr, 1000);
258 
259     assertEquals(longStr.length() * 2, bb.remaining());
260 
261     bytes = new byte[bb.remaining()];
262     bb.get(bytes);
263     assertEquals(longStr, col.read(bytes));
264     
265 
266     db.close();
267   }
268 }