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

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