@Test public void shouldPrintToStringAllResults() { results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); List<String> lines = StringUtil.splitLines(results.toString()); assertThat( lines.size(), is(tuples.size() + 4)); // = delim + header + delim + (...lines...) + delim }
@Before public void beforeEach() { MockitoAnnotations.initMocks(this); context = new QueryContext( executionContext, mock(RepositoryCache.class), Collections.singleton("workspace"), schemata); columnList = new ArrayList<Column>(); columnList.add(new Column(selector("table1"), "colA", "colA")); columnList.add(new Column(selector("table1"), "colB", "colB")); columnList.add(new Column(selector("table1"), "colC", "colC")); columnList.add(new Column(selector("table2"), "colA", "colA2")); columnList.add(new Column(selector("table2"), "colB", "colB2")); columnList.add(new Column(selector("table2"), "colX", "colX")); columnTypes = new ArrayList<String>(); for (int i = 0; i != columnList.size(); ++i) { columnTypes.add(org.modeshape.jcr.value.PropertyType.STRING.getName()); } columns = new QueryResultColumns(columnList, columnTypes, false); tuples = new ArrayList<Object[]>(); tuples.add(tuple(columns, new String[] {"/a/b/c", "/a/x/y"}, 1, 2, 3, "2a", "2b", "x")); tuples.add(tuple(columns, new String[] {"/a/b/d", "/a/x/y"}, 4, 5, 6, "2a", "2b", "x")); results = new QueryResults(columns, statistics, context.getProblems()); }
@Test(expected = IllegalStateException.class) public void shouldRequireNextOnCursorToBeCalledBeforeGettingValueUsingColumnName() { results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); Cursor cursor = results.getCursor(); assertThat(cursor.hasNext(), is(true)); cursor.getValue("colA"); }
@Test public void shouldReturnCursorThatAccessesTuples() { results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); Cursor cursor = results.getCursor(); Iterator<Object[]> expectedIter = tuples.iterator(); int rowNumber = 0; while (cursor.hasNext() && expectedIter.hasNext()) { cursor.next(); Object[] tuple = expectedIter.next(); // Check the column values by column name and index ... for (Column column : results.getColumns().getColumns()) { String columnName = column.getColumnName(); int columnIndex = columns.getColumnIndexForName(columnName); assertThat(cursor.getValue(columnName), is(tuple[columnIndex])); assertThat(cursor.getValue(columnIndex), is(tuple[columnIndex])); // Get the location for this column ... int locationIndex = columns.getLocationIndex(column.selectorName().name()); Location location = (Location) tuple[locationIndex]; assertThat(cursor.getLocation(columnIndex), is(location)); } // Check the locations by selector name and index ... for (String selectorName : results.getColumns().getSelectorNames()) { int locationIndex = columns.getLocationIndex(selectorName); Location location = (Location) tuple[locationIndex]; assertThat(cursor.getLocation(selectorName), is(location)); assertThat(location.getPath(), is(notNullValue())); } // Check the row index ... assertThat(cursor.getRowIndex(), is(rowNumber++)); } assertThat(cursor.hasNext(), is(false)); assertThat(expectedIter.hasNext(), is(false)); }
@Test public void shouldHaveNoTuplesIfConstructedWithEmptyTuplesList() { tuples.clear(); results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); assertThat(results.getTuples().isEmpty(), is(true)); assertThat(results.getCursor().hasNext(), is(false)); }
@Test public void shouldPrintToStringBuilderOnlyFirstLinesOfResults() { results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); StringBuilder sb = new StringBuilder(); results.toString(typeSystem, sb, 1); List<String> lines = StringUtil.splitLines(sb.toString()); assertThat(lines.size(), is(1 + 4)); // = delim + header + delim + (...lines...) + delim }
@Test public void shouldReturnMutableTuplesList() { results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); assertThat(results.getTuples().isEmpty(), is(false)); results.getTuples().clear(); assertThat(results.getTuples().isEmpty(), is(true)); assertThat(tuples.isEmpty(), is(true)); }
@Test public void shouldPrintToStringBuilderAllResultsWhenMaxRowParameterIsLargerThanNumberOfTuples() { tuples.clear(); results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); StringBuilder sb = new StringBuilder(); results.toString(typeSystem, sb, 3); List<String> lines = StringUtil.splitLines(sb.toString()); assertThat( lines.size(), is(tuples.size() + 4)); // = delim + header + delim + (...lines...) + delim }
@Test public void shouldReturnSameProblemsObjectAsInQueryContext() { assertThat(results.getProblems(), is(sameInstance(context.getProblems()))); }
@Test public void shouldReturnSameTuplesListPassedIntoConstructor() { results = new QueryResults(columns, statistics, tuples, context.getProblems(), null); assertThat(results.getTuples(), is(sameInstance(tuples))); }