Skip to content

Commit

Permalink
Hibernate Entity Manager DataStore Harness (#1156)
Browse files Browse the repository at this point in the history
* Hibernate Entity Manager DataStore Harness

* Reuse fork
  • Loading branch information
wcekan authored and aklish committed Jan 23, 2020
1 parent f7e29a0 commit 601265e
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 5 deletions.
3 changes: 2 additions & 1 deletion elide-datastore/elide-datastore-hibernate3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<failIfNoTests>true</failIfNoTests>
</configuration>
</plugin>
Expand Down
6 changes: 3 additions & 3 deletions elide-datastore/elide-datastore-hibernate5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
</goals>
<configuration>
<systemPropertyVariables>
<dataStoreHarness>com.yahoo.elide.datastores.hibernate5.HibernateDataStoreHarness</dataStoreHarness>
<dataStoreHarness>com.yahoo.elide.datastores.hibernate5.HibernateEntityManagerDataStoreHarness</dataStoreHarness>
</systemPropertyVariables>
</configuration>
</execution>
Expand All @@ -239,8 +239,8 @@
</execution>
</executions>
<configuration>
<forkMode>once</forkMode>
<reuseForks>false</reuseForks>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<trimStackTrace>false</trimStackTrace>
<failIfNoTests>true</failIfNoTests>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.datastores.hibernate5;

import com.yahoo.elide.core.DataStore;
import com.yahoo.elide.core.datastore.test.DataStoreTestHarness;
import com.yahoo.elide.models.generics.Manager;
import com.yahoo.elide.models.triggers.Invoice;
import com.yahoo.elide.utils.ClassScanner;

import example.Filtered;
import example.Parent;
import example.TestCheckMappings;

import org.hibernate.MappingException;
import org.hibernate.ScrollMode;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.Environment;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
* Supplier of Hibernate 5 Data Store.
*/
public class HibernateEntityManagerDataStoreHarness implements DataStoreTestHarness {
private static final String JDBC = "jdbc:h2:mem:root;IGNORECASE=TRUE";
private static final String ROOT = "root";

private DataStore store;
private MetadataImplementor metadataImplementor;

public HibernateEntityManagerDataStoreHarness() {
// Add additional checks to our static check mappings map.
// NOTE: This is a bit hacky. We need to do a major overhaul on our test architecture
TestCheckMappings.MAPPINGS.put("filterCheck", Filtered.FilterCheck.class);
TestCheckMappings.MAPPINGS.put("filterCheck3", Filtered.FilterCheck3.class);

Map<String, Object> options = new HashMap<>();
ArrayList<Class> bindClasses = new ArrayList<>();

try {
bindClasses.addAll(ClassScanner.getAnnotatedClasses(Parent.class.getPackage(), Entity.class));
bindClasses.addAll(ClassScanner.getAnnotatedClasses(Manager.class.getPackage(), Entity.class));
bindClasses.addAll(ClassScanner.getAnnotatedClasses(Invoice.class.getPackage(), Entity.class));
} catch (MappingException e) {
throw new IllegalStateException(e);
}

options.put("javax.persistence.jdbc.driver", "org.h2.Driver");
options.put("javax.persistence.jdbc.url", JDBC);
options.put("javax.persistence.jdbc.user", ROOT);
options.put("javax.persistence.jdbc.password", ROOT);
options.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
options.put(AvailableSettings.LOADED_CLASSES, bindClasses);

EntityManagerFactory emf = Persistence.createEntityManagerFactory("elide-tests", options);
EntityManager em = emf.createEntityManager();

// method to force class initialization
MetadataSources metadataSources = new MetadataSources(
new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml")
.applySetting(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread")
.applySetting(Environment.URL, JDBC)
.applySetting(Environment.USER, ROOT)
.applySetting(Environment.PASS, ROOT)
.applySetting(Environment.DIALECT, "org.hibernate.dialect.H2Dialect")
.build());

try {
ClassScanner.getAnnotatedClasses(Parent.class.getPackage(), Entity.class)
.forEach(metadataSources::addAnnotatedClass);
} catch (MappingException e) {
throw new IllegalStateException(e);
}

metadataImplementor = (MetadataImplementor) metadataSources.buildMetadata();

EnumSet<TargetType> type = EnumSet.of(TargetType.DATABASE);
// create example tables from beans
SchemaExport schemaExport = new SchemaExport();
schemaExport.drop(type, metadataImplementor);
schemaExport.execute(type, SchemaExport.Action.CREATE, metadataImplementor);

if (!schemaExport.getExceptions().isEmpty()) {
throw new IllegalStateException(schemaExport.getExceptions().toString());
}

store = new AbstractHibernateStore.Builder(em)
.withScrollEnabled(true)
.withScrollMode(ScrollMode.FORWARD_ONLY)
.build();
}

private void resetSchema() {
EnumSet<TargetType> type = EnumSet.of(TargetType.DATABASE);
// create example tables from beans
SchemaExport schemaExport = new SchemaExport();
schemaExport.drop(type, metadataImplementor);
schemaExport.execute(type, SchemaExport.Action.CREATE, metadataImplementor);

if (!schemaExport.getExceptions().isEmpty()) {
throw new IllegalStateException(schemaExport.getExceptions().toString());
}
}

@Override
public DataStore getDataStore() {
return store;
}

@Override
public void cleanseTestData() {
resetSchema();
}
}
3 changes: 2 additions & 1 deletion elide-datastore/elide-datastore-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<failIfNoTests>true</failIfNoTests>
</configuration>
</plugin>
Expand Down

0 comments on commit 601265e

Please sign in to comment.