You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PropertiesTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess;
  14. import java.io.File;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.UUID;
  21. import static com.healthmarketscience.jackcess.Database.*;
  22. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  23. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  24. import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
  25. import com.healthmarketscience.jackcess.impl.PropertyMaps;
  26. import com.healthmarketscience.jackcess.impl.TableImpl;
  27. import static com.healthmarketscience.jackcess.TestUtil.*;
  28. import static com.healthmarketscience.jackcess.DatabaseBuilder.*;
  29. import static org.junit.jupiter.api.Assertions.*;
  30. import org.junit.jupiter.api.Test;
  31. /**
  32. * @author James Ahlborn
  33. */
  34. public class PropertiesTest
  35. {
  36. @Test
  37. public void testPropertyMaps() throws Exception
  38. {
  39. PropertyMaps maps = new PropertyMaps(10, null, null, null);
  40. assertTrue(maps.isEmpty());
  41. assertEquals(0, maps.getSize());
  42. assertFalse(maps.iterator().hasNext());
  43. assertEquals(10, maps.getObjectId());
  44. PropertyMapImpl defMap = maps.getDefault();
  45. assertTrue(defMap.isEmpty());
  46. assertEquals(0, defMap.getSize());
  47. assertFalse(defMap.iterator().hasNext());
  48. PropertyMapImpl colMap = maps.get("testcol");
  49. assertTrue(colMap.isEmpty());
  50. assertEquals(0, colMap.getSize());
  51. assertFalse(colMap.iterator().hasNext());
  52. assertFalse(maps.isEmpty());
  53. assertEquals(2, maps.getSize());
  54. assertSame(defMap, maps.get(PropertyMaps.DEFAULT_NAME));
  55. assertEquals(PropertyMaps.DEFAULT_NAME, defMap.getName());
  56. assertSame(colMap, maps.get("TESTCOL"));
  57. assertEquals("testcol", colMap.getName());
  58. defMap.put("foo", DataType.TEXT, "bar", false);
  59. defMap.put("baz", DataType.LONG, 13, true);
  60. assertFalse(defMap.isEmpty());
  61. assertEquals(2, defMap.getSize());
  62. assertFalse(defMap.get("foo").isDdl());
  63. assertTrue(defMap.get("baz").isDdl());
  64. colMap.put("buzz", DataType.BOOLEAN, Boolean.TRUE, true);
  65. assertFalse(colMap.isEmpty());
  66. assertEquals(1, colMap.getSize());
  67. assertEquals("bar", defMap.getValue("foo"));
  68. assertEquals("bar", defMap.getValue("FOO"));
  69. assertNull(colMap.getValue("foo"));
  70. assertEquals(13, defMap.get("baz").getValue());
  71. assertEquals(Boolean.TRUE, colMap.getValue("Buzz"));
  72. assertEquals("bar", defMap.getValue("foo", "blah"));
  73. assertEquals("blah", defMap.getValue("bogus", "blah"));
  74. List<PropertyMap.Property> props = new ArrayList<PropertyMap.Property>();
  75. for(PropertyMap map : maps) {
  76. for(PropertyMap.Property prop : map) {
  77. props.add(prop);
  78. }
  79. }
  80. assertEquals(Arrays.asList(defMap.get("foo"), defMap.get("baz"),
  81. colMap.get("buzz")), props);
  82. }
  83. @Test
  84. public void testInferTypes() throws Exception
  85. {
  86. PropertyMaps maps = new PropertyMaps(10, null, null, null);
  87. PropertyMap defMap = maps.getDefault();
  88. assertEquals(DataType.TEXT,
  89. defMap.put(PropertyMap.FORMAT_PROP, null).getType());
  90. assertEquals(DataType.BOOLEAN,
  91. defMap.put(PropertyMap.REQUIRED_PROP, null).getType());
  92. assertEquals(DataType.TEXT,
  93. defMap.put("strprop", "this is a string").getType());
  94. assertEquals(DataType.BOOLEAN,
  95. defMap.put("boolprop", true).getType());
  96. assertEquals(DataType.LONG,
  97. defMap.put("intprop", 37).getType());
  98. }
  99. @Test
  100. public void testReadProperties() throws Exception
  101. {
  102. byte[] nameMapBytes = null;
  103. for(TestDB testDb : SUPPORTED_DBS_TEST_FOR_READ) {
  104. Database db = open(testDb);
  105. TableImpl t = (TableImpl)db.getTable("Table1");
  106. assertEquals(t.getTableDefPageNumber(),
  107. t.getPropertyMaps().getObjectId());
  108. PropertyMap tProps = t.getProperties();
  109. assertEquals(PropertyMaps.DEFAULT_NAME, tProps.getName());
  110. int expectedNumProps = 3;
  111. if(db.getFileFormat() != Database.FileFormat.V1997) {
  112. assertEquals("{5A29A676-1145-4D1A-AE47-9F5415CDF2F1}",
  113. tProps.getValue(PropertyMap.GUID_PROP));
  114. if(nameMapBytes == null) {
  115. nameMapBytes = (byte[])tProps.getValue("NameMap");
  116. } else {
  117. assertTrue(Arrays.equals(nameMapBytes,
  118. (byte[])tProps.getValue("NameMap")));
  119. }
  120. expectedNumProps += 2;
  121. }
  122. assertEquals(expectedNumProps, tProps.getSize());
  123. assertEquals((byte)0, tProps.getValue("Orientation"));
  124. assertEquals(Boolean.FALSE, tProps.getValue("OrderByOn"));
  125. assertEquals((byte)2, tProps.getValue("DefaultView"));
  126. PropertyMap colProps = t.getColumn("A").getProperties();
  127. assertEquals("A", colProps.getName());
  128. expectedNumProps = 9;
  129. if(db.getFileFormat() != Database.FileFormat.V1997) {
  130. assertEquals("{E9EDD90C-CE55-4151-ABE1-A1ACE1007515}",
  131. colProps.getValue(PropertyMap.GUID_PROP));
  132. ++expectedNumProps;
  133. }
  134. assertEquals(expectedNumProps, colProps.getSize());
  135. assertEquals((short)-1, colProps.getValue("ColumnWidth"));
  136. assertEquals((short)0, colProps.getValue("ColumnOrder"));
  137. assertEquals(Boolean.FALSE, colProps.getValue("ColumnHidden"));
  138. assertEquals(Boolean.FALSE,
  139. colProps.getValue(PropertyMap.REQUIRED_PROP));
  140. assertEquals(Boolean.FALSE,
  141. colProps.getValue(PropertyMap.ALLOW_ZERO_LEN_PROP));
  142. assertEquals((short)109, colProps.getValue("DisplayControl"));
  143. assertEquals(Boolean.TRUE, colProps.getValue("UnicodeCompression"));
  144. assertEquals((byte)0, colProps.getValue("IMEMode"));
  145. assertEquals((byte)3, colProps.getValue("IMESentenceMode"));
  146. PropertyMap dbProps = db.getDatabaseProperties();
  147. assertTrue(((String)dbProps.getValue(PropertyMap.ACCESS_VERSION_PROP))
  148. .matches("[0-9]{2}[.][0-9]{2}"));
  149. PropertyMap sumProps = db.getSummaryProperties();
  150. assertEquals(3, sumProps.getSize());
  151. assertEquals("test", sumProps.getValue(PropertyMap.TITLE_PROP));
  152. assertEquals("tmccune", sumProps.getValue(PropertyMap.AUTHOR_PROP));
  153. assertEquals("Health Market Science", sumProps.getValue(PropertyMap.COMPANY_PROP));
  154. PropertyMap userProps = db.getUserDefinedProperties();
  155. assertEquals(1, userProps.getSize());
  156. assertEquals(Boolean.TRUE, userProps.getValue("ReplicateProject"));
  157. db.close();
  158. }
  159. }
  160. @Test
  161. public void testParseProperties() throws Exception
  162. {
  163. for(FileFormat ff : SUPPORTED_FILEFORMATS_FOR_READ) {
  164. File[] dbFiles = new File(DIR_TEST_DATA, ff.name()).listFiles();
  165. if(dbFiles == null) {
  166. continue;
  167. }
  168. for(File f : dbFiles) {
  169. if(!f.isFile()) {
  170. continue;
  171. }
  172. Database db = open(ff, f);
  173. PropertyMap dbProps = db.getDatabaseProperties();
  174. assertFalse(dbProps.isEmpty());
  175. assertTrue(((String)dbProps.getValue(PropertyMap.ACCESS_VERSION_PROP))
  176. .matches("[0-9]{2}[.][0-9]{2}"));
  177. for(Row row : ((DatabaseImpl)db).getSystemCatalog()) {
  178. int id = row.getInt("Id");
  179. byte[] propBytes = row.getBytes("LvProp");
  180. PropertyMaps propMaps = ((DatabaseImpl)db).getPropertiesForObject(
  181. id, null);
  182. int byteLen = ((propBytes != null) ? propBytes.length : 0);
  183. if(byteLen == 0) {
  184. assertTrue(propMaps.isEmpty());
  185. } else if(propMaps.isEmpty()) {
  186. assertTrue(byteLen < 80);
  187. } else {
  188. assertTrue(byteLen > 0);
  189. }
  190. }
  191. db.close();
  192. }
  193. }
  194. }
  195. @Test
  196. public void testWriteProperties() throws Exception
  197. {
  198. for(TestDB testDb : SUPPORTED_DBS_TEST) {
  199. Database db = open(testDb);
  200. TableImpl t = (TableImpl)db.getTable("Table1");
  201. PropertyMap tProps = t.getProperties();
  202. PropertyMaps maps = ((PropertyMapImpl)tProps).getOwner();
  203. byte[] mapsBytes = maps.write();
  204. PropertyMaps maps2 = ((DatabaseImpl)db).readProperties(
  205. mapsBytes, maps.getObjectId(), null);
  206. Iterator<PropertyMapImpl> iter = maps.iterator();
  207. Iterator<PropertyMapImpl> iter2 = maps2.iterator();
  208. while(iter.hasNext() && iter2.hasNext()) {
  209. PropertyMapImpl propMap = iter.next();
  210. PropertyMapImpl propMap2 = iter2.next();
  211. checkProperties(propMap, propMap2);
  212. }
  213. assertFalse(iter.hasNext());
  214. assertFalse(iter2.hasNext());
  215. db.close();
  216. }
  217. }
  218. @Test
  219. public void testModifyProperties() throws Exception
  220. {
  221. for(TestDB testDb : SUPPORTED_DBS_TEST) {
  222. Database db = openCopy(testDb);
  223. File dbFile = db.getFile();
  224. Table t = db.getTable("Table1");
  225. // grab originals
  226. PropertyMap origCProps = t.getColumn("C").getProperties();
  227. PropertyMap origFProps = t.getColumn("F").getProperties();
  228. PropertyMap origDProps = t.getColumn("D").getProperties();
  229. db.close();
  230. // modify but do not save
  231. db = open(dbFile);
  232. t = db.getTable("Table1");
  233. PropertyMap cProps = t.getColumn("C").getProperties();
  234. PropertyMap fProps = t.getColumn("F").getProperties();
  235. PropertyMap dProps = t.getColumn("D").getProperties();
  236. assertFalse((Boolean)cProps.getValue(PropertyMap.REQUIRED_PROP));
  237. assertEquals("0", fProps.getValue(PropertyMap.DEFAULT_VALUE_PROP));
  238. assertEquals((short)109, dProps.getValue("DisplayControl"));
  239. cProps.put(PropertyMap.REQUIRED_PROP, DataType.BOOLEAN, true);
  240. fProps.get(PropertyMap.DEFAULT_VALUE_PROP).setValue("42");
  241. dProps.remove("DisplayControl");
  242. db.close();
  243. // modify and save
  244. db = open(dbFile);
  245. t = db.getTable("Table1");
  246. cProps = t.getColumn("C").getProperties();
  247. fProps = t.getColumn("F").getProperties();
  248. dProps = t.getColumn("D").getProperties();
  249. assertFalse((Boolean)cProps.getValue(PropertyMap.REQUIRED_PROP));
  250. assertEquals("0", fProps.getValue(PropertyMap.DEFAULT_VALUE_PROP));
  251. assertEquals((short)109, dProps.getValue("DisplayControl"));
  252. checkProperties(origCProps, cProps);
  253. checkProperties(origFProps, fProps);
  254. checkProperties(origDProps, dProps);
  255. cProps.put(PropertyMap.REQUIRED_PROP, DataType.BOOLEAN, true);
  256. cProps.save();
  257. fProps.get(PropertyMap.DEFAULT_VALUE_PROP).setValue("42");
  258. fProps.save();
  259. dProps.remove("DisplayControl");
  260. dProps.save();
  261. db.close();
  262. // reload saved props
  263. db = open(dbFile);
  264. t = db.getTable("Table1");
  265. cProps = t.getColumn("C").getProperties();
  266. fProps = t.getColumn("F").getProperties();
  267. dProps = t.getColumn("D").getProperties();
  268. assertTrue((Boolean)cProps.getValue(PropertyMap.REQUIRED_PROP));
  269. assertEquals("42", fProps.getValue(PropertyMap.DEFAULT_VALUE_PROP));
  270. assertNull(dProps.getValue("DisplayControl"));
  271. cProps.put(PropertyMap.REQUIRED_PROP, DataType.BOOLEAN, false);
  272. fProps.get(PropertyMap.DEFAULT_VALUE_PROP).setValue("0");
  273. dProps.put("DisplayControl", DataType.INT, (short)109);
  274. checkProperties(origCProps, cProps);
  275. checkProperties(origFProps, fProps);
  276. checkProperties(origDProps, dProps);
  277. db.close();
  278. }
  279. }
  280. @Test
  281. public void testCreateDbProperties() throws Exception
  282. {
  283. for(FileFormat ff : SUPPORTED_FILEFORMATS) {
  284. if(ff == FileFormat.GENERIC_JET4) {
  285. // weirdo format, no properties
  286. continue;
  287. }
  288. File file = TestUtil.createTempFile(false);
  289. Database db = newDatabase(file)
  290. .setFileFormat(ff)
  291. .putUserDefinedProperty("testing", "123")
  292. .create();
  293. UUID u1 = UUID.randomUUID();
  294. UUID u2 = UUID.randomUUID();
  295. Table t = newTable("Test")
  296. .putProperty("awesome_table", true)
  297. .addColumn(newColumn("id", DataType.LONG)
  298. .setAutoNumber(true)
  299. .putProperty(PropertyMap.REQUIRED_PROP, true)
  300. .putProperty(PropertyMap.GUID_PROP, u1))
  301. .addColumn(newColumn("data", DataType.TEXT)
  302. .putProperty(PropertyMap.ALLOW_ZERO_LEN_PROP, false)
  303. .putProperty(PropertyMap.GUID_PROP, u2))
  304. .toTable(db);
  305. t.addRow(Column.AUTO_NUMBER, "value");
  306. db.close();
  307. db = open(file);
  308. assertEquals("123", db.getUserDefinedProperties().getValue("testing"));
  309. t = db.getTable("Test");
  310. assertEquals(Boolean.TRUE,
  311. t.getProperties().getValue("awesome_table"));
  312. Column c = t.getColumn("id");
  313. assertEquals(Boolean.TRUE,
  314. c.getProperties().getValue(PropertyMap.REQUIRED_PROP));
  315. assertEquals("{" + u1.toString().toUpperCase() + "}",
  316. c.getProperties().getValue(PropertyMap.GUID_PROP));
  317. c = t.getColumn("data");
  318. assertEquals(Boolean.FALSE,
  319. c.getProperties().getValue(PropertyMap.ALLOW_ZERO_LEN_PROP));
  320. assertEquals("{" + u2.toString().toUpperCase() + "}",
  321. c.getProperties().getValue(PropertyMap.GUID_PROP));
  322. }
  323. }
  324. @Test
  325. public void testEnforceProperties() throws Exception
  326. {
  327. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS)
  328. {
  329. try (final Database db = create(fileFormat))
  330. {
  331. final Table testReq = newTable("testReq")
  332. .addColumn(newColumn("id", DataType.LONG)
  333. .setAutoNumber(true)
  334. .putProperty(PropertyMap.REQUIRED_PROP, true))
  335. .addColumn(newColumn("value", DataType.TEXT)
  336. .putProperty(PropertyMap.REQUIRED_PROP, true))
  337. .toTable(db);
  338. testReq.addRow(Column.AUTO_NUMBER, "v1");
  339. assertThrows(InvalidValueException.class, () -> testReq.addRow(Column.AUTO_NUMBER, null));
  340. testReq.addRow(Column.AUTO_NUMBER, "");
  341. List<? extends Map<String, Object>> expectedRows
  342. = createExpectedTable(
  343. createExpectedRow(
  344. "id", 1,
  345. "value", "v1"),
  346. createExpectedRow(
  347. "id", 2,
  348. "value", ""));
  349. assertTable(expectedRows, testReq);
  350. final Table testNz = newTable("testNz")
  351. .addColumn(newColumn("id", DataType.LONG)
  352. .setAutoNumber(true)
  353. .putProperty(PropertyMap.REQUIRED_PROP, true))
  354. .addColumn(newColumn("value", DataType.TEXT)
  355. .putProperty(PropertyMap.ALLOW_ZERO_LEN_PROP, false))
  356. .toTable(db);
  357. testNz.addRow(Column.AUTO_NUMBER, "v1");
  358. assertThrows(InvalidValueException.class, () -> testNz.addRow(Column.AUTO_NUMBER, ""));
  359. testNz.addRow(Column.AUTO_NUMBER, null);
  360. expectedRows = createExpectedTable(createExpectedRow("id", 1, "value", "v1"),
  361. createExpectedRow("id", 2, "value", null));
  362. assertTable(expectedRows, testNz);
  363. final Table testReqNz = newTable("testReqNz")
  364. .addColumn(newColumn("id", DataType.LONG)
  365. .setAutoNumber(true)
  366. .putProperty(PropertyMap.REQUIRED_PROP, true))
  367. .addColumn(newColumn("value", DataType.TEXT))
  368. .toTable(db);
  369. Column col = testReqNz.getColumn("value");
  370. PropertyMap props = col.getProperties();
  371. props.put(PropertyMap.REQUIRED_PROP, true);
  372. props.put(PropertyMap.ALLOW_ZERO_LEN_PROP, false);
  373. props.save();
  374. testReqNz.addRow(Column.AUTO_NUMBER, "v1");
  375. assertThrows(InvalidValueException.class, () -> testReqNz.addRow(Column.AUTO_NUMBER, ""));
  376. assertThrows(InvalidValueException.class, () -> testReqNz.addRow(Column.AUTO_NUMBER, null));
  377. testReqNz.addRow(Column.AUTO_NUMBER, "v2");
  378. expectedRows = createExpectedTable(createExpectedRow("id", 1, "value", "v1"),
  379. createExpectedRow("id", 2, "value", "v2"));
  380. assertTable(expectedRows, testReqNz);
  381. }
  382. }
  383. }
  384. @Test
  385. public void testEnumValues() throws Exception
  386. {
  387. PropertyMaps maps = new PropertyMaps(10, null, null, null);
  388. PropertyMapImpl colMap = maps.get("testcol");
  389. colMap.put(PropertyMap.DISPLAY_CONTROL_PROP,
  390. PropertyMap.DisplayControl.TEXT_BOX);
  391. assertEquals(PropertyMap.DisplayControl.TEXT_BOX.getValue(),
  392. colMap.getValue(PropertyMap.DISPLAY_CONTROL_PROP));
  393. }
  394. private static void checkProperties(PropertyMap propMap1,
  395. PropertyMap propMap2)
  396. {
  397. assertEquals(propMap1.getSize(), propMap2.getSize());
  398. for(PropertyMap.Property prop : propMap1) {
  399. PropertyMap.Property prop2 = propMap2.get(prop.getName());
  400. assertEquals(prop.getName(), prop2.getName());
  401. assertEquals(prop.getType(), prop2.getType());
  402. Object v1 = prop.getValue();
  403. Object v2 = prop2.getValue();
  404. if(v1 instanceof byte[]) {
  405. assertTrue(Arrays.equals((byte[])v1, (byte[])v2));
  406. } else {
  407. assertEquals(v1, v2);
  408. }
  409. }
  410. }
  411. }