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

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