Ejemplo n.º 1
1
  @Test
  public void testNewResultSet_GetMergeCost() 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(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);

    // 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);
    int size = carsWithAbs.getMergeCost();

    assertEquals(3, size);
    verify(connection, times(1)).close();
  }
Ejemplo n.º 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();
  }
Ejemplo n.º 3
0
  @Test
  public void testNewResultSet_Iterator_Close() throws Exception {

    QueryOptions queryOptions = new QueryOptions();

    // 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);
    @SuppressWarnings("unchecked")
    SimpleAttribute<Integer, Car> idToObject =
        (SimpleAttribute<Integer, Car>) mock(SimpleAttribute.class);

    // Behaviour
    when(connectionManager.getConnection(any(SQLiteIndex.class))).thenReturn(connection);
    when(connection.prepareStatement(
            "SELECT DISTINCT objectKey, value FROM " + TABLE_NAME + " WHERE value = ?;"))
        .thenReturn(preparedStatement);
    when(preparedStatement.executeQuery()).thenReturn(resultSet);
    when(resultSet.getStatement()).thenReturn(preparedStatement);
    when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(resultSet.getInt(1)).thenReturn(1).thenReturn(3);
    when(idToObject.getValue(1, queryOptions)).thenReturn(data.get(0));
    when(idToObject.getValue(3, queryOptions)).thenReturn(data.get(2));

    // Iterator
    ResultSet<Car> carsWithAbs =
        new SQLiteIndex<String, Car, Integer>(
                Car.FEATURES, OBJECT_TO_ID, idToObject, connectionManager)
            .retrieve(equal(Car.FEATURES, "abs"), queryOptions);

    Assert.assertNotNull(carsWithAbs);
    Iterator carsWithAbsIterator = carsWithAbs.iterator();

    assertTrue(carsWithAbsIterator.hasNext());
    Assert.assertNotNull(carsWithAbsIterator.next());
    assertTrue(carsWithAbsIterator.hasNext());
    Assert.assertNotNull(carsWithAbsIterator.next());
    assertFalse(carsWithAbsIterator.hasNext());

    // The end of the iteration should close the resources
    verify(connection, times(1)).close();
    verify(preparedStatement, times(1)).close();
    verify(resultSet, times(1)).close();
  }
Ejemplo n.º 4
0
    /**
     * This resets the key generator from the values in the database Usefull if the generated key
     * has been modified via some other means Connection must be open before calling this
     */
    public synchronized void reset() {
      m_nextKey = 0;
      java.sql.Statement stmnt = null;
      java.sql.ResultSet rs = null;
      try {
        stmnt = GenOrmDataSource.createStatement();
        rs = stmnt.executeQuery(MAX_QUERY);

        if (rs.next()) m_nextKey = rs.getInt(1);
      } catch (java.sql.SQLException sqle) {
        // The exception may occur if the table does not yet exist
        if (WARNINGS) System.out.println(sqle);
      } finally {
        try {
          if (rs != null) rs.close();

          if (stmnt != null) stmnt.close();
        } catch (java.sql.SQLException sqle2) {
          throw new GenOrmException(sqle2);
        }
      }
    }
Ejemplo n.º 5
0
    public DataPointKeyGenerator(javax.sql.DataSource ds) {
      m_nextKey = 0;
      java.sql.Connection con = null;
      java.sql.Statement stmnt = null;
      try {
        con = ds.getConnection();
        con.setAutoCommit(true);
        stmnt = con.createStatement();
        java.sql.ResultSet rs = stmnt.executeQuery(MAX_QUERY);
        if (rs.next()) m_nextKey = rs.getInt(1);

        rs.close();
      } catch (java.sql.SQLException sqle) {
        // The exception may occur if the table does not yet exist
        if (WARNINGS) System.out.println(sqle);
      } finally {
        try {
          if (stmnt != null) stmnt.close();

          if (con != null) con.close();
        } catch (java.sql.SQLException sqle) {
        }
      }
    }
  /**
   * A method to build a list of Collaboration objects representing a list of collaborations
   * associated with a collaborator
   *
   * @param id the unique identifier of a collaborator
   * @return a CollaborationList object containing a list of Collaboration objects
   */
  public CollaborationList getCollaborationList_org(String org_id, String id) {

    // check on the input parameters
    if (InputUtils.isValidInt(id) == false) {
      throw new IllegalArgumentException("The id parameter cannot be null");
    }

    // declare helper variables
    CollaborationList list = new CollaborationList(id);

    // define other helper variables
    Collaboration collaboration = null;

    // define other helper variables
    String partner = null;
    Integer count = null;
    String firstDate = null;
    String lastDate = null;

    // define the base sql query
    String sqlQuery =
        "SELECT distinct count(*), con.contributorid, min(e.first_date), max(e.first_date) "
            + "FROM contributor con, conevlink c , conevlink c2, orgevlink o, events e "
            + "WHERE o.organisationid = ? " // + org_id + " "
            + "AND c.eventid = O.EVENTID "
            + "AND e.eventid = O.EVENTID "
            + "AND e.eventid = c.EVENTID "
            + "AND c.contributorid != ?" // + id + " "
            + "AND con.contributorid = c.contributorid "
            + "AND c2.contributorid = ? " // + id + " "
            + "AND c2.eventid = c.eventid "
            + "GROUP BY con.contributorid, con.first_name ";

    int[] param = {Integer.parseInt(org_id), Integer.parseInt(id), Integer.parseInt(id)};
    // execute the query
    java.sql.ResultSet resultSet = db.exePreparedStatement(sqlQuery, param);

    try {
      //		 	check to see that data was returned
      if (!resultSet.last()) {
        db.tidyup();
        return null;
      } else resultSet.beforeFirst();

      // loop though the resulset
      while (resultSet.next()) {
        // get the data
        partner = resultSet.getString(2);
        count = resultSet.getInt(1);
        firstDate = resultSet.getDate(3).toString();
        lastDate = resultSet.getDate(4).toString();

        // create the collaboration object
        collaboration = new Collaboration(id, partner, count, firstDate, lastDate);

        // add the collaboration to the list
        list.addCollaboration(collaboration);
      }

    } catch (java.sql.SQLException ex) {
      System.out.println("Exception: " + ex.getMessage());
      resultSet = null;
    }

    db.tidyup();
    // return the list of collaborations
    return list;
  } // end the CollaborationList method