[133310] Type/element resolution fails when multiple schema imports with the same namespace are used
diff --git a/bundles/org.eclipse.wst.wsdl/src-wsdl/org/eclipse/wst/wsdl/internal/impl/DefinitionImpl.java b/bundles/org.eclipse.wst.wsdl/src-wsdl/org/eclipse/wst/wsdl/internal/impl/DefinitionImpl.java
index e4604c4..158658c 100644
--- a/bundles/org.eclipse.wst.wsdl/src-wsdl/org/eclipse/wst/wsdl/internal/impl/DefinitionImpl.java
+++ b/bundles/org.eclipse.wst.wsdl/src-wsdl/org/eclipse/wst/wsdl/internal/impl/DefinitionImpl.java
@@ -66,6 +66,7 @@
 import org.eclipse.wst.wsdl.internal.util.WSDLUtil;
 import org.eclipse.wst.wsdl.util.WSDLConstants;
 import org.eclipse.wst.wsdl.util.WSDLResourceImpl;
+import org.eclipse.xsd.XSDConcreteComponent;
 import org.eclipse.xsd.XSDElementDeclaration;
 import org.eclipse.xsd.XSDImport;
 import org.eclipse.xsd.XSDSchema;
@@ -1960,6 +1961,13 @@
     return result;
   }
 
+  private boolean isComponentDefined(XSDConcreteComponent component)
+  {
+    // note the getContainer() test to eliminate 'synthetic' types
+    // that are created by the XMLSchema model when resolution fails   
+    return component != null && component.getContainer() != null;
+  } 
+  
   public XSDElementDeclaration resolveElementDeclaration(String namespace, String localName)
   {
     XSDElementDeclaration result = null;
@@ -1967,8 +1975,8 @@
     {
       XSDSchema schema = (XSDSchema) i.next();
       result = schema.resolveElementDeclaration(namespace, localName);
-      if (result != null)
-        return result;
+      if (isComponentDefined(result))      
+        return result; 
     }
     
     // Could not resolve. Try against all <import>ed and inlined schemas.
@@ -1976,8 +1984,8 @@
     {
       XSDSchema schema = (XSDSchema)i.next();
       result = schema.resolveElementDeclaration(namespace, localName);
-      if (result != null)
-        return result;
+      if (isComponentDefined(result))      
+        return result;  
     }
     
     return result;
@@ -2001,8 +2009,8 @@
     {
       XSDSchema schema = (XSDSchema)i.next();
       result = schema.resolveTypeDefinition(namespace, localName);
-      if (result != null)
-        return result;
+      if (isComponentDefined(result))      
+        return result;      
     }
     
     // Could not resolve. Try against all <import>ed and inlined schemas.
@@ -2010,8 +2018,8 @@
     {
       XSDSchema schema = (XSDSchema)i.next();
       result = schema.resolveTypeDefinition(namespace, localName);
-      if (result != null)
-        return result;
+      if (isComponentDefined(result))      
+        return result; 
     }
     
     return result; // Failed to resolve.
diff --git a/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test.wsdl b/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test.wsdl
new file mode 100644
index 0000000..8dde88d
--- /dev/null
+++ b/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test.wsdl
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<definitions name="TestService"

+	targetNamespace="http://test.org/Service/"

+	xmlns="http://schemas.xmlsoap.org/wsdl/"

+	xmlns:tns="http://test.org/Service/"

+	xmlns:xsd="http://www.w3.org/2001/XMLSchema"

+	xmlns:test="http://test.org/">

+	<types>

+		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

+			<xs:import namespace="http://test.org/"

+				schemaLocation="Test1.xsd" />

+		</xs:schema>

+		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

+			<xs:import namespace="http://test.org/"

+				schemaLocation="Test2.xsd" />

+		</xs:schema>

+	</types>

+	<message name="testRequest">

+		<part name="requestPart" type="test:MyType" />

+	</message>

+	<message name="testResponse">

+		<part name="responsePart" element="test:MyElement" />

+	</message>

+	<portType name="TestPort">

+		<operation name="testOperation">

+			<input message="tns:testRequest" name="testRequest" />

+			<output message="tns:testResponse" name="testResponse" />

+		</operation>

+

+	</portType>

+</definitions>

diff --git a/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test1.xsd b/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test1.xsd
new file mode 100644
index 0000000..82c36aa
--- /dev/null
+++ b/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test1.xsd
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<schema xmlns="http://www.w3.org/2001/XMLSchema"

+	targetNamespace="http://test.org/"

+	xmlns:tns="http://test.org/">

+</schema>

diff --git a/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test2.xsd b/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test2.xsd
new file mode 100644
index 0000000..e281a19
--- /dev/null
+++ b/tests/org.eclipse.wst.wsdl.tests/samples/BugFixes/TypeAndElementResolution/Test2.xsd
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>

+<schema xmlns="http://www.w3.org/2001/XMLSchema"

+	targetNamespace="http://test.org/"

+	xmlns:tns="http://test.org/">

+	

+	<element name="MyElement" type="string"></element>

+

+	<simpleType name="MyType">

+		<restriction base="string"></restriction>

+	</simpleType>

+

+</schema>

diff --git a/tests/org.eclipse.wst.wsdl.tests/src/org/eclipse/wst/wsdl/tests/AllTestCases.java b/tests/org.eclipse.wst.wsdl.tests/src/org/eclipse/wst/wsdl/tests/AllTestCases.java
index 5fee2e1..350ee41 100644
--- a/tests/org.eclipse.wst.wsdl.tests/src/org/eclipse/wst/wsdl/tests/AllTestCases.java
+++ b/tests/org.eclipse.wst.wsdl.tests/src/org/eclipse/wst/wsdl/tests/AllTestCases.java
@@ -40,7 +40,8 @@
     suite.addTest(WSDLGenerationTest.suite());
     suite.addTest(WSDL4JAPITest.suite());
     suite.addTest(WSDLEMFAPITest.suite());
-	suite.addTest(UtilTest.suite());
+    suite.addTest(UtilTest.suite());
+    suite.addTest(BugFixesTest.suite());
     
     return suite;
   }
diff --git a/tests/org.eclipse.wst.wsdl.tests/src/org/eclipse/wst/wsdl/tests/BugFixesTest.java b/tests/org.eclipse.wst.wsdl.tests/src/org/eclipse/wst/wsdl/tests/BugFixesTest.java
new file mode 100644
index 0000000..e87ad71
--- /dev/null
+++ b/tests/org.eclipse.wst.wsdl.tests/src/org/eclipse/wst/wsdl/tests/BugFixesTest.java
@@ -0,0 +1,147 @@
+/*******************************************************************************
+ * Copyright (c) 2006 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 java.util.Iterator;
+
+import junit.framework.Assert;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.wst.wsdl.Definition;
+import org.eclipse.wst.wsdl.Message;
+import org.eclipse.wst.wsdl.Part;
+import org.eclipse.wst.wsdl.WSDLPackage;
+import org.eclipse.wst.wsdl.internal.util.WSDLResourceFactoryImpl;
+import org.eclipse.wst.wsdl.tests.util.DefinitionLoader;
+import org.eclipse.xsd.XSDElementDeclaration;
+import org.eclipse.xsd.XSDImport;
+import org.eclipse.xsd.XSDPackage;
+import org.eclipse.xsd.XSDSchema;
+import org.eclipse.xsd.XSDTypeDefinition;
+import org.eclipse.xsd.util.XSDResourceFactoryImpl;
+
+/**
+ * Contains unit tests for reported bugs.
+ */
+public class BugFixesTest extends TestCase
+{
+  private String PLUGIN_ABSOLUTE_PATH = WSDLTestsPlugin.getInstallURL();
+
+  public BugFixesTest(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 BugFixesTest("TypeAndElementResolution")
+    {
+      protected void runTest()
+      {
+        testTypeAndElementResolution();
+      }
+    });
+
+    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();
+  }
+
+  /**
+   * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=133310
+   */
+  public void testTypeAndElementResolution()
+  {
+    try
+    {
+      Definition definition = DefinitionLoader.load(PLUGIN_ABSOLUTE_PATH + "samples/BugFixes/TypeAndElementResolution/Test.wsdl"); //$NON-NLS-1$
+
+      // There are two inline schemas, each importing an external schema.
+      // The first schema is empty and used just to show the type resolution
+      // mechanism's fault.
+      // The schema containing the type and element declaration we're interested
+      // in is the second schema in the collection.
+
+      XSDSchema inlineSchema = (XSDSchema) definition.getETypes().getSchemas().get(1);
+
+      // The first and only component in this schema is an import.
+
+      XSDImport xsdImport = (XSDImport) inlineSchema.getContents().get(0);
+
+      // The imported schema was resolved when the resource was loaded.
+      // This is the schema containing our type/element.
+
+      XSDSchema schema = xsdImport.getResolvedSchema();
+
+      // Now check to make sure the resolved type/element for the messages in
+      // the WSDL document
+      // are the ones in the schema and not some bogus ones.
+
+      Iterator messagesIterator = definition.getEMessages().iterator();
+
+      while (messagesIterator.hasNext())
+      {
+        Message message = (Message) messagesIterator.next();
+        String name = message.getQName().getLocalPart();
+        if (name.equals("testRequest")) //$NON-NLS-1$
+        {
+          // We know there is only one part in the message and it refers to a
+          // type. Make sure the type can be resolved.
+
+          Part part = (Part) message.getEParts().get(0);
+          XSDTypeDefinition myType = part.getTypeDefinition();
+          assertEquals(schema, myType.getContainer());
+        }
+        else if (name.equals("testResponse")) //$NON-NLS-1$
+        {
+          // We know there is only one part in the message and it refers to an
+          // element.
+
+          Part part = (Part) message.getEParts().get(0);
+          XSDElementDeclaration myElement = part.getElementDeclaration();
+          assertEquals(schema, myElement.getContainer());
+        }
+      }
+
+    }
+    catch (Exception e)
+    {
+      Assert.fail("Test failed due to an exception: " + e.getLocalizedMessage());
+    }
+  }
+
+}