Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 75 and 76: Chapter 6 ■ Using Resource Files
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
- Page 125: Chapter 9 ■ Adding Persistence to
- Page 129 and 130: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137 and 138: Chapter 10 ■ Showing Your Spring
- Page 139 and 140: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
- Page 149 and 150: Chapter 11 ■ Integrating Your Spr
- Page 151 and 152: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
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