CCM NG: Current status

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3490 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2015-06-17 18:26:02 +00:00
parent 078c23ea8a
commit 619a0abadf
13 changed files with 365 additions and 58 deletions

View File

@ -115,6 +115,15 @@
</resource>
</resources>-->
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>${project.build.directory}/generated-resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -390,7 +399,7 @@
<scope>test</scope>
</dependency>
<!--Required for WebServices and RESTful WebServices-->
<!--<dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-webservices</artifactId>
<version>1.7.1</version>
@ -399,13 +408,12 @@
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-jaxrs</artifactId>
<version>1.7.1</version>
</dependency>-->
<!--<dependency>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.187</version>
<scope>test</scope>
</dependency>-->
</dependency>
<!--<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.core</artifactId>
@ -427,6 +435,9 @@
<testResource>
<directory>src/test/resources-tomee-embedded</directory>
</testResource>
<testResource>
<directory>${project.build.directory}/generated-resources</directory>
</testResource>
</testResources>
<plugins>
@ -468,7 +479,7 @@
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<groups>
org.libreccm.tests.categories.UnitTests,
org.libreccm.tests.categories.UnitTest,
org.libreccm.tests.categories.IntegrationTest
</groups>
<additionalClasspathElements>
@ -480,6 +491,83 @@
</build>
</profile>
<profile>
<id>tomee-remote-pgsql</id>
<dependencies>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-remote</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>apache-tomee</artifactId>
<version>1.7.2</version>
<classifier>plus</classifier>
<type>zip</type>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources-tomee-remote-pgsql</directory>
</testResource>
<testResource>
<directory>${project.build.directory}/generated-resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>de.jpdigital</groupId>
<artifactId>hibernate4-ddl-maven-plugin</artifactId>
<configuration>
<dialects>
<param>h2</param>
<param>mysql5_innodb</param>
<param>postgresql9</param>
</dialects>
<packages>
<param>org.libreccm</param>
</packages>
<useEnvers>true</useEnvers>
</configuration>
<executions>
<execution>
<goals>
<goal>gen-ddl</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<forkMode>always</forkMode>
<forkCount>999</forkCount>
<reuseForks>true</reuseForks>
<systemPropertyVariables>
<tomee.classifier>plus</tomee.classifier>
<tomee.version>1.7.2</tomee.version>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<groups>
org.libreccm.tests.categories.UnitTest,
org.libreccm.tests.categories.IntegrationTest
</groups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly8-embedded</id>
<dependencies>

View File

@ -40,6 +40,8 @@ import java.util.Collections;
import java.util.Objects;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.validation.constraints.Pattern;
@ -58,6 +60,10 @@ import javax.validation.constraints.Pattern;
*/
@Entity
@Table(name = "categories")
@NamedQueries({
@NamedQuery(name = "topLevelCategories",
query = "SELECT c FROM Category c WHERE c.parentCategory IS NULL")
})
public class Category extends CcmObject implements Serializable {
private static final long serialVersionUID = -7250208963391878547L;

View File

@ -0,0 +1,58 @@
/*
* 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.categorization;
import org.libreccm.core.AbstractEntityRepository;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.persistence.TypedQuery;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class CategoryRepository extends AbstractEntityRepository<Long, Category> {
@Override
public Class<Category> getEntityClass() {
return Category.class;
}
@Override
public boolean isNew(final Category entity) {
return entity.getObjectId() == 0;
}
/**
* Retrieves a list of all top level categories (Categories without a
* parent category).
*
* @return A list of all top level categories.
*/
public List<Category> getTopLevelCategories() {
final TypedQuery<Category> query = getEntityManager().createNamedQuery(
"topLevelCategories", Category.class);
return query.getResultList();
}
}

View File

@ -1,7 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* 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;
@ -15,26 +28,58 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
/**
*
* A base class providing common method needed by every repository.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @param <K>
* @param <E>
* @param <K> Type of the primary key of the entity
* @param <E> Type of the entity.
*/
public abstract class AbstractEntityRepository<K, E> {
/**
* The {@link EntityManager} instance to use. Provided by the container via
* CDI.
*/
@Inject
private transient EntityManager entityManager;
/**
* Getter method for retrieving the injected {@link EntityManager}.
*
* @return The {@code EntityManager} used by the repository.
*/
protected EntityManager getEntityManager() {
return entityManager;
}
/**
* The class of entities for which this repository can be used.
* For creating a repository class overwrite this method.
*
* @return The {@code Class} of the Entity which are managed by this
* repository.
*/
public abstract Class<E> getEntityClass();
/**
* Finds an entity by it ID.
*
* @param entityId The ID of the entity to retrieve.
*
* @return The entity identified by the provided ID of {@code null} if there
* is no such entity.
*/
public E findById(final K entityId) {
return entityManager.find(getEntityClass(), entityId);
}
/**
* Finds all instances of the entity of the type this repository is
* responsible for.
*
* @return The list of entities in the database which are of the type
* provided by {@link #getEntityClass()}.
*/
public List<E> findAll() {
// We are using the Critiera API here because otherwise we can't
// pass the type of the entity dynmacially.
@ -50,8 +95,21 @@ public abstract class AbstractEntityRepository<K, E> {
return query.getResultList();
}
/**
* Used by {@link #save(java.lang.Object)} to determine if the provided
* entity is a a new one.
*
* @param entity The entity to check.
* @return {@code true} if the entity is new (isn't in the database yet),
* {@code false} otherwise.
*/
public abstract boolean isNew(final E entity);
/**
* Save a new or changed entity.
*
* @param entity The entity to save.
*/
public void save(final E entity) {
if (isNew(entity)) {
entityManager.persist(entity);
@ -60,6 +118,11 @@ public abstract class AbstractEntityRepository<K, E> {
}
}
/**
* Deletes an entity from the database.
*
* @param entity The entity to delete.
*/
public void delete(final E entity) {
if (entity == null) {
throw new IllegalArgumentException("Can't delete a null entity.");

View File

@ -137,6 +137,12 @@ public class CcmObject implements Serializable {
this.displayName = displayName;
}
/**
*
* @return Returns all permissions for this {@code CcmObject}. Please note
* that the returned {@link List} can't be modified. For adding and removing
* permissions use the methods provided by the {@link CcmObjectManager}.
*/
public List<Permission> getPermissions() {
return Collections.unmodifiableList(permissions);
}

View File

@ -22,7 +22,8 @@ package org.libreccm.core;
import javax.enterprise.context.RequestScoped;
/**
*
* A repository class for {@link CcmObject}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped

View File

@ -91,10 +91,10 @@ public class CcmObjectRepositoryTest {
@Deployment
public static WebArchive createDeployment() {
final PomEquippedResolveStage pom = Maven
.resolver()
.loadPomFromFile("pom.xml");
.resolver()
.loadPomFromFile("pom.xml");
final PomEquippedResolveStage dependencies = pom
.importCompileAndRuntimeDependencies();
.importCompileAndRuntimeDependencies();
final File[] libs = dependencies.resolve().withTransitivity().asFile();
for (File lib : libs) {
@ -103,24 +103,25 @@ public class CcmObjectRepositoryTest {
}
return ShrinkWrap
.create(WebArchive.class,
"LibreCCM-org.libreccm.core.CcmObjectRepositoryTest.war").
addPackage(CcmObject.class.getPackage())
.addPackage(org.libreccm.categorization.Category.class.
getPackage())
.addPackage(org.libreccm.l10n.LocalizedString.class.getPackage()).
addPackage(org.libreccm.jpa.EntityManagerProducer.class
.getPackage())
.addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class
.getPackage())
.addPackage(org.libreccm.testutils.EqualsVerifier.class.
getPackage())
.addPackage(org.libreccm.tests.categories.IntegrationTest.class
.getPackage())
.addAsLibraries(libs)
.addAsResource("test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
.create(WebArchive.class,
"LibreCCM-org.libreccm.core.CcmObjectRepositoryTest.war").
addPackage(CcmObject.class.getPackage())
.addPackage(org.libreccm.categorization.Category.class.
getPackage())
.addPackage(org.libreccm.l10n.LocalizedString.class.getPackage()).
addPackage(org.libreccm.jpa.EntityManagerProducer.class
.getPackage())
.addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class
.getPackage())
.addPackage(org.libreccm.testutils.EqualsVerifier.class.
getPackage())
.addPackage(org.libreccm.tests.categories.IntegrationTest.class
.getPackage())
.addAsLibraries(libs)
.addAsResource("test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebInfResource("test-web.xml", "WEB-INF/web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
}
@Test
@ -135,15 +136,15 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@InSequence(4)
public void datasetOnly() {
System.out.println("Dataset loaded successfully.");
}
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json")
@InSequence(4)
public void datasetOnly2() {
System.out.println("Dataset loaded successfully.");
@ -151,7 +152,7 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@InSequence(5)
public void entityManagerFindCcmObjectByLongPrimitive() {
final CcmObject obj1 = entityManager.find(CcmObject.class, -10L);
@ -176,7 +177,7 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@InSequence(6)
public void entityManagerFindCcmObjectByLongClass() {
final CcmObject obj1 = entityManager.find(CcmObject.class,
@ -205,7 +206,7 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@InSequence(10)
public void findCcmObjectById() {
final CcmObject obj1 = ccmObjectRepository.findById(-10L);
@ -230,7 +231,7 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@InSequence(10)
public void findAllCcmObjects() {
final List<CcmObject> objects = ccmObjectRepository.findAll();
@ -240,9 +241,9 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@ShouldMatchDataSet(value
= "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-new.json",
= "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-new.json",
excludeColumns = {"object_id"})
@InSequence(300)
public void saveNewCcmObject() {
@ -254,9 +255,9 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@ShouldMatchDataSet(value
= "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json",
= "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json",
excludeColumns = {"object_id"})
@InSequence(400)
public void saveChangedCcmObject() {
@ -275,9 +276,9 @@ public class CcmObjectRepositoryTest {
@Test
@UsingDataSet(
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
"datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json")
@ShouldMatchDataSet(value
= "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-delete.json",
= "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-delete.json",
excludeColumns = {"object_id"})
@InSequence(600)
public void deleteCcmObject() {

View File

@ -42,10 +42,10 @@ 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"
"/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"
});
}

View File

@ -3,11 +3,11 @@
author: Jens Pelzetter
-->
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="LibreCCM" transaction-type="JTA">

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd" >
<engine>
<property name="deploymentExportPath">target/deployments</property>
</engine>
<extension qualifier="persistence">
<property name="defaultDataSource">java:/comp/env/jdbc/org/libreccm/ccm-core/pgsql</property>
<!--<property name="javaVmArguments">
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y
</property>-->
<!--
Disable automatic cleanup, does not work because of referential
integrity constrains.
-->
<property name="defaultCleanupPhase">NONE</property>
<property name="dumpData">true</property>
<property name="dumpDirectory">target</property>
</extension>
<extension qualifier="persistence-dbunit">
<property name="defaultDataSetFormat">json</property>
<!--<property name="datatypeFactory">org.dbunit.ext.h2.H2DataTypeFactory</property>-->
<property name="excludePoi">true</property>
</extension>
<extension qualifier="persistence-script">
<property name="scriptsToExecuteAfterTest">scripts/pgsql-cleanup.sql</property>
</extension>
</arquillian>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
author: Jens Pelzetter
-->
<persistence
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="LibreCCM" transaction-type="JTA">
<!--
Enforce JPA provider
Not really necessary here because we don't use any Hibernate
specific features, but makes it easier to manage to database
creation scripts.
-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/comp/env/jdbc/org/libreccm/ccm-core/pgsql</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.connection.autocommit" value="false" />
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>CcmCoreTest</display-name>
</web-app>

View File

@ -39,6 +39,11 @@ import java.sql.DriverManager;
import java.sql.SQLException;
import org.h2.tools.RunScript;
import org.jboss.arquillian.persistence.dbunit.dataset.json.JsonDataSet;
import static org.junit.Assert.*;
import java.io.BufferedReader;
/**
*
@ -68,9 +73,8 @@ public class DatasetsVerifier {
connection.commit();
//Get dataset to test
final DataSetBuilder builder = DataSetBuilder
.builderFor(Format.JSON);
final IDataSet dataSet = builder.build(datasetPath);
final IDataSet dataSet = new JsonDataSet(getClass()
.getResourceAsStream(datasetPath));
//Create DBUnit DB connection
final IDatabaseConnection dbUnitConn