From dc50b78fe5a89eb0ce97d28e69f52dec755c43b5 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Sun, 22 Sep 2024 19:57:23 +0100 Subject: [PATCH] feat: Replace Jcabi Plugin by AspectJ Maven Plugin - Meeds-io/meeds#2417 Prior to this change, the jcabi maven plugin was used to weave classes for annotation processing, especially and annotations. This plugin isn't maintained and should be replace by the default AspectJ libraries for Annotation Processing. In addition, the Jcabi plugin makes the build longer and takes a lot of time in addition to heigh I/O operations on disk. This change applies the required configuration (In addition to https://github.com/Meeds-io/maven-parent-pom/commit/bea09a8fcbc02b87ee8748cd9bbf9a524e4d74cd) in order to replace usage of Jcabi plugin. Besides, this change will remove Tests of moved API defined in Portal layer. --- .../impl/ExoTransactionalAnnotationTest.java | 242 ------------------ .../commons/persistence/impl/Project.java | 25 -- .../commons/persistence/impl/Task.java | 39 --- .../commons/persistence/impl/TaskDao.java | 40 --- .../commons/persistence/impl/TaskService.java | 20 -- .../impl/TestEntityManagerService.java | 89 ------- pom.xml | 21 ++ 7 files changed, 21 insertions(+), 455 deletions(-) delete mode 100644 commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/ExoTransactionalAnnotationTest.java delete mode 100644 commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Project.java delete mode 100644 commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Task.java delete mode 100644 commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskDao.java delete mode 100644 commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskService.java delete mode 100644 commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TestEntityManagerService.java diff --git a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/ExoTransactionalAnnotationTest.java b/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/ExoTransactionalAnnotationTest.java deleted file mode 100644 index bceb7aad2f..0000000000 --- a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/ExoTransactionalAnnotationTest.java +++ /dev/null @@ -1,242 +0,0 @@ -package org.exoplatform.commons.persistence.impl; - -import org.exoplatform.component.test.ConfigurationUnit; -import org.exoplatform.component.test.ConfiguredBy; -import org.exoplatform.component.test.ContainerScope; -import org.exoplatform.container.PortalContainer; -import org.exoplatform.jpa.BaseTest; -import org.junit.Before; -import org.junit.Test; - -import jakarta.persistence.TransactionRequiredException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.*; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -@ConfiguredBy({ - @ConfigurationUnit(scope = ContainerScope.ROOT, path = "conf/configuration.xml"), - @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/portal/configuration.xml"), - @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/standalone/storage-test-configuration.xml"), -}) -public class ExoTransactionalAnnotationTest extends BaseTest { - @Override - public void setUp() { - getContainer(); - } - - @Override - public void tearDown() { - // don't call tearDown on super to not clean DB - // super.tearDown(); - } - - @Test - public void testCreateDaoMethodIsTransactional() { - // Given - TaskDao dao = new TaskDao(); - // When - long id = dao.create(new Task()).getId(); - // Then - assertNotNull(dao.find(id)); - } - - @Test - public void testCreateServiceMethodIsTransactional() { - // Given - TaskService service = new TaskService(); - // When - long id = service.create(new Task()).getId(); - // Then - assertNotNull(service.find(id)); - } - - @Test - public void testDeleteDaoMethodIsTransactional() { - // Given - TaskService service = new TaskService(); - Task task = service.create(new Task()); - // When - service.delete(task); - // Then - assertNull(service.find(task.getId())); - } - - @Test - public void testNonTransactionalMethodDoesNotCommit() { - // Given - TaskService service = new TaskService(); - TaskDao dao = new TaskDao(); - service.create(new Task()); - // When - try { - dao.nonTransactionalDeleteAll(); - fail("deleteAll must throw an exception"); - } catch (TransactionRequiredException e) { - // Then - } - } - - @Test - public void testManualRollback() { - // Given - TaskDao dao = new TaskDao(); - // When - dao.createWithRollback(new Task()); - // Then - assertThat(dao.findAll().size(), is(0)); - } - - @Test - public void testManualCommit() { - // Given - TaskDao dao = new TaskDao(); - // When - dao.createWithCommit(new Task()); - // Then - assertThat(dao.findAll().size(), is(1)); - } - - @Test - public void testCommitFailed() { - EntityManagerService service = PortalContainer.getInstance().getComponentInstanceOfType(EntityManagerService.class); - assertNull(service.getEntityManager()); - - TaskDao dao = new TaskDao(); - Task task = new Task(); - task.setId((long) 1); // Invalid set to cause commit failed. - try { - dao.create(task); - } catch (Exception e) { - // Expected. - } - - assertNull(service.getEntityManager()); - } - - @Test - public void testMultipleTransactions() { - PortalContainer container = getContainer(); - EntityManagerService entityManagerService = getContainer().getComponentInstanceOfType(EntityManagerService.class); - entityManagerService.startRequest(container); - - // Given - TaskDao dao = new TaskDao(); - int initialSize = dao.findAll().size(); - // When - try { - // create first task - Task task1 = new Task(); - task1.setName("task"); - dao.create(task1); - } catch(Exception e) { - fail(e.getMessage()); - } - try { - // create second task with the same name -> fails because of unicity constraints on task name - Task task2 = new Task(); - task2.setName("task"); - dao.create(task2); - } catch(Exception e) { - // expected exception - } - try { - // create thrid task -> should work since it is in another transaction - Task task3 = new Task(); - task3.setName("other task"); - dao.create(task3); - } catch(Exception e) { - fail(e.getMessage()); - } - - // Then - assertThat(dao.findAll().size(), is(initialSize + 2)); - - entityManagerService.endRequest(container); - } - - @Test - public void testMultiThreading() throws ExecutionException, InterruptedException { - ExecutorService executor = Executors.newFixedThreadPool(2); - - List> list = new ArrayList<>(); - Callable callable = new MyCallable(); - for (int i = 0; i < 10; i++) { - Future future = executor.submit(callable); - list.add(future); - } - - for (Future fut : list) { - assertTrue(fut.get()); - } - // shut down the executor service now - executor.shutdown(); - } - - private class MyCallable implements Callable { - @Override - public Boolean call() throws Exception { - // Make sure the container has been created - PortalContainer.getInstance(); - - // Given - TaskDao dao = new TaskDao(); - // When - long id = dao.create(new Task()).getId(); - // Then - return (dao.find(id) != null); - } - } - - @Test - public void testRequestLifeCycle() { - PortalContainer container = getContainer(); - // Given - EntityManagerService service = getContainer().getComponentInstanceOfType(EntityManagerService.class); - service.startRequest(container); - TaskDao dao = new TaskDao(); - int initialSize = dao.findAll().size(); - dao.create(new Task()); - // When - service.endRequest(container); - // Then - assertThat(dao.findAll().size(), is(initialSize + 1)); - } - - @Before - public void deleteAllTask() { - begin(); - new TaskDao().deleteAll(); - end(); - } - - @Test - public void test_ifRollbackOnlyTrue_transactionRollbackWithNoError() { - // Given - TaskDao dao = new TaskDao(); - // When - dao.createWithSetRollbackOnly(new Task()); - // Then - assertThat(dao.findAll().size(), is(0)); - } - - @Test - public void test_ifRollbackPreviousTransaction_noErrorToCommitNextTransaction() { - // Given - PortalContainer container = getContainer(); - EntityManagerService service = container.getComponentInstanceOfType(EntityManagerService.class); - service.startRequest(container); - - TaskDao dao = new TaskDao(); - int initialSize = dao.findAll().size(); - - dao.createWithSetRollbackOnly(new Task()); - // When - dao.create(new Task()); - // Then - service.endRequest(container); - assertThat(dao.findAll().size(), is(initialSize + 1)); - } -} diff --git a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Project.java b/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Project.java deleted file mode 100644 index 487336b178..0000000000 --- a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Project.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.exoplatform.commons.persistence.impl; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.Table; - -import org.exoplatform.commons.api.persistence.ExoEntity; - -@ExoEntity -@Entity(name = "Project") -@Table(name = "PROJECT") -public class Project { - - @Id - @GeneratedValue - @Column(name = "PROJECT_ID") - private Long id; - - public Long getId() { - return id; - } - -} diff --git a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Task.java b/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Task.java deleted file mode 100644 index 96143b01fa..0000000000 --- a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/Task.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.exoplatform.commons.persistence.impl; - -import org.exoplatform.commons.api.persistence.ExoEntity; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.Table; - -@ExoEntity -@Entity(name = "Task") -@Table(name = "TASK") -public class Task { - @Id - @GeneratedValue(generator = "hibernate_sequence") - @Column(name = "TASK_ID") - private Long id; - - // uniqueness constraint to test failures and rollbacks on transaction commits - @Column(name = "TASK_NAME", unique = true) - private String name; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskDao.java b/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskDao.java deleted file mode 100644 index 08b94735a8..0000000000 --- a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskDao.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.exoplatform.commons.persistence.impl; - -import org.exoplatform.commons.api.persistence.ExoTransactional; - -import jakarta.persistence.Query; -import java.util.List; - -public class TaskDao extends GenericDAOJPAImpl { - - @ExoTransactional - public void createWithRollback(Task task) { - getEntityManager().persist(task); - getEntityManager().getTransaction().rollback(); - } - - public void nonTransactionalDeleteAll() { - Query query = getEntityManager().createQuery("Delete from Task"); - query.executeUpdate(); - } - - @ExoTransactional - public void createWithCommit(Task task) { - getEntityManager().persist(task); - getEntityManager().getTransaction().commit(); - } - - @ExoTransactional - public void createWithSetRollbackOnly(Task task) { - getEntityManager().persist(task); - getEntityManager().getTransaction().setRollbackOnly(); - } - - @Override - @ExoTransactional - //We invoke this method within an @ExoTransactional context because - //our TU are not run within a portal lifecycle => there is no EM in the threadLocal - public List findAll() { - return super.findAll(); - } -} diff --git a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskService.java b/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskService.java deleted file mode 100644 index 5bc263aa16..0000000000 --- a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TaskService.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.exoplatform.commons.persistence.impl; - -import org.exoplatform.commons.api.persistence.ExoTransactional; - -public class TaskService { - private TaskDao dao = new TaskDao(); - - @ExoTransactional - public Task create(Task task) { - return dao.create(task); - } - - public Task find(long id) { - return dao.find(id); - } - - public void delete(Task task) { - dao.delete(task); - } -} diff --git a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TestEntityManagerService.java b/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TestEntityManagerService.java deleted file mode 100644 index 86831667dd..0000000000 --- a/commons-component-common/src/test/java/org/exoplatform/commons/persistence/impl/TestEntityManagerService.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.exoplatform.commons.persistence.impl; - -import org.exoplatform.container.PortalContainer; -import org.exoplatform.container.component.RequestLifeCycle; -import org.exoplatform.jpa.CommonsDAOJPAImplTest; - -import jakarta.persistence.EntityManager; - -public class TestEntityManagerService extends CommonsDAOJPAImplTest { - - private EntityManagerService service; - - @Override - public void setUp() throws Exception { - super.setUp(); - service = PortalContainer.getInstance().getComponentInstanceOfType(EntityManagerService.class); - } - - public void testServiceInitialize() { - assertNotNull(service); - - EntityManager em1 = service.getEntityManager(); - assertNotNull(em1); - } - - public void testStartLifecycleTwice() { - EntityManager em1 = service.getEntityManager(); - assertNotNull(em1); - - // - RequestLifeCycle.begin(service); - EntityManager em2 = service.getEntityManager(); - assertSame(em2, em1); - - RequestLifeCycle.end(); - EntityManager em3 = service.getEntityManager(); - assertNotNull(em3); - assertSame(em3, em1); - } - - public void testEntityManagerOutsideDefaultLifecycle() { - EntityManager em1 = service.getEntityManager(); - assertNotNull(em1); - - // - A a = new A(em1); - a.run(); - assertTrue(a.succeed); - assertTrue(a.sameEntity); - - // Try to run in a different thread (out of default lifecycle) - a = new A(em1); - Thread t = new Thread(a); - t.start(); - try { - t.join(); - } catch (InterruptedException e) { - fail("Thread is interrupted"); - } - assertTrue(a.succeed); - assertFalse(a.sameEntity); - } - - class A implements Runnable { - - private EntityManager em1; - - private boolean succeed; - - private boolean sameEntity; - - public A(EntityManager em) { - this.em1 = em; - } - - @Override - public void run() { - RequestLifeCycle.begin(service); - - EntityManager em2 = service.getEntityManager(); - if (em2 != null && em2.isOpen()) { - succeed = true; - } - sameEntity = em2 == em1; - - RequestLifeCycle.end(); - } - } -} diff --git a/pom.xml b/pom.xml index 99ff5b8774..cf83dd2697 100644 --- a/pom.xml +++ b/pom.xml @@ -177,8 +177,29 @@ + + + + io.meeds.portal + portal.component.common + + ${project.artifactId} + + + org.codehaus.mojo + aspectj-maven-plugin + + + + io.meeds.portal + portal.component.common + + + + +