Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch07] Spring OXM abstraction layer test
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Oct 22, 2017
1 parent 1ba668a commit f6c2f2a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
<version>${spring.framework-version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.framework-version}</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/OxmTest-context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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-4.3.xsd" >

<bean id="unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
<property name="contextPath" value="ch07.springbook.sql.reader.jaxb" />
</bean>

</beans>
47 changes: 47 additions & 0 deletions src/test/java/ch07/springbook/OxmTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ch07.springbook;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.List;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ch07.springbook.sql.reader.jaxb.SqlType;
import ch07.springbook.sql.reader.jaxb.Sqlmap;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/OxmTest-context.xml")
public class OxmTest {

@Autowired
private Unmarshaller unmarshaller;

@Test
public void unmarshallSqlMap() throws XmlMappingException, IOException {

Source xmlSource = new StreamSource(getClass().getResourceAsStream("/sql/sqlmap_test.xml"));
Sqlmap sqlMap = (Sqlmap)unmarshaller.unmarshal(xmlSource);

List<SqlType> sqlList = sqlMap.getSql();

assertThat(sqlList.size(), is(3));

assertThat(sqlList.get(0).getKey(), is("add"));
assertThat(sqlList.get(0).getValue(), is("insert"));
assertThat(sqlList.get(1).getKey(), is("get"));
assertThat(sqlList.get(1).getValue(), is("select"));
assertThat(sqlList.get(2).getKey(), is("delete"));
assertThat(sqlList.get(2).getValue(), is("delete"));
}
}

0 comments on commit f6c2f2a

Please sign in to comment.