View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  package org.kuali.mobility.xsl.dao;
16  
17  import org.apache.log4j.Logger;
18  import org.junit.AfterClass;
19  import org.junit.Before;
20  import org.junit.BeforeClass;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.kuali.mobility.xsl.entity.Xsl;
24  import org.unitils.UnitilsJUnit4TestClassRunner;
25  import org.unitils.orm.jpa.JpaUnitils;
26  import org.unitils.orm.jpa.annotation.JpaEntityManagerFactory;
27  
28  import javax.persistence.EntityManagerFactory;
29  import javax.persistence.PersistenceUnit;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import static org.junit.Assert.assertTrue;
34  
35  /**
36   * A service for doing the actual work of interacting with Campus objects.
37   *
38   * @author Kuali Mobility Team (mobility.collab@kuali.org)
39   */
40  @RunWith(UnitilsJUnit4TestClassRunner.class)
41  @JpaEntityManagerFactory(persistenceUnit="mdot")
42  public class XslDaoImplTest {
43  	private static final Logger LOG = Logger.getLogger(XslDaoImplTest.class);
44  
45  	@PersistenceUnit
46  	private EntityManagerFactory entityManagerFactory;
47  
48  	private XslDao dao;
49  
50  	private static final String[] TEST_CODE = {"AA","BB","CC","DD"};
51  	private static final String[] TEST_VALUE = {"TEST_VALUE_A","TEST_VALUE_B","TEST_VALUE_C",null};
52  
53  	@BeforeClass
54  	public static void setUpClass() throws Exception {
55  	}
56  
57  	@AfterClass
58  	public static void tearDownClass() throws Exception {
59  	}
60  
61  	@Before
62  	public void preTest() {
63  		setDao(new XslDaoImpl());
64  		JpaUnitils.injectEntityManagerInto(getDao());
65  	}
66  
67  	@Test
68  	public void testXslDao() {
69  		List<Xsl> xsls = new ArrayList<Xsl>();
70  
71  		Long badId = getDao().saveXsl(null);
72  		assertTrue("Saved a null object. How is that possible?",badId==null);
73  
74  		int i = 0;
75  		for( String s : TEST_CODE) {
76  			Xsl xsl = new Xsl();
77  			xsl.setCode(s);
78  			xsl.setValue(TEST_VALUE[i]);
79  			assertTrue("XSL ID not null and should have been.", xsl.getXslId() == null);
80  			Long id = getDao().saveXsl(xsl);
81  			assertTrue("XSL ID is null and should not have been, failed to save.", id != null );
82  			i++;
83  			List<Xsl> crossCheckList = getDao().findAllXsl();
84  			assertTrue("Could not find Xsl in database and should have.", crossCheckList != null && crossCheckList.size() > 0);
85  			assertTrue("Failed to find expected number of results in DB. "+crossCheckList.size()+" != "+i,crossCheckList.size() == i);
86  			xsls.add(xsl);
87  		}
88  
89  		Xsl lookup = getDao().findXslByCode(xsls.get(1).getCode());
90  		assertTrue("Failed to find object by code",lookup != null);
91  		assertTrue("Failed to find correct object by code",lookup.getCode().equals(xsls.get(1).getCode()));
92  
93  		lookup = null;
94  		lookup = getDao().findXslById(xsls.get(2).getXslId());
95  
96  		assertTrue("Failed to find object by id",lookup != null);
97  		assertTrue("Failed to find correct object by code",lookup.getXslId().equals(xsls.get(2).getXslId()));
98  
99  		for( Xsl xsl : xsls ) {
100 			getDao().deleteXslById(xsl.getXslId());
101 		}
102 
103 		List<Xsl> crossCheckList = getDao().findAllXsl();
104 		assertTrue("Objects remain in the db when there should be none.",crossCheckList==null || crossCheckList.isEmpty());
105 	}
106 
107 
108 	public EntityManagerFactory getEntityManagerFactory() {
109 		return entityManagerFactory;
110 	}
111 
112 	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
113 		this.entityManagerFactory = entityManagerFactory;
114 	}
115 
116 	public XslDao getDao() {
117 		return dao;
118 	}
119 
120 	public void setDao(XslDao dao) {
121 		this.dao = dao;
122 	}
123 }