Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 9 ■ Adding Persistence to Your Spring Application import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.apress.isf.java.model.Document; import com.apress.isf.java.model.Type; import com.apress.isf.spring.data.DocumentDAO; @Repository("documentDAO") public class AnnotatedDocumentRepository implements DocumentDAO { private static final String queryAll = " select d.documentId, d.name, d.location, d.description as doc_desc," + "d.typeId, d.created, d.modified, "+ "t.name as type_name, t.description as type_desc," + "t.extension from documents d join types t on d.typeId = t.typeId"; @Autowired private DataSource dataSource; public List getAll() { List result = new ArrayList(); Connection connection = null; Statement statement = null; ResultSet resultSet = null; Document document = null; Type type=null; try { connection = dataSource.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery(queryAll); while (resultSet.next()) { document = new Document(); document.setDocumentId(resultSet.getString("documentId")); document.setName(resultSet.getString("name")); document.setLocation(resultSet.getString("location")); document.setCreated(resultSet.getDate("created")); document.setModified(resultSet.getDate("modified")); document.setDescription("doc_desc"); type = new Type(); type.setTypeId(resultSet.getString("typeId")); type.setName(resultSet.getString("type_name")); type.setDesc(resultSet.getString("type_desc")); type.setExtension(resultSet.getString("extension")); document.setType(type); result.add(document); } } catch (SQLException ex) { throw new RuntimeException(ex); } finally { 127

Chapter 9 ■ Adding Persistence to Your Spring Application } if (null != connection) { try { connection.close(); } catch (SQLException ex) { } } } return result; } Listing 9-13 shows how you can avoid writing too much code to initialize the database. You removed the initialize code that had a lot of lines to read two files in order to create and populate the database with the information you needed. Now, let’s see the unit test (see Listing 9-14). Listing 9-14. MyDocumentsJDBCEmbeddedAnnotatedTest.java package com.apress.isf.spring.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.apress.isf.java.model.Document; import com.apress.isf.java.model.Type; import com.apress.isf.java.service.SearchEngine; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:META-INF/spring/mydocuments-jdbc-embedded-context.xml") public class MyDocumentsJDBCEmbeddedAnnotatedTest { @Autowired private SearchEngine engine; private Type webType = new Type("WEB",".url"); @Test public void testJDBCEmbedded() { List documents = engine.findByType(webType); assertNotNull(documents); assertTrue(documents.size() == 1); assertEquals(webType.getName(),documents.get(0).getType().getName()); assertEquals(webType.getExtension(),documents.get(0).getType().getExtension()); 128

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

}<br />

if (null != connection) {<br />

try {<br />

connection.close();<br />

} catch (SQLException ex) {<br />

}<br />

}<br />

}<br />

return result;<br />

}<br />

Listing 9-13 shows how you can avoid writing too much code to initialize the database. You removed the initialize<br />

code that had a lot of lines to read two files in order to create and populate the database with the information you needed.<br />

Now, let’s see the unit test (see Listing 9-14).<br />

Listing 9-14. MyDocumentsJDBCEmbeddedAnnotatedTest.java<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.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-embedded-context.xml")<br />

public class MyDocumentsJDBCEmbeddedAnnotatedTest {<br />

@Autowired<br />

private SearchEngine engine;<br />

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

@Test<br />

public void testJDBCEmbedded() {<br />

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

assertNotNull(documents);<br />

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

assertEquals(webType.getName(),documents.get(0).getType().getName());<br />

assertEquals(webType.getExtension(),documents.get(0).getType().getExtension());<br />

128

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

Saved successfully!

Ooh no, something went wrong!