Exemplo n.º 1
0
  @Test
  public void should_set_lazy_field() throws Exception {
    bean = em.find(CompleteBean.class, bean.getId());

    bean.setLabel("newLabel");

    assertThat(bean.getLabel()).isEqualTo("newLabel");
  }
Exemplo n.º 2
0
  @Test
  public void should_initialize_lazy_properties() throws Exception {
    Tweet tweet = new Tweet();
    tweet.setId(UUIDGen.getTimeUUID());
    tweet.setContent("welcome");

    CompleteBean entity =
        CompleteBeanTestBuilder.builder()
            .randomId()
            .name("name")
            .label("label")
            .age(45L)
            .addFriends("foo", "bar")
            .welcomeTweet(tweet)
            .version(CounterBuilder.incr(11L))
            .buid();

    manager.insert(entity);

    CompleteBean foundEntity = manager.find(CompleteBean.class, entity.getId());

    CompleteBean rawEntity = manager.initAndRemoveProxy(foundEntity);

    assertThat(rawEntity.getName()).isEqualTo("name");
    assertThat(rawEntity.getLabel()).isEqualTo("label");
    assertThat(rawEntity.getAge()).isEqualTo(45L);
    assertThat(rawEntity.getFriends()).containsExactly("foo", "bar");
    assertThat(rawEntity.getWelcomeTweet().getContent()).isEqualTo("welcome");
    assertThat(rawEntity.getVersion()).isInstanceOf(InternalCounterImpl.class);
    assertThat(rawEntity.getVersion().get()).isEqualTo(11L);
  }
Exemplo n.º 3
0
  @Test
  public void should_initialize_counter_value() throws Exception {
    CompleteBean entity = CompleteBeanTestBuilder.builder().randomId().name("name").buid();

    entity = manager.insert(entity);

    entity.getVersion().incr(2L);

    manager.update(entity);

    RegularStatement statement = select().from("CompleteBean").where(eq("id", bindMarker()));

    CompleteBean foundEntity =
        manager.typedQuery(CompleteBean.class, statement, entity.getId()).getFirst();

    CompleteBean rawEntity = manager.initAndRemoveProxy(foundEntity);

    assertThat(rawEntity.getVersion()).isInstanceOf(InternalCounterImpl.class);
    assertThat(rawEntity.getVersion().get()).isEqualTo(2L);
  }
Exemplo n.º 4
0
  @Test
  public void should_batch_counters() throws Exception {
    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    CompleteBean entity = CompleteBeanTestBuilder.builder().randomId().name("name").buid();

    entity = batchEm.merge(entity);

    entity.setLabel("label");

    Tweet welcomeTweet = TweetTestBuilder.tweet().randomId().content("welcomeTweet").buid();
    entity.setWelcomeTweet(welcomeTweet);

    entity.getVersion().incr(10L);
    batchEm.merge(entity);

    Row result = session.execute("SELECT label from CompleteBean where id=" + entity.getId()).one();
    assertThat(result).isNull();

    result =
        session
            .execute(
                "SELECT counter_value from achilles_counter_table where fqcn='"
                    + CompleteBean.class.getCanonicalName()
                    + "' and primary_key='"
                    + entity.getId()
                    + "' and property_name='version'")
            .one();
    assertThat(result.getLong("counter_value")).isEqualTo(10L);

    // Flush
    batchEm.endBatch();

    result = session.execute("SELECT label from CompleteBean where id=" + entity.getId()).one();
    assertThat(result.getString("label")).isEqualTo("label");

    result =
        session
            .execute(
                "SELECT counter_value from achilles_counter_table where fqcn='"
                    + CompleteBean.class.getCanonicalName()
                    + "' and primary_key='"
                    + entity.getId()
                    + "' and property_name='version'")
            .one();
    assertThat(result.getLong("counter_value")).isEqualTo(10L);
    assertThatBatchContextHasBeenReset(batchEm);
  }
Exemplo n.º 5
0
  @Test
  public void should_batch_several_entities() throws Exception {
    CompleteBean bean = CompleteBeanTestBuilder.builder().randomId().name("name").buid();
    Tweet tweet1 = TweetTestBuilder.tweet().randomId().content("tweet1").buid();
    Tweet tweet2 = TweetTestBuilder.tweet().randomId().content("tweet2").buid();

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    batchEm.merge(bean);
    batchEm.merge(tweet1);
    batchEm.merge(tweet2);
    batchEm.merge(user);

    CompleteBean foundBean = batchEm.find(CompleteBean.class, bean.getId());
    Tweet foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());
    Tweet foundTweet2 = batchEm.find(Tweet.class, tweet2.getId());
    User foundUser = batchEm.find(User.class, user.getId());

    assertThat(foundBean).isNull();
    assertThat(foundTweet1).isNull();
    assertThat(foundTweet2).isNull();
    assertThat(foundUser).isNull();

    // Flush
    batchEm.endBatch();

    foundBean = batchEm.find(CompleteBean.class, bean.getId());
    foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());
    foundTweet2 = batchEm.find(Tweet.class, tweet2.getId());
    foundUser = batchEm.find(User.class, user.getId());

    assertThat(foundBean.getName()).isEqualTo("name");
    assertThat(foundTweet1.getContent()).isEqualTo("tweet1");
    assertThat(foundTweet2.getContent()).isEqualTo("tweet2");
    assertThat(foundUser.getFirstname()).isEqualTo("fn");
    assertThat(foundUser.getLastname()).isEqualTo("ln");
    assertThatBatchContextHasBeenReset(batchEm);
  }
Exemplo n.º 6
0
  @Test
  public void should_not_load_lazy_fields() throws Exception {
    bean = em.find(CompleteBean.class, bean.getId());

    Factory proxy = (Factory) bean;
    ThriftEntityInterceptor<?> interceptor = (ThriftEntityInterceptor<?>) proxy.getCallback(0);
    CompleteBean trueBean = (CompleteBean) interceptor.getTarget();

    assertThat(trueBean.getLabel()).isNull();
    assertThat(trueBean.getFriends()).isNull();

    // Trigger loading of lazy fields
    assertThat(bean.getLabel()).isEqualTo("label");
    assertThat(bean.getFriends()).containsExactly("foo", "bar");

    assertThat(trueBean.getLabel()).isEqualTo("label");
    assertThat(trueBean.getFriends()).containsExactly("foo", "bar");
  }
Exemplo n.º 7
0
  @Test
  public void should_re_prepare_statements_when_cache_size_exceeded() throws Exception {
    // Given
    CompleteBean bean = builder().id(RandomUtils.nextLong(0, Long.MAX_VALUE)).name("name").buid();

    CompleteBean managed = pm.insert(bean);

    // When
    managed.setAge(10L);
    pm.update(managed);

    managed.setFriends(Arrays.asList("foo", "bar"));
    pm.update(managed);

    managed.setFollowers(Sets.newHashSet("George", "Paul"));
    pm.update(managed);

    managed.setAge(11L);
    pm.update(managed);

    // Then
    CompleteBean found = pm.find(CompleteBean.class, bean.getId());

    assertThat(found.getAge()).isEqualTo(11L);
    assertThat(found.getName()).isEqualTo("name");
    assertThat(found.getFriends()).containsExactly("foo", "bar");
    assertThat(found.getFollowers()).containsOnly("George", "Paul");
  }