View Javadoc
1   package org.apache.onami.persist.test.transaction.testframework;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.google.common.annotations.VisibleForTesting;
23  import org.apache.onami.persist.EntityManagerProvider;
24  import org.apache.onami.persist.test.TestEntity;
25  import org.apache.onami.persist.test.transaction.testframework.exceptions.RuntimeTestException;
26  import org.apache.onami.persist.test.transaction.testframework.exceptions.TestException;
27  
28  import javax.inject.Inject;
29  import javax.persistence.EntityManager;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * A {@link TransactionalTask} is a task which is executed during a transaction
35   * test. {@link TransactionalTask}s are passed to a {@link TransactionalWorker} which will call
36   * them one after another.
37   * The sub classes of {@link TransactionalTask} should create a {@link org.apache.onami.persist.test.TestEntity} and
38   * use {@link #storeEntity(org.apache.onami.persist.test.TestEntity)} to persist entities in the DB. They also must
39   * call {@link #doOtherTasks()} to allow the {@link TransactionalWorker} to call the other scheduled
40   * tasks.
41   */
42  public abstract class TransactionalTask
43  {
44  
45      @Inject
46      private EntityManagerProvider emProvider;
47  
48      private TransactionalWorker worker;
49  
50      private final List<TestEntity> persistedEntities = new ArrayList<TestEntity>();
51  
52      /**
53       * Should 'try to' create entities in the persistent storage (i.e. DB).
54       * Use {@link #storeEntity(org.apache.onami.persist.test.TestEntity)} to persist entities.
55       *
56       * @throws TestException        may be thrown to test rollback.
57       * @throws RuntimeTestException may be thrown to test rollback.
58       */
59      public abstract void doTransactional()
60          throws TestException, RuntimeTestException;
61  
62      /**
63       * Does other tasks.
64       *
65       * @throws TestException        may be thrown to test rollback.
66       * @throws RuntimeTestException may be thrown to test rollback.
67       */
68      protected final void doOtherTasks()
69          throws TestException, RuntimeTestException
70      {
71          worker.doNextTask();
72      }
73  
74      /**
75       * Stores an entity.
76       *
77       * @param entity the entity to store.
78       */
79      protected final void storeEntity( TestEntity entity )
80      {
81          final EntityManager entityManager = emProvider.get();
82          entityManager.persist( entity );
83          entityManager.flush();
84          persistedEntities.add( entity );
85      }
86  
87      @VisibleForTesting
88      void setWorker( TransactionalWorker transactionalWorker )
89      {
90          worker = transactionalWorker;
91      }
92  
93      @VisibleForTesting
94      List<TestEntity> getPersistedEntities()
95      {
96          return persistedEntities;
97      }
98  
99  }