Ejemplo n.º 1
0
  @Test
  public void testQueryForForeign() throws Exception {
    Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
    Dao<Foreign, Object> foreignDao = createDao(Foreign.class, true);

    Foo foo = new Foo();
    foo.val = 1231;
    assertEquals(1, fooDao.create(foo));

    Foreign foreign = new Foreign();
    foreign.foo = foo;
    assertEquals(1, foreignDao.create(foreign));

    // use the auto-extract method to extract by id
    List<Foreign> results =
        foreignDao.queryBuilder().where().eq(Foreign.FOO_COLUMN_NAME, foo).query();
    assertEquals(1, results.size());
    assertEquals(foreign.id, results.get(0).id);

    // query for the id directly
    List<Foreign> results2 =
        foreignDao.queryBuilder().where().eq(Foreign.FOO_COLUMN_NAME, foo.id).query();
    assertEquals(1, results2.size());
    assertEquals(foreign.id, results2.get(0).id);
  }
  @Test
  public void
      addManySideEntities_ClearCacheAndQueryForAll_TestIfPersistedEntitiesMatchAddedOnesButDoNotEqual()
          throws Exception {
    RelationEntities.LazyOneSide oneSide1 = new RelationEntities.LazyOneSide("One 1");
    oneSideDao.create(oneSide1);

    RelationEntities.LazyOneSide oneSide2 = new RelationEntities.LazyOneSide("One 2");
    oneSideDao.create(oneSide2);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");
    RelationEntities.LazyManySide manySide6 = new RelationEntities.LazyManySide("Many 6");

    oneSide1.getManySides().add(manySide1);
    oneSide1.getManySides().add(manySide2);
    oneSide1.getManySides().add(manySide3);

    oneSide2.getManySides().add(manySide4);
    oneSide2.getManySides().add(manySide5);
    oneSide2.getManySides().add(manySide6);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();
    List<RelationEntities.LazyOneSide> persistedEntities = oneSideDao.queryForAll();

    Assert.assertEquals(2, persistedEntities.size());

    RelationEntities.LazyOneSide persistedOneSide1 = persistedEntities.get(0);
    RelationEntities.LazyOneSide persistedOneSide2 = persistedEntities.get(1);

    // assert ids are the same
    Assert.assertEquals(oneSide1.getId(), persistedOneSide1.getId());
    Assert.assertEquals(oneSide2.getId(), persistedOneSide2.getId());
    // but instances don't equal (otherwise they would have been just taken from cache)
    Assert.assertNotSame(oneSide1, persistedOneSide1);
    Assert.assertNotSame(oneSide2, persistedOneSide2);

    Assert.assertEquals(3, persistedOneSide1.getManySides().size());
    Assert.assertEquals(3, persistedOneSide2.getManySides().size());

    Assert.assertEquals(manySide1.getId(), persistedOneSide1.getManySides().get(0).getId());
    Assert.assertEquals(manySide2.getId(), persistedOneSide1.getManySides().get(1).getId());
    Assert.assertEquals(manySide3.getId(), persistedOneSide1.getManySides().get(2).getId());

    Assert.assertEquals(manySide4.getId(), persistedOneSide2.getManySides().get(0).getId());
    Assert.assertEquals(manySide5.getId(), persistedOneSide2.getManySides().get(1).getId());
    Assert.assertEquals(manySide6.getId(), persistedOneSide2.getManySides().get(2).getId());

    Assert.assertNotSame(manySide1, persistedOneSide1.getManySides().get(0));
    Assert.assertNotSame(manySide2, persistedOneSide1.getManySides().get(1));
    Assert.assertNotSame(manySide3, persistedOneSide1.getManySides().get(2));

    Assert.assertNotSame(manySide4, persistedOneSide2.getManySides().get(0));
    Assert.assertNotSame(manySide5, persistedOneSide2.getManySides().get(1));
    Assert.assertNotSame(manySide6, persistedOneSide2.getManySides().get(2));
  }
Ejemplo n.º 3
0
  @Test
  public void testCountOf() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);

    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
    assertEquals(0, qb.countOf());

    Foo foo1 = new Foo();
    int val = 123213;
    foo1.val = val;
    assertEquals(1, dao.create(foo1));
    assertEquals(1, qb.countOf());

    Foo foo2 = new Foo();
    foo2.val = val;
    assertEquals(1, dao.create(foo2));
    assertEquals(2, qb.countOf());

    String distinct = "DISTINCT(" + Foo.VAL_COLUMN_NAME + ")";
    assertEquals(1, qb.countOf(distinct));

    qb.setCountOf(distinct);
    assertEquals(1, dao.countOf(qb.prepare()));

    distinct = "DISTINCT(" + Foo.ID_COLUMN_NAME + ")";
    assertEquals(2, qb.countOf(distinct));

    qb.setCountOf(distinct);
    assertEquals(2, dao.countOf(qb.prepare()));
  }
Ejemplo n.º 4
0
  @Test
  public void testJoinOrder() throws Exception {
    Dao<Bar, Integer> barDao = createDao(Bar.class, true);
    Dao<Baz, Integer> bazDao = createDao(Baz.class, true);

    Bar bar1 = new Bar();
    bar1.val = 2234;
    assertEquals(1, barDao.create(bar1));
    Bar bar2 = new Bar();
    bar2.val = 324322234;
    assertEquals(1, barDao.create(bar2));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));
    Baz baz2 = new Baz();
    baz2.bar = bar2;
    assertEquals(1, bazDao.create(baz2));

    QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
    barQb.orderBy(Bar.VAL_FIELD, true);
    List<Baz> results = bazDao.queryBuilder().join(barQb).query();
    assertEquals(2, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);
    assertEquals(bar2.id, results.get(1).bar.id);

    // reset the query to change the order direction
    barQb.reset();
    barQb.orderBy(Bar.VAL_FIELD, false);
    results = bazDao.queryBuilder().join(barQb).query();
    assertEquals(2, results.size());
    assertEquals(bar2.id, results.get(0).bar.id);
    assertEquals(bar1.id, results.get(1).bar.id);
  }
Ejemplo n.º 5
0
  @Test
  public void testHavingOrderBy() throws Exception {
    Dao<Foo, Object> fooDao = createDao(Foo.class, true);

    Foo foo1 = new Foo();
    foo1.val = 10;
    assertEquals(1, fooDao.create(foo1));
    Foo foo2 = new Foo();
    foo2.val = 20;
    assertEquals(1, fooDao.create(foo2));
    Foo foo3 = new Foo();
    foo3.val = 30;
    assertEquals(1, fooDao.create(foo3));
    Foo foo4 = new Foo();
    foo4.val = 40;
    assertEquals(1, fooDao.create(foo4));

    QueryBuilder<Foo, Object> qb = fooDao.queryBuilder();
    qb.groupBy(Foo.ID_COLUMN_NAME);
    qb.orderBy(Foo.VAL_COLUMN_NAME, false);
    qb.having("val < " + foo3.val);
    List<Foo> results = qb.query();
    assertEquals(2, results.size());
    assertEquals(foo2.val, results.get(0).val);
    assertEquals(foo1.val, results.get(1).val);
  }
Ejemplo n.º 6
0
  @Test
  public void testMaxJoin() throws Exception {
    Dao<Foo, Object> dao = createDao(Foo.class, true);

    Foo foo1 = new Foo();
    foo1.val = 10;
    assertEquals(1, dao.create(foo1));
    Foo foo2 = new Foo();
    foo2.val = 20;
    assertEquals(1, dao.create(foo2));
    Foo foo3 = new Foo();
    foo3.val = 30;
    assertEquals(1, dao.create(foo3));
    Foo foo4 = new Foo();
    foo4.val = 40;
    assertEquals(1, dao.create(foo4));

    QueryBuilder<Foo, Object> iqb = dao.queryBuilder();
    iqb.selectRaw("max(id)");

    QueryBuilder<Foo, Object> oqb = dao.queryBuilder();
    Foo result = oqb.where().in(Foo.ID_COLUMN_NAME, iqb).queryForFirst();
    assertNotNull(result);
    assertEquals(foo4.id, result.id);
  }
Ejemplo n.º 7
0
  @Test
  public void testLeftJoin() throws Exception {
    Dao<Bar, Integer> barDao = createDao(Bar.class, true);
    Dao<Baz, Integer> bazDao = createDao(Baz.class, true);

    Bar bar1 = new Bar();
    bar1.val = 2234;
    assertEquals(1, barDao.create(bar1));
    Bar bar2 = new Bar();
    bar2.val = 324322234;
    assertEquals(1, barDao.create(bar2));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));
    Baz baz2 = new Baz();
    baz2.bar = bar2;
    assertEquals(1, bazDao.create(baz2));
    Baz baz3 = new Baz();
    // no bar
    assertEquals(1, bazDao.create(baz3));

    QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
    List<Baz> results = bazDao.queryBuilder().query();
    assertEquals(3, results.size());
    results = bazDao.queryBuilder().join(barQb).query();
    assertEquals(2, results.size());

    results = bazDao.queryBuilder().leftJoin(barQb).query();
    assertEquals(3, results.size());

    results = bazDao.queryBuilder().join(barQb, JoinType.LEFT, JoinWhereOperation.AND).query();
    assertEquals(3, results.size());
  }
Ejemplo n.º 8
0
  @Test
  public void testJoinGroup() throws Exception {
    Dao<Bar, Integer> barDao = createDao(Bar.class, true);
    Dao<Baz, Integer> bazDao = createDao(Baz.class, true);

    Bar bar1 = new Bar();
    bar1.val = 2234;
    assertEquals(1, barDao.create(bar1));
    Bar bar2 = new Bar();
    bar2.val = 324322234;
    assertEquals(1, barDao.create(bar2));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));
    Baz baz2 = new Baz();
    baz2.bar = bar2;
    assertEquals(1, bazDao.create(baz2));

    QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
    barQb.where().eq(Bar.VAL_FIELD, bar1.val);
    barQb.groupBy(Bar.ID_FIELD);
    List<Baz> results = bazDao.queryBuilder().query();
    assertEquals(2, results.size());
    results = bazDao.queryBuilder().groupBy(Baz.ID_FIELD).join(barQb).query();
    assertEquals(1, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);
  }
Ejemplo n.º 9
0
  @Test
  public void testPickTheRightJoinReverse() throws Exception {
    Dao<One, Integer> oneDao = createDao(One.class, true);
    Dao<Two, Integer> twoDao = createDao(Two.class, true);

    One one1 = new One();
    one1.val = 2234;
    assertEquals(1, oneDao.create(one1));
    One one2 = new One();
    one2.val = 324322234;
    assertEquals(1, oneDao.create(one2));

    Two two1 = new Two();
    two1.val = 431231232;
    two1.one = one1;
    assertEquals(1, twoDao.create(two1));
    Two two2 = new Two();
    two2.one = one2;
    assertEquals(1, twoDao.create(two2));

    QueryBuilder<Two, Integer> twoQb = twoDao.queryBuilder();
    twoQb.where().eq(Two.VAL_FIELD, two1.val);
    List<One> results = oneDao.queryBuilder().query();
    assertEquals(2, results.size());
    results = oneDao.queryBuilder().join(twoQb).query();
    assertEquals(1, results.size());
    assertEquals(two1.one.id, results.get(0).id);
  }
Ejemplo n.º 10
0
  @Test
  public void testQueryRawColumns() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);
    Foo foo1 = new Foo();
    foo1.val = 1;
    foo1.equal = 10;
    assertEquals(1, dao.create(foo1));
    Foo foo2 = new Foo();
    foo2.val = 10;
    foo2.equal = 5;
    assertEquals(1, dao.create(foo2));
    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
    qb.selectRaw("COUNT(*)");
    GenericRawResults<String[]> rawResults = dao.queryRaw(qb.prepareStatementString());
    List<String[]> results = rawResults.getResults();
    assertEquals(1, results.size());
    // 2 rows inserted
    assertEquals("2", results.get(0)[0]);

    qb = dao.queryBuilder();
    qb.selectRaw("MIN(val)", "MAX(val)");
    rawResults = dao.queryRaw(qb.prepareStatementString());
    results = rawResults.getResults();
    assertEquals(1, results.size());
    String[] result = results.get(0);
    assertEquals(2, result.length);
    // foo1 has the maximum value
    assertEquals(Integer.toString(foo1.val), result[0]);
    // foo2 has the maximum value
    assertEquals(Integer.toString(foo2.val), result[1]);
  }
Ejemplo n.º 11
0
  @Test
  public void testHaving() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);

    Foo foo = new Foo();
    int val1 = 243342;
    foo.val = val1;
    assertEquals(1, dao.create(foo));
    foo = new Foo();
    foo.val = val1;
    assertEquals(1, dao.create(foo));
    foo = new Foo();
    // only one of these
    int val2 = 6543;
    foo.val = val2;
    assertEquals(1, dao.create(foo));

    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
    qb.selectColumns(Foo.VAL_COLUMN_NAME);
    qb.groupBy(Foo.VAL_COLUMN_NAME);
    qb.having("COUNT(VAL) > 1");
    GenericRawResults<String[]> results = dao.queryRaw(qb.prepareStatementString());
    List<String[]> list = results.getResults();
    // only val2 has 2 of them
    assertEquals(1, list.size());
    assertEquals(String.valueOf(val1), list.get(0)[0]);

    qb.having("COUNT(VAL) > 2");
    results = dao.queryRaw(qb.prepareStatementString());
    list = results.getResults();
    assertEquals(0, list.size());
  }
Ejemplo n.º 12
0
 @Test
 public void testOrderByRawArg() throws Exception {
   Dao<Foo, Integer> dao = createDao(Foo.class, true);
   Foo foo1 = new Foo();
   foo1.val = 1;
   assertEquals(1, dao.create(foo1));
   Foo foo2 = new Foo();
   foo2.val = 2;
   assertEquals(1, dao.create(foo2));
   List<Foo> results =
       dao.queryBuilder()
           .orderByRaw(
               "(" + Foo.VAL_COLUMN_NAME + " = ? ) DESC", new SelectArg(SqlType.INTEGER, 2))
           .query();
   assertEquals(2, results.size());
   assertEquals(foo2.id, results.get(0).id);
   assertEquals(foo1.id, results.get(1).id);
   results =
       dao.queryBuilder()
           .orderByRaw("(" + Foo.VAL_COLUMN_NAME + " = ? )", new SelectArg(SqlType.INTEGER, 2))
           .query();
   assertEquals(2, results.size());
   assertEquals(foo1.id, results.get(0).id);
   assertEquals(foo2.id, results.get(1).id);
 }
Ejemplo n.º 13
0
  @Test
  public void testBaseClassComparison() throws Exception {
    Dao<Bar, String> barDao = createDao(Bar.class, true);
    Dao<Baz, String> bazDao = createDao(Baz.class, true);

    BarSuperClass bar1 = new BarSuperClass();
    bar1.val = 10;
    assertEquals(1, barDao.create(bar1));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));

    List<Baz> results = bazDao.queryBuilder().where().eq(Baz.BAR_FIELD, bar1).query();
    assertEquals(1, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);

    try {
      // we allow a super class of the field but _not_ a sub class
      results = bazDao.queryBuilder().where().eq(Baz.BAR_FIELD, new Object()).query();
      fail("Should have thrown");
    } catch (SQLException e) {
      // expected
    }
  }
  @Test
  public void
      addManySideEntities_ClearCacheQueryForAllAndAddSomeMoreEntities_TestIfLatestAddedEntitiesGetPersisted()
          throws Exception {
    RelationEntities.LazyOneSide oneSide1 = new RelationEntities.LazyOneSide("One 1");
    oneSideDao.create(oneSide1);

    RelationEntities.LazyOneSide oneSide2 = new RelationEntities.LazyOneSide("One 2");
    oneSideDao.create(oneSide2);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide1.getManySides().add(manySide1);
    oneSide1.getManySides().add(manySide2);
    oneSide1.getManySides().add(manySide3);
    oneSide1.getManySides().add(manySide4);
    oneSide2.getManySides().add(manySide2);
    oneSide2.getManySides().add(manySide3);
    oneSide2.getManySides().add(manySide4);
    oneSide2.getManySides().add(manySide5);

    manySideDao.clearObjectCache();
    List<RelationEntities.LazyOneSide> persistedEntities = oneSideDao.queryForAll();

    RelationEntities.LazyOneSide oneSide3 = new RelationEntities.LazyOneSide("One 3");
    RelationEntities.LazyManySide manySide6 = new RelationEntities.LazyManySide("Many 6");
    RelationEntities.LazyManySide manySide7 = new RelationEntities.LazyManySide("Many 7");

    oneSide1.getManySides().add(manySide6);
    Assert.assertNotNull(manySide6.getId());
    Assert.assertEquals(oneSide1, manySide6.getOneSide());
    Assert.assertEquals(5, oneSide1.getManySides().size());

    oneSide1.getManySides().add(manySide7);
    Long manySide7Id = manySide7.getId();
    oneSide2.getManySides().add(manySide7);
    Assert.assertEquals(manySide7Id, manySide7.getId());
    Assert.assertEquals(6, oneSide1.getManySides().size());
    Assert.assertEquals(5, oneSide2.getManySides().size());

    oneSide3.getManySides().add(manySide6);
    oneSide3.getManySides().add(manySide7);

    oneSideDao.create(oneSide3);
    Assert.assertNotNull(oneSide3.getId());
    Assert.assertEquals(manySide7Id, manySide7.getId());
    Assert.assertEquals(2, oneSide3.getManySides().size());
  }
Ejemplo n.º 15
0
 public void createCompany(CompanyDTO dto) throws Exception {
   validate(dto);
   Boolean exists = dao.idExists(dto.id);
   if (exists) {
     throw new RequestException("Company with id %s already exists", dto.id);
   }
   Company company = JsonUtil.fromJson(dto.toJson(), Company.class);
   company.setCreatedAt();
   dao.create(company);
   for (BeneficiaryDTO beneDTO : dto.beneficiaries) {
     beneDao.create(new Beneficiary(beneDTO.name, company));
   }
 }
Ejemplo n.º 16
0
 @Test
 public void testShortCuts() throws Exception {
   Dao<Foo, Integer> dao = createDao(Foo.class, true);
   Foo foo1 = new Foo();
   assertEquals(1, dao.create(foo1));
   Foo foo2 = new Foo();
   assertEquals(1, dao.create(foo2));
   List<Foo> results = dao.queryBuilder().where().eq(Foo.ID_COLUMN_NAME, foo2.id).query();
   assertEquals(1, results.size());
   assertEquals(foo2.id, results.get(0).id);
   Iterator<Foo> iterator = dao.queryBuilder().where().eq(Foo.ID_COLUMN_NAME, foo2.id).iterator();
   assertTrue(iterator.hasNext());
   assertEquals(foo2.id, iterator.next().id);
   assertFalse(iterator.hasNext());
 }
Ejemplo n.º 17
0
  @Override
  public List<Lecture> save(List<Lecture> lectures) {
    try {
      Dao<Lecture, Integer> lDao = getDao();
      Dao<Speaker, Integer> sDao = getSpeakerDao();
      Dao<LectureSpeaker, Integer> lsDao = getLectureSpeakerDao();
      Dao<Category, Integer> cDao = getCategoryDao();
      Dao<Place, Integer> pDao = getPlaceDao();

      for (Lecture lecture : lectures) {
        pDao.createIfNotExists(lecture.getPlace());
        cDao.createIfNotExists(lecture.getCategory());

        lecture.setUserFavorite(false);
        lDao.createIfNotExists(lecture);
        for (Speaker s1 : lecture.getSpeakers()) {
          Speaker s2 = sDao.createIfNotExists(s1);
          lsDao.create(new LectureSpeaker(lecture, s2));
        }
      }
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    return lectures;
  }
Ejemplo n.º 18
0
 @Test
 public void testBooleanObj() throws Exception {
   Class<LocalBooleanChar> clazz = LocalBooleanChar.class;
   Dao<LocalBooleanChar, Object> dao = createDao(clazz, true);
   boolean val = true;
   String valStr = Boolean.toString(val);
   LocalBooleanChar foo = new LocalBooleanChar();
   foo.bool = val;
   assertEquals(1, dao.create(foo));
   testType(
       dao,
       foo,
       clazz,
       val,
       '1',
       '1',
       valStr,
       DataType.BOOLEAN_CHAR,
       BOOLEAN_COLUMN,
       false,
       false,
       false,
       true,
       false,
       false,
       true,
       false);
 }
Ejemplo n.º 19
0
 /**
  * 增加一条已发送的短信
  *
  * @param sendMsg
  */
 public void addSendMsg(SendMsg sendMsg) {
   try {
     sendMsgDaoOpe.create(sendMsg);
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 20
0
 public void insert(CarInfo carInfo) {
   try {
     carInfoDao.create(carInfo);
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 21
0
  @Test
  public void testInsertLotsOfEvents() throws ClassNotFoundException, SQLException {
    Class.forName("org.h2.Driver");
    String databaseUrl = "jdbc:h2:res\\test\\db\\orm-event-test";
    ConnectionSource cs = new JdbcConnectionSource(databaseUrl);

    initDatabase(cs);

    Dao<Event, Integer> eventDao = DaoManager.createDao(cs, Event.class);
    Dao<EventArtist, Integer> eventArtistDao = DaoManager.createDao(cs, EventArtist.class);

    List<Event> events = getEvents();
    Set<String> artists = new HashSet<String>();

    for (Event event : events) {
      eventDao.create(event);
      /*
      for (Artist artist : event.getArtists()) {
          artistDao.createIfNotExists(artist);
          eventArtistDao.create(new EventArtist(event, artist));
          artists.add(artist.getName());
      }
      */
    }

    assertTrue(eventDao.countOf() == events.size());
    assertTrue(eventArtistDao.countOf() >= artists.size());
  }
Ejemplo n.º 22
0
  @Test
  public void testTableCreationAndPersist() throws SQLException, ClassNotFoundException {
    Class.forName("org.h2.Driver");
    String databaseUrl = "jdbc:h2:res\\test\\db\\ormLiteDb";
    ConnectionSource cs = new JdbcConnectionSource(databaseUrl);

    TableUtils.createTableIfNotExists(cs, Event.class);
    TableUtils.clearTable(cs, Event.class);

    int count = 10;
    String namePrefix = "Testinimi";
    Dao<Event, Integer> eventDao = DaoManager.createDao(cs, Event.class);

    for (int i = 0; i < count; i++) {
      Event event = new Event();
      event.setTitle(namePrefix + i);
      eventDao.create(event);
    }

    assertTrue(eventDao.countOf() == count);
    for (Iterator<Event> it = eventDao.iterator(); it.hasNext(); ) {
      Event event = it.next();
      assertTrue(event.getTitle().startsWith(namePrefix));
    }
    ;

    cs.close();
  }
Ejemplo n.º 23
0
  public DownloadListOfTipSegResult downloadTipSeg() {

    ImageServices _service = new ImageServices();
    SoapServices service = new SoapServices(this.context);
    DownloadListOfTipSegResult result = service.getTipSeg();

    if (result.isSuccess()) {
      try {
        if (result.getData().size() > 0) {
          Dao<TipSeg, Integer> asDao = getHelper().getTipSegDao();
          TableUtils.clearTable(asDao.getConnectionSource(), TipSeg.class);

          for (TipSeg as : result.getData()) {
            // _service.saveImage(this.context, as.getImagen(),
            // _service.downloadImage(as.getImagen()));
            asDao.create(as);
          }
        } else {
          result.setErrorMessage("No hay tipos de seguros registrados.");
          result.setUpdateMessage(result.getErrorMessage());
          result.setSuccess(false);
        }

        return result;
      } catch (SQLException e) {
        e.printStackTrace();
        result.setData(null);
        result.setErrorMessage(e.getMessage());
        result.setSuccess(false);
        return result;
      }
    }
    return result;
  }
  @Test
  public void addOneSideEntity_toGenericArray_TestIfInstancesMatch() throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);
    oneSide.getManySides().add(manySide4);
    oneSide.getManySides().add(manySide5);

    RelationEntities.LazyManySide[] array =
        new RelationEntities.LazyManySide[oneSide.getManySides().size()];
    array = oneSide.getManySides().toArray(array);

    Assert.assertEquals(oneSide.getManySides().size(), array.length);
    for (int i = 0; i < array.length; i++) {
      Assert.assertSame(array[i], oneSide.getManySides().get(i));
    }
  }
  @Test
  public void addOneSideEntity_ClearCacheQueryForAllThenGetEntity_EntityGottenIsNowCached()
      throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);
    oneSide.getManySides().add(manySide4);
    oneSide.getManySides().add(manySide5);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());
    OpenLazyLoadingEntitiesCollection persistedManySides =
        (OpenLazyLoadingEntitiesCollection) persistedOneSide.getManySides();
    RelationEntities.LazyManySide anyEntity = persistedOneSide.getManySides().get(0);

    Assert.assertEquals(1, persistedManySides.getCachedEntities().size());
    Assert.assertTrue(persistedManySides.getCachedEntities().containsValue(anyEntity));
  }
Ejemplo n.º 26
0
 /**
  * ProductVOBean
  *
  * @param productVOBean
  */
 public void add(ProductVOBean productVOBean) {
   try {
     dao.create(productVOBean);
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 27
0
 public void updateCompany(CompanyDTO dto) throws Exception {
   Company company = getCompany(dto.id);
   validate(dto);
   List<String> beneficiaryIds =
       company
           .getBeneficiaries()
           .stream()
           .map(beneficiary -> beneficiary.getId().toString())
           .collect(Collectors.toList());
   for (BeneficiaryDTO beneDTO : dto.beneficiaries) {
     Beneficiary bene = new Beneficiary(beneDTO.name, company);
     Integer id = beneDTO.id;
     if (id != null) {
       bene.setId(id);
       String idString = id.toString();
       if (!beneDao.idExists(idString)) {
         throw new RequestException("beneficiary with id %s does not exist", id);
       }
       beneDao.update(bene);
       beneficiaryIds.remove(idString);
     } else {
       beneDao.create(bene);
     }
   }
   if (beneficiaryIds.size() > 0) {
     beneDao.deleteIds(beneficiaryIds);
   }
   company = JsonUtil.fromJson(dto.toJson(), Company.class);
   dao.update(company);
 }
Ejemplo n.º 28
0
  @Test
  public void testUtf8() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);

    Foo foo = new Foo();
    foo.stringField = "اعصاب";
    assertEquals(1, dao.create(foo));

    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();

    List<Foo> results =
        qb.where().like(Foo.STRING_COLUMN_NAME, '%' + foo.stringField + '%').query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(foo.id, results.get(0).id);
    assertEquals(foo.stringField, results.get(0).stringField);

    qb.reset();
    results =
        qb.where().like(Foo.STRING_COLUMN_NAME, new SelectArg('%' + foo.stringField + '%')).query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(foo.id, results.get(0).id);
    assertEquals(foo.stringField, results.get(0).stringField);
  }
  @Test
  public void addOneSideEntity_ClearCache_TestForPersistedEntitiesIfCachingIsWorking()
      throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());

    int iteration = 0;
    while (iteration < 5) { // test if also after 5 iterations objects are the same
      RelationEntities.LazyOneSide newlyRetrievedOneSide = oneSideDao.queryForId(oneSide.getId());
      Assert.assertSame(persistedOneSide, newlyRetrievedOneSide);

      Assert.assertSame(
          persistedOneSide.getManySides().get(0), manySideDao.queryForId(manySide1.getId()));
      Assert.assertSame(
          persistedOneSide.getManySides().get(1), manySideDao.queryForId(manySide2.getId()));
      Assert.assertSame(
          persistedOneSide.getManySides().get(2), manySideDao.queryForId(manySide3.getId()));

      iteration++;
    }
  }
  @Test
  public void
      addOneSideEntity_ClearCacheQueryForAll_LazyCollectionRetrievedIdsButEntitiesNotCachedYet()
          throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);
    oneSide.getManySides().add(manySide4);
    oneSide.getManySides().add(manySide5);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());
    OpenLazyLoadingEntitiesCollection persistedManySides =
        (OpenLazyLoadingEntitiesCollection) persistedOneSide.getManySides();

    Assert.assertEquals(
        oneSide.getManySides().size(), persistedManySides.getRetrievedIndices().size());
    Assert.assertEquals(0, persistedManySides.getCachedEntities().size());
  }