PageRenderTime 30ms CodeModel.GetById 7ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/t11e/discovery/datatool/SubqueryTest.java

http://github.com/t11e/discovery_datatool
Java | 220 lines | 195 code | 25 blank | 0 comment | 0 complexity | 0bc79f46612429a6ec0b295d2ee5e3e2 MD5 | raw file
  1package com.t11e.discovery.datatool;
  2
  3import java.util.Arrays;
  4
  5import org.dom4j.Document;
  6import org.junit.Assert;
  7import org.junit.Test;
  8
  9public class SubqueryTest
 10  extends EndToEndTestBase
 11{
 12  @Test
 13  public void testSnapshotWithVarcharSubquery()
 14  {
 15    final Document doc = assertChangeset("test-snapshot-varchar", "", "snapshot",
 16      Arrays.asList("1", "2", "3"),
 17      Arrays.asList("4", "5"),
 18      false);
 19
 20    Assert.assertEquals("red",
 21      doc.selectSingleNode("/changeset/set-item[@id='1']/properties/struct/entry[@name='color']/string/text()")
 22        .getText());
 23    Assert.assertEquals("orange,yellow",
 24      doc.selectSingleNode("/changeset/set-item[@id='2']/properties/struct/entry[@name='color']/string/text()")
 25        .getText());
 26    Assert.assertNull(doc.selectSingleNode("/changeset/set-item[@id='3']/properties/struct/entry[@name='color']"));
 27
 28    Assert.assertEquals(
 29      "red",
 30      doc.selectSingleNode(
 31        "/changeset/set-item[@id='1']/properties/struct/entry[@name='color-delimited']/string/text()")
 32        .getText());
 33    Assert.assertEquals(
 34      "orange|yellow",
 35      doc.selectSingleNode(
 36        "/changeset/set-item[@id='2']/properties/struct/entry[@name='color-delimited']/string/text()")
 37        .getText());
 38    Assert.assertNull(doc
 39      .selectSingleNode("/changeset/set-item[@id='3']/properties/struct/entry[@name='color-delimited']"));
 40
 41    assertColorArraySubquery(doc, "color-default");
 42    assertColorArraySubquery(doc, "color-array");
 43
 44  }
 45
 46  private void assertColorArraySubquery(final Document doc, final String propertyName)
 47  {
 48    Assert.assertEquals("red",
 49      doc.selectSingleNode(
 50        "/changeset/set-item[@id='1']/properties/struct/entry[@name='" + propertyName + "']/string/text()")
 51        .getText());
 52    Assert.assertEquals(2,
 53      doc.selectNodes(
 54        "/changeset/set-item[@id='2']/properties/struct/entry[@name='" + propertyName + "']/array/element/string/text()")
 55        .size());
 56    Assert.assertEquals(
 57      Arrays.asList("orange", "yellow"),
 58      nodesAsStrings(doc,
 59        "/changeset/set-item[@id='2']/properties/struct/entry[@name='" + propertyName + "']/array/element/string/text()"));
 60    Assert.assertNull(doc
 61      .selectSingleNode("/changeset/set-item[@id='3']/properties/struct/entry[@name='" + propertyName + "']"));
 62  }
 63
 64  @Test
 65  public void testSnapshotWithIntSubquery()
 66  {
 67    final Document doc = assertChangeset("test-snapshot-int", "", "snapshot",
 68      Arrays.asList("1", "2", "3"),
 69      Arrays.asList("4", "5"),
 70      false);
 71    Assert.assertEquals("10",
 72      doc.selectSingleNode("/changeset/set-item[@id='1']/properties/struct/entry[@name='color']/string/text()")
 73        .getText());
 74    Assert.assertEquals("20,30",
 75      doc.selectSingleNode("/changeset/set-item[@id='2']/properties/struct/entry[@name='color']/string/text()")
 76        .getText());
 77    Assert.assertNull(doc.selectSingleNode("/changeset/set-item[@id='3']/properties/struct/entry[@name='color']"));
 78  }
 79
 80  @Test
 81  public void testSnapshotWithTimestampSubquery()
 82  {
 83    final Document doc = assertChangeset("test-snapshot-timestamp", "", "snapshot",
 84      Arrays.asList("1", "2", "3"),
 85      Arrays.asList("4", "5"),
 86      false);
 87    Assert.assertEquals("2011-01-01T00:00:00.000",
 88      doc.selectSingleNode("/changeset/set-item[@id='1']/properties/struct/entry[@name='color']/string/text()")
 89        .getText());
 90    Assert.assertEquals("2011-01-02T00:00:00.000,2011-01-03T00:00:00.000",
 91      doc.selectSingleNode("/changeset/set-item[@id='2']/properties/struct/entry[@name='color']/string/text()")
 92        .getText());
 93    Assert.assertNull(doc.selectSingleNode("/changeset/set-item[@id='3']/properties/struct/entry[@name='color']"));
 94  }
 95
 96  @Test
 97  public void testSubQueryWithMultipleColumn()
 98  {
 99    final Document doc = assertChangeset("test-snapshot-multi-column", "", "snapshot",
100      Arrays.asList("1", "2", "3"),
101      Arrays.asList("4", "5"),
102      false);
103    Assert.assertEquals("red",
104      doc.selectSingleNode(
105      "/changeset/set-item[@id='1']/properties/struct/entry[@name='name']/string/text()")
106      .getText());
107    Assert.assertEquals("10",
108      doc.selectSingleNode(
109      "/changeset/set-item[@id='1']/properties/struct/entry[@name='renamed']/string/text()")
110      .getText());
111    Assert.assertEquals("red",
112      doc.selectSingleNode(
113      "/changeset/set-item[@id='1']/properties/struct/entry[@name='color']/struct/entry[@name='name']/string/text()")
114      .getText());
115    Assert.assertEquals("10",
116      doc.selectSingleNode(
117      "/changeset/set-item[@id='1']/properties/struct/entry[@name='color']/struct/entry[@name='renamed']/string/text()")
118      .getText());
119    Assert.assertEquals("red",
120      doc.selectSingleNode(
121        "/changeset/set-item[@id='1']/properties/struct/entry[@name='color_delimited_name']/string/text()")
122        .getText());
123    Assert.assertEquals("10",
124      doc.selectSingleNode(
125        "/changeset/set-item[@id='1']/properties/struct/entry[@name='color_delimited_renamed']/string/text()")
126        .getText());
127
128    Assert.assertEquals(2,
129      doc.selectNodes("/changeset/set-item[@id='2']/properties/struct/entry[@name='name']/array/element").size());
130    Assert.assertEquals(2,
131      doc.selectNodes("/changeset/set-item[@id='2']/properties/struct/entry[@name='renamed']/array/element").size());
132
133    Assert.assertEquals("orange",
134      doc.selectSingleNode(
135        "/changeset/set-item[@id='2']/properties/struct/entry[@name='name']/array/element[1]/string/text()")
136        .getText());
137    Assert.assertEquals("yellow",
138      doc.selectSingleNode(
139        "/changeset/set-item[@id='2']/properties/struct/entry[@name='name']/array/element[2]/string/text()")
140        .getText());
141
142    Assert.assertEquals("20",
143      doc.selectSingleNode(
144        "/changeset/set-item[@id='2']/properties/struct/entry[@name='renamed']/array/element[1]/string/text()")
145        .getText());
146    Assert.assertEquals("30",
147      doc.selectSingleNode(
148        "/changeset/set-item[@id='2']/properties/struct/entry[@name='renamed']/array/element[2]/string/text()")
149        .getText());
150
151    Assert.assertEquals(2,
152      doc.selectNodes("/changeset/set-item[@id='2']/properties/struct/entry[@name='color']/array/element").size());
153
154    Assert.assertEquals("orange",
155      doc.selectSingleNode(
156      "/changeset/set-item[@id='2']/properties/struct/entry[@name='color']/array/element[1]/struct/entry[@name='name']/string/text()")
157      .getText());
158    Assert.assertEquals("yellow",
159      doc.selectSingleNode(
160      "/changeset/set-item[@id='2']/properties/struct/entry[@name='color']/array/element[2]/struct/entry[@name='name']/string/text()")
161      .getText());
162
163    Assert.assertEquals("orange,yellow",
164      doc.selectSingleNode(
165        "/changeset/set-item[@id='2']/properties/struct/entry[@name='color_delimited_name']/string/text()")
166        .getText());
167    Assert.assertEquals("20,30",
168      doc.selectSingleNode(
169        "/changeset/set-item[@id='2']/properties/struct/entry[@name='color_delimited_renamed']/string/text()")
170        .getText());
171
172    Assert.assertNull(doc.selectSingleNode("/changeset/set-item[@id='3']/properties/struct/entry[@name='color']"));
173  }
174
175  @Test
176  public void testSubQueryWithMultipleColumnAndDiscriminator()
177  {
178    final Document doc = assertChangeset("test-snapshot-multi-column-discriminator", "", "snapshot",
179      Arrays.asList("1", "2", "3"),
180      Arrays.asList("4", "5"),
181      false);
182
183    Assert.assertEquals("1 main st",
184      doc.selectSingleNode(
185        "/changeset/set-item[@id='1']/properties/struct/entry[@name='address']/struct/entry[@name='biz']/struct/entry[@name='street']/string/text()")
186      .getText());
187    Assert.assertNull(
188      doc.selectSingleNode(
189        "/changeset/set-item[@id='1']/properties/struct/entry[@name='address']/struct/entry[@name='biz']/struct/entry[@name='discriminator']"));
190
191    Assert.assertEquals(2,
192      doc.selectNodes("/changeset/set-item[@id='2']/properties/struct/entry[@name='address']/struct/entry").size());
193
194    Assert.assertEquals(
195      "123 main st",
196      doc.selectSingleNode(
197        "/changeset/set-item[@id='2']/properties/struct/entry[@name='address']/struct/entry[@name='biz']/struct/entry[@name='street']/string/text()")
198      .getText());
199    Assert.assertNull(
200      doc.selectSingleNode(
201        "/changeset/set-item[@id='2']/properties/struct/entry[@name='address']/struct/entry[@name='biz']/struct/entry[@name='discriminator']"));
202
203    Assert.assertEquals(
204      "456 main st",
205      doc.selectSingleNode(
206            "/changeset/set-item[@id='2']/properties/struct/entry[@name='address']/struct/entry[@name='per']/struct/entry[@name='street']/string/text()")
207      .getText());
208
209    Assert.assertNull(doc.selectSingleNode("/changeset/set-item[@id='3']/properties/struct/entry[@name='address']"));
210  }
211
212  @Test
213  public void testEmptySubQuery()
214  {
215    assertChangeset("test-snapshot-empty-subquery", "", "snapshot",
216      Arrays.asList("1", "2", "3"),
217      Arrays.asList("4", "5"),
218      false);
219  }
220}