KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > TestXtasImpl


1 /**
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.xml.querying.impl.xtas;
6
7 import junit.framework.TestCase;
8 import org.exoplatform.container.PortalContainer;
9 import org.exoplatform.services.xml.querying.Statement;
10 import org.exoplatform.services.xml.querying.XMLQueryingService;
11 import org.exoplatform.services.xml.querying.impl.xtas.Query;
12 import org.exoplatform.services.xml.querying.impl.xtas.UniFormConverter;
13 import org.exoplatform.services.xml.querying.impl.xtas.UniFormTree;
14 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
15 import org.xml.sax.InputSource JavaDoc;
16
17 import java.io.ByteArrayInputStream JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 /**
21  * Created by the Exo Development team.
22  */

23 public class TestXtasImpl extends TestCase {
24
25    public void setUp() throws Exception JavaDoc
26    {
27    }
28
29    public void testSelect() throws Exception JavaDoc
30    {
31        try {
32
33           PortalContainer manager = PortalContainer.getInstance();
34           XMLQueryingService service = (XMLQueryingService) manager.getComponentInstanceOfType(XMLQueryingService.class);
35           Query query = (Query)service.createQuery();
36
37           Statement qc = service.createStatementHelper().select("employees/employee[firstname='Gennady']", "tmp/employees.xml", "tmp/employees-out.xml");
38           query.prepare(qc);
39           query.execute();
40           query.serialize();
41
42           qc = service.createStatementHelper().select("employees/employee[firstname='Gennady']/lastname/text()", "tmp/employees.xml");
43           query.prepare(qc);
44           query.execute();
45
46           assertEquals( "Azarenkov", query.getResult().toString());
47
48        } catch ( Exception JavaDoc e) {
49
50             fail( "testSelect() ERROR: "+e.toString());
51         }
52
53    }
54
55
56    public void testDelete() throws Exception JavaDoc
57    {
58        try {
59
60           PortalContainer manager = PortalContainer.getInstance();
61           XMLQueryingService service = (XMLQueryingService) manager.getComponentInstanceOfType(XMLQueryingService.class);
62           Query query = (Query)service.createQuery();
63
64           Statement qc = service.createStatementHelper().delete("employees/employee[firstname='Gennady']", "tmp/employees.xml", "tmp/employees-del.xml");
65           query.prepare(qc);
66           query.execute();
67           query.serialize();
68  
69           qc = service.createStatementHelper().select("count(employee[firstname='Gennady']/lastname/text())");
70           query.prepareNext(qc);
71           query.execute();
72
73           assertEquals( "0", query.getResult().toString());
74
75        } catch ( Exception JavaDoc e) {
76
77             fail( "testDelete() ERROR: "+e.toString());
78        }
79
80    }
81
82    public void testUpdate() throws Exception JavaDoc
83    {
84        try {
85
86           PortalContainer manager = PortalContainer.getInstance();
87           XMLQueryingService service = (XMLQueryingService) manager.
88               getComponentInstanceOfType(XMLQueryingService.class);
89           Query query = (Query)service.createQuery();
90
91           WellFormedUniFormTree tree = new WellFormedUniFormTree();
92           tree.init(new InputSource JavaDoc(new ByteArrayInputStream JavaDoc("<lastname>Azarenkov1</lastname>".getBytes())));
93           Statement qc = service.createStatementHelper().update("employees/employee[firstname='Gennady']/lastname", "tmp/employees.xml", tree);
94           query.prepare(qc);
95           query.execute();
96  
97           qc = service.createStatementHelper().select("employees/employee[firstname='Gennady']/lastname/text()");
98           query.prepareNext(qc);
99           query.execute();
100
101           assertEquals( "Azarenkov1", query.getResult().toString());
102
103
104        } catch ( Exception JavaDoc e) {
105
106             fail( "testUpdate() ERROR: "+e.toString());
107        }
108
109    }
110
111    public void testAppend() throws Exception JavaDoc
112    {
113        try {
114
115           PortalContainer manager = PortalContainer.getInstance();
116           XMLQueryingService service = (XMLQueryingService) manager.
117               getComponentInstanceOfType(XMLQueryingService.class);
118           Query query = (Query)service.createQuery();
119
120           WellFormedUniFormTree tree = new WellFormedUniFormTree();
121           tree.init(new InputSource JavaDoc(new ByteArrayInputStream JavaDoc("<employee id=\"N4543\"><firstname>Bill</firstname><lastname>Gates</lastname></employee>".getBytes())));
122           Statement qc = service.createStatementHelper().append("employees/*[last()]", "tmp/employees.xml", tree);
123           query.prepare(qc);
124           query.execute();
125
126           qc = service.createStatementHelper().select("employees/employee[lastname='Gates']/firstname/text()");
127           query.prepareNext(qc);
128           query.execute();
129
130           assertEquals( "Bill", query.getResult().toString());
131
132
133        } catch ( Exception JavaDoc e) {
134
135             fail( "testAppend() ERROR: "+e.toString());
136        }
137
138    }
139
140    public void testCreateDrop()
141     {
142
143        Statement stat = null;
144        String JavaDoc f1 = "tmp/new-res.xml";
145        String JavaDoc updStr = "<employee><firstname>Gennady</firstname></employee>";
146
147        try {
148
149           PortalContainer manager = PortalContainer.getInstance();
150           XMLQueryingService service = (XMLQueryingService) manager.
151               getComponentInstanceOfType(XMLQueryingService.class);
152           Query query = (Query)service.createQuery();
153
154           WellFormedUniFormTree tree = new WellFormedUniFormTree();
155           tree.init(new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(updStr.getBytes())));
156
157           stat = service.createStatementHelper().create(f1, tree);
158           query.prepare(stat);
159           query.execute();
160           query.serialize();
161
162           String JavaDoc str = util.getFileContent(f1);
163
164           query.prepare(service.createStatementHelper().drop(f1));
165           query.execute();
166
167           assertEquals( "", util.XML_DECLARATION+updStr, str );
168
169         } catch ( Exception JavaDoc e) {
170 // e.printStackTrace();
171
fail( "testCreateDrop() ERROR: "+e.toString() + " "+stat);
172         }
173
174     }
175
176    public void testObjectMarshalling()
177     {
178
179        try {
180
181             PortalContainer manager = PortalContainer.getInstance();
182             XMLQueryingService service = (XMLQueryingService) manager.
183                 getComponentInstanceOfType(XMLQueryingService.class);
184             Query query = (Query)service.createQuery();
185
186             WellFormedUniFormTree tree = new WellFormedUniFormTree();
187             tree.init(new City("UK","London","55555"));
188
189             query.prepare(service.createStatementHelper().select("/",null,"tmp/addresses-out.xml"));
190             query.setInput(tree);
191             query.execute();
192             query.serialize();
193
194             query.prepare(service.createStatementHelper().select("/","tmp/addresses-out.xml"));
195             query.execute();
196             Iterator JavaDoc iter = UniFormConverter.toFragment((UniFormTree)query.getResult()).
197                             getAsCollection(City.class).iterator();
198             City city = (City)iter.next();
199
200             assertEquals( "London", city.getName() );
201             assertEquals( "UK", city.getCountry() );
202
203
204         } catch ( Exception JavaDoc e) {
205 // e.printStackTrace();
206
fail( "testObjectMarshalling() ERROR: "+e.toString());
207         }
208
209     }
210
211 /*
212    public void testJdbcResource()
213     {
214
215        try {
216
217           PortalContainer manager = PortalContainer.getInstance();
218           XMLQueryingService service = (XMLQueryingService) manager.getService(XMLQueryingServiceImpl.class);
219           Query query = (Query)service.createQuery();
220
221           // Load the HSQL Database Engine JDBC driver
222           String driverClass = "org.hsqldb.jdbcDriver";
223
224           connURL = "jdbc:hsqldb:./tmp/test?user=sa;rootTable=exo_user";
225
226           query.prepare(SimpleStatementHelper..select("tables/exo_user", connURL);
227           query.execute();
228
229         } catch ( Exception e) {
230 // e.printStackTrace();
231             fail( "testObjectMarshalling() ERROR: "+e.toString());
232         }
233
234
235     }
236 */

237
238 }
239
Popular Tags