KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbicommon > descriptor > JBIDescriptorBuilderTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: JBIDescriptorBuilderTest.java 632 2006-06-22 07:28:26Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.tools.jbicommon.descriptor;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.xml.transform.TransformerException JavaDoc;
32
33 import junit.framework.TestCase;
34
35 import org.easymock.classextension.EasyMock;
36 import org.objectweb.petals.tools.jbicommon.util.XMLUtil;
37 import org.w3c.dom.Document JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39
40 /**
41  * @version $Rev: 632 $ $Date: 2006-06-22 07:28:26Z $
42  * @since Petals 1.0
43  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
44  * @author ddesjardins - eBMWebsourcing
45  */

46 public class JBIDescriptorBuilderTest extends TestCase { // NOPMD by ofabre
47

48     private static final String JavaDoc TARGET = "target";
49
50     private String JavaDoc baseDir;
51
52     public void setUp() {
53         baseDir = this.getClass().getResource(".").toString();
54         baseDir = baseDir.substring(0, baseDir.indexOf(TARGET));
55         baseDir = baseDir.substring(baseDir.indexOf(':') + 1);
56     }
57
58     // public void testExtractXmlAttributeQName() throws SecurityException,
59
// NoSuchMethodException {
60
// JBIDescriptorBuilder builder = EasyMock.createMock(
61
// JBIDescriptorBuilder.class,
62
// new Method[] {JBIDescriptorBuilder.class.getDeclaredMethod(
63
// "buildDescriptor", new Class[] {Document.class})});
64
//
65
// Node node = EasyMock.createMock(Node.class);
66
// NamedNodeMap nodeMap = EasyMock.createMock(NamedNodeMap.class);
67
// Node outNode = EasyMock.createMock(Node.class);
68
//
69
// EasyMock.expect(node.getAttributes()).andReturn(nodeMap);
70
// EasyMock.expect(nodeMap.getNamedItem("test")).andReturn(outNode);
71
// EasyMock.expect(outNode.getNodeValue()).andReturn("out");
72
//
73
// EasyMock.replay(nodeMap);
74
// EasyMock.replay(outNode);
75
// EasyMock.replay(node);
76
//
77
// assertEquals(QName.valueOf("out"), builder.extractXmlAttributeQName(
78
// node, "test"));
79
// }
80

81     // public void testExtractXmlAttributeQNameWithNS() throws
82
// SecurityException,
83
// NoSuchMethodException {
84
// JBIDescriptorBuilder builder = EasyMock.createMock(
85
// JBIDescriptorBuilder.class,
86
// new Method[] {JBIDescriptorBuilder.class.getDeclaredMethod(
87
// "buildDescriptor", new Class[] {Document.class})});
88
//
89
// Node node = EasyMock.createMock(Node.class);
90
// NamedNodeMap nodeMap = EasyMock.createMock(NamedNodeMap.class);
91
// Node outNode = EasyMock.createMock(Node.class);
92
// Node nsNode = EasyMock.createMock(Node.class);
93
//
94
// EasyMock.expect(node.getAttributes()).andReturn(nodeMap).anyTimes();
95
// EasyMock.expect(nodeMap.getNamedItem("test")).andReturn(outNode);
96
// EasyMock.expect(outNode.getNodeValue()).andReturn("ns:out");
97
// EasyMock.expect(node.getParentNode()).andReturn(null).anyTimes();
98
// EasyMock.expect(nodeMap.getLength()).andReturn(1);
99
// EasyMock.expect(nodeMap.item(0)).andReturn(nsNode);
100
// EasyMock.expect(nsNode.getNodeName()).andReturn("xmlns:ns");
101
// EasyMock.expect(nsNode.getNodeValue()).andReturn("outvalue");
102
//
103
// EasyMock.replay(nsNode);
104
// EasyMock.replay(nodeMap);
105
// EasyMock.replay(outNode);
106
// EasyMock.replay(node);
107
//
108
// assertEquals(QName.valueOf("{outvalue}out"), builder
109
// .extractXmlAttributeQName(node, "test"));
110
// }
111

112     public void testParseJbiXml() throws SecurityException JavaDoc,
113             NoSuchMethodException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
114         File JavaDoc jbiFile = new File JavaDoc(baseDir + "src" + File.separator + "test-data"
115                 + File.separator + "descriptors" + File.separator
116                 + "emptyDescriptor.xml");
117
118         JBIDescriptorBuilder builder = EasyMock.createMock(
119                 JBIDescriptorBuilder.class, new Method JavaDoc[] {
120                         JBIDescriptorBuilder.class.getDeclaredMethod(
121                                 "validateJbiXml", new Class JavaDoc[] { URI JavaDoc.class }),
122                         JBIDescriptorBuilder.class.getDeclaredMethod(
123                                 "buildDescriptor",
124                                 new Class JavaDoc[] { Document JavaDoc.class }) });
125
126         try {
127             assertNotNull(builder.parseJbiXml(jbiFile.toURI()));
128         } catch (JBIDescriptorException e) {
129             e.printStackTrace();
130         }
131     }
132
133     public void testValidateJbiXml() throws SecurityException JavaDoc,
134             NoSuchMethodException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
135         File JavaDoc jbiFile = new File JavaDoc(baseDir + "src" + File.separator + "test-data"
136                 + File.separator + "descriptors" + File.separator
137                 + "componentInstallationDescriptor.xml");
138
139         JBIDescriptorBuilder builder = EasyMock.createMock(
140                 JBIDescriptorBuilder.class,
141                 new Method JavaDoc[] { JBIDescriptorBuilder.class.getDeclaredMethod(
142                         "buildDescriptor", new Class JavaDoc[] { Document JavaDoc.class }) });
143         Logger JavaDoc logger = EasyMock.createMock(Logger JavaDoc.class);
144
145         EasyMock.replay(logger);
146
147         builder.setLog(logger);
148
149         assertTrue(builder.validateJbiXml(jbiFile.toURI()));
150     }
151
152     public void testValidateJbiXmlException() throws SecurityException JavaDoc,
153             NoSuchMethodException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
154         File JavaDoc jbiFile = new File JavaDoc(baseDir + "src" + File.separator + "test-data"
155                 + File.separator + "descriptors" + File.separator
156                 + "badDescriptor.xml");
157
158         JBIDescriptorBuilder builder = EasyMock.createMock(
159                 JBIDescriptorBuilder.class,
160                 new Method JavaDoc[] { JBIDescriptorBuilder.class.getDeclaredMethod(
161                         "buildDescriptor", new Class JavaDoc[] { Document JavaDoc.class }) });
162         Logger JavaDoc logger = EasyMock.createMock(Logger JavaDoc.class);
163
164         builder.setLog(logger);
165
166         assertFalse(builder.validateJbiXml(jbiFile.toURI()));
167     }
168
169     public void testComponentDescriptorBuild() {
170         File JavaDoc jbiFile = new File JavaDoc(baseDir + "src" + File.separator + "test-data"
171                 + File.separator + "descriptors" + File.separator
172                 + "componentInstallationDescriptor.xml");
173         Logger JavaDoc logger = EasyMock.createMock(Logger JavaDoc.class);
174         JBIDescriptorBuilder descriptorBuilder = new JBIDescriptorBuilder(
175                 jbiFile.toURI(), logger);
176
177         try {
178             JBIDescriptor generatedDescriptor = descriptorBuilder.build();
179             System.out.println("###### A Component Descriptor");
180             System.out.println(generatedDescriptor);
181             System.out.println(generatedDescriptor.getComponent()
182                     .getExtensions());
183             System.out.println(generatedDescriptor.getComponent()
184                     .getIdentification().getExtensions());
185             System.out.println(XMLUtil
186                     .createStringFromDOMDocument(generatedDescriptor
187                             .getComponent().getExtensions()
188                             .getDocumentFragment()));
189         } catch (JBIDescriptorException e) {
190             e.printStackTrace();
191         } catch (TransformerException JavaDoc e) {
192             e.printStackTrace();
193         }
194
195     }
196
197     public void testSharedLibDescriptorBuild() {
198         File JavaDoc jbiFile = new File JavaDoc(baseDir + "src" + File.separator + "test-data"
199                 + File.separator + "descriptors" + File.separator
200                 + "sharedLibraryInstallationDescriptor.xml");
201         Logger JavaDoc logger = EasyMock.createMock(Logger JavaDoc.class);
202         JBIDescriptorBuilder descriptorBuilder = new JBIDescriptorBuilder(
203                 jbiFile.toURI(), logger);
204
205         try {
206             JBIDescriptor generatedDescriptor = descriptorBuilder.build();
207             System.out.println("###### A Shared Lib Descriptor");
208             System.out.println(generatedDescriptor);
209             System.out.println(generatedDescriptor.getSharedLibrary()
210                     .getIdentification().getExtensions());
211         } catch (JBIDescriptorException e) {
212             e.printStackTrace();
213         }
214
215     }
216
217     public void testServiceAssemblyDescriptorBuild() {
218         File JavaDoc jbiFile = new File JavaDoc(baseDir + "src" + File.separator + "test-data"
219                 + File.separator + "descriptors" + File.separator
220                 + "serviceAssemblyDescriptor.xml");
221         Logger JavaDoc logger = EasyMock.createMock(Logger JavaDoc.class);
222         JBIDescriptorBuilder descriptorBuilder = new JBIDescriptorBuilder(
223                 jbiFile.toURI(), logger);
224
225         try {
226             JBIDescriptor generatedDescriptor = descriptorBuilder.build();
227             System.out.println("###### A Service Assembly Descriptor");
228             System.out.println(generatedDescriptor);
229             System.out.println(generatedDescriptor.getServiceAssembly()
230                     .getExtensions());
231             System.out.println(generatedDescriptor.getServiceAssembly()
232                     .getServiceUnits().get(1).getExtensions());
233             System.out.println(generatedDescriptor.getServiceAssembly()
234                     .getIdentification().getExtensions());
235         } catch (JBIDescriptorException e) {
236             e.printStackTrace();
237         }
238     }
239
240     public void testServicesDescriptorBuild() {
241         File JavaDoc jbiFile = new File JavaDoc(baseDir + "src" + File.separator + "test-data"
242                 + File.separator + "descriptors" + File.separator
243                 + "servicesDescriptor.xml");
244         Logger JavaDoc logger = EasyMock.createMock(Logger JavaDoc.class);
245         JBIDescriptorBuilder descriptorBuilder = new JBIDescriptorBuilder(
246                 jbiFile.toURI(), logger);
247
248         try {
249             JBIDescriptor generatedDescriptor = descriptorBuilder.build();
250             System.out.println("###### A Services Descriptor");
251             System.out.println(generatedDescriptor);
252             System.out.println(generatedDescriptor.getServices().getConsumes()
253                     .get(0).getExtensions());
254             System.out.println(generatedDescriptor.getServices().getProvides()
255                     .get(0).getExtensions());
256         } catch (JBIDescriptorException e) {
257             e.printStackTrace();
258         }
259     }
260 }
261
Popular Tags