@Test public void testDateMath() { // Create a collection of Order objects, with shipDates 2015-08-01, 2015-08-02 and 2015-08-03... IndexedCollection<Order> collection = new ConcurrentIndexedCollection<Order>(); collection.add(createOrder("2015-08-01")); collection.add(createOrder("2015-08-02")); collection.add(createOrder("2015-08-03")); // Create a parser for CQN queries on Order objects... CQNParser<Order> parser = CQNParser.forPojoWithAttributes(Order.class, createAttributes(Order.class)); // Register a DateMathParser which can parse date math expressions // relative to the given date value for "now" ("2015-08-04"). // The custom value for "now" can be omitted to have it always calculate relative to the current // time... parser.registerValueParser(Date.class, new DateMathParser(createDate("2015-08-04"))); // Retrieve orders whose ship date is between 3 days ago and 2 days ago... ResultSet<Order> results = parser.retrieve(collection, "between(\"shipDate\", \"-3DAYS\", \"-2DAYS\")"); // Assert that the following two orders are returned... Assert.assertTrue(results.contains(createOrder("2015-08-01"))); Assert.assertTrue(results.contains(createOrder("2015-08-02"))); Assert.assertEquals(2, results.size()); }
@Test public void testNewResultSet_Size() throws Exception { // Mocks ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); // Behaviour when(connectionManager.getConnection(any(SQLiteIndex.class))).thenReturn(connection); when(connection.prepareStatement( "SELECT COUNT(DISTINCT objectKey) FROM " + TABLE_NAME + " WHERE value = ?;")) .thenReturn(preparedStatement); when(preparedStatement.executeQuery()).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(preparedStatement); when(resultSet.next()).thenReturn(true); when(resultSet.getInt(1)).thenReturn(3); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager) .retrieve(equal(Car.FEATURES, "abs"), new QueryOptions()); Assert.assertNotNull(carsWithAbs); int size = carsWithAbs.size(); assertEquals(3, size); verify(connection, times(1)).close(); }