25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 ■ Adding Persistence to Your <strong>Spring</strong> Application<br />

Listing 9-6. jdbc.properties<br />

jdbc.driverClassName=org.hsqldb.jdbcDriver<br />

jdbc.url=jdbc:hsqldb:mem:mydocuments;shutdown=false<br />

jdbc.username=SA<br />

jdbc.password=<br />

Listing 9-6 shows the JDBC parameters that are used to connect to the database engine, and you can see that in<br />

the jdbc.url (format: jdbc:::[:|/];) property you are using<br />

an in-memory database. Next, you need to create the unit test class to test the persistence (see Listing 9-7).<br />

Listing 9-7. Unit Test Class<br />

package com.apress.isf.spring.test;<br />

import static org.junit.Assert.assertEquals;<br />

import static org.junit.Assert.assertNotNull;<br />

import static org.junit.Assert.assertTrue;<br />

import java.util.List;<br />

import org.junit.Test;<br />

import org.junit.runner.RunWith;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.test.context.ContextConfiguration;<br />

import org.springframework.test.context.junit4.<strong>Spring</strong>JUnit4ClassRunner;<br />

import com.apress.isf.java.model.Document;<br />

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.java.service.SearchEngine;<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration("classpath:META-INF/spring/mydocuments-jdbc-context.xml")<br />

public class MyDocumentsJDBCTest {<br />

private static final Logger log = LoggerFactory.getLogger(MyDocumentsJDBCTest.class);<br />

@Autowired<br />

private SearchEngine engine;<br />

private Type webType = new Type("WEB",".url");<br />

@Test<br />

public void testUsing<strong>Spring</strong>JDBC() {<br />

log.debug("Using <strong>Spring</strong> JDBC...");<br />

List documents = engine.listAll();<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 4);<br />

documents = engine.findByType(webType);<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 1);<br />

119

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!