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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import static com.healthmarketscience.jackcess.Database.*;
  24. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  25. import com.healthmarketscience.jackcess.impl.ByteUtil;
  26. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  27. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  28. import com.healthmarketscience.jackcess.impl.PropertyMapImpl;
  29. import com.healthmarketscience.jackcess.impl.PropertyMaps;
  30. import com.healthmarketscience.jackcess.impl.TableImpl;
  31. import junit.framework.TestCase;
  32. /**
  33. * @author James Ahlborn
  34. */
  35. public class PropertiesTest extends TestCase
  36. {
  37. public PropertiesTest(String name) throws Exception {
  38. super(name);
  39. }
  40. public void testPropertyMaps() throws Exception
  41. {
  42. PropertyMaps maps = new PropertyMaps(10, null);
  43. assertTrue(maps.isEmpty());
  44. assertEquals(0, maps.getSize());
  45. assertFalse(maps.iterator().hasNext());
  46. assertEquals(10, maps.getObjectId());
  47. PropertyMapImpl defMap = maps.getDefault();
  48. assertTrue(defMap.isEmpty());
  49. assertEquals(0, defMap.getSize());
  50. assertFalse(defMap.iterator().hasNext());
  51. PropertyMapImpl colMap = maps.get("testcol");
  52. assertTrue(colMap.isEmpty());
  53. assertEquals(0, colMap.getSize());
  54. assertFalse(colMap.iterator().hasNext());
  55. assertFalse(maps.isEmpty());
  56. assertEquals(2, maps.getSize());
  57. assertSame(defMap, maps.get(PropertyMaps.DEFAULT_NAME));
  58. assertEquals(PropertyMaps.DEFAULT_NAME, defMap.getName());
  59. assertSame(colMap, maps.get("TESTCOL"));
  60. assertEquals("testcol", colMap.getName());
  61. defMap.put("foo", DataType.TEXT, (byte)0, "bar");
  62. defMap.put("baz", DataType.LONG, (byte)1, 13);
  63. assertFalse(defMap.isEmpty());
  64. assertEquals(2, defMap.getSize());
  65. colMap.put("buzz", DataType.BOOLEAN, (byte)0, Boolean.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 testReadProperties() throws Exception
  85. {
  86. byte[] nameMapBytes = null;
  87. for(TestDB testDb : SUPPORTED_DBS_TEST_FOR_READ) {
  88. Database db = open(testDb);
  89. TableImpl t = (TableImpl)db.getTable("Table1");
  90. assertEquals(t.getTableDefPageNumber(),
  91. t.getPropertyMaps().getObjectId());
  92. PropertyMap tProps = t.getProperties();
  93. assertEquals(PropertyMaps.DEFAULT_NAME, tProps.getName());
  94. int expectedNumProps = 3;
  95. if(db.getFileFormat() != Database.FileFormat.V1997) {
  96. assertEquals("{5A29A676-1145-4D1A-AE47-9F5415CDF2F1}",
  97. tProps.getValue(PropertyMap.GUID_PROP));
  98. if(nameMapBytes == null) {
  99. nameMapBytes = (byte[])tProps.getValue("NameMap");
  100. } else {
  101. assertTrue(Arrays.equals(nameMapBytes,
  102. (byte[])tProps.getValue("NameMap")));
  103. }
  104. expectedNumProps += 2;
  105. }
  106. assertEquals(expectedNumProps, tProps.getSize());
  107. assertEquals((byte)0, tProps.getValue("Orientation"));
  108. assertEquals(Boolean.FALSE, tProps.getValue("OrderByOn"));
  109. assertEquals((byte)2, tProps.getValue("DefaultView"));
  110. PropertyMap colProps = t.getColumn("A").getProperties();
  111. assertEquals("A", colProps.getName());
  112. expectedNumProps = 9;
  113. if(db.getFileFormat() != Database.FileFormat.V1997) {
  114. assertEquals("{E9EDD90C-CE55-4151-ABE1-A1ACE1007515}",
  115. colProps.getValue(PropertyMap.GUID_PROP));
  116. ++expectedNumProps;
  117. }
  118. assertEquals(expectedNumProps, colProps.getSize());
  119. assertEquals((short)-1, colProps.getValue("ColumnWidth"));
  120. assertEquals((short)0, colProps.getValue("ColumnOrder"));
  121. assertEquals(Boolean.FALSE, colProps.getValue("ColumnHidden"));
  122. assertEquals(Boolean.FALSE,
  123. colProps.getValue(PropertyMap.REQUIRED_PROP));
  124. assertEquals(Boolean.FALSE,
  125. colProps.getValue(PropertyMap.ALLOW_ZERO_LEN_PROP));
  126. assertEquals((short)109, colProps.getValue("DisplayControl"));
  127. assertEquals(Boolean.TRUE, colProps.getValue("UnicodeCompression"));
  128. assertEquals((byte)0, colProps.getValue("IMEMode"));
  129. assertEquals((byte)3, colProps.getValue("IMESentenceMode"));
  130. PropertyMap dbProps = db.getDatabaseProperties();
  131. assertTrue(((String)dbProps.getValue(PropertyMap.ACCESS_VERSION_PROP))
  132. .matches("[0-9]{2}[.][0-9]{2}"));
  133. PropertyMap sumProps = db.getSummaryProperties();
  134. assertEquals(3, sumProps.getSize());
  135. assertEquals("test", sumProps.getValue(PropertyMap.TITLE_PROP));
  136. assertEquals("tmccune", sumProps.getValue(PropertyMap.AUTHOR_PROP));
  137. assertEquals("Health Market Science", sumProps.getValue(PropertyMap.COMPANY_PROP));
  138. PropertyMap userProps = db.getUserDefinedProperties();
  139. assertEquals(1, userProps.getSize());
  140. assertEquals(Boolean.TRUE, userProps.getValue("ReplicateProject"));
  141. db.close();
  142. }
  143. }
  144. public void testParseProperties() throws Exception
  145. {
  146. for(FileFormat ff : SUPPORTED_FILEFORMATS_FOR_READ) {
  147. File[] dbFiles = new File(DIR_TEST_DATA, ff.name()).listFiles();
  148. if(dbFiles == null) {
  149. continue;
  150. }
  151. for(File f : dbFiles) {
  152. if(!f.isFile()) {
  153. continue;
  154. }
  155. Database db = open(ff, f);
  156. PropertyMap dbProps = db.getDatabaseProperties();
  157. assertFalse(dbProps.isEmpty());
  158. assertTrue(((String)dbProps.getValue(PropertyMap.ACCESS_VERSION_PROP))
  159. .matches("[0-9]{2}[.][0-9]{2}"));
  160. for(Map<String,Object> row : ((DatabaseImpl)db).getSystemCatalog()) {
  161. int id = (Integer)row.get("Id");
  162. byte[] propBytes = (byte[])row.get("LvProp");
  163. PropertyMaps propMaps = ((DatabaseImpl)db).getPropertiesForObject(id);
  164. int byteLen = ((propBytes != null) ? propBytes.length : 0);
  165. if(byteLen == 0) {
  166. assertTrue(propMaps.isEmpty());
  167. } else if(propMaps.isEmpty()) {
  168. assertTrue(byteLen < 80);
  169. } else {
  170. assertTrue(byteLen > 0);
  171. }
  172. }
  173. db.close();
  174. }
  175. }
  176. }
  177. public void testWriteProperties() throws Exception
  178. {
  179. for(TestDB testDb : SUPPORTED_DBS_TEST) {
  180. Database db = open(testDb);
  181. TableImpl t = (TableImpl)db.getTable("Table1");
  182. PropertyMap tProps = t.getProperties();
  183. PropertyMaps maps = ((PropertyMapImpl)tProps).getOwner();
  184. byte[] mapsBytes = maps.write();
  185. PropertyMaps maps2 = ((DatabaseImpl)db).readProperties(
  186. mapsBytes, maps.getObjectId());
  187. Iterator<PropertyMapImpl> iter = maps.iterator();
  188. Iterator<PropertyMapImpl> iter2 = maps2.iterator();
  189. while(iter.hasNext() && iter2.hasNext()) {
  190. PropertyMapImpl propMap = iter.next();
  191. PropertyMapImpl propMap2 = iter2.next();
  192. assertEquals(propMap.getSize(), propMap2.getSize());
  193. for(PropertyMap.Property prop : propMap) {
  194. PropertyMap.Property prop2 = propMap2.get(prop.getName());
  195. assertEquals(prop.getName(), prop2.getName());
  196. assertEquals(prop.getType(), prop2.getType());
  197. Object v1 = prop.getValue();
  198. Object v2 = prop2.getValue();
  199. if(v1 instanceof byte[]) {
  200. assertTrue(Arrays.equals((byte[])v1, (byte[])v2));
  201. } else {
  202. assertEquals(v1, v2);
  203. }
  204. }
  205. }
  206. assertFalse(iter.hasNext());
  207. assertFalse(iter2.hasNext());
  208. db.close();
  209. }
  210. }
  211. }