コード例 #1
0
ファイル: CQNDateMathTest.java プロジェクト: kminder/cqengine
  @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());
  }
コード例 #2
0
  @Test
  public void testNewResultSet_Contains() throws Exception {

    // Mocks
    ConnectionManager connectionManager = mock(ConnectionManager.class);
    Connection connectionContains = mock(Connection.class);
    Connection connectionDoNotContain = mock(Connection.class);
    PreparedStatement preparedStatementContains = mock(PreparedStatement.class);
    PreparedStatement preparedStatementDoNotContains = mock(PreparedStatement.class);
    java.sql.ResultSet resultSetContains = mock(java.sql.ResultSet.class);
    java.sql.ResultSet resultSetDoNotContain = mock(java.sql.ResultSet.class);

    // Behaviour
    when(connectionManager.getConnection(any(SQLiteIndex.class)))
        .thenReturn(connectionContains)
        .thenReturn(connectionDoNotContain);
    when(connectionContains.prepareStatement(
            "SELECT COUNT(objectKey) FROM " + TABLE_NAME + " WHERE value = ? AND objectKey = ?;"))
        .thenReturn(preparedStatementContains);
    when(connectionDoNotContain.prepareStatement(
            "SELECT COUNT(objectKey) FROM " + TABLE_NAME + " WHERE value = ? AND objectKey = ?;"))
        .thenReturn(preparedStatementDoNotContains);
    when(preparedStatementContains.executeQuery()).thenReturn(resultSetContains);
    when(preparedStatementDoNotContains.executeQuery()).thenReturn(resultSetDoNotContain);
    when(resultSetContains.next()).thenReturn(true).thenReturn(false);
    when(resultSetContains.getInt(1)).thenReturn(1);
    when(resultSetDoNotContain.next()).thenReturn(true).thenReturn(false);
    when(resultSetDoNotContain.getInt(1)).thenReturn(0);

    // Iterator
    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);
    boolean resultContains = carsWithAbs.contains(data.get(0));
    assertTrue(resultContains);
    verify(connectionContains, times(1)).close();

    boolean resultDoNotContain = carsWithAbs.contains(data.get(1));
    assertFalse(resultDoNotContain);
    verify(connectionDoNotContain, times(1)).close();
  }