View Javadoc
1   package com.healthmarketscience.jackcess.impl;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import com.healthmarketscience.jackcess.ColumnBuilder;
9   import com.healthmarketscience.jackcess.DataType;
10  import com.healthmarketscience.jackcess.Database;
11  import com.healthmarketscience.jackcess.DatabaseBuilder;
12  import com.healthmarketscience.jackcess.Table;
13  import com.healthmarketscience.jackcess.TableBuilder;
14  import junit.framework.TestCase;
15  import static com.healthmarketscience.jackcess.TestUtil.*;
16  import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
17  
18  /**
19   * @author Dan Rollo
20   *         Date: Mar 5, 2010
21   *         Time: 2:21:22 PM
22   */
23  public final class UsageMapTest extends TestCase {
24  
25    public void testRead() throws Exception {
26      for (final TestDB testDB : SUPPORTED_DBS_TEST) {
27        final int expectedFirstPage;
28        final int expectedLastPage;
29        final Database.FileFormat expectedFileFormat = testDB.getExpectedFileFormat();
30        if (Database.FileFormat.V2000.equals(expectedFileFormat)) {
31          expectedFirstPage = 743;
32          expectedLastPage = 767;
33        } else if (Database.FileFormat.V2003.equals(expectedFileFormat)) {
34          expectedFirstPage = 16;
35          expectedLastPage = 799;
36        } else if (Database.FileFormat.V2007.equals(expectedFileFormat)) {
37          expectedFirstPage = 94;
38          expectedLastPage = 511;
39        } else if (Database.FileFormat.V2010.equals(expectedFileFormat)) {
40          expectedFirstPage = 109;
41          expectedLastPage = 511;
42        } else {
43          throw new IllegalAccessException("Unknown file format: " + expectedFileFormat);
44        }
45        checkUsageMapRead(testDB.getFile(), expectedFirstPage, expectedLastPage);
46      }
47    }
48  
49    private static void checkUsageMapRead(
50        final File dbFile, final int expectedFirstPage, final int expectedLastPage)
51      throws IOException {
52  
53      final Database db = DatabaseBuilder.open(dbFile);
54      final UsageMap usageMap = UsageMap.read((DatabaseImpl)db,
55                                              PageChannel.PAGE_GLOBAL_USAGE_MAP,
56                                              PageChannel.ROW_GLOBAL_USAGE_MAP,
57                                              true);
58      assertEquals("Unexpected FirstPageNumber.", expectedFirstPage, 
59                   usageMap.getFirstPageNumber());
60      assertEquals("Unexpected LastPageNumber.", expectedLastPage, 
61                   usageMap.getLastPageNumber());
62    }
63  
64    public void testGobalReferenceUsageMap() throws Exception
65    {
66      Database db = openCopy(
67          Database.FileFormat.V2000, 
68          new File("src/test/data/V2000/testRefGlobalV2000.mdb"));
69  
70      Table t = new TableBuilder("Test2")
71        .addColumn(new ColumnBuilder("id", DataType.LONG))
72        .addColumn(new ColumnBuilder("data1", DataType.TEXT))
73        .addColumn(new ColumnBuilder("data2", DataType.TEXT))
74        .toTable(db);
75  
76  
77      ((DatabaseImpl)db).getPageChannel().startWrite();
78      try {
79        List<Object[]> rows = new ArrayList<Object[]>();
80        for(int i = 0; i < 300000; ++i) {
81          String s1 = "r" + i + "-" + createString(100);
82          String s2 = "r" + i + "-" + createString(200);
83  
84          rows.add(new Object[]{i, s1, s2});
85  
86          if((i % 2000) == 0) {
87            t.addRows(rows);
88            rows.clear();
89          }
90        }
91      } finally {
92        ((DatabaseImpl)db).getPageChannel().finishWrite();
93      }
94  
95      db.close();
96    }
97  }