Exemple #1
0
  public void testConvenienceMethodWithParam() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    List<Map<String, Object>> r = h.select("select * from something where id = ?", 1);
    assertEquals(1, r.size());
    assertEquals("eric", r.get(0).get("name"));
  }
Exemple #2
0
  public void testCreateQueryObject() throws Exception {
    h.createStatement("insert into something (id, name) values (1, 'eric')").execute();
    h.createStatement("insert into something (id, name) values (2, 'brian')").execute();

    List<Map<String, Object>> results = h.createQuery("select * from something order by id").list();
    assertEquals(2, results.size());
    Map<String, Object> first_row = results.get(0);
    assertEquals("eric", first_row.get("name"));
  }
Exemple #3
0
  public void testFirstResult() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    Something r = h.createQuery("select * from something order by id").map(Something.class).first();

    assertNotNull(r);
    assertEquals("eric", r.getName());
  }
Exemple #4
0
  public void testStatementCustomizersPersistAfterMap() throws Exception {
    h.insert("insert into something (id, name) values (?, ?)", 1, "hello");
    h.insert("insert into something (id, name) values (?, ?)", 2, "world");

    List<Something> rs =
        h.createQuery("select id, name from something").setMaxRows(1).map(Something.class).list();

    assertEquals(1, rs.size());
  }
Exemple #5
0
  public void testListWithMaxRows() throws Exception {
    h.prepareBatch("insert into something (id, name) values (:id, :name)")
        .add(1, "Brian")
        .add(2, "Keith")
        .add(3, "Eric")
        .execute();

    assertEquals(
        1, h.createQuery("select id, name from something").map(Something.class).list(1).size());
    assertEquals(
        2, h.createQuery("select id, name from something").map(Something.class).list(2).size());
  }
Exemple #6
0
  public void testMappedQueryObject() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    Query<Something> query =
        h.createQuery("select * from something order by id").map(Something.class);

    List<Something> r = query.list();
    Something eric = r.get(0);
    assertEquals("eric", eric.getName());
    assertEquals(1, eric.getId());
  }
Exemple #7
0
  public void testIteratorBehavior3() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'eric')");

    int count = 0;
    for (Something s : h.createQuery("select * from something order by id").map(Something.class)) {
      count++;
      assertEquals("eric", s.getName());
    }

    assertEquals(2, count);
  }
Exemple #8
0
  public void testMappedQueryObjectWithNullForPrimitiveIntField() throws Exception {
    h.insert("insert into something (id, name, intValue) values (1, 'eric', null)");

    Query<Something> query =
        h.createQuery("select * from something order by id").map(Something.class);

    List<Something> r = query.list();
    Something eric = r.get(0);
    assertEquals("eric", eric.getName());
    assertEquals(1, eric.getId());
    assertEquals(0, eric.getIntValue());
  }
Exemple #9
0
  public void testPositionalArgWithNamedParam() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    List<Something> r =
        h.createQuery("select * from something where name = :name")
            .bind(0, "eric")
            .map(Something.class)
            .list();

    assertEquals(1, r.size());
    assertEquals("eric", r.get(0).getName());
  }
Exemple #10
0
  public void testMixedSetting() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    List<Something> r =
        h.createQuery("select * from something where name = :name and id = :id")
            .bind(0, "eric")
            .bind("id", 1)
            .map(Something.class)
            .list();

    assertEquals(1, r.size());
    assertEquals("eric", r.get(0).getName());
  }
Exemple #11
0
  public void testIteratorBehavior2() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    ResultIterator<Something> i =
        h.createQuery("select * from something order by id").map(Something.class).iterator();

    Something first = i.next();
    assertEquals("eric", first.getName());
    Something second = i.next();
    assertEquals(2, second.getId());
    assertFalse(i.hasNext());

    i.close();
  }
Exemple #12
0
  public void testFetchSize() throws Exception {
    h.createScript("default-data").execute();

    Query<Something> q =
        h.createQuery("select id, name from something order by id").map(Something.class);

    q.setFetchSize(1);

    ResultIterator<Something> r = q.iterator();

    assertTrue(r.hasNext());
    r.next();
    assertTrue(r.hasNext());
    r.next();
    assertFalse(r.hasNext());
  }
Exemple #13
0
  public void testMapper() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    Query<String> query =
        h.createQuery("select name from something order by id")
            .map(
                new ResultSetMapper<String>() {
                  public String map(int index, ResultSet r, StatementContext ctx)
                      throws SQLException {
                    return r.getString(1);
                  }
                });

    String name = query.list().get(0);
    assertEquals("eric", name);
  }
Exemple #14
0
 public void testQueriesWithNullResultSets() throws Exception {
   try {
     h.select("insert into something (id, name) values (?, ?)", 1, "hello");
   } catch (NoResultsException e) {
     return;
   }
   fail("expected NoResultsException");
 }
Exemple #15
0
 public void testHelpfulErrorOnNothingSet() throws Exception {
   try {
     h.createQuery("select * from something where name = :name").list();
     fail("should have raised exception");
   } catch (UnableToExecuteStatementException e) {
     assertTrue("execution goes through here", true);
   } catch (Exception e) {
     fail("Raised incorrect exception");
   }
 }
Exemple #16
0
  public void testFold() throws Exception {
    h.prepareBatch("insert into something (id, name) values (:id, :name)")
        .add(1, "Brian")
        .add(2, "Keith")
        .execute();

    Map<String, Integer> rs =
        h.createQuery("select id, name from something")
            .fold(
                new HashMap<String, Integer>(),
                new Folder2<Map<String, Integer>>() {
                  public Map<String, Integer> fold(
                      Map<String, Integer> a, ResultSet rs, StatementContext context)
                      throws SQLException {
                    a.put(rs.getString("name"), rs.getInt("id"));
                    return a;
                  }
                });
    assertEquals(2, rs.size());
    assertEquals(Integer.valueOf(1), rs.get("Brian"));
    assertEquals(Integer.valueOf(2), rs.get("Keith"));
  }
Exemple #17
0
  public void testFold3() throws Exception {
    h.prepareBatch("insert into something (id, name) values (:id, :name)")
        .add(1, "Brian")
        .add(2, "Keith")
        .execute();

    List<String> rs =
        h.createQuery("select name from something order by id")
            .map(StringMapper.FIRST)
            .fold(
                new ArrayList<String>(),
                new Folder3<List<String>, String>() {
                  public List<String> fold(
                      List<String> a, String rs, FoldController ctl, StatementContext ctx)
                      throws SQLException {
                    a.add(rs);
                    return a;
                  }
                });
    assertEquals(2, rs.size());
    assertEquals(Arrays.asList("Brian", "Keith"), rs);
  }
Exemple #18
0
 public void testUsefulArgumentOutputForDebug() throws Exception {
   try {
     h.createStatement("insert into something (id, name) values (:id, :name)")
         .bind("name", "brian")
         .bind(7, 8)
         .bindFromMap(new HandyMapThing<String>().add("one", "two"))
         .bindFromProperties(new Object())
         .execute();
   } catch (StatementException e) {
     assertTrue(
         e.getMessage()
             .contains(
                 "arguments:{ positional:{7:8}, named:{name:'brian'}, finder:[{one=two},{lazy bean proprty arguments \"java.lang.Object"));
   }
 }
Exemple #19
0
 public void testFirstWithNoResult() throws Exception {
   Something s = h.createQuery("select id, name from something").map(Something.class).first();
   assertNull(s);
 }
Exemple #20
0
 @Override
 public void tearDown() throws Exception {
   if (h != null) h.close();
   Tools.stop();
 }