View Javadoc
1   /*
2   Copyright (c) 2017 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.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import com.healthmarketscience.jackcess.Column;
26  import com.healthmarketscience.jackcess.ColumnBuilder;
27  import com.healthmarketscience.jackcess.Cursor;
28  import com.healthmarketscience.jackcess.CursorBuilder;
29  import com.healthmarketscience.jackcess.DataType;
30  import com.healthmarketscience.jackcess.Database;
31  import com.healthmarketscience.jackcess.IndexBuilder;
32  import com.healthmarketscience.jackcess.Table;
33  import com.healthmarketscience.jackcess.TableBuilder;
34  import junit.framework.TestCase;
35  import static com.healthmarketscience.jackcess.TestUtil.*;
36  import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
37  
38  /**
39   *
40   * @author James Ahlborn
41   */
42  public class BigIntTest extends TestCase
43  {
44  
45    public BigIntTest(String name) throws Exception {
46      super(name);
47    }
48  
49    public void testBigInt() throws Exception {
50  
51      for (final Database.FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
52        JetFormat format = DatabaseImpl.getFileFormatDetails(fileFormat)
53          .getFormat();
54  
55        if(!format.isSupportedDataType(DataType.BIG_INT)) {
56          continue;
57        }
58  
59        Database db = create(fileFormat);
60  
61        Table t = new TableBuilder("Test")
62          .addColumn(new ColumnBuilder("id", DataType.LONG)
63                     .setAutoNumber(true))
64          .addColumn(new ColumnBuilder("data1", DataType.TEXT))
65          .addColumn(new ColumnBuilder("num1", DataType.BIG_INT))
66          .addIndex(new IndexBuilder("idx").addColumns("num1"))
67          .toTable(db);
68  
69        long[] vals = new long[] {
70          0L, -10L, 3844L, -45309590834L, 50392084913L, 65000L, -6489273L};
71  
72        List<Map<String, Object>> expectedTable = 
73          new ArrayList<Map<String, Object>>();
74  
75        int idx = 1;
76        for(long lng : vals) {
77          t.addRow(Column.AUTO_NUMBER, "" + lng, lng);
78  
79          expectedTable.add(createExpectedRow(
80                                "id", idx++,
81                                "data1", "" + lng,
82                                "num1", lng));
83        }
84  
85        Collections.sort(expectedTable, new Comparator<Map<String, Object>>() {
86            public int compare(
87                Map<String, Object> r1,
88                Map<String, Object> r2) {
89              Long l1 = (Long)r1.get("num1");
90              Long l2 = (Long)r2.get("num1");
91              return l1.compareTo(l2);
92            }
93          });
94        
95        Cursor c = new CursorBuilder(t).setIndexByName("idx").toIndexCursor();
96  
97        assertCursor(expectedTable, c);
98  
99        db.close();
100     }
101   }
102 }