diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2018-04-29 03:54:10 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2018-04-29 03:54:10 +0000 |
commit | 5a39a80966669d8280490e0e3b138c03d481a823 (patch) | |
tree | e22b32e387c47872998c34f262c7a3472731bef9 /src/test/java | |
parent | 9908a0afe96e08dbb4315fea5aaa685819a29dc7 (diff) | |
download | jackcess-5a39a80966669d8280490e0e3b138c03d481a823.tar.gz jackcess-5a39a80966669d8280490e0e3b138c03d481a823.zip |
start enforcing the required and no-zero len column properties
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1147 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/PropertiesTest.java | 167 |
1 files changed, 140 insertions, 27 deletions
diff --git a/src/test/java/com/healthmarketscience/jackcess/PropertiesTest.java b/src/test/java/com/healthmarketscience/jackcess/PropertiesTest.java index 69eb7c6..08cedc0 100644 --- a/src/test/java/com/healthmarketscience/jackcess/PropertiesTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/PropertiesTest.java @@ -21,9 +21,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.UUID; import static com.healthmarketscience.jackcess.Database.*; +import com.healthmarketscience.jackcess.InvalidValueException; import com.healthmarketscience.jackcess.impl.DatabaseImpl; import static com.healthmarketscience.jackcess.impl.JetFormatTest.*; import com.healthmarketscience.jackcess.impl.PropertyMapImpl; @@ -44,7 +46,7 @@ public class PropertiesTest extends TestCase public void testPropertyMaps() throws Exception { - PropertyMaps maps = new PropertyMaps(10, null, null); + PropertyMaps maps = new PropertyMaps(10, null, null, null); assertTrue(maps.isEmpty()); assertEquals(0, maps.getSize()); assertFalse(maps.iterator().hasNext()); @@ -59,10 +61,10 @@ public class PropertiesTest extends TestCase assertTrue(colMap.isEmpty()); assertEquals(0, colMap.getSize()); assertFalse(colMap.iterator().hasNext()); - + assertFalse(maps.isEmpty()); assertEquals(2, maps.getSize()); - + assertSame(defMap, maps.get(PropertyMaps.DEFAULT_NAME)); assertEquals(PropertyMaps.DEFAULT_NAME, defMap.getName()); assertSame(colMap, maps.get("TESTCOL")); @@ -97,25 +99,25 @@ public class PropertiesTest extends TestCase } } - assertEquals(Arrays.asList(defMap.get("foo"), defMap.get("baz"), + assertEquals(Arrays.asList(defMap.get("foo"), defMap.get("baz"), colMap.get("buzz")), props); } public void testInferTypes() throws Exception { - PropertyMaps maps = new PropertyMaps(10, null, null); + PropertyMaps maps = new PropertyMaps(10, null, null, null); PropertyMap defMap = maps.getDefault(); - assertEquals(DataType.TEXT, + assertEquals(DataType.TEXT, defMap.put(PropertyMap.FORMAT_PROP, null).getType()); - assertEquals(DataType.BOOLEAN, + assertEquals(DataType.BOOLEAN, defMap.put(PropertyMap.REQUIRED_PROP, null).getType()); - assertEquals(DataType.TEXT, + assertEquals(DataType.TEXT, defMap.put("strprop", "this is a string").getType()); - assertEquals(DataType.BOOLEAN, + assertEquals(DataType.BOOLEAN, defMap.put("boolprop", true).getType()); - assertEquals(DataType.LONG, + assertEquals(DataType.LONG, defMap.put("intprop", 37).getType()); } @@ -127,22 +129,22 @@ public class PropertiesTest extends TestCase Database db = open(testDb); TableImpl t = (TableImpl)db.getTable("Table1"); - assertEquals(t.getTableDefPageNumber(), + assertEquals(t.getTableDefPageNumber(), t.getPropertyMaps().getObjectId()); PropertyMap tProps = t.getProperties(); assertEquals(PropertyMaps.DEFAULT_NAME, tProps.getName()); int expectedNumProps = 3; if(db.getFileFormat() != Database.FileFormat.V1997) { - assertEquals("{5A29A676-1145-4D1A-AE47-9F5415CDF2F1}", + assertEquals("{5A29A676-1145-4D1A-AE47-9F5415CDF2F1}", tProps.getValue(PropertyMap.GUID_PROP)); if(nameMapBytes == null) { nameMapBytes = (byte[])tProps.getValue("NameMap"); } else { - assertTrue(Arrays.equals(nameMapBytes, + assertTrue(Arrays.equals(nameMapBytes, (byte[])tProps.getValue("NameMap"))); } expectedNumProps += 2; - } + } assertEquals(expectedNumProps, tProps.getSize()); assertEquals((byte)0, tProps.getValue("Orientation")); assertEquals(Boolean.FALSE, tProps.getValue("OrderByOn")); @@ -152,7 +154,7 @@ public class PropertiesTest extends TestCase assertEquals("A", colProps.getName()); expectedNumProps = 9; if(db.getFileFormat() != Database.FileFormat.V1997) { - assertEquals("{E9EDD90C-CE55-4151-ABE1-A1ACE1007515}", + assertEquals("{E9EDD90C-CE55-4151-ABE1-A1ACE1007515}", colProps.getValue(PropertyMap.GUID_PROP)); ++expectedNumProps; } @@ -160,9 +162,9 @@ public class PropertiesTest extends TestCase assertEquals((short)-1, colProps.getValue("ColumnWidth")); assertEquals((short)0, colProps.getValue("ColumnOrder")); assertEquals(Boolean.FALSE, colProps.getValue("ColumnHidden")); - assertEquals(Boolean.FALSE, + assertEquals(Boolean.FALSE, colProps.getValue(PropertyMap.REQUIRED_PROP)); - assertEquals(Boolean.FALSE, + assertEquals(Boolean.FALSE, colProps.getValue(PropertyMap.ALLOW_ZERO_LEN_PROP)); assertEquals((short)109, colProps.getValue("DisplayControl")); assertEquals(Boolean.TRUE, colProps.getValue("UnicodeCompression")); @@ -210,7 +212,8 @@ public class PropertiesTest extends TestCase for(Row row : ((DatabaseImpl)db).getSystemCatalog()) { int id = row.getInt("Id"); byte[] propBytes = row.getBytes("LvProp"); - PropertyMaps propMaps = ((DatabaseImpl)db).getPropertiesForObject(id); + PropertyMaps propMaps = ((DatabaseImpl)db).getPropertiesForObject( + id, null); int byteLen = ((propBytes != null) ? propBytes.length : 0); if(byteLen == 0) { assertTrue(propMaps.isEmpty()); @@ -251,12 +254,12 @@ public class PropertiesTest extends TestCase checkProperties(propMap, propMap2); } - + assertFalse(iter.hasNext()); assertFalse(iter2.hasNext()); db.close(); - } + } } public void testModifyProperties() throws Exception @@ -333,7 +336,7 @@ public class PropertiesTest extends TestCase assertTrue((Boolean)cProps.getValue(PropertyMap.REQUIRED_PROP)); assertEquals("42", fProps.getValue(PropertyMap.DEFAULT_VALUE_PROP)); - assertNull(dProps.getValue("DisplayControl")); + assertNull(dProps.getValue("DisplayControl")); cProps.put(PropertyMap.REQUIRED_PROP, DataType.BOOLEAN, false); fProps.get(PropertyMap.DEFAULT_VALUE_PROP).setValue("0"); @@ -355,7 +358,7 @@ public class PropertiesTest extends TestCase // weirdo format, no properties continue; } - + File file = TestUtil.createTempFile(false); Database db = new DatabaseBuilder(file) .setFileFormat(ff) @@ -380,16 +383,16 @@ public class PropertiesTest extends TestCase db.close(); db = new DatabaseBuilder(file).open(); - + assertEquals("123", db.getUserDefinedProperties().getValue("testing")); t = db.getTable("Test"); - assertEquals(Boolean.TRUE, + assertEquals(Boolean.TRUE, t.getProperties().getValue("awesome_table")); Column c = t.getColumn("id"); - assertEquals(Boolean.TRUE, + assertEquals(Boolean.TRUE, c.getProperties().getValue(PropertyMap.REQUIRED_PROP)); assertEquals("{" + u1.toString().toUpperCase() + "}", c.getProperties().getValue(PropertyMap.GUID_PROP)); @@ -397,13 +400,123 @@ public class PropertiesTest extends TestCase c = t.getColumn("data"); assertEquals(Boolean.FALSE, c.getProperties().getValue(PropertyMap.ALLOW_ZERO_LEN_PROP)); - assertEquals("{" + u2.toString().toUpperCase() + "}", + assertEquals("{" + u2.toString().toUpperCase() + "}", c.getProperties().getValue(PropertyMap.GUID_PROP)); } } - private static void checkProperties(PropertyMap propMap1, + public void testEnforceProperties() throws Exception + { + for(final FileFormat fileFormat : SUPPORTED_FILEFORMATS) { + Database db = create(fileFormat); + + Table t = new TableBuilder("testReq") + .addColumn(new ColumnBuilder("id", DataType.LONG) + .setAutoNumber(true) + .putProperty(PropertyMap.REQUIRED_PROP, true)) + .addColumn(new ColumnBuilder("value", DataType.TEXT) + .putProperty(PropertyMap.REQUIRED_PROP, true)) + .toTable(db); + + t.addRow(Column.AUTO_NUMBER, "v1"); + + try { + t.addRow(Column.AUTO_NUMBER, null); + fail("InvalidValueException should have been thrown"); + } catch(InvalidValueException expected) { + // success + } + + t.addRow(Column.AUTO_NUMBER, ""); + + List<? extends Map<String, Object>> expectedRows = + createExpectedTable( + createExpectedRow( + "id", 1, + "value", "v1"), + createExpectedRow( + "id", 2, + "value", "")); + assertTable(expectedRows, t); + + + t = new TableBuilder("testNz") + .addColumn(new ColumnBuilder("id", DataType.LONG) + .setAutoNumber(true) + .putProperty(PropertyMap.REQUIRED_PROP, true)) + .addColumn(new ColumnBuilder("value", DataType.TEXT) + .putProperty(PropertyMap.ALLOW_ZERO_LEN_PROP, false)) + .toTable(db); + + t.addRow(Column.AUTO_NUMBER, "v1"); + + try { + t.addRow(Column.AUTO_NUMBER, ""); + fail("InvalidValueException should have been thrown"); + } catch(InvalidValueException expected) { + // success + } + + t.addRow(Column.AUTO_NUMBER, null); + + expectedRows = + createExpectedTable( + createExpectedRow( + "id", 1, + "value", "v1"), + createExpectedRow( + "id", 2, + "value", null)); + assertTable(expectedRows, t); + + + t = new TableBuilder("testReqNz") + .addColumn(new ColumnBuilder("id", DataType.LONG) + .setAutoNumber(true) + .putProperty(PropertyMap.REQUIRED_PROP, true)) + .addColumn(new ColumnBuilder("value", DataType.TEXT)) + .toTable(db); + + Column col = t.getColumn("value"); + PropertyMap props = col.getProperties(); + props.put(PropertyMap.REQUIRED_PROP, true); + props.put(PropertyMap.ALLOW_ZERO_LEN_PROP, false); + props.save(); + + t.addRow(Column.AUTO_NUMBER, "v1"); + + try { + t.addRow(Column.AUTO_NUMBER, ""); + fail("InvalidValueException should have been thrown"); + } catch(InvalidValueException expected) { + // success + } + + try { + t.addRow(Column.AUTO_NUMBER, null); + fail("InvalidValueException should have been thrown"); + } catch(InvalidValueException expected) { + // success + } + + t.addRow(Column.AUTO_NUMBER, "v2"); + + expectedRows = + createExpectedTable( + createExpectedRow( + "id", 1, + "value", "v1"), + createExpectedRow( + "id", 2, + "value", "v2")); + assertTable(expectedRows, t); + + db.close(); + } + } + + private static void checkProperties(PropertyMap propMap1, PropertyMap propMap2) { assertEquals(propMap1.getSize(), propMap2.getSize()); |