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.

JoinerTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.util;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.HashMap;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.stream.Collectors;
  22. import com.healthmarketscience.jackcess.Database;
  23. import com.healthmarketscience.jackcess.Index;
  24. import com.healthmarketscience.jackcess.Row;
  25. import com.healthmarketscience.jackcess.Table;
  26. import com.healthmarketscience.jackcess.impl.RowImpl;
  27. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  28. import static com.healthmarketscience.jackcess.TestUtil.*;
  29. import static org.junit.jupiter.api.Assertions.*;
  30. import org.junit.jupiter.api.Test;
  31. /**
  32. *
  33. * @author James Ahlborn
  34. */
  35. public class JoinerTest
  36. {
  37. @Test
  38. public void testJoiner() throws Exception
  39. {
  40. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
  41. Database db = openCopy(testDB);
  42. Table t1 = db.getTable("Table1");
  43. Table t2 = db.getTable("Table2");
  44. Table t3 = db.getTable("Table3");
  45. Index t1t2 = t1.getIndex("Table2Table1");
  46. Index t1t3 = t1.getIndex("Table3Table1");
  47. Index t2t1 = t1t2.getReferencedIndex();
  48. assertSame(t2, t2t1.getTable());
  49. Joiner t2t1Join = Joiner.create(t2t1);
  50. assertSame(t2, t2t1Join.getFromTable());
  51. assertSame(t2t1, t2t1Join.getFromIndex());
  52. assertSame(t1, t2t1Join.getToTable());
  53. assertSame(t1t2, t2t1Join.getToIndex());
  54. doTestJoiner(t2t1Join, createT2T1Data());
  55. Index t3t1 = t1t3.getReferencedIndex();
  56. assertSame(t3, t3t1.getTable());
  57. Joiner t3t1Join = Joiner.create(t3t1);
  58. assertSame(t3, t3t1Join.getFromTable());
  59. assertSame(t3t1, t3t1Join.getFromIndex());
  60. assertSame(t1, t3t1Join.getToTable());
  61. assertSame(t1t3, t3t1Join.getToIndex());
  62. doTestJoiner(t3t1Join, createT3T1Data());
  63. doTestJoinerDelete(t2t1Join);
  64. }
  65. }
  66. private static void doTestJoiner(
  67. Joiner join, Map<Integer,List<Row>> expectedData)
  68. throws Exception
  69. {
  70. final Set<String> colNames = new HashSet<String>(
  71. Arrays.asList("id", "data"));
  72. Joiner revJoin = join.createReverse();
  73. for(Row row : join.getFromTable()) {
  74. Integer id = row.getInt("id");
  75. List<Row> joinedRows = join.findRows(row).stream()
  76. .collect(Collectors.toList());
  77. List<Row> expectedRows = expectedData.get(id);
  78. assertEquals(expectedData.get(id), joinedRows);
  79. if(!expectedRows.isEmpty()) {
  80. assertTrue(join.hasRows(row));
  81. assertEquals(expectedRows.get(0), join.findFirstRow(row));
  82. assertEquals(row, revJoin.findFirstRow(expectedRows.get(0)));
  83. } else {
  84. assertFalse(join.hasRows(row));
  85. assertNull(join.findFirstRow(row));
  86. }
  87. List<Row> expectedRows2 = new ArrayList<Row>();
  88. for(Row tmpRow : expectedRows) {
  89. Row tmpRow2 = new RowImpl(tmpRow);
  90. tmpRow2.keySet().retainAll(colNames);
  91. expectedRows2.add(tmpRow2);
  92. }
  93. joinedRows = join.findRows(row).setColumnNames(colNames)
  94. .stream().collect(Collectors.toList());
  95. assertEquals(expectedRows2, joinedRows);
  96. if(!expectedRows2.isEmpty()) {
  97. assertEquals(expectedRows2.get(0), join.findFirstRow(row, colNames));
  98. } else {
  99. assertNull(join.findFirstRow(row, colNames));
  100. }
  101. }
  102. }
  103. private static void doTestJoinerDelete(Joiner t2t1Join) throws Exception
  104. {
  105. assertEquals(4, countRows(t2t1Join.getToTable()));
  106. Row row = createExpectedRow("id", 1);
  107. assertTrue(t2t1Join.hasRows(row));
  108. assertTrue(t2t1Join.deleteRows(row));
  109. assertFalse(t2t1Join.hasRows(row));
  110. assertFalse(t2t1Join.deleteRows(row));
  111. assertEquals(2, countRows(t2t1Join.getToTable()));
  112. for(Row t1Row : t2t1Join.getToTable()) {
  113. assertFalse(t1Row.get("otherfk1").equals(1));
  114. }
  115. }
  116. private static Map<Integer,List<Row>> createT2T1Data()
  117. {
  118. Map<Integer,List<Row>> data = new
  119. HashMap<Integer,List<Row>>();
  120. data.put(0,
  121. createExpectedTable(
  122. createExpectedRow("id", 0, "otherfk1", 0, "otherfk2", 10,
  123. "data", "baz0", "otherfk3", 0)));
  124. data.put(1,
  125. createExpectedTable(
  126. createExpectedRow("id", 1, "otherfk1", 1, "otherfk2", 11,
  127. "data", "baz11", "otherfk3", 0),
  128. createExpectedRow("id", 2, "otherfk1", 1, "otherfk2", 11,
  129. "data", "baz11-2", "otherfk3", 0)));
  130. data.put(2,
  131. createExpectedTable(
  132. createExpectedRow("id", 3, "otherfk1", 2, "otherfk2", 13,
  133. "data", "baz13", "otherfk3", 0)));
  134. return data;
  135. }
  136. private static Map<Integer,List<Row>> createT3T1Data()
  137. {
  138. Map<Integer,List<Row>> data = new HashMap<Integer,List<Row>>();
  139. data.put(10,
  140. createExpectedTable(
  141. createExpectedRow("id", 0, "otherfk1", 0, "otherfk2", 10,
  142. "data", "baz0", "otherfk3", 0)));
  143. data.put(11,
  144. createExpectedTable(
  145. createExpectedRow("id", 1, "otherfk1", 1, "otherfk2", 11,
  146. "data", "baz11", "otherfk3", 0),
  147. createExpectedRow("id", 2, "otherfk1", 1, "otherfk2", 11,
  148. "data", "baz11-2", "otherfk3", 0)));
  149. data.put(12,
  150. createExpectedTable());
  151. data.put(13,
  152. createExpectedTable(
  153. createExpectedRow("id", 3, "otherfk1", 2, "otherfk2", 13,
  154. "data", "baz13", "otherfk3", 0)));
  155. return data;
  156. }
  157. }