View Javadoc
1   /*
2   Copyright (c) 2017 James Ahlborn
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.util;
18  
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  
23  import com.healthmarketscience.jackcess.ColumnBuilder;
24  import com.healthmarketscience.jackcess.DataType;
25  import com.healthmarketscience.jackcess.Database;
26  import com.healthmarketscience.jackcess.Database.FileFormat;
27  import com.healthmarketscience.jackcess.Table;
28  import com.healthmarketscience.jackcess.TableBuilder;
29  import junit.framework.TestCase;
30  import static com.healthmarketscience.jackcess.TestUtil.*;
31  import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
32  
33  /**
34   *
35   * @author James Ahlborn
36   */
37  public class CustomLinkResolverTest extends TestCase
38  {
39  
40    public CustomLinkResolverTest(String name) {
41      super(name);
42    }
43  
44    public void testCustomLinkResolver() throws Exception {
45      for(final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
46        Database db = create(fileFormat);
47  
48        db.setLinkResolver(new TestLinkResolver());
49  
50        db.createLinkedTable("Table1", "testFile1.txt", "Table1");
51        db.createLinkedTable("Table2", "testFile2.txt", "OtherTable2");
52        db.createLinkedTable("Table3", "missingFile3.txt", "MissingTable3");
53        db.createLinkedTable("Table4", "testFile2.txt", "MissingTable4");
54  
55        Table t1 = db.getTable("Table1");
56        assertNotNull(t1);
57        assertNotSame(db, t1.getDatabase());
58        
59        assertTable(createExpectedTable(createExpectedRow("id", 0,
60                                                          "data1", "row0"),
61                                        createExpectedRow("id", 1,
62                                                          "data1", "row1"),
63                                        createExpectedRow("id", 2,
64                                                          "data1", "row2")),
65                    t1);
66  
67        Table t2 = db.getTable("Table2");
68        assertNotNull(t2);
69        assertNotSame(db, t2.getDatabase());
70  
71        assertTable(createExpectedTable(createExpectedRow("id", 3,
72                                                          "data2", "row3"),
73                                        createExpectedRow("id", 4,
74                                                          "data2", "row4"),
75                                        createExpectedRow("id", 5,
76                                                          "data2", "row5")),
77                    t2);
78  
79        assertNull(db.getTable("Table4"));
80  
81        try {
82          db.getTable("Table3");
83          fail("FileNotFoundException should have been thrown");
84        } catch(FileNotFoundException e) {
85          // success
86        }
87  
88        db.close();
89      }
90    }
91  
92    private static class TestLinkResolver extends CustomLinkResolver
93    {
94      private TestLinkResolver()
95      {
96        super(DEFAULT_FORMAT, true, DEFAULT_TEMP_DIR);
97      }
98  
99      @Override
100     protected Object loadCustomFile(
101         Database linkerDb, String linkeeFileName) throws IOException
102     {
103       return (("testFile1.txt".equals(linkeeFileName) ||
104                "testFile2.txt".equals(linkeeFileName)) ? 
105               linkeeFileName : null);
106     }
107 
108     @Override
109     protected boolean loadCustomTable(
110         Database tempDb, Object customFile, String tableName)
111       throws IOException
112     {
113       if("Table1".equals(tableName)) {
114 
115         assertEquals("testFile1.txt", customFile);
116         Table t = new TableBuilder(tableName)
117           .addColumn(new ColumnBuilder("id", DataType.LONG))
118           .addColumn(new ColumnBuilder("data1", DataType.TEXT))
119           .toTable(tempDb);
120 
121         for(int i = 0; i < 3; ++i) {
122           t.addRow(i, "row" + i);
123         }
124         
125         return true;
126 
127       } else if("OtherTable2".equals(tableName)) {
128 
129         assertEquals("testFile2.txt", customFile);
130         Table t = new TableBuilder(tableName)
131           .addColumn(new ColumnBuilder("id", DataType.LONG))
132           .addColumn(new ColumnBuilder("data2", DataType.TEXT))
133           .toTable(tempDb);
134 
135         for(int i = 3; i < 6; ++i) {
136           t.addRow(i, "row" + i);
137         }
138         
139         return true;
140 
141       } else if("Table4".equals(tableName)) {
142 
143         assertEquals("testFile2.txt", customFile);
144         return false;
145       }
146 
147       return false;
148     }
149 
150     @Override
151     protected Database createTempDb(Object customFile, FileFormat format,
152                                     boolean inMemory, File tempDir)
153       throws IOException
154     {
155       inMemory = "testFile1.txt".equals(customFile);
156       return super.createTempDb(customFile, format, inMemory, tempDir);
157     }
158   }
159 }