Пример #1
0
  @Test
  public void should_initialize_entity() throws Exception {

    Class<? extends CompleteBean> beanClass = bean.getClass();

    PropertyMeta nameMeta = new PropertyMeta();
    nameMeta.setEntityClassName("beanClass");
    nameMeta.setType(SIMPLE);
    nameMeta.setGetter(beanClass.getMethod("getName"));

    PropertyMeta friendsMeta = new PropertyMeta();
    friendsMeta.setEntityClassName("beanClass");
    friendsMeta.setType(LAZY_LIST);
    friendsMeta.setGetter(beanClass.getMethod("getFriends"));

    PropertyMeta followersMeta = new PropertyMeta();
    followersMeta.setEntityClassName("beanClass");
    followersMeta.setType(LAZY_SET);
    followersMeta.setGetter(beanClass.getMethod("getFollowers"));

    Set<Method> alreadyLoaded = Sets.newHashSet(friendsMeta.getGetter(), nameMeta.getGetter());

    Map<Method, PropertyMeta> getterMetas =
        ImmutableMap.<Method, PropertyMeta>of(
            nameMeta.getGetter(), nameMeta,
            friendsMeta.getGetter(), friendsMeta,
            followersMeta.getGetter(), followersMeta);

    Map<String, PropertyMeta> allMetas =
        ImmutableMap.<String, PropertyMeta>of(
            "name", nameMeta,
            "friends", friendsMeta,
            "followers", followersMeta);

    EntityMeta entityMeta = new EntityMeta();
    entityMeta.setPropertyMetas(allMetas);
    entityMeta.setGetterMetas(getterMetas);

    when(interceptor.getAlreadyLoaded()).thenReturn(alreadyLoaded);

    initializer.initializeEntity(bean, entityMeta, interceptor);

    assertThat(calledMethods).containsExactly("getFollowers");
  }
Пример #2
0
  @Test
  public void should_throw_exception_when_error_initializing() throws Exception {
    CompleteBean bean =
        new CompleteBean() {

          public Long getId() {
            throw new RuntimeException();
          }
        };

    PropertyMeta pm = new PropertyMeta();
    pm.setType(PropertyType.LAZY_SIMPLE);
    pm.setGetter(bean.getClass().getMethod("getId"));

    EntityMeta entityMeta = new EntityMeta();
    entityMeta.setPropertyMetas(ImmutableMap.<String, PropertyMeta>of("id", pm));

    entityMeta.setGetterMetas(ImmutableMap.<Method, PropertyMeta>of(pm.getGetter(), pm));

    when(interceptor.getAlreadyLoaded()).thenReturn(new HashSet<Method>());

    exception.expect(AchillesException.class);
    initializer.initializeEntity(bean, entityMeta, interceptor);
  }