Skip to main content
summaryrefslogtreecommitdiffstats
blob: 922a78aa470ddb12bc02c3cbeb5bfe246bb3c66a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.tests;

import javax.xml.namespace.QName;

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.ExtensibilityElement;
import org.eclipse.wst.wsdl.WSDLPackage;
import org.eclipse.wst.wsdl.WSDLPlugin;
import org.eclipse.wst.wsdl.binding.soap.SOAPHeader;
import org.eclipse.wst.wsdl.binding.soap.internal.util.SOAPConstants;
import org.eclipse.wst.wsdl.internal.impl.ExtensibilityElementImpl;
import org.eclipse.wst.wsdl.internal.util.WSDLResourceFactoryImpl;
import org.eclipse.wst.wsdl.tests.util.DefinitionLoader;
import org.eclipse.wst.wsdl.util.ExtensibilityElementFactory;
import org.eclipse.wst.wsdl.util.ExtensibilityElementFactoryRegistry;
import org.eclipse.wst.wsdl.util.WSDLConstants;
import org.eclipse.wst.wsdl.util.WSDLResourceImpl;
import org.eclipse.xsd.XSDPackage;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class UtilTest extends TestCase {
	  private String PLUGIN_ABSOLUTE_PATH = WSDLTestsPlugin.getInstallURL();
	  
	  public UtilTest(String name) 
	  {
	    super(name);
	  }
	
	  public static void main(String args[]) 
	  {
	    junit.textui.TestRunner.run(suite());
	  }

	  public static Test suite() 
	  {
	    TestSuite suite = new TestSuite();
	    
	    suite.addTest
	      (new UtilTest("WSDLConstants") 
	         {
	           protected void runTest() 
	           {
				 testConstants();
	           }
	         }
	       );
	    
	    suite.addTest
	      (new UtilTest("WSDLResourceFactoryImpl") 
	        {
	          protected void runTest() 
	          {
	            testWSDLResourceFactoryImpl();
	          }
	        }
	      );
		
	    suite.addTest
	      (new UtilTest("WSDLResourceImpl") 
	        {
	          protected void runTest() 
	          {
	            testWSDLResourceImpl();
	          }
	        }
	      );
	    suite.addTest
	      (new UtilTest("ExtensibilityElementFactory") 
	        {
	          protected void runTest() 
	          {
	            testExtensibilityElementFactory();
	          }
	        }
	      );
	    suite.addTest
	      (new UtilTest("ExtensibilityElementFactoryRegistry") 
	        {
	          protected void runTest() 
	          {
	            testExtensibilityElementFactoryRegistry();
	          }
	        }
	      );
		

		
	    return suite;
	  }
	  
	  protected void setUp() throws Exception 
	  {
	    super.setUp();
	    
	    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("wsdl", new WSDLResourceFactoryImpl());
	    WSDLPackage pkg = WSDLPackage.eINSTANCE;
	    
	    // We need this for XSD <import>.
	    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", new XSDResourceFactoryImpl());
	    XSDPackage xsdpkg = XSDPackage.eINSTANCE; 
	  }

	  protected void tearDown() throws Exception 
	  {
	    super.tearDown();
	  }  
	  
	  static private Definition definition = null;
	  
	  public void testConstants()
	  {
        try
		{
		  WSDLConstants wsdlConstants = new WSDLConstants();
		  int type = WSDLConstants.nodeType(WSDLConstants.PORT_ELEMENT_TAG);
		  Assert.assertTrue("Node type is not that of port", type == 10);
		  
	      definition = DefinitionLoader.load(PLUGIN_ABSOLUTE_PATH + "samples/LoadAndPrintTest.wsdl");
	      Assert.assertTrue(definition.eResource() instanceof WSDLResourceImpl);

		  int definitionNodeType = WSDLConstants.nodeType(definition.getElement());
		  Assert.assertTrue("Node type is not that of definition", definitionNodeType == 1);
		  
		  boolean isWSDLNamespace = WSDLConstants.isWSDLNamespace(definition.getTargetNamespace());
		  Assert.assertFalse("This should not be the WSDL Namespace", isWSDLNamespace);
		  
		  boolean isMatching = WSDLConstants.isMatchingNamespace("http://www.example.org", "http://www.example.org");  
		  Assert.assertTrue("isMatchingNamespace is incorrect", isMatching);
		  
		  String attr = WSDLConstants.getAttribute(definition.getElement(), "name");
		  Assert.assertTrue("getAttribute is incorrect", attr.equals("LoadAndPrintTest"));
		}
		catch (Exception e)
	    {
	      Assert.fail("Test failed due to an exception: " + e.getLocalizedMessage());
		}
	  }
  
	  public void testWSDLResourceFactoryImpl()
	  {
	    try
	    {
		  WSDLResourceFactoryImpl factoryImpl = new WSDLResourceFactoryImpl();
		  Resource resource = factoryImpl.createResource(URI.createFileURI("./samples/createResourceTest.wsdl"));
          Assert.assertTrue("Resource is not of type WSDLResourceImpl", resource instanceof WSDLResourceImpl);
	    }
	    catch (Exception e)
	    {
	      Assert.fail("Test failed due to an exception: " + e.getLocalizedMessage());
	    }   
	  }
	  
	  public void testWSDLResourceImpl()
	  {
	    try
		{
		  WSDLResourceImpl resourceImpl = new WSDLResourceImpl(URI.createFileURI("./samples/createResourceTest.wsdl"));
          Assert.assertTrue("Resource is not of type WSDLResourceImpl", resourceImpl instanceof WSDLResourceImpl);
		  
	      definition = DefinitionLoader.load(PLUGIN_ABSOLUTE_PATH + "samples/LoadAndPrintTest.wsdl");
	      Assert.assertTrue(definition.eResource() instanceof WSDLResourceImpl);
		  Element element = definition.getElement();
		  Document document = definition.getDocument();

	      if (element != null)
	      {
	        resourceImpl.serialize(System.out, element, null);
	      }
		  
		  if (document != null)
		  {
			resourceImpl.serialize(System.out, document, null);
		  }
		  
		  try
		  {
		    resourceImpl.attached(definition);
		  }
		  catch (Exception e)
		  {

		  }
	  
		}
		catch (Exception e)
		{
		  Assert.fail("Test failed due to an exception: " + e.getLocalizedMessage());
		}   
	  }
	  
	  public void testExtensibilityElementFactory()
	  {
	    try
	    {
		    ExtensibilityElementFactory factory = WSDLPlugin.INSTANCE.getExtensibilityElementFactory(SOAPConstants.SOAP_NAMESPACE_URI);
		    if (factory != null)
			{
		      ExtensibilityElement ee = factory.createExtensibilityElement(SOAPConstants.SOAP_NAMESPACE_URI, SOAPConstants.HEADER_ELEMENT_TAG);
              Assert.assertTrue("Problem creating SOAP extensibility element", ee instanceof SOAPHeader);
			}
	    }
		catch (Exception e)
		{
		  Assert.fail("Test failed due to an exception: " + e.getLocalizedMessage());
		}   
	  }
	  
	  class TestExtensibilityElement extends ExtensibilityElementImpl implements ExtensibilityElement
	  {
		String ns, name;
		
		public TestExtensibilityElement(String ns, String name)
		{
		  super();
		  this.ns = ns;
		  this.name = name;
		}

		public QName getElementType()
		{
		  if (elementType == null)
		  {
			elementType = new QName(ns, name);
		  }
	      return elementType;
		}
	  }
	  
	  class WSDLTestFactory implements ExtensibilityElementFactory
	  {
		public WSDLTestFactory()
		{
		}
				  
		public ExtensibilityElement createExtensibilityElement(String namespace, String localName)
		{
		  return new TestExtensibilityElement(namespace, localName);
		}
	  }
	  
	  public void testExtensibilityElementFactoryRegistry()
	  {
	    try
	    {	
		    ExtensibilityElementFactoryRegistry factoryRegistry = WSDLPlugin.INSTANCE.getExtensibilityElementFactoryRegistry();
			factoryRegistry.registerFactory("http://org.eclipse.wst.wsdl.tests", new WSDLTestFactory());
			
			ExtensibilityElementFactory factory = WSDLPlugin.INSTANCE.getExtensibilityElementFactory("http://org.eclipse.wst.wsdl.tests");
			ExtensibilityElement ee = factory.createExtensibilityElement("http://org.eclipse.wst.wsdl.tests", "TestElement");
			Assert.assertTrue("1. Problem creating custom Test extensibility element", ee instanceof TestExtensibilityElement);
			Assert.assertTrue("2. Problem creating custom Test extensibility element", ee.getElementType().getLocalPart().equals("TestElement"));
			Assert.assertTrue("3. Problem creating custom Test extensibility element", ee.getElementType().getNamespaceURI().equals("http://org.eclipse.wst.wsdl.tests"));
			
	    }
		catch (Exception e)
		{
		  Assert.fail("Test failed due to an exception: " + e.getLocalizedMessage());
		}   
	  }

}

Back to the top