KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > api > serialization > ExportDocViewTest


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
6 package org.exoplatform.services.jcr.api.serialization;
7
8 import java.io.ByteArrayInputStream JavaDoc;
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import javax.jcr.BinaryValue;
12 import javax.jcr.Node;
13 import javax.jcr.RepositoryException;
14 import javax.jcr.StringValue;
15 import org.apache.commons.codec.binary.Base64;
16 import org.exoplatform.container.PortalContainer;
17 import org.exoplatform.services.jcr.BaseTest;
18 import org.exoplatform.services.xml.querying.InvalidSourceException;
19 import org.exoplatform.services.xml.querying.InvalidStatementException;
20 import org.exoplatform.services.xml.querying.QueryRunTimeException;
21 import org.exoplatform.services.xml.querying.UniFormTransformationException;
22 import org.exoplatform.services.xml.querying.XMLQuery;
23 import org.exoplatform.services.xml.querying.XMLQueryingService;
24 import org.exoplatform.services.xml.querying.helper.SimpleStatementHelper;
25 import org.exoplatform.services.xml.querying.helper.XMLDataManager;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * Created y the eXo platform team
32  * User: Benjamin Mestrallet
33  * Date: 17 ao�t 2004
34  */

35 public class ExportDocViewTest extends BaseTest {
36
37   private XMLQueryingService xmlQueryingService;
38
39   public void initRepository() throws RepositoryException {
40     Node root = ticket.getRootNode();
41     Node file = root.addNode("childNode", "nt:folder").addNode("childNode2", "nt:file");
42     Node contentNode = file.getNode("jcr:content");
43     contentNode.setProperty("exo:content", new StringValue("this is the content"));
44     file = root.getNode("childNode").addNode("childNode3", "nt:file");
45     contentNode = file.getNode("jcr:content");
46     contentNode.setProperty("exo:content", new BinaryValue("this is the binary content"));
47     ticket.save();
48
49
50     xmlQueryingService = (XMLQueryingService) PortalContainer.getInstance().
51         getComponentInstanceOfType(XMLQueryingService.class);
52   }
53
54   public void tearDown() throws RepositoryException {
55     Node root = ticket.getRootNode();
56     root.remove("childNode");
57     ticket.save();
58   }
59
60   public void testWithOutputStream() throws RepositoryException, IOException JavaDoc, InvalidSourceException,
61       InvalidStatementException, QueryRunTimeException, UniFormTransformationException {
62     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
63     workspace.exportDocView("/", out, false, false);
64     byte[] bArray = out.toByteArray();
65     SimpleStatementHelper sHelper = xmlQueryingService.createStatementHelper();
66     XMLDataManager dManager = xmlQueryingService.createXMLDataManager();
67     XMLQuery query = xmlQueryingService.createQuery();
68     query.setInputStream(new ByteArrayInputStream JavaDoc(bArray));
69     query.prepare(sHelper.select("//childNode"));
70     query.execute();
71     NodeList JavaDoc nodes = dManager.toFragment(query.getResult()).getAsNodeList();
72     assertEquals(1, nodes.getLength());
73     query.prepare(sHelper.select("//*[name()='childNode2']"));
74     query.execute();
75     nodes = dManager.toFragment(query.getResult()).getAsNodeList();
76     assertEquals(1, nodes.getLength());
77     query.prepare(sHelper.select("//*[name()='childNode3']"));
78     query.execute();
79     nodes = dManager.toFragment(query.getResult()).getAsNodeList();
80     assertEquals(1, nodes.getLength());
81
82     query.prepare(sHelper.select("//jcr:content"));
83     query.execute();
84     nodes = dManager.toFragment(query.getResult()).getAsNodeList();
85     assertEquals(2, nodes.getLength());
86     for (int i = 0; i < nodes.getLength(); i++) {
87       Element JavaDoc node = (Element JavaDoc) nodes.item(i);
88       String JavaDoc value = node.getAttribute("exo:content");
89       if (!(new String JavaDoc(Base64.encodeBase64("this is the binary content".getBytes())).
90           equals(value) || "this is the content".equals(value))) {
91         fail("incorrect property value");
92       }
93     }
94
95     out = new ByteArrayOutputStream JavaDoc();
96     workspace.exportDocView("/", out, true, false);
97     bArray = out.toByteArray();
98     query.setInputStream(new ByteArrayInputStream JavaDoc(bArray));
99     query.prepare(sHelper.select("//jcr:content"));
100     query.execute();
101     nodes = dManager.toFragment(query.getResult()).getAsNodeList();
102     assertEquals(2, nodes.getLength());
103     for (int i = 0; i < nodes.getLength(); i++) {
104       Element JavaDoc node = (Element JavaDoc) nodes.item(i);
105       String JavaDoc value = node.getAttribute("exo:content");
106       if (!("/childNode/childNode3/jcr:content/exo:content".equals(value) ||
107           "this is the content".equals(value))) {
108         fail("incorrect property value");
109       }
110     }
111
112     out = new ByteArrayOutputStream JavaDoc();
113     workspace.exportDocView("/childNode", out, true, true);
114     bArray = out.toByteArray();
115     query.setInputStream(new ByteArrayInputStream JavaDoc(bArray));
116     query.prepare(sHelper.select("childNode"));
117     query.execute();
118     nodes = dManager.toFragment(query.getResult()).getAsNodeList();
119     assertEquals(1, nodes.getLength());
120     for (int i = 0; i < nodes.getLength(); i++) {
121       Element JavaDoc node = (Element JavaDoc) nodes.item(i);
122       assertEquals(9, node.getAttributes().getLength());
123     }
124     query.prepare(sHelper.select("//*[name()='childNode3']"));
125     query.execute();
126     nodes = dManager.toFragment(query.getResult()).getAsNodeList();
127     assertEquals(0, nodes.getLength());
128   }
129
130   public void testWithContentHandler() throws RepositoryException, SAXException JavaDoc {
131     MockContentHandler mock = new MockContentHandler();
132     workspace.exportDocView("/", mock, false, false);
133
134     assertTrue(mock.reached);
135     assertEquals(6, mock.docElement);
136
137     mock = new MockContentHandler();
138     workspace.exportDocView("/childNode", mock, false, true);
139     assertEquals(1, mock.docElement);
140   }
141
142 }
143
Popular Tags