Ejemplo n.º 1
1
  /**
   * Test importing an unsupported field e.g. datetime, the program should continue and assign null
   * value to the field.
   *
   * @throws Exception
   */
  @Test
  public void testImportUnsupportedField() throws Exception {
    ConnectionProperties p = getConnectionProperties();
    Connection cn = DatabaseConnection.getConnection(p);
    try {
      List<Field> verifiedFields = new Vector<Field>();
      String[] fields = "ListingId, Title".split(",");
      String tableName = "Listings";
      DatabaseConnection.verifyTable(p, tableName, fields, verifiedFields);

      Collection<JsonNode> importNodes = new LinkedList<JsonNode>();
      JsonNodeFactory f = JsonNodeFactory.instance;

      int listingId = 1559350;

      ObjectNode n;
      n = new ObjectNode(f);
      n.put("ListingId", listingId);
      importNodes.add(n);

      JsonArrayImporter importer = new JsonArrayImporter(p);
      importer.doImport(tableName, verifiedFields, importNodes);

      Statement st = cn.createStatement();
      ResultSet rs = st.executeQuery("SELECT ListingId, Title FROM Listings");
      assertTrue("Expected result set to contain a record", rs.next());
      assertEquals(listingId, rs.getInt("ListingId"));
      assertEquals(null, rs.getString("Title"));
    } finally {
      cn.close();
    }
  }
Ejemplo n.º 2
0
  /**
   * Test importing string, int, and decimal values work successfully
   *
   * @throws Exception
   */
  @Test
  public void testImportSimple() throws Exception {
    ConnectionProperties p = getConnectionProperties();
    Connection cn = DatabaseConnection.getConnection(p);
    try {
      List<Field> verifiedFields = new Vector<Field>();
      String[] fields = "ListingId, Title, StartPrice".split(",");
      String tableName = "Listings";
      DatabaseConnection.verifyTable(p, tableName, fields, verifiedFields);

      Collection<JsonNode> importNodes = new LinkedList<JsonNode>();
      JsonNodeFactory f = JsonNodeFactory.instance;

      int listingId = 1559350;
      String title = "Product Pro1-Beta-0066";
      BigDecimal startPrice = BigDecimal.valueOf(20.64);

      ObjectNode n;
      n = new ObjectNode(f);
      n.put("ListingId", listingId);
      n.put("Title", title);
      n.put("StartPrice", startPrice);
      importNodes.add(n);

      JsonArrayImporter importer = new JsonArrayImporter(p);
      importer.doImport(tableName, verifiedFields, importNodes);

      Statement st = cn.createStatement();
      ResultSet rs = st.executeQuery("SELECT ListingId, Title, StartPrice FROM Listings");
      assertTrue("Expected result set to contain a record", rs.next());
      assertEquals(listingId, rs.getInt("ListingId"));
      assertEquals(title, rs.getString("Title"));
      assertEquals(startPrice, rs.getBigDecimal("StartPrice"));
      assertFalse("Expect result set to only contain one record", rs.next());
    } finally {
      cn.close();
    }
  }
Ejemplo n.º 3
0
 private void truncateTable() throws SQLException, ClassNotFoundException {
   ConnectionProperties p = getConnectionProperties();
   Connection cn = DatabaseConnection.getConnection(p);
   Statement st = cn.createStatement();
   st.execute("TRUNCATE TABLE Listings");
 }