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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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.UUID;
  20. import static com.healthmarketscience.jackcess.Database.*;
  21. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  22. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  23. import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
  24. import com.healthmarketscience.jackcess.impl.PropertyMaps;
  25. import com.healthmarketscience.jackcess.impl.TableImpl;
  26. import junit.framework.TestCase;
  27. import static com.healthmarketscience.jackcess.TestUtil.*;
  28. /**
  29. * @author James Ahlborn
  30. */
  31. public class PropertiesTest extends TestCase
  32. {
  33. public PropertiesTest(String name) throws Exception {
  34. super(name);
  35. }
  36. public void testPropertyMaps() throws Exception
  37. {
  38. PropertyMaps maps = new PropertyMaps(10, null, null);
  39. assertTrue(maps.isEmpty());
  40. assertEquals(0, maps.getSize());
  41. assertFalse(maps.iterator().hasNext());
  42. assertEquals(10, maps.getObjectId());
  43. PropertyMapImpl defMap = maps.getDefault();
  44. assertTrue(defMap.isEmpty());
  45. assertEquals(0, defMap.getSize());
  46. assertFalse(defMap.iterator().hasNext());
  47. PropertyMapImpl colMap = maps.get("testcol");
  48. assertTrue(colMap.isEmpty());
  49. assertEquals(0, colMap.getSize());
  50. assertFalse(colMap.iterator().hasNext());
  51. assertFalse(maps.isEmpty());
  52. assertEquals(2, maps.getSize());
  53. assertSame(defMap, maps.get(PropertyMaps.DEFAULT_NAME));
  54. assertEquals(PropertyMaps.DEFAULT_NAME, defMap.getName());
  55. assertSame(colMap, maps.get("TESTCOL"));
  56. assertEquals("testcol", colMap.getName());
  57. defMap.put("foo", DataType.TEXT, (byte)0, "bar");
  58. defMap.put("baz", DataType.LONG, (byte)1, 13);
  59. assertFalse(defMap.isEmpty());
  60. assertEquals(2, defMap.getSize());
  61. colMap.put("buzz", DataType.BOOLEAN, (byte)0, Boolean.TRUE);
  62. assertFalse(colMap.isEmpty());
  63. assertEquals(1, colMap.getSize());
  64. assertEquals("bar", defMap.getValue("foo"));
  65. assertEquals("bar", defMap.getValue("FOO"));
  66. assertNull(colMap.getValue("foo"));
  67. assertEquals(13, defMap.get("baz").getValue());
  68. assertEquals(Boolean.TRUE, colMap.getValue("Buzz"));
  69. assertEquals("bar", defMap.getValue("foo", "blah"));
  70. assertEquals("blah", defMap.getValue("bogus", "blah"));
  71. List<PropertyMap.Property> props = new ArrayList<PropertyMap.Property>();
  72. for(PropertyMap map : maps) {
  73. for(PropertyMap.Property prop : map) {
  74. props.add(prop);
  75. }
  76. }
  77. assertEquals(Arrays.asList(defMap.get("foo"), defMap.get("baz"),
  78. colMap.get("buzz")), props);
  79. }
  80. public void testInferTypes() throws Exception
  81. {
  82. PropertyMaps maps = new PropertyMaps(10, null, null);
  83. PropertyMap defMap = maps.getDefault();
  84. assertEquals(DataType.TEXT,
  85. defMap.put(PropertyMap.FORMAT_PROP, null).getType());
  86. assertEquals(DataType.BOOLEAN,
  87. defMap.put(PropertyMap.REQUIRED_PROP, null).getType());
  88. assertEquals(DataType.TEXT,
  89. defMap.put("strprop", "this is a string").getType());
  90. assertEquals(DataType.BOOLEAN,
  91. defMap.put("boolprop", true).getType());
  92. assertEquals(DataType.LONG,
  93. defMap.put("intprop", 37).getType());
  94. }
  95. public void testReadProperties() throws Exception
  96. {
  97. byte[] nameMapBytes = null;
  98. for(TestDB testDb : SUPPORTED_DBS_TEST_FOR_READ) {
  99. Database db = open(testDb);
  100. TableImpl t = (TableImpl)db.getTable("Table1");
  101. assertEquals(t.getTableDefPageNumber(),
  102. t.getPropertyMaps().getObjectId());
  103. PropertyMap tProps = t.getProperties();
  104. assertEquals(PropertyMaps.DEFAULT_NAME, tProps.getName());
  105. int expectedNumProps = 3;
  106. if(db.getFileFormat() != Database.FileFormat.V1997) {
  107. assertEquals("{5A29A676-1145-4D1A-AE47-9F5415CDF2F1}",
  108. tProps.getValue(PropertyMap.GUID_PROP));
  109. if(nameMapBytes == null) {
  110. nameMapBytes = (byte[])tProps.getValue("NameMap");
  111. } else {
  112. assertTrue(Arrays.equals(nameMapBytes,
  113. (byte[])tProps.getValue("NameMap")));
  114. }
  115. expectedNumProps += 2;
  116. }
  117. assertEquals(expectedNumProps, tProps.getSize());
  118. assertEquals((byte)0, tProps.getValue("Orientation"));
  119. assertEquals(Boolean.FALSE, tProps.getValue("OrderByOn"));
  120. assertEquals((byte)2, tProps.getValue("DefaultView"));
  121. PropertyMap colProps = t.getColumn("A").getProperties();
  122. assertEquals("A", colProps.getName());
  123. expectedNumProps = 9;
  124. if(db.getFileFormat() != Database.FileFormat.V1997) {
  125. assertEquals("{E9EDD90C-CE55-4151-ABE1-A1ACE1007515}",
  126. colProps.getValue(PropertyMap.GUID_PROP));
  127. ++expectedNumProps;
  128. }
  129. assertEquals(expectedNumProps, colProps.getSize());
  130. assertEquals((short)-1, colProps.getValue("ColumnWidth"));
  131. assertEquals((short)0, colProps.getValue("ColumnOrder"));
  132. assertEquals(Boolean.FALSE, colProps.getValue("ColumnHidden"));
  133. assertEquals(Boolean.FALSE,
  134. colProps.getValue(PropertyMap.REQUIRED_PROP));
  135. assertEquals(Boolean.FALSE,
  136. colProps.getValue(PropertyMap.ALLOW_ZERO_LEN_PROP));
  137. assertEquals((short)109, colProps.getValue("DisplayControl"));
  138. assertEquals(Boolean.TRUE, colProps.getValue("UnicodeCompression"));
  139. assertEquals((byte)0, colProps.getValue("IMEMode"));
  140. assertEquals((byte)3, colProps.getValue("IMESentenceMode"));
  141. PropertyMap dbProps = db.getDatabaseProperties();
  142. assertTrue(((String)dbProps.getValue(PropertyMap.ACCESS_VERSION_PROP))
  143. .matches("[0-9]{2}[.][0-9]{2}"));
  144. PropertyMap sumProps = db.getSummaryProperties();
  145. assertEquals(3, sumProps.getSize());
  146. assertEquals("test", sumProps.getValue(PropertyMap.TITLE_PROP));
  147. assertEquals("tmccune", sumProps.getValue(PropertyMap.AUTHOR_PROP));
  148. assertEquals("Health Market Science", sumProps.getValue(PropertyMap.COMPANY_PROP));
  149. PropertyMap userProps = db.getUserDefinedProperties();
  150. assertEquals(1, userProps.getSize());
  151. assertEquals(Boolean.TRUE, userProps.getValue("ReplicateProject"));
  152. db.close();
  153. }
  154. }
  155. public void testParseProperties() throws Exception
  156. {
  157. for(FileFormat ff : SUPPORTED_FILEFORMATS_FOR_READ) {
  158. File[] dbFiles = new File(DIR_TEST_DATA, ff.name()).listFiles();
  159. if(dbFiles == null) {
  160. continue;
  161. }
  162. for(File f : dbFiles) {
  163. if(!f.isFile()) {
  164. continue;
  165. }
  166. Database db = open(ff, f);
  167. PropertyMap dbProps = db.getDatabaseProperties();
  168. assertFalse(dbProps.isEmpty());
  169. assertTrue(((String)dbProps.getValue(PropertyMap.ACCESS_VERSION_PROP))
  170. .matches("[0-9]{2}[.][0-9]{2}"));
  171. for(Row row : ((DatabaseImpl)db).getSystemCatalog()) {
  172. int id = row.getInt("Id");
  173. byte[] propBytes = row.getBytes("LvProp");
  174. PropertyMaps propMaps = ((DatabaseImpl)db).getPropertiesForObject(id);
  175. int byteLen = ((propBytes != null) ? propBytes.length : 0);
  176. if(byteLen == 0) {
  177. assertTrue(propMaps.isEmpty());
  178. } else if(propMaps.isEmpty()) {
  179. assertTrue(byteLen < 80);
  180. } else {
  181. assertTrue(byteLen > 0);
  182. }
  183. }
  184. db.close();
  185. }
  186. }
  187. }
  188. public void testWriteProperties() throws Exception
  189. {
  190. for(TestDB testDb : SUPPORTED_DBS_TEST) {
  191. Database db = open(testDb);
  192. TableImpl t = (TableImpl)db.getTable("Table1");
  193. PropertyMap tProps = t.getProperties();
  194. PropertyMaps maps = ((PropertyMapImpl)tProps).getOwner();
  195. byte[] mapsBytes = maps.write();
  196. PropertyMaps maps2 = ((DatabaseImpl)db).readProperties(
  197. mapsBytes, maps.getObjectId(), null);
  198. Iterator<PropertyMapImpl> iter = maps.iterator();
  199. Iterator<PropertyMapImpl> iter2 = maps2.iterator();
  200. while(iter.hasNext() && iter2.hasNext()) {
  201. PropertyMapImpl propMap = iter.next();
  202. PropertyMapImpl propMap2 = iter2.next();
  203. checkProperties(propMap, propMap2);
  204. }
  205. assertFalse(iter.hasNext());
  206. assertFalse(iter2.hasNext());
  207. db.close();
  208. }
  209. }
  210. public void testModifyProperties() throws Exception
  211. {
  212. for(TestDB testDb : SUPPORTED_DBS_TEST) {
  213. Database db = openCopy(testDb);
  214. File dbFile = db.getFile();
  215. Table t = db.getTable("Table1");
  216. // grab originals
  217. PropertyMap origCProps = t.getColumn("C").getProperties();
  218. PropertyMap origFProps = t.getColumn("F").getProperties();
  219. PropertyMap origDProps = t.getColumn("D").getProperties();
  220. db.close();
  221. // modify but do not save
  222. db = new DatabaseBuilder(dbFile).open();
  223. t = db.getTable("Table1");
  224. PropertyMap cProps = t.getColumn("C").getProperties();
  225. PropertyMap fProps = t.getColumn("F").getProperties();
  226. PropertyMap dProps = t.getColumn("D").getProperties();
  227. assertFalse((Boolean)cProps.getValue(PropertyMap.REQUIRED_PROP));
  228. assertEquals("0", fProps.getValue(PropertyMap.DEFAULT_VALUE_PROP));
  229. assertEquals((short)109, dProps.getValue("DisplayControl"));
  230. cProps.put(PropertyMap.REQUIRED_PROP, DataType.BOOLEAN, true);
  231. fProps.get(PropertyMap.DEFAULT_VALUE_PROP).setValue("42");
  232. dProps.remove("DisplayControl");
  233. db.close();
  234. // modify and save
  235. db = new DatabaseBuilder(dbFile).open();
  236. t = db.getTable("Table1");
  237. cProps = t.getColumn("C").getProperties();
  238. fProps = t.getColumn("F").getProperties();
  239. dProps = t.getColumn("D").getProperties();
  240. assertFalse((Boolean)cProps.getValue(PropertyMap.REQUIRED_PROP));
  241. assertEquals("0", fProps.getValue(PropertyMap.DEFAULT_VALUE_PROP));
  242. assertEquals((short)109, dProps.getValue("DisplayControl"));
  243. checkProperties(origCProps, cProps);
  244. checkProperties(origFProps, fProps);
  245. checkProperties(origDProps, dProps);
  246. cProps.put(PropertyMap.REQUIRED_PROP, DataType.BOOLEAN, true);
  247. cProps.save();
  248. fProps.get(PropertyMap.DEFAULT_VALUE_PROP).setValue("42");
  249. fProps.save();
  250. dProps.remove("DisplayControl");
  251. dProps.save();
  252. db.close();
  253. // reload saved props
  254. db = new DatabaseBuilder(dbFile).open();
  255. t = db.getTable("Table1");
  256. cProps = t.getColumn("C").getProperties();
  257. fProps = t.getColumn("F").getProperties();
  258. dProps = t.getColumn("D").getProperties();
  259. assertTrue((Boolean)cProps.getValue(PropertyMap.REQUIRED_PROP));
  260. assertEquals("42", fProps.getValue(PropertyMap.DEFAULT_VALUE_PROP));
  261. assertNull(dProps.getValue("DisplayControl"));
  262. cProps.put(PropertyMap.REQUIRED_PROP, DataType.BOOLEAN, false);
  263. fProps.get(PropertyMap.DEFAULT_VALUE_PROP).setValue("0");
  264. dProps.put("DisplayControl", DataType.INT, (short)109);
  265. checkProperties(origCProps, cProps);
  266. checkProperties(origFProps, fProps);
  267. checkProperties(origDProps, dProps);
  268. db.close();
  269. }
  270. }
  271. public void testCreateDbProperties() throws Exception
  272. {
  273. for(FileFormat ff : SUPPORTED_FILEFORMATS) {
  274. if(ff == FileFormat.GENERIC_JET4) {
  275. // weirdo format, no properties
  276. continue;
  277. }
  278. File file = TestUtil.createTempFile(false);
  279. Database db = new DatabaseBuilder(file)
  280. .setFileFormat(ff)
  281. .putUserDefinedProperty("testing", "123")
  282. .create();
  283. UUID u1 = UUID.randomUUID();
  284. UUID u2 = UUID.randomUUID();
  285. Table t = new TableBuilder("Test")
  286. .putProperty("awesome_table", true)
  287. .addColumn(new ColumnBuilder("id", DataType.LONG)
  288. .setAutoNumber(true)
  289. .putProperty(PropertyMap.REQUIRED_PROP, true)
  290. .putProperty(PropertyMap.GUID_PROP, u1))
  291. .addColumn(new ColumnBuilder("data", DataType.TEXT)
  292. .putProperty(PropertyMap.ALLOW_ZERO_LEN_PROP, false)
  293. .putProperty(PropertyMap.GUID_PROP, u2))
  294. .toTable(db);
  295. t.addRow(Column.AUTO_NUMBER, "value");
  296. db.close();
  297. db = new DatabaseBuilder(file).open();
  298. assertEquals("123", db.getUserDefinedProperties().getValue("testing"));
  299. t = db.getTable("Test");
  300. assertEquals(Boolean.TRUE,
  301. t.getProperties().getValue("awesome_table"));
  302. Column c = t.getColumn("id");
  303. assertEquals(Boolean.TRUE,
  304. c.getProperties().getValue(PropertyMap.REQUIRED_PROP));
  305. assertEquals("{" + u1.toString().toUpperCase() + "}",
  306. c.getProperties().getValue(PropertyMap.GUID_PROP));
  307. c = t.getColumn("data");
  308. assertEquals(Boolean.FALSE,
  309. c.getProperties().getValue(PropertyMap.ALLOW_ZERO_LEN_PROP));
  310. assertEquals("{" + u2.toString().toUpperCase() + "}",
  311. c.getProperties().getValue(PropertyMap.GUID_PROP));
  312. }
  313. }
  314. private static void checkProperties(PropertyMap propMap1,
  315. PropertyMap propMap2)
  316. {
  317. assertEquals(propMap1.getSize(), propMap2.getSize());
  318. for(PropertyMap.Property prop : propMap1) {
  319. PropertyMap.Property prop2 = propMap2.get(prop.getName());
  320. assertEquals(prop.getName(), prop2.getName());
  321. assertEquals(prop.getType(), prop2.getType());
  322. Object v1 = prop.getValue();
  323. Object v2 = prop2.getValue();
  324. if(v1 instanceof byte[]) {
  325. assertTrue(Arrays.equals((byte[])v1, (byte[])v2));
  326. } else {
  327. assertEquals(v1, v2);
  328. }
  329. }
  330. }
  331. }