From cbe2b198fda33f2a2084b42328a3241bb1553dd4 Mon Sep 17 00:00:00 2001 From: jensp Date: Wed, 10 Jun 2015 18:14:58 +0000 Subject: [PATCH] CCM NG: Current status, including basic repository class and some more tests git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3476 8810af33-2d31-482b-a856-94f89814c4df --- ccm-core/pom.xml | 10 +- .../core/AbstractEntityRepository.java | 5 +- .../libreccm/core/CcmObjectRepository.java | 43 ++++++++ .../org/libreccm/notification/QueueItem.java | 2 +- ccm-core/src/site/site.xml | 2 +- .../core/CcmObjectRepositoryTest.java | 67 +++++++++++- .../libreccm/core/DatasetsExampleTest.java | 101 ++++++++++++++++++ .../java/org/libreccm/core/DatasetsTest.java | 72 +++++++++++++ .../libreccm/testutils/DatasetsVerifier.java | 79 ++++++++++++++ pom.xml | 35 +++--- src/site/site.xml | 2 +- 11 files changed, 395 insertions(+), 23 deletions(-) create mode 100644 ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java create mode 100644 ccm-core/src/test/java/org/libreccm/core/DatasetsExampleTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/core/DatasetsTest.java create mode 100644 ccm-core/src/test/java/org/libreccm/testutils/DatasetsVerifier.java diff --git a/ccm-core/pom.xml b/ccm-core/pom.xml index f181ebc71..dc6559e85 100644 --- a/ccm-core/pom.xml +++ b/ccm-core/pom.xml @@ -102,6 +102,11 @@ ccm-core + + + ${project.build.directory}/generated-resources + + org.apache.maven.plugins @@ -408,7 +413,6 @@ de.jpdigital hibernate4-ddl-maven-plugin - 1.0.0-alpha.1 h2 @@ -447,6 +451,9 @@ org.libreccm.tests.categories.UnitTests, org.libreccm.tests.categories.IntegrationTest + + ${project.build.directory}/generated-resources + @@ -494,7 +501,6 @@ de.jpdigital hibernate4-ddl-maven-plugin - 1.0.0-alpha.1 h2 diff --git a/ccm-core/src/main/java/org/libreccm/core/AbstractEntityRepository.java b/ccm-core/src/main/java/org/libreccm/core/AbstractEntityRepository.java index d14fd1565..18c4790c7 100644 --- a/ccm-core/src/main/java/org/libreccm/core/AbstractEntityRepository.java +++ b/ccm-core/src/main/java/org/libreccm/core/AbstractEntityRepository.java @@ -31,7 +31,7 @@ public abstract class AbstractEntityRepository { public abstract Class getEntityClass(); - public E findById(final K entityId) { + public E findById(final K entityId) { return entityManager.find(getEntityClass(), entityId); } @@ -64,7 +64,8 @@ public abstract class AbstractEntityRepository { if (entity == null) { throw new IllegalArgumentException("Can't delete a null entity."); } - + entityManager.remove(entity); } + } diff --git a/ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java b/ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java new file mode 100644 index 000000000..495c2f423 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/core/CcmObjectRepository.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core; + + +import javax.enterprise.context.RequestScoped; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class CcmObjectRepository extends AbstractEntityRepository { + + @Override + public Class getEntityClass() { + return CcmObject.class; + } + + @Override + public boolean isNew(final CcmObject entity) { + return entity.getObjectId() == 0; + } + + + +} diff --git a/ccm-core/src/main/java/org/libreccm/notification/QueueItem.java b/ccm-core/src/main/java/org/libreccm/notification/QueueItem.java index dd11c191b..53552fa35 100644 --- a/ccm-core/src/main/java/org/libreccm/notification/QueueItem.java +++ b/ccm-core/src/main/java/org/libreccm/notification/QueueItem.java @@ -65,7 +65,7 @@ public class QueueItem implements Serializable { @Column(name = "retry_count") private long retryCount; - @Column(name = "successful;") + @Column(name = "successful_sended") private boolean successful; @Column(name = "receiver_address", length = 512) diff --git a/ccm-core/src/site/site.xml b/ccm-core/src/site/site.xml index 3d12fa6cc..c2c0f1125 100644 --- a/ccm-core/src/site/site.xml +++ b/ccm-core/src/site/site.xml @@ -10,7 +10,7 @@ org.apache.maven.skins maven-fluido-skin - 1.3.1 + 1.4 diff --git a/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java b/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java index c46c2efcc..0a7091358 100644 --- a/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java +++ b/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java @@ -18,8 +18,6 @@ */ package org.libreccm.core; -import org.dbunit.util.fileloader.DataFileLoader; - import static org.hamcrest.CoreMatchers.*; import org.jboss.arquillian.container.test.api.Deployment; @@ -50,6 +48,8 @@ import java.util.List; import javax.ejb.EJBTransactionRolledbackException; import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; import static org.junit.Assert.*; @@ -66,6 +66,9 @@ public class CcmObjectRepositoryTest { @Inject private transient CcmObjectRepository ccmObjectRepository; + @PersistenceContext(name = "LibreCCM") + private transient EntityManager entityManager; + public CcmObjectRepositoryTest() { } @@ -116,7 +119,67 @@ public class CcmObjectRepositoryTest { .addAsResource("test-persistence.xml", "META-INF/persistence.xml") .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); } + + @Test + public void repoIsInjected() { + assertThat(ccmObjectRepository, is(not((nullValue())))); + } + + @Test + public void entityManagerIsInjected() { + assertThat(entityManager, is(not((nullValue())))); + } + + @Test + @UsingDataSet( + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + @InSequence(5) + public void entityManagerFindCcmObjectByLongPrimitive() { + final CcmObject obj1 = entityManager.find(CcmObject.class, -10L); + final CcmObject obj2 = entityManager.find(CcmObject.class, -20L); + final CcmObject obj3 = entityManager.find(CcmObject.class, -30L); + final CcmObject none = entityManager.find(CcmObject.class, -999L); + + assertThat(obj1, is(not(nullValue()))); + assertThat(obj1.getObjectId(), is(-10L)); + assertThat(obj1.getDisplayName(), is(equalTo("Test Object 1"))); + + assertThat(obj2, is(not(nullValue()))); + assertThat(obj2.getObjectId(), is(-20L)); + assertThat(obj2.getDisplayName(), is(equalTo("Test Object 2"))); + + assertThat(obj3, is(not(nullValue()))); + assertThat(obj3.getObjectId(), is(-30L)); + assertThat(obj3.getDisplayName(), is(equalTo("Test Object 3"))); + + assertThat(none, is(nullValue())); + } + @Test + @UsingDataSet( + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + @InSequence(6) + public void entityManagerFindCcmObjectByLongClass() { + final CcmObject obj1 = entityManager.find(CcmObject.class, new Long(-10L)); + final CcmObject obj2 = entityManager.find(CcmObject.class, new Long(-20L)); + final CcmObject obj3 = entityManager.find(CcmObject.class, new Long(-30L)); + final CcmObject none = entityManager.find(CcmObject.class, new Long(-999L)); + + assertThat(obj1, is(not(nullValue()))); + assertThat(obj1.getObjectId(), is(-10L)); + assertThat(obj1.getDisplayName(), is(equalTo("Test Object 1"))); + + assertThat(obj2, is(not(nullValue()))); + assertThat(obj2.getObjectId(), is(-20L)); + assertThat(obj2.getDisplayName(), is(equalTo("Test Object 2"))); + + assertThat(obj3, is(not(nullValue()))); + assertThat(obj3.getObjectId(), is(-30L)); + assertThat(obj3.getDisplayName(), is(equalTo("Test Object 3"))); + + assertThat(none, is(nullValue())); + } + @Test @UsingDataSet( "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") diff --git a/ccm-core/src/test/java/org/libreccm/core/DatasetsExampleTest.java b/ccm-core/src/test/java/org/libreccm/core/DatasetsExampleTest.java new file mode 100644 index 000000000..d99eab008 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/DatasetsExampleTest.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core; + +import org.dbunit.DatabaseUnitException; +import org.dbunit.database.DatabaseConnection; +import org.dbunit.database.IDatabaseConnection; +import org.dbunit.dataset.IDataSet; +import org.dbunit.operation.DatabaseOperation; +import org.h2.tools.RunScript; +import org.jboss.arquillian.persistence.core.data.descriptor.Format; +import org.jboss.arquillian.persistence.dbunit.dataset.DataSetBuilder; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.libreccm.tests.categories.UnitTest; + +import java.io.FileReader; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import static org.junit.Assert.*; + +/** + * + * @author Jens Pelzetter + */ +@Category(UnitTest.class) +public class DatasetsExampleTest { + + public DatasetsExampleTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Test + public void verifyBaseDataset() throws ClassNotFoundException, + SQLException, + DatabaseUnitException, + IOException, + URISyntaxException { + final DataSetBuilder builder = DataSetBuilder.builderFor(Format.JSON); + final IDataSet dataSet = builder.build( + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json"); + final Path schemaPath = Paths.get(getClass().getResource( + "/sql/ddl/auto/h2.sql").toURI()); + + Class.forName("org.h2.Driver"); + try (Connection connection = DriverManager.getConnection( + "jdbc:h2:mem:testdatabase", "sa", "")) { + //Create db schema + RunScript.execute(connection, Files.newBufferedReader(schemaPath)); + connection.commit(); + + final IDatabaseConnection dbUnitConn + = new DatabaseConnection(connection); + DatabaseOperation.CLEAN_INSERT.execute(dbUnitConn, dataSet); + } + } + +} diff --git a/ccm-core/src/test/java/org/libreccm/core/DatasetsTest.java b/ccm-core/src/test/java/org/libreccm/core/DatasetsTest.java new file mode 100644 index 000000000..7c300d6b3 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/DatasetsTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.libreccm.tests.categories.UnitTest; +import org.libreccm.testutils.DatasetsVerifier; + +import java.util.Arrays; +import java.util.Collection; + +/** + * + * @author Jens Pelzetter + */ +@RunWith(Parameterized.class) +@Category(UnitTest.class) +public class DatasetsTest extends DatasetsVerifier { + + @Parameterized.Parameters(name = "Dataset {0}") + public static Collection data() { + return Arrays.asList(new String[]{ + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json", + "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-delete.json", + "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json", + "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-new.json" + }); + } + + public DatasetsTest(final String datasetPath) { + super(datasetPath); + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + +} diff --git a/ccm-core/src/test/java/org/libreccm/testutils/DatasetsVerifier.java b/ccm-core/src/test/java/org/libreccm/testutils/DatasetsVerifier.java new file mode 100644 index 000000000..a34d6cc8e --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/testutils/DatasetsVerifier.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.testutils; + +import org.dbunit.DatabaseUnitException; +import org.dbunit.database.DatabaseConnection; +import org.dbunit.database.IDatabaseConnection; +import org.dbunit.dataset.IDataSet; +import org.dbunit.operation.DatabaseOperation; +import org.h2.tools.RunScript; +import org.jboss.arquillian.persistence.core.data.descriptor.Format; +import org.jboss.arquillian.persistence.dbunit.dataset.DataSetBuilder; +import org.junit.Test; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +/** + * + * @author Jens Pelzetter + */ +public class DatasetsVerifier { + + private final String datasetPath; + + public DatasetsVerifier(final String datasetsPath) { + this.datasetPath = datasetsPath; + } + + @Test + public void verifyDataset() throws SQLException, + URISyntaxException, + IOException, + DatabaseUnitException { + //Create database connection to an in memory h2 database. Placed in + //try-with-resources block to ensure that the connection is closed. + try (Connection connection = DriverManager.getConnection( + "jdbc:h2:mem:testdatabase", "sa", "")) { + //Create DB schema + final Path schemaPath = Paths.get(getClass().getResource( + "/sql/ddl/auto/h2.sql").toURI()); + RunScript.execute(connection, Files.newBufferedReader(schemaPath)); + connection.commit(); + + //Get dataset to test + final DataSetBuilder builder = DataSetBuilder + .builderFor(Format.JSON); + final IDataSet dataSet = builder.build(datasetPath); + + //Put dataset into DB + final IDatabaseConnection dbUnitConn + = new DatabaseConnection(connection); + DatabaseOperation.CLEAN_INSERT.execute(dbUnitConn, dataSet); + } + } + +} diff --git a/pom.xml b/pom.xml index 1392b9179..f0c87cfbd 100644 --- a/pom.xml +++ b/pom.xml @@ -120,12 +120,12 @@ org.apache.maven.plugins maven-surefire-plugin - 2.18.1 + 2.18.1 org.apache.maven.plugins maven-assembly-plugin - 2.5.3 + 2.5.4 org.apache.maven.plugins @@ -198,7 +198,7 @@ de.jpdigital hibernate4-ddl-maven-plugin - 1.0.0-alpha.1 + 1.0.0-alpha.2 @@ -227,7 +227,7 @@ org.hibernate hibernate-entitymanager - 4.3.8.Final + 4.3.10.Final - - org.hibernate - hibernate-validator - 5.1.3.Final - + Hibernate Validator used as implemenation of the Bean + Validation API --> + + org.hibernate + hibernate-validator + 5.1.3.Final + + + com.h2database + h2 + 1.4.187 + diff --git a/src/site/site.xml b/src/site/site.xml index 716e5103b..d98a745f8 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -11,7 +11,7 @@ org.apache.maven.skins maven-fluido-skin - 1.3.1 + 1.4