View Javadoc
1   package org.kuali.rice.krad.data.provider.jpa;
2   
3   import org.junit.Assert;
4   import org.junit.Test;
5   import org.kuali.rice.krad.data.provider.PersistenceProvider;
6   import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
7   import org.kuali.rice.krad.data.jpa.testbo.TestDataObjectExtension;
8   import org.kuali.rice.krad.data.jpa.testbo.YetAnotherReferencedDataObject;
9   import org.kuali.rice.krad.test.KRADTestCase;
10  import org.kuali.rice.test.BaselineTestCase;
11  
12  import javax.persistence.EntityManagerFactory;
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  // avoid wrapping test in rollback since JPA requires transaction boundary to flush
17  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
18  public class ExtensionForAnnotationTest extends KRADTestCase {
19      protected PersistenceProvider getPersistenceProvider() {
20          return getKRADTestHarnessContext().getBean("kradTestJpaPersistenceProvider", PersistenceProvider.class);
21      }
22  
23      @Override
24      protected List<String> getPerTestTablesToClear() {
25          ArrayList<String> tables = new ArrayList<String>();
26          tables.add( "KRTST_TEST_TABLE_T" );
27          tables.add( "KRTST_TEST_TABLE_EXT_T" );
28          tables.add( "KRTST_TEST_YARDO_T" );
29          return tables;
30      }
31  
32      protected TestDataObject createTestDataObject(String key) {
33          TestDataObject dataObject = new TestDataObject();
34          dataObject.setPrimaryKeyProperty(key);
35          dataObject.setStringProperty("aString");
36  
37          TestDataObjectExtension extension = new TestDataObjectExtension();
38          extension.setPrimaryKeyProperty(dataObject);
39          extension.setExtensionProperty("extraData");
40          dataObject.setExtension(extension);
41  
42          YetAnotherReferencedDataObject yardo = new YetAnotherReferencedDataObject();
43          yardo.setId(key);
44          yardo.setSomeOtherStringProperty("otherString");
45          dataObject.setYetAnotherReferencedObject(yardo);
46  
47          return dataObject;
48      }
49  
50      @Test
51      public void testCreateDataObjectWithExtension() {
52          TestDataObject dataObject = createTestDataObject("1");
53  
54          getPersistenceProvider().save(dataObject);
55  
56          getKRADTestHarnessContext().getBean("kradTestEntityManagerFactory", EntityManagerFactory.class).getCache().evictAll();
57  
58          dataObject = getPersistenceProvider().find(TestDataObject.class, "1");
59          TestDataObjectExtension extension = getPersistenceProvider().find(TestDataObjectExtension.class, "1");
60          YetAnotherReferencedDataObject yardo = getPersistenceProvider().find(YetAnotherReferencedDataObject.class, "1");
61  
62          Assert.assertNotNull("TestDataObject 1 not saved", dataObject);
63          Assert.assertNotNull("TestDataObjectExtension 1 not saved", extension);
64          Assert.assertNotNull("YetAnotherReferencedDataObject 1 not saved", yardo);
65      }
66  
67      @Test
68      public void testRetrieveDataObjectWithExtension() {
69          TestDataObject dataObject = createTestDataObject("2");
70  
71          getPersistenceProvider().save(dataObject);
72  
73          getKRADTestHarnessContext().getBean("kradTestEntityManagerFactory", EntityManagerFactory.class).getCache().evictAll();
74  
75          dataObject = getPersistenceProvider().find(TestDataObject.class, "2");
76          YetAnotherReferencedDataObject yardo = dataObject.getYetAnotherReferencedObject();
77          Assert.assertNotNull("yardo reference was null - not loaded automatically", yardo );
78          Assert.assertNotNull("Extension reference was null - extension not loaded automatically", dataObject.getExtension() );
79          Assert.assertTrue("extension was not a TestDataObjectExtension: " + dataObject.getExtension().getClass(), dataObject.getExtension() instanceof TestDataObjectExtension );
80      }
81  }