Skip to content
Snippets Groups Projects
Commit fe364cf9 authored by Markus Löwe's avatar Markus Löwe
Browse files

Improve handling of created EntityManagerFactory instances

parent b1f57b85
No related branches found
No related tags found
1 merge request!10Improve implementation and tests
...@@ -7,6 +7,9 @@ import java.util.List; ...@@ -7,6 +7,9 @@ import java.util.List;
*/ */
public abstract class Lab01Data { public abstract class Lab01Data {
/**
* Can be overridden to perform additional initialization tasks.
*/
public void init() { public void init() {
} }
......
package de.hda.fbi.db2.api; package de.hda.fbi.db2.api;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
/** /**
* API Class for lab02 Created by l.koehler on 05.08.2019. * API Class for lab02 Created by l.koehler on 05.08.2019.
*/ */
public abstract class Lab02EntityManager { public abstract class Lab02EntityManager {
public void init() { /**
} * Creates the {@link EntityManagerFactory} and stores it in a field. {@link #destroy()} should
* then clean up the factory later again.
*/
public abstract void init();
/**
* {@linkplain EntityManagerFactory#close() Closes} the {@code EntityManagerFactory} created by
* {@link #init()}.
*/
public abstract void destroy();
/** /**
* You can use the data from lab01, this variable will be automatically set. * You can use the data from lab01, this variable will be automatically set.
...@@ -25,14 +35,14 @@ public abstract class Lab02EntityManager { ...@@ -25,14 +35,14 @@ public abstract class Lab02EntityManager {
} }
/** /**
* There you have to persist the data in the database. * Here you have to persist the data in the database.
*/ */
public abstract void persistData(); public abstract void persistData();
/** /**
* Return a valid EntityManager. * Creates a new {@code EntityManager}.
* *
* @return EntityManager * @return new EntityManager
*/ */
public abstract EntityManager getEntityManager(); public abstract EntityManager getEntityManager();
} }
...@@ -8,6 +8,9 @@ import java.util.List; ...@@ -8,6 +8,9 @@ import java.util.List;
*/ */
public abstract class Lab03Game { public abstract class Lab03Game {
/**
* Can be overridden to perform additional initialization tasks.
*/
public void init() { public void init() {
} }
......
...@@ -7,6 +7,9 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; ...@@ -7,6 +7,9 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
*/ */
public abstract class Lab04MassData { public abstract class Lab04MassData {
/**
* Can be overridden to perform additional initialization tasks.
*/
public void init() { public void init() {
} }
......
...@@ -14,6 +14,7 @@ import java.net.URL; ...@@ -14,6 +14,7 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager;
/** /**
* Controller Created by l.koehler on 05.08.2019. * Controller Created by l.koehler on 05.08.2019.
...@@ -168,12 +169,31 @@ public class Controller { ...@@ -168,12 +169,31 @@ public class Controller {
return classes; return classes;
} }
private boolean isMissingLabImplementation(int highestLabNumber) {
if (highestLabNumber >= 1 && lab01Data == null) {
System.err.println("Could not find Lab01Data implementation");
return true;
}
if (highestLabNumber >= 2 && lab02EntityManager == null) {
System.err.println("Could not find Lab02EntityManager implementation");
return true;
}
if (highestLabNumber >= 3 && lab03Game == null) {
System.err.println("Could not find Lab03Game implementation");
return true;
}
if (highestLabNumber >= 4 && lab04MassData == null) {
System.err.println("Could not find Lab04MassData implementation");
return true;
}
return false;
}
/** /**
* reads the csv data. * Reads the CSV data.
*/ */
public void readCsv() { public void readCsv() {
if (lab01Data == null) { if (isMissingLabImplementation(1)) {
System.err.println("Could not find Lab01Data Implementation");
return; return;
} }
try { try {
...@@ -208,21 +228,10 @@ public class Controller { ...@@ -208,21 +228,10 @@ public class Controller {
} }
/** /**
* starts the main menu. * Starts the main menu.
*/ */
public void startMenu() { public void startMenu() {
if (lab01Data == null) { if (isMissingLabImplementation(3)) {
System.err.println("Could not find Lab01Data Implementation");
return;
}
if (lab02EntityManager == null) {
System.err.println("Could not find Lab02EntityManager Implementation");
return;
}
if (lab03Game == null) {
System.err.println("Could not find Lab03Game Implementation");
return; return;
} }
...@@ -233,22 +242,19 @@ public class Controller { ...@@ -233,22 +242,19 @@ public class Controller {
} }
/** /**
* persist data. * Persists data.
*/ */
public void persistData() { public void persistData() {
if (lab01Data == null) { if (isMissingLabImplementation(2)) {
System.err.println("Could not find Lab01Data Implementation");
return; return;
} }
if (lab02EntityManager == null) { EntityManager entityManager = lab02EntityManager.getEntityManager();
System.err.println("Could not find Lab02EntityManager Implementation"); String schemaGeneration = (String) entityManager.getProperties()
return;
}
String schemaGeneration = (String) lab02EntityManager.getEntityManager().getProperties()
.get("javax.persistence.schema-generation.database.action"); .get("javax.persistence.schema-generation.database.action");
entityManager.close();
if (schemaGeneration.equals("drop-and-create") || schemaGeneration.equals("create")) { if (schemaGeneration.equals("drop-and-create") || schemaGeneration.equals("create")) {
lab02EntityManager.persistData(); lab02EntityManager.persistData();
} }
...@@ -264,26 +270,10 @@ public class Controller { ...@@ -264,26 +270,10 @@ public class Controller {
} }
/** /**
* creates massdata. * Creates mass data.
*/ */
public void createMassData() { public void createMassData() {
if (lab01Data == null) { if (isMissingLabImplementation(4)) {
System.err.println("Could not find Lab01Data Implementation");
return;
}
if (lab02EntityManager == null) {
System.err.println("Could not find Lab02EntityManager Implementation");
return;
}
if (lab03Game == null) {
System.err.println("Could not find Lab03Game Implementation");
return;
}
if (lab04MassData == null) {
System.err.println("Could not find Lab04MassData Implementation");
return; return;
} }
......
...@@ -2,6 +2,8 @@ package de.hda.fbi.db2.test; ...@@ -2,6 +2,8 @@ package de.hda.fbi.db2.test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
...@@ -11,6 +13,7 @@ import de.hda.fbi.db2.api.Lab02EntityManager; ...@@ -11,6 +13,7 @@ import de.hda.fbi.db2.api.Lab02EntityManager;
import de.hda.fbi.db2.controller.Controller; import de.hda.fbi.db2.controller.Controller;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.Attribute; import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel; import javax.persistence.metamodel.Metamodel;
...@@ -23,6 +26,7 @@ import org.eclipse.persistence.mappings.AggregateCollectionMapping; ...@@ -23,6 +26,7 @@ import org.eclipse.persistence.mappings.AggregateCollectionMapping;
import org.eclipse.persistence.mappings.DatabaseMapping; import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.DirectCollectionMapping; import org.eclipse.persistence.mappings.DirectCollectionMapping;
import org.eclipse.persistence.mappings.DirectMapMapping; import org.eclipse.persistence.mappings.DirectMapMapping;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -41,6 +45,8 @@ import org.junit.jupiter.api.condition.DisabledIfSystemProperty; ...@@ -41,6 +45,8 @@ import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
@TestMethodOrder(MethodOrderer.MethodName.class) @TestMethodOrder(MethodOrderer.MethodName.class)
class Lab02Test { class Lab02Test {
private static EntityManager entityManager;
private static Metamodel metaData; private static Metamodel metaData;
private static EntityType<?> categoryEntity; private static EntityType<?> categoryEntity;
...@@ -52,22 +58,6 @@ class Lab02Test { ...@@ -52,22 +58,6 @@ class Lab02Test {
private static Controller controller; private static Controller controller;
private static synchronized void setMetaData(Metamodel metaData) {
Lab02Test.metaData = metaData;
}
private static synchronized void setCategoryEntity(EntityType<?> categoryEntity) {
Lab02Test.categoryEntity = categoryEntity;
}
private static synchronized void setQuestionEntity(EntityType<?> questionEntity) {
Lab02Test.questionEntity = questionEntity;
}
private static synchronized void setAnswerEntity(EntityType<?> answerEntity) {
Lab02Test.answerEntity = answerEntity;
}
/** /**
* Lab02Test init. * Lab02Test init.
*/ */
...@@ -93,14 +83,30 @@ class Lab02Test { ...@@ -93,14 +83,30 @@ class Lab02Test {
} }
} }
@AfterAll
static void cleanUp() {
if (entityManager != null) {
entityManager.close();
}
}
@SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
@Test @Test
void test1EntityManager() { void test1EntityManager() {
try { try {
if (controller.getLab02EntityManager().getEntityManager() == null) { Lab02EntityManager lab02EntityManager = controller.getLab02EntityManager();
fail("Lab02EntityManager.getEntityManager() returns null");
} entityManager = lab02EntityManager.getEntityManager();
setMetaData(controller.getLab02EntityManager().getEntityManager().getMetamodel()); assertNotNull(entityManager, "Lab02EntityManager.getEntityManager() returns null");
assertTrue(entityManager.isOpen(), "EntityManager should be open");
// Should create new instances; should not always return the same instance
EntityManager otherEntityManager = lab02EntityManager.getEntityManager();
assertNotSame(entityManager, otherEntityManager,
"Lab02EntityManager.getEntityManager() should create new instances");
otherEntityManager.close();
metaData = entityManager.getMetamodel();
} catch (Exception e) { } catch (Exception e) {
fail("Exception during entityManager creation", e); fail("Exception during entityManager creation", e);
} }
...@@ -115,7 +121,7 @@ class Lab02Test { ...@@ -115,7 +121,7 @@ class Lab02Test {
for (EntityType<?> current : metaData.getEntities()) { for (EntityType<?> current : metaData.getEntities()) {
if (current.getName().equalsIgnoreCase("category")) { if (current.getName().equalsIgnoreCase("category")) {
setCategoryEntity(current); categoryEntity = current;
List<?> categories = controller.getLab01Data().getCategories(); List<?> categories = controller.getLab01Data().getCategories();
if (categories != null && !categories.isEmpty()) { if (categories != null && !categories.isEmpty()) {
...@@ -139,7 +145,7 @@ class Lab02Test { ...@@ -139,7 +145,7 @@ class Lab02Test {
for (EntityType<?> current : metaData.getEntities()) { for (EntityType<?> current : metaData.getEntities()) {
if (current.getName().equalsIgnoreCase("question")) { if (current.getName().equalsIgnoreCase("question")) {
setQuestionEntity(current); questionEntity = current;
List<?> questions = controller.getLab01Data().getQuestions(); List<?> questions = controller.getLab01Data().getQuestions();
if (questions != null && !questions.isEmpty()) { if (questions != null && !questions.isEmpty()) {
......
...@@ -12,8 +12,10 @@ import de.hda.fbi.db2.controller.Controller; ...@@ -12,8 +12,10 @@ import de.hda.fbi.db2.controller.Controller;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel; import javax.persistence.metamodel.Metamodel;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -32,16 +34,14 @@ import org.junit.jupiter.api.condition.DisabledIfSystemProperty; ...@@ -32,16 +34,14 @@ import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
@TestMethodOrder(MethodOrderer.MethodName.class) @TestMethodOrder(MethodOrderer.MethodName.class)
class Lab03Test { class Lab03Test {
private static EntityManager entityManager;
private static Metamodel metaData; private static Metamodel metaData;
private static Controller controller; private static Controller controller;
private static EntityType<?> gameEntity; private static EntityType<?> gameEntity;
private static synchronized void setGameEntity(EntityType<?> gameEntity) {
Lab03Test.gameEntity = gameEntity;
}
/** /**
* Lab03Test init. * Lab03Test init.
*/ */
...@@ -67,12 +67,18 @@ class Lab03Test { ...@@ -67,12 +67,18 @@ class Lab03Test {
} }
try { try {
if (controller.getLab02EntityManager().getEntityManager() == null) { entityManager = controller.getLab02EntityManager().getEntityManager();
fail("Lab02EntityManager.getEntityManager() returns null"); assertNotNull(entityManager, "Lab02EntityManager.getEntityManager() returns null");
} metaData = entityManager.getMetamodel();
metaData = controller.getLab02EntityManager().getEntityManager().getMetamodel();
} catch (Exception e) { } catch (Exception e) {
fail("Exception during entityManager creation"); fail("Exception during entityManager creation", e);
}
}
@AfterAll
static void cleanUp() {
if (entityManager != null) {
entityManager.close();
} }
} }
...@@ -128,7 +134,7 @@ class Lab03Test { ...@@ -128,7 +134,7 @@ class Lab03Test {
for (EntityType<?> classes : metaData.getEntities()) { for (EntityType<?> classes : metaData.getEntities()) {
if (classes.getJavaType() == game.getClass()) { if (classes.getJavaType() == game.getClass()) {
gameFound = true; gameFound = true;
setGameEntity(classes); gameEntity = classes;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment