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
pull/2/head
jensp 2015-06-10 18:14:58 +00:00
parent 61e6971152
commit cbe2b198fd
11 changed files with 395 additions and 23 deletions

View File

@ -102,6 +102,11 @@
<build>
<finalName>ccm-core</finalName>
<resources>
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -408,7 +413,6 @@
<plugin>
<groupId>de.jpdigital</groupId>
<artifactId>hibernate4-ddl-maven-plugin</artifactId>
<version>1.0.0-alpha.1</version>
<configuration>
<dialects>
<param>h2</param>
@ -447,6 +451,9 @@
org.libreccm.tests.categories.UnitTests,
org.libreccm.tests.categories.IntegrationTest
</groups>
<additionalClasspathElements>
<additionalClasspathElement>${project.build.directory}/generated-resources</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
@ -494,7 +501,6 @@
<plugin>
<groupId>de.jpdigital</groupId>
<artifactId>hibernate4-ddl-maven-plugin</artifactId>
<version>1.0.0-alpha.1</version>
<configuration>
<dialects>
<param>h2</param>

View File

@ -31,7 +31,7 @@ public abstract class AbstractEntityRepository<K, E> {
public abstract Class<E> 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<K, E> {
if (entity == null) {
throw new IllegalArgumentException("Can't delete a null entity.");
}
entityManager.remove(entity);
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class CcmObjectRepository extends AbstractEntityRepository<Long, CcmObject> {
@Override
public Class<CcmObject> getEntityClass() {
return CcmObject.class;
}
@Override
public boolean isNew(final CcmObject entity) {
return entity.getObjectId() == 0;
}
}

View File

@ -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)

View File

@ -10,7 +10,7 @@
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.3.1</version>
<version>1.4</version>
</skin>
</project>

View File

@ -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")

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@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);
}
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RunWith(Parameterized.class)
@Category(UnitTest.class)
public class DatasetsTest extends DatasetsVerifier {
@Parameterized.Parameters(name = "Dataset {0}")
public static Collection<String> 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() {
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
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);
}
}
}

35
pom.xml
View File

@ -120,12 +120,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<version>2.18.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<version>2.5.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -198,7 +198,7 @@
<plugin>
<groupId>de.jpdigital</groupId>
<artifactId>hibernate4-ddl-maven-plugin</artifactId>
<version>1.0.0-alpha.1</version>
<version>1.0.0-alpha.2</version>
</plugin>
</plugins>
</pluginManagement>
@ -227,7 +227,7 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
<version>4.3.10.Final</version>
</dependency>
<!--
@ -237,17 +237,17 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.3.8.Final</version>
<version>4.3.10.Final</version>
</dependency>
<!--
Hibernate Validator used as implemenation of the Bean
Validation API -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
Hibernate Validator used as implemenation of the Bean
Validation API -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<!--
*********************
@ -266,7 +266,7 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.2</version>
<version>2.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -403,7 +403,7 @@
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.7.Final</version>
<version>1.1.8.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
@ -447,6 +447,13 @@
<artifactId>equalsverifier</artifactId>
<version>1.7.2</version>
</dependency>
<!-- h2 database in used to check some database related things -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.187</version>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -11,7 +11,7 @@
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.3.1</version>
<version>1.4</version>
</skin>
</project>