PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/org.gatherdata.data.dao.jpa/src/test/java/org/gatherdata/data/dao/jpa/internal/JpaFlatFormDaoTest.java

https://github.com/gatherdata/gather-data
Java | 171 lines | 127 code | 29 blank | 15 comment | 5 complexity | 5da2ec7fcdb4cfadab5803d48d4957c2 MD5 | raw file
  1. /**
  2. * The contents of this file are subject to the AED Public Use License Agreement, Version 1.0 (the "License");
  3. * use in any manner is strictly prohibited except in compliance with the terms of the License.
  4. * The License is available at http://gatherdata.org/license.
  5. *
  6. * Copyright (c) AED. All Rights Reserved
  7. */
  8. package org.gatherdata.data.dao.jpa.internal;
  9. import static org.junit.Assert.assertEquals;
  10. import static org.junit.Assert.assertNotNull;
  11. import static org.junit.Assert.assertTrue;
  12. import static org.gatherdata.commons.junit.ContainsAll.containsAll;
  13. import static org.hamcrest.MatcherAssert.assertThat;
  14. import static org.hamcrest.CoreMatchers.notNullValue;
  15. import static org.hamcrest.CoreMatchers.equalTo;
  16. import static org.hamcrest.CoreMatchers.not;
  17. import java.net.URI;
  18. import java.util.ArrayList;
  19. import java.util.GregorianCalendar;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Random;
  23. import java.util.Set;
  24. import javax.persistence.EntityManager;
  25. import javax.persistence.EntityManagerFactory;
  26. import javax.persistence.EntityTransaction;
  27. import javax.persistence.Persistence;
  28. import javax.persistence.spi.PersistenceProvider;
  29. import org.apache.commons.lang.ObjectUtils;
  30. import org.apache.commons.lang.StringUtils;
  31. import org.gatherdata.data.core.model.FlatForm;
  32. import org.gatherdata.data.core.model.RenderedValue;
  33. import org.gatherdata.data.core.spi.BaseFlatFormDaoTest;
  34. import org.gatherdata.data.core.spi.FlatFormDao;
  35. import org.gatherdata.data.dao.jpa.model.JpaFlatForm;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import com.google.inject.Guice;
  39. import com.google.inject.Inject;
  40. import com.google.inject.Injector;
  41. public class JpaFlatFormDaoTest extends BaseFlatFormDaoTest {
  42. int mockPlainTextCount = 0;
  43. @Inject
  44. private PersistenceProvider persistenceProvider;
  45. private EntityTransaction tx;
  46. @Override
  47. protected FlatFormDao createStorageDaoImpl() {
  48. JpaFlatFormDao dao = new JpaFlatFormDao();
  49. dao.persistenceUnitName = "flatform-test";
  50. // guice up the instance
  51. Injector injector = Guice.createInjector(new JpaTestingModule());
  52. injector.injectMembers(dao);
  53. injector.injectMembers(this);
  54. return dao;
  55. }
  56. @Override
  57. protected void beginTransaction() {
  58. EntityManager em = ((JpaFlatFormDao) dao).getEntityManager();
  59. tx = em.getTransaction();
  60. tx.begin();
  61. }
  62. @Override
  63. protected void endTransaction() {
  64. tx.commit();
  65. }
  66. @Override
  67. protected void rollbackTransaction() {
  68. tx.rollback();
  69. }
  70. /**
  71. * Must have at least one test here to convince Eclipse that
  72. * there are tests to run.
  73. */
  74. @Test
  75. public void shouldRunParentTestsInEclipse() {
  76. assertTrue(true);
  77. }
  78. /**
  79. * Sanity-check for the test.
  80. */
  81. @Test
  82. public void shouldHavePersistenceProviderInjected() {
  83. assertThat(persistenceProvider, notNullValue());
  84. }
  85. @Test
  86. public void shouldProduceUniqueIdsForMockEntities() {
  87. final int EXPECTED_NUMBER_OF_ENTITIES = 10000;
  88. Set<URI> uniqueURI = new HashSet<URI>();
  89. Set<Integer> uniqueHashcodes = new HashSet<Integer>();
  90. Set<FlatForm> uniqueEntities = new HashSet<FlatForm>();
  91. for (int i=0; i<EXPECTED_NUMBER_OF_ENTITIES; i++) {
  92. FlatForm mocked = createMockEntity();
  93. assertTrue("duplicate URI #" + (i+1) + " " + mocked, uniqueURI.add(mocked.getUid()));
  94. assertTrue("duplicate hashcode #" + (i+1) + " " + mocked, uniqueHashcodes.add(mocked.hashCode()));
  95. assertTrue("duplicate entity #" + (i+1) + " " + mocked, uniqueEntities.add(mocked));
  96. }
  97. assertThat(uniqueURI.size(), equalTo(EXPECTED_NUMBER_OF_ENTITIES));
  98. }
  99. @Test
  100. public void shouldGetAllSavedEntities() {
  101. final int EXPECTED_NUMBER_OF_ENTITIES = new Random().nextInt(100);
  102. List<FlatForm> entitiesToSave = new ArrayList<FlatForm>();
  103. beginTransaction();
  104. FlatForm previousForm = null;
  105. FlatForm previousMock = null;
  106. for (int i=0; i< EXPECTED_NUMBER_OF_ENTITIES; i++) {
  107. FlatForm entityToSave = createMockEntity();
  108. entitiesToSave.add(entityToSave);
  109. FlatForm savedEntity = dao.save(entityToSave);
  110. int daoCount = dao.getCount();
  111. if (i<daoCount) {
  112. previousForm = savedEntity;
  113. previousMock = entityToSave;
  114. } else {
  115. System.err.println("Duplicate at #" + (i+1));
  116. printEntity("mocked", entityToSave);
  117. printEntity("previous mocked", previousMock);
  118. printEntity("saved", savedEntity);
  119. printEntity("previous saved", previousForm);
  120. break;
  121. }
  122. }
  123. endTransaction();
  124. assertThat(dao.getCount(), equalTo(EXPECTED_NUMBER_OF_ENTITIES));
  125. beginTransaction();
  126. Iterable<FlatForm> allEntitiesList = (Iterable<FlatForm>) dao.getAll();
  127. assertThat(allEntitiesList, containsAll(entitiesToSave));
  128. endTransaction();
  129. }
  130. private void printEntity(String title, FlatForm entity) {
  131. System.out.println("Entity \"" + title + "\"");
  132. System.out.println(ObjectUtils.toString(entity.getUid()) + "; " +
  133. ObjectUtils.toString(entity.getNamespace()) + "; " +
  134. ObjectUtils.toString(entity.getDateCreated()));
  135. for (RenderedValue rv : entity.getValues()) {
  136. System.out.println("\t" + "; " +
  137. rv.getPath() + "; " +
  138. rv.getTag() + "; " +
  139. rv.getValueAsString());
  140. }
  141. }
  142. }