Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. <profile>
  2. <id>test</id>
  3. <properties>
  4. <jpa.dialect>org.springframework.orm.jpa.vendor.HibernateJpaDialect</jpa.dialect>
  5. <jpa.vendor.adapter>HibernateJpaVendorAdapter</jpa.vendor.adapter>
  6. <jdbc.dialect>org.hibernate.dialect.HSQLDialect</jdbc.dialect>
  7. <jdbc.url>jdbc:hsqldb:mem:testDatabase</jdbc.url>
  8. <jdbc.driver>org.hsqldb.jdbcDriver</jdbc.driver>
  9. <jdbc.username>sa</jdbc.username>
  10. <jdbc.password></jdbc.password>
  11. <jdbc.format_sql>true</jdbc.format_sql>
  12. <jdbc.show_sql>true</jdbc.show_sql>
  13. <!-- import.sql is readen ; it must have this name and be in classpath -->
  14. <jpa.generateDdl>true</jpa.generateDdl>
  15. <hibernate.format_sql>true</hibernate.format_sql>
  16. <hibernate.hbm2ddl.auto>create</hibernate.hbm2ddl.auto>
  17. </properties>
  18. </profile>
  19.  
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <beans xmlns="http://www.springframework.org/schema/beans"
  22. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  23. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  24. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  25. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  26. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
  27.  
  28. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  29. <property name="locations">
  30. <list>
  31. <value>classpath:jdbc.properties</value>
  32. </list>
  33. </property>
  34. </bean>
  35.  
  36. <!-- enables interpretation of the @PersistenceUnit/@PersistenceContext
  37. annotations providing convenient access to EntityManagerFactory/EntityManager -->
  38. <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
  39.  
  40. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  41. <property name="driverClassName" value="${jdbc.driver}" />
  42. <property name="url" value="${jdbc.url}" />
  43. <property name="username" value="${jdbc.username}" />
  44. <property name="password" value="${jdbc.password}" />
  45. </bean>
  46.  
  47. <context:component-scan base-package="foo.bar.dao">
  48. <context:include-filter type="annotation"
  49. expression="org.springframework.stereotype.Repository" />
  50. </context:component-scan>
  51.  
  52. <bean id="entityManagerFactory"
  53. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  54. <property name="dataSource" ref="dataSource" />
  55. <property name="persistenceUnitName" value="jpaLibrary" />
  56. <property name="jpaDialect">
  57. <bean class="${jpa.dialect}" />
  58. </property>
  59. <property name="jpaVendorAdapter">
  60. <bean class="org.springframework.orm.jpa.vendor.${jpa.vendor.adapter}">
  61. <property name="showSql" value="${jdbc.show_sql}" />
  62. <property name="databasePlatform" value="${jdbc.dialect}" />
  63. <!-- On genere la BDD au demarrage -->
  64. <property name="generateDdl" value="${jpa.generateDdl}" />
  65. </bean>
  66. </property>
  67. </bean>
  68.  
  69. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  70. <property name="entityManagerFactory" ref="entityManagerFactory" />
  71. </bean>
  72.  
  73. <!-- enable the configuration of transactional behavior based on annotations -->
  74. <tx:annotation-driven transaction-manager="transactionManager" />
  75.  
  76. <context:spring-configured />
  77. <context:annotation-config />
  78.  
  79. </beans>
  80.  
  81. INSERT INTO `testDatabase`.`authors`(author_id, name) VALUES
  82. (1, "JRR Tolkien"),
  83. (2, "Albert Camus"),
  84. (3, "Victor Hugo");
  85. ...
  86.  
  87. import static org.junit.Assert.assertEquals;
  88. import static org.junit.Assert.assertNotNull;
  89. import static org.junit.Assert.assertNull;
  90. import static org.junit.Assert.assertTrue;
  91.  
  92. import org.junit.Before;
  93. import org.junit.Test;
  94. import org.junit.runner.RunWith;
  95. import org.springframework.beans.factory.annotation.Autowired;
  96. import org.springframework.test.context.ContextConfiguration;
  97. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
  98. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  99.  
  100. import foo.bar.dao.BookDao;
  101.  
  102. @RunWith(SpringJUnit4ClassRunner.class)
  103. @ContextConfiguration(locations = { "/test-applicationContext.xml" })
  104. public class BookDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
  105.  
  106. @Autowired
  107. private BookDao bookDao;
  108.  
  109. private int initialSize = 0;
  110.  
  111. @Before
  112. public void init() {
  113. initialSize = bookDao.findAll().size();
  114. }
  115.  
  116. @Test
  117. public void getAllBooks() {
  118. assertEquals(12, initialSize);
  119. }
  120.  
  121. ...
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement