KickJava   Java API By Example, From Geeks To Geeks.

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


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.jcr.api.serialization;
6
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.apache.commons.lang.ArrayUtils;
17 import org.exoplatform.container.PortalContainer;
18 import org.exoplatform.services.jcr.BaseTest;
19 import org.exoplatform.services.xml.querying.InvalidSourceException;
20 import org.exoplatform.services.xml.querying.InvalidStatementException;
21 import org.exoplatform.services.xml.querying.QueryRunTimeException;
22 import org.exoplatform.services.xml.querying.UniFormTransformationException;
23 import org.exoplatform.services.xml.querying.XMLQuery;
24 import org.exoplatform.services.xml.querying.XMLQueryingService;
25 import org.exoplatform.services.xml.querying.helper.SimpleStatementHelper;
26 import org.exoplatform.services.xml.querying.helper.XMLDataManager;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * Created y the eXo platform team
33  * User: Benjamin Mestrallet
34  * Date: 17 ao�t 2004
35  */

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