net.lamberto.junit.jpa.GuiceJPAPersistModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guice-jpa-junit-runner Show documentation
Show all versions of guice-jpa-junit-runner Show documentation
A JUnit Runner allowing Guice-based testing with JPA Hibernate4-based. Each test method is running with a clean Injector instance.
package net.lamberto.junit.jpa;
import static java.util.Arrays.asList;
import java.util.Collection;
import java.util.Properties;
import java.util.Set;
import javax.persistence.Entity;
import org.hibernate.jpa.AvailableSettings;
import org.reflections.Reflections;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.inject.AbstractModule;
import com.google.inject.persist.jpa.JpaPersistModule;
import lombok.NoArgsConstructor;
@NoArgsConstructor
public class GuiceJPAPersistModule extends AbstractModule {
private final Collection entitiesPackages = Lists.newArrayList();
public GuiceJPAPersistModule(final Package ... entitiesPackages) {
this(asList(entitiesPackages));
}
public GuiceJPAPersistModule(final Collection entitiesPackages) {
this.entitiesPackages.addAll(entitiesPackages);
}
@Override
public final void configure() {
preConfigure();
final Properties properties = new Properties();
properties.put("hibernate.ejb.entitymanager_factory_name", "EF." + System.currentTimeMillis());
final Set> entitiesClasses = Sets.newHashSet();
for (final Package entitiesPackage : entitiesPackages) {
entitiesClasses.addAll(new Reflections(entitiesPackage.getName()).getTypesAnnotatedWith(Entity.class));
}
if (entitiesPackages.isEmpty()) {
entitiesClasses.addAll(new Reflections("").getTypesAnnotatedWith(Entity.class));
}
properties.put(AvailableSettings.LOADED_CLASSES, Lists.newArrayList(entitiesClasses));
install(new JpaPersistModule("GuicePersistTest").properties(properties));
postConfigure();
}
protected void preConfigure() {
// extension hook
}
protected void postConfigure() {
// extension hook
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy