Esempio n. 1
0
  /**
   * Get or create the list of EntityListeners for an object. If a list is created, it will be kept
   * around and re-used for later persistence operations.
   *
   * @param theObj the object to get EntityLIsteners for
   * @return the list of EntityListeners for the object, or null if they do not exist
   */
  private Collection<Object> getEntityListeners(Object theObj) {
    Collection<Object> aListeners = mManagedEntityListeners.get(theObj);

    if (aListeners == null) {
      EntityListeners aEntityListeners =
          BeanReflectUtil.getAnnotation(theObj.getClass(), EntityListeners.class);

      if (aEntityListeners != null) {
        // if there are entity listeners, lets create them
        aListeners = new HashSet<Object>();
        for (Class<?> aClass : aEntityListeners.value()) {
          try {
            aListeners.add(Empire.get().instance(aClass));
          } catch (Exception e) {
            LOGGER.error("There was an error instantiating an EntityListener. ", e);
          }
        }

        mManagedEntityListeners.put(theObj, aListeners);
      } else {
        aListeners = Collections.emptyList();
      }
    }

    return aListeners;
  }
 @BeforeClass
 public static void beforeClass() {
   Empire.init(
       new DefaultEmpireModule(),
       new OpenRdfEmpireModule(),
       new JenaEmpireModule(),
       new TestModule());
 }
Esempio n. 3
0
 @Test
 public void testNoFactory() {
   assertTrue(
       null
           == Empire.get()
               .persistenceProvider()
               .createEntityManagerFactory("no a factory", Collections.emptyMap()));
 }
Esempio n. 4
0
 @Test
 public void testInvalidFactory() {
   assertTrue(
       null
           == Empire.get()
               .persistenceProvider()
               .createEntityManagerFactory(
                   "no a factory",
                   Collections.singletonMap("factory", "not a factory class name")));
 }
Esempio n. 5
0
 @Test
 public void testNotMutableDataSource() {
   assertTrue(
       null
           == Empire.get()
               .persistenceProvider()
               .createEntityManagerFactory(
                   "not mutable",
                   Collections.singletonMap("factory", TestDataSourceFactory.class.getName())));
 }
  @Test
  public void testPersistCascading() {
    Map<String, String> aMap = new HashMap<String, String>();
    aMap.put(ConfigKeys.FACTORY, MutableTestDataSourceFactory.class.getName());
    aMap.put("files", TestJPA.DATA_FILE);

    EntityManager aManager =
        Empire.get()
            .persistenceProvider()
            .createEntityManagerFactory("test", aMap)
            .createEntityManager();

    // ================= test persist cascade

    LaunchUsingProxy aNewLaunchWithProxy = new LaunchUsingProxy();
    Launch aNewLaunch = new Launch();

    Spacecraft aNewOtherSpacecraft = new Spacecraft();
    aNewOtherSpacecraft.setRdfId(
        new SupportsRdfId.URIKey(
            URI.create("http://empire.clarkparsia.com/test/persist/cascade/aNewOtherSpacecraft")));
    aNewOtherSpacecraft.setAgency("agency");

    aNewLaunchWithProxy.setOtherSpacecraft(aNewOtherSpacecraft);
    aNewLaunch.setOtherSpacecraft(aNewOtherSpacecraft);

    aManager.persist(aNewLaunch);

    assertTrue(aManager.contains(aNewLaunch));

    // it was persisted as a relation...
    assertTrue(
        aManager
            .find(Launch.class, aNewLaunch.getRdfId())
            .getOtherSpacecraft()
            .getRdfId()
            .equals(aNewOtherSpacecraft.getRdfId()));

    // but persist does not cascade
    assertFalse(aManager.contains(aNewOtherSpacecraft));

    aManager.persist(aNewLaunchWithProxy);

    assertTrue(aManager.contains(aNewLaunchWithProxy));

    // it was persisted as a relation...
    assertTrue(
        aManager
            .find(LaunchUsingProxy.class, aNewLaunchWithProxy.getRdfId())
            .getOtherSpacecraft()
            .equals(aNewOtherSpacecraft));

    // and this should be true now because the persist was cascaded
    assertTrue(aManager.contains(aNewOtherSpacecraft));
  }
  @Before
  public void before() {
    Map<String, String> aMap = new HashMap<String, String>();
    aMap.put(ConfigKeys.FACTORY, "test-source");
    aMap.put("files", TestJPA.DATA_FILE);
    aMap.put("use.cache", "false");

    mManager =
        Empire.get()
            .persistenceProvider()
            .createEntityManagerFactory("test", aMap)
            .createEntityManager();
  }