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

Commit

Permalink
[ch07] Test JAXB for sqlmap.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Oct 11, 2017
1 parent d86a72d commit bc9e655
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 0 deletions.
22 changes: 22 additions & 0 deletions sqlmap.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.epril.com/sqlmap"
xmlns:tns="http://www.epril.com/sqlmap" elementFormDefault="qualified">

<element name="sqlmap">
<complexType>
<sequence>
<element name="sql" maxOccurs="unbounded" type="tns:sqlType" />
</sequence>
</complexType>
</element>

<complexType name="sqlType">
<simpleContent>
<extension base="string">
<attribute name="key" use="required" type="string" />
</extension>
</simpleContent>
</complexType>

</schema>
55 changes: 55 additions & 0 deletions src/main/java/ch07/springbook/sql/ObjectFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// 이 파일은 JAXB(JavaTM Architecture for XML Binding) 참조 구현 2.2.8-b130911.1802 버전을 통해 생성되었습니다.
// <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>를 참조하십시오.
// 이 파일을 수정하면 소스 스키마를 재컴파일할 때 수정 사항이 손실됩니다.
// 생성 날짜: 2017.10.10 시간 08:59:54 PM KST
//


package ch07.springbook.sql;

import javax.xml.bind.annotation.XmlRegistry;


/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the ch07.springbook.sql package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {


/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ch07.springbook.sql
*
*/
public ObjectFactory() {
}

/**
* Create an instance of {@link Sqlmap }
*
*/
public Sqlmap createSqlmap() {
return new Sqlmap();
}

/**
* Create an instance of {@link SqlType }
*
*/
public SqlType createSqlType() {
return new SqlType();
}

}
94 changes: 94 additions & 0 deletions src/main/java/ch07/springbook/sql/SqlType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// 이 파일은 JAXB(JavaTM Architecture for XML Binding) 참조 구현 2.2.8-b130911.1802 버전을 통해 생성되었습니다.
// <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>를 참조하십시오.
// 이 파일을 수정하면 소스 스키마를 재컴파일할 때 수정 사항이 손실됩니다.
// 생성 날짜: 2017.10.10 시간 08:59:54 PM KST
//


package ch07.springbook.sql;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
* <p>sqlType complex type에 대한 Java 클래스입니다.
*
* <p>다음 스키마 단편이 이 클래스에 포함되는 필요한 콘텐츠를 지정합니다.
*
* <pre>
* &lt;complexType name="sqlType">
* &lt;simpleContent>
* &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
* &lt;attribute name="key" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;/extension>
* &lt;/simpleContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sqlType", propOrder = {
"value"
})
public class SqlType {

@XmlValue
protected String value;
@XmlAttribute(name = "key", required = true)
protected String key;

/**
* value 속성의 값을 가져옵니다.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}

/**
* value 속성의 값을 설정합니다.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}

/**
* key 속성의 값을 가져옵니다.
*
* @return
* possible object is
* {@link String }
*
*/
public String getKey() {
return key;
}

/**
* key 속성의 값을 설정합니다.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setKey(String value) {
this.key = value;
}

}
78 changes: 78 additions & 0 deletions src/main/java/ch07/springbook/sql/Sqlmap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// 이 파일은 JAXB(JavaTM Architecture for XML Binding) 참조 구현 2.2.8-b130911.1802 버전을 통해 생성되었습니다.
// <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>를 참조하십시오.
// 이 파일을 수정하면 소스 스키마를 재컴파일할 때 수정 사항이 손실됩니다.
// 생성 날짜: 2017.10.10 시간 08:59:54 PM KST
//


package ch07.springbook.sql;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>anonymous complex type에 대한 Java 클래스입니다.
*
* <p>다음 스키마 단편이 이 클래스에 포함되는 필요한 콘텐츠를 지정합니다.
*
* <pre>
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="sql" type="{http://www.epril.com/sqlmap}sqlType" maxOccurs="unbounded"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"sql"
})
@XmlRootElement(name = "sqlmap")
public class Sqlmap {

@XmlElement(required = true)
protected List<SqlType> sql;

/**
* Gets the value of the sql property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the sql property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getSql().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link SqlType }
*
*
*/
public List<SqlType> getSql() {
if (sql == null) {
sql = new ArrayList<SqlType>();
}
return this.sql;
}

}
9 changes: 9 additions & 0 deletions src/main/java/ch07/springbook/sql/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// 이 파일은 JAXB(JavaTM Architecture for XML Binding) 참조 구현 2.2.8-b130911.1802 버전을 통해 생성되었습니다.
// <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>를 참조하십시오.
// 이 파일을 수정하면 소스 스키마를 재컴파일할 때 수정 사항이 손실됩니다.
// 생성 날짜: 2017.10.10 시간 08:59:54 PM KST
//

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.epril.com/sqlmap", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ch07.springbook.sql;
10 changes: 10 additions & 0 deletions src/main/resources/sqlmap_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<sqlmap xmlns="http://www.epril.com/sqlmap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.epril.com/sqlmap ../../../sqlmap.xsd ">

<sql key="add">insert</sql>
<sql key="get">select</sql>
<sql key="delete">delete</sql>

</sqlmap>
38 changes: 38 additions & 0 deletions src/test/java/ch07/springbook/JaxbTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.junit.Test;

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

public class JaxbTest {

@Test
public void readSqlMap() throws JAXBException, IOException {
String contextPath = Sqlmap.class.getPackage().getName();
JAXBContext context = JAXBContext.newInstance(contextPath);
Unmarshaller unmarshaller = context.createUnmarshaller();

Sqlmap sqlmap = (Sqlmap)unmarshaller.unmarshal(getClass().getResourceAsStream("/sqlmap_test.xml"));
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 bc9e655

Please sign in to comment.