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.

CustomLinkResolverTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright (c) 2017 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.util;
  14. import java.io.FileNotFoundException;
  15. import java.io.IOException;
  16. import java.nio.file.Path;
  17. import com.healthmarketscience.jackcess.ColumnBuilder;
  18. import com.healthmarketscience.jackcess.DataType;
  19. import com.healthmarketscience.jackcess.Database;
  20. import com.healthmarketscience.jackcess.Database.FileFormat;
  21. import com.healthmarketscience.jackcess.Table;
  22. import com.healthmarketscience.jackcess.TableBuilder;
  23. import static com.healthmarketscience.jackcess.TestUtil.*;
  24. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  25. import static org.junit.jupiter.api.Assertions.*;
  26. import org.junit.jupiter.api.Test;
  27. /**
  28. *
  29. * @author James Ahlborn
  30. */
  31. public class CustomLinkResolverTest
  32. {
  33. @Test
  34. public void testCustomLinkResolver() throws Exception
  35. {
  36. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS)
  37. {
  38. try (final Database db = create(fileFormat))
  39. {
  40. db.setLinkResolver(new TestLinkResolver());
  41. db.createLinkedTable("Table1", "testFile1.txt", "Table1");
  42. db.createLinkedTable("Table2", "testFile2.txt", "OtherTable2");
  43. db.createLinkedTable("Table3", "missingFile3.txt", "MissingTable3");
  44. db.createLinkedTable("Table4", "testFile2.txt", "MissingTable4");
  45. Table t1 = db.getTable("Table1");
  46. assertNotNull(t1);
  47. assertNotSame(db, t1.getDatabase());
  48. assertTable(createExpectedTable(createExpectedRow("id", 0, "data1", "row0"),
  49. createExpectedRow("id", 1, "data1", "row1"),
  50. createExpectedRow("id", 2, "data1", "row2")),
  51. t1);
  52. Table t2 = db.getTable("Table2");
  53. assertNotNull(t2);
  54. assertNotSame(db, t2.getDatabase());
  55. assertTable(createExpectedTable(createExpectedRow("id", 3, "data2", "row3"),
  56. createExpectedRow("id", 4, "data2", "row4"),
  57. createExpectedRow("id", 5, "data2", "row5")),
  58. t2);
  59. assertNull(db.getTable("Table4"));
  60. assertThrows(FileNotFoundException.class, () -> db.getTable("Table3"));
  61. }
  62. }
  63. }
  64. private static class TestLinkResolver extends CustomLinkResolver
  65. {
  66. private TestLinkResolver()
  67. {
  68. super(DEFAULT_FORMAT, true, DEFAULT_TEMP_DIR);
  69. }
  70. @Override
  71. protected Object loadCustomFile(
  72. Database linkerDb, String linkeeFileName) throws IOException
  73. {
  74. return (("testFile1.txt".equals(linkeeFileName) ||
  75. "testFile2.txt".equals(linkeeFileName)) ?
  76. linkeeFileName : null);
  77. }
  78. @Override
  79. protected boolean loadCustomTable(
  80. Database tempDb, Object customFile, String tableName)
  81. throws IOException
  82. {
  83. if("Table1".equals(tableName)) {
  84. assertEquals("testFile1.txt", customFile);
  85. Table t = new TableBuilder(tableName)
  86. .addColumn(new ColumnBuilder("id", DataType.LONG))
  87. .addColumn(new ColumnBuilder("data1", DataType.TEXT))
  88. .toTable(tempDb);
  89. for(int i = 0; i < 3; ++i) {
  90. t.addRow(i, "row" + i);
  91. }
  92. return true;
  93. } else if("OtherTable2".equals(tableName)) {
  94. assertEquals("testFile2.txt", customFile);
  95. Table t = new TableBuilder(tableName)
  96. .addColumn(new ColumnBuilder("id", DataType.LONG))
  97. .addColumn(new ColumnBuilder("data2", DataType.TEXT))
  98. .toTable(tempDb);
  99. for(int i = 3; i < 6; ++i) {
  100. t.addRow(i, "row" + i);
  101. }
  102. return true;
  103. } else if("Table4".equals(tableName)) {
  104. assertEquals("testFile2.txt", customFile);
  105. return false;
  106. }
  107. return false;
  108. }
  109. @Override
  110. protected Database createTempDb(Object customFile, FileFormat format,
  111. boolean inMemory, Path tempDir,
  112. boolean readOnly)
  113. throws IOException
  114. {
  115. inMemory = "testFile1.txt".equals(customFile);
  116. return super.createTempDb(customFile, format, inMemory, tempDir,
  117. readOnly);
  118. }
  119. }
  120. }