Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.kuali.student.common.test.spring;
 
 import java.io.File;
 import java.util.List;
 
 
This test class will load your dao and gives you access to the shared entityManager em. Also passes the @Dao and @PersistenceFileLocation to system properties from the annotations.

Extend this class and set the

  • @PersistenceFileLocation
  • @Dao

@PersistenceFileLocation defines the persistence.xml location if it is named something else.

@Dao defines the Dao implementation class, and an optional application context that contains a list of beans that should be persisted. The list bean should be called "persistList". SQL files that should be loaded can also be defined here with the testSqlFile parameter. This should be an SQL file.

This test class is @Transactional, so all tests will be rolled back. That means the data you load will be in the same state for each test.

Example:

 @PersistenceFileLocation("classpath:META-INF/custom-persistence.xml")
 public class DaoCommonTest extends AbstractTransactionalDaoTest {
 
 @Dao(value = "org.kuali.student.MyDaoImpl", 
      testDataFile = "classpath:META-INF/pretest-data-beans-1.xml,pretest-data-beans-2.xml")
 public MyDao myDao;
 
 @Test
 public void test1() {
 MyObject a = myDao.foo();
 MyObject b = em.find(MyObject.class,1);
 assertEquals(a.id,b.id);
 }
 }
 
Example of application context for preloading data:
 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
  <bean id="persistList"
  class="org.springframework.beans.factory.config.ListFactoryBean">
 <property name="sourceList">
  <list>
   <ref bean="value1" />
   <ref bean="value2" />
  </list>
 </property>
  </bean>
  
  <bean id="value1"
  class="org.kuali.student.Value">
 <property name="value" value="Value Number One" />
  </bean>
  
  <bean id="value2"
  class="org.kuali.student.Value">
 <property name="value" value="Value Number Two" />
  </bean>
 
 </beans>
 
@ContextConfiguration(locations = { "classpath:META-INF/default-dao-context-test.xml" })
@TransactionConfiguration(transactionManager = "JtaTxManager")
public abstract class AbstractTransactionalDaoTest {
	protected EntityManager em;
	private static boolean preloadedData=false;
Loads the application context defined in the @Dao testDataFile attribute. Then uses the EntityManager em to persist the beans in persistList
	public void preLoadBeans() {
		for (Field f : this.getClass().getDeclaredFields()) {
			if (f.isAnnotationPresent(Dao.class)) {
				Dao dao = f.getAnnotation(Dao.class);
				if (dao.testDataFile().length() > 0) {
					for (Object o : (List<?>) ac.getBean("persistList")) {
					}
				}
			}
		}
	}
	public void preLoadData() throws IOException  {
			for (Field f : this.getClass().getDeclaredFields()) {
				if (f.isAnnotationPresent(Dao.class)) {
					Dao dao = f.getAnnotation(Dao.class);
					if (dao.testSqlFile().length() > 0) {
						if (dao.testSqlFile().startsWith("classpath:")) {
							String file = dao.testSqlFile().substring("classpath:".length());
							String[] files = file.split("\\s*,\\s*");
							for(String testFile : files) {
								File sqlFile = new ClassPathResource(testFile).getFile();
								process(sqlFile);
							}
else {
							String[] files = dao.testSqlFile().split("\\s*,\\s*");
							for(String testFile : files) {
								File sqlFile = new File(testFile);
								process(sqlFile);
							}
						}
					}
				}
			}
		}
	}
	private void process(File sqlFilethrows FileNotFoundException {
		   = new BufferedReader(new FileReader(sqlFile));
		String ln;
		//Check if oracle
		TransactionStatus txStatus = .getTransaction(txDefinition);
		try {
			while((ln=in.readLine())!=null){
				if(!ln.startsWith("/")&&!ln.startsWith("--")&&StringUtils.isNotBlank(ln)){
					ln=ln.replaceFirst("[;/]\\s*$","");
				}
			}
catch (Exception e) {
		}
		finally{
			try {
				in.close();
catch (IOException e) {
				.error("IO Stream closed " + e);
			}
		}
	}

Passes some variables so they can be used in the application context
		super();
		// Grab annotations and pass them as System properties
			System.setProperty("ks.test.persistenceLocation"a.value());
else {
			System.setProperty("ks.test.persistenceLocation",
					"classpath:META-INF/persistence.xml");
		}
	}
New to GrepCode? Check out our FAQ X