IteratorStreamMergedResult extends StreamMergedResult { @Override public boolean next() throws SQLException { if (getCurrentQueryResult().next()) { return true; } if (!queryResults.hasNext()) { return false; } setCurrentQueryResult(queryResults.next()); boolean hasNext = getCurrentQueryResult().next(); if (hasNext) { return true; } while (!hasNext && queryResults.hasNext()) { setCurrentQueryResult(queryResults.next()); hasNext = getCurrentQueryResult().next(); } return hasNext; } IteratorStreamMergedResult(final List<QueryResult> queryResults); @Override boolean next();  }
@Test public void assertNextForResultSetsAllEmpty() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, null); assertFalse(actual.next()); }
@Test public void assertNextForResultSetsAllNotEmpty() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); for (QueryResult each : queryResults) { when(each.next()).thenReturn(true, false); } ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, null); assertTrue(actual.next()); assertTrue(actual.next()); assertTrue(actual.next()); assertFalse(actual.next()); }
@Test public void assertNextForFirstResultSetsNotEmptyOnly() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); when(queryResults.get(0).next()).thenReturn(true, false); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, null); assertTrue(actual.next()); assertFalse(actual.next()); }
@Test public void assertNextForMiddleResultSetsNotEmpty() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); when(queryResults.get(1).next()).thenReturn(true, false); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, null); assertTrue(actual.next()); assertFalse(actual.next()); }
@Test public void assertNextForLastResultSetsNotEmptyOnly() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); when(queryResults.get(2).next()).thenReturn(true, false); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, null); assertTrue(actual.next()); assertFalse(actual.next()); }
@Test public void assertNextForMix() throws SQLException { List<QueryResult> queryResults = Arrays.asList( mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); when(queryResults.get(1).next()).thenReturn(true, false); when(queryResults.get(3).next()).thenReturn(true, false); when(queryResults.get(5).next()).thenReturn(true, false); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, null); assertTrue(actual.next()); assertTrue(actual.next()); assertTrue(actual.next()); assertFalse(actual.next()); }