View Javadoc
1   /*
2   Copyright (c) 2014 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  
20  import java.math.BigDecimal;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.Map;
25  
26  import com.healthmarketscience.jackcess.Column;
27  import com.healthmarketscience.jackcess.ColumnBuilder;
28  import com.healthmarketscience.jackcess.DataType;
29  import com.healthmarketscience.jackcess.Database;
30  import com.healthmarketscience.jackcess.PropertyMap;
31  import com.healthmarketscience.jackcess.Row;
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 CalcFieldTest extends TestCase
43  {
44  
45    public CalcFieldTest(String name) throws Exception {
46      super(name);
47    }
48  
49    public void testCreateCalcField() throws Exception {
50  
51      ColumnBuilder cb = new ColumnBuilder("calc_data", DataType.TEXT)
52        .setCalculatedInfo("[id] & \"_\" & [data]");
53  
54      try {
55        cb.validate(JetFormat.VERSION_12);
56        fail("IllegalArgumentException should have been thrown");
57      } catch(IllegalArgumentException e) {
58        assertTrue(e.getMessage().contains(""));
59      }
60  
61      cb.validate(JetFormat.VERSION_14);
62  
63      for (final Database.FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
64        JetFormat format = DatabaseImpl.getFileFormatDetails(fileFormat).getFormat();
65        if(!format.isSupportedCalculatedDataType(DataType.TEXT)) {
66          continue;
67        }
68  
69        Database db = create(fileFormat);
70  
71        Table t = new TableBuilder("Test")
72          .putProperty("awesome_table", true)
73          .addColumn(new ColumnBuilder("id", DataType.LONG)
74                     .setAutoNumber(true))
75          .addColumn(new ColumnBuilder("data", DataType.TEXT))
76          .addColumn(new ColumnBuilder("calc_text", DataType.TEXT)
77                     .setCalculatedInfo("[id] & \"_\" & [data]"))
78          .addColumn(new ColumnBuilder("calc_memo", DataType.MEMO)
79                     .setCalculatedInfo("[id] & \"_\" & [data]"))
80          .addColumn(new ColumnBuilder("calc_bool", DataType.BOOLEAN)
81                     .setCalculatedInfo("[id] > 0"))
82          .addColumn(new ColumnBuilder("calc_long", DataType.LONG)
83                     .setCalculatedInfo("[id] + 1"))
84          .addColumn(new ColumnBuilder("calc_numeric", DataType.NUMERIC)
85                     .setCalculatedInfo("[id] / 0.03"))
86          .toTable(db);
87  
88        Column col = t.getColumn("calc_text");
89        assertTrue(col.isCalculated());
90        assertEquals("[id] & \"_\" & [data]", col.getProperties().getValue(
91                         PropertyMap.EXPRESSION_PROP));
92        assertEquals(DataType.TEXT.getValue(), 
93                     col.getProperties().getValue(
94                         PropertyMap.RESULT_TYPE_PROP));
95  
96        String longStr = createString(1000);
97        BigDecimal bd1 = new BigDecimal("-1234.5678");
98        BigDecimal bd2 = new BigDecimal("0.0234");
99  
100       t.addRow(Column.AUTO_NUMBER, "foo", "1_foo", longStr, true, 2, bd1);
101       t.addRow(Column.AUTO_NUMBER, "bar", "2_bar", longStr, false, -37, bd2);
102       t.addRow(Column.AUTO_NUMBER, "", "", "", false, 0, BigDecimal.ZERO);
103       t.addRow(Column.AUTO_NUMBER, null, null, null, null, null, null);
104 
105       List<? extends Map<String, Object>> expectedRows =
106         createExpectedTable(
107             createExpectedRow(
108                 "id", 1,
109                 "data", "foo",
110                 "calc_text", "1_foo",
111                 "calc_memo", longStr,
112                 "calc_bool", true,
113                 "calc_long", 2,
114                 "calc_numeric", bd1),
115             createExpectedRow(
116                 "id", 2,
117                 "data", "bar",
118                 "calc_text", "2_bar",
119                 "calc_memo", longStr,
120                 "calc_bool", false,
121                 "calc_long", -37,
122                 "calc_numeric", bd2),
123             createExpectedRow(
124                 "id", 3,
125                 "data", "",
126                 "calc_text", "",
127                 "calc_memo", "",
128                 "calc_bool", false,
129                 "calc_long", 0,
130                 "calc_numeric", BigDecimal.ZERO),
131             createExpectedRow(
132                 "id", 4,
133                 "data", null,
134                 "calc_text", null,
135                 "calc_memo", null,
136                 "calc_bool", null,
137                 "calc_long", null,
138                 "calc_numeric", null));
139 
140       assertTable(expectedRows, t);
141 
142       db.close();
143     }
144   }
145 
146   public void testReadCalcFields() throws Exception {
147 
148     for(TestDB testDB : TestDB.getSupportedForBasename(Basename.CALC_FIELD)) {
149       Database db = open(testDB);
150       Table t = db.getTable("Table1");
151 
152       List<String> rows = new ArrayList<String>();
153       for(Row r : t) {
154         rows.add(r.entrySet().toString());
155       }
156 
157       List<String> expectedRows = Arrays.asList(
158           "[ID=1, FirstName=Bruce, LastName=Wayne, LastFirst=Wayne, Bruce, City=Gotham, LastFirstLen=12, Salary=1000000.0000, MonthlySalary=83333.3333, IsRich=true, AllNames=Wayne, Bruce=Wayne, Bruce, WeeklySalary=19230.7692307692, SalaryTest=1000000.0000, BoolTest=true, Popularity=50.325000, DecimalTest=50.325000, FloatTest=2583.2092, BigNumTest=56505085819.424791296572280180]",
159           "[ID=2, FirstName=Bart, LastName=Simpson, LastFirst=Simpson, Bart, City=Springfield, LastFirstLen=13, Salary=-1.0000, MonthlySalary=-0.0833, IsRich=false, AllNames=Simpson, Bart=Simpson, Bart, WeeklySalary=-0.0192307692307692, SalaryTest=-1.0000, BoolTest=true, Popularity=-36.222200, DecimalTest=-36.222200, FloatTest=0.0035889593, BigNumTest=-0.0784734499180612994241100748]",
160           "[ID=3, FirstName=John, LastName=Doe, LastFirst=Doe, John, City=Nowhere, LastFirstLen=9, Salary=0.0000, MonthlySalary=0.0000, IsRich=false, AllNames=Doe, John=Doe, John, WeeklySalary=0, SalaryTest=0.0000, BoolTest=true, Popularity=0.012300, DecimalTest=0.012300, FloatTest=0.0, BigNumTest=0E-8]",
161           "[ID=4, FirstName=Test, LastName=User, LastFirst=User, Test, City=Hockessin, LastFirstLen=10, Salary=100.0000, MonthlySalary=8.3333, IsRich=false, AllNames=User, Test=User, Test, WeeklySalary=1.92307692307692, SalaryTest=100.0000, BoolTest=true, Popularity=102030405060.654321, DecimalTest=102030405060.654321, FloatTest=1.27413E-10, BigNumTest=2.787019289824216980830E-7]");
162 
163       assertEquals(expectedRows, rows);
164 
165       db.close();
166     }    
167   }
168 
169 }