KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > dom4j > Dom4jAccessorTest


1 // $Id: Dom4jAccessorTest.java,v 1.3 2005/02/21 05:35:11 oneovthafew Exp $
2
package org.hibernate.test.dom4j;
3
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import org.dom4j.DocumentFactory;
8 import org.dom4j.Element;
9 import org.dom4j.util.NodeComparator;
10 import org.hibernate.EntityMode;
11 import org.hibernate.engine.SessionFactoryImplementor;
12 import org.hibernate.mapping.Property;
13 import org.hibernate.mapping.SimpleValue;
14 import org.hibernate.property.Getter;
15 import org.hibernate.property.PropertyAccessorFactory;
16 import org.hibernate.property.Setter;
17 import org.hibernate.test.TestCase;
18
19 /**
20  * Implementation of Dom4jAccessorTest.
21  *
22  * @author Steve Ebersole
23  */

24 public class Dom4jAccessorTest extends TestCase {
25
26     public static final Element DOM = generateTestElement();
27
28     public Dom4jAccessorTest(String JavaDoc name) {
29         super(name);
30     }
31
32     protected String JavaDoc[] getMappings() {
33         return new String JavaDoc[0];
34     }
35
36     public void testStringElementExtraction() throws Throwable JavaDoc {
37         Property property = generateNameProperty();
38         Getter getter = PropertyAccessorFactory.getPropertyAccessor( property, EntityMode.DOM4J ).getGetter( null, null );
39         String JavaDoc name = ( String JavaDoc ) getter.get( DOM );
40         assertEquals( "Not equals", "JBoss", name );
41     }
42
43     public void testStringTextExtraction() throws Throwable JavaDoc {
44         Property property = generateTextProperty();
45         Getter getter = PropertyAccessorFactory.getPropertyAccessor( property, EntityMode.DOM4J ).getGetter( null, null );
46         String JavaDoc name = ( String JavaDoc ) getter.get( DOM );
47         assertEquals( "Not equals", "description...", name );
48     }
49
50     public void testLongAttributeExtraction() throws Throwable JavaDoc {
51         Property property = generateIdProperty();
52         Getter getter = PropertyAccessorFactory.getPropertyAccessor( property, EntityMode.DOM4J ).getGetter( null, null );
53         Long JavaDoc id = ( Long JavaDoc ) getter.get( DOM );
54         assertEquals( "Not equals", new Long JavaDoc(123), id );
55     }
56
57     public void testLongElementAttributeExtraction() throws Throwable JavaDoc {
58         Property property = generateAccountIdProperty();
59         Getter getter = PropertyAccessorFactory.getPropertyAccessor( property, EntityMode.DOM4J ).getGetter( null, null );
60         Long JavaDoc id = ( Long JavaDoc ) getter.get( DOM );
61         assertEquals( "Not equals", new Long JavaDoc(456), id );
62     }
63
64     public void testCompanyElementGeneration() throws Throwable JavaDoc {
65         Setter idSetter = PropertyAccessorFactory.getPropertyAccessor( generateIdProperty(), EntityMode.DOM4J )
66                 .getSetter( null, null );
67         Setter nameSetter = PropertyAccessorFactory.getPropertyAccessor( generateNameProperty(), EntityMode.DOM4J )
68                 .getSetter( null, null );
69         Setter textSetter = PropertyAccessorFactory.getPropertyAccessor( generateTextProperty(), EntityMode.DOM4J )
70         .getSetter( null, null );
71         Setter accountIdSetter = PropertyAccessorFactory.getPropertyAccessor( generateAccountIdProperty(), EntityMode.DOM4J )
72                 .getSetter(null, null);
73         
74         Element root = generateRootTestElement();
75
76         idSetter.set( root, new Long JavaDoc(123), getSFI() );
77         textSetter.set( root, "description...", getSFI() );
78         nameSetter.set( root, "JBoss", getSFI() );
79         accountIdSetter.set( root, new Long JavaDoc(456), getSFI() );
80
81         assertTrue( "DOMs not equal", new NodeComparator().compare(DOM, root) == 0 );
82     }
83
84     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85

86     private static Element generateTestElement() {
87         Element company =generateRootTestElement();
88         company.addAttribute( "id", "123" );
89         company.setText("description...");
90         company.addElement( "name" ).setText( "JBoss" );
91         company.addElement( "account" ).addAttribute( "num", "456" );
92
93         System.out.println("Returning DOM : " + company.asXML() );
94
95         return company;
96     }
97
98     private static Element generateRootTestElement() {
99         return DocumentFactory.getInstance().createElement("company");
100     }
101
102     public static Test suite() {
103         return new TestSuite( Dom4jAccessorTest.class );
104     }
105
106     private SessionFactoryImplementor getSFI() {
107         return ( SessionFactoryImplementor ) getSessions();
108     }
109
110     private Property generateIdProperty() {
111         SimpleValue value = new SimpleValue();
112         value.setTypeName( "long" );
113
114         Property property = new Property();
115         property.setName( "id" );
116         property.setNodeName( "@id" );
117         property.setValue( value );
118
119         return property;
120     }
121
122     private Property generateTextProperty() {
123         SimpleValue value = new SimpleValue();
124         value.setTypeName( "string" );
125
126         Property property = new Property();
127         property.setName( "text" );
128         property.setNodeName( "." );
129         property.setValue( value );
130
131         return property;
132     }
133
134     private Property generateAccountIdProperty() {
135         SimpleValue value = new SimpleValue();
136         value.setTypeName( "long" );
137
138         Property property = new Property();
139         property.setName( "number" );
140         property.setNodeName( "account/@num" );
141         property.setValue( value );
142
143         return property;
144     }
145
146     private Property generateNameProperty() {
147         SimpleValue value = new SimpleValue();
148         value.setTypeName( "string" );
149
150         Property property = new Property();
151         property.setName( "name" );
152         property.setNodeName( "name" );
153         property.setValue( value );
154
155         return property;
156     }
157 }
158
Popular Tags