Package jbprocessor

Source Code of jbprocessor.BpelScannerUnitTester

package jbprocessor;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.util.XSDConstants;
import org.junit.Before;
import org.junit.Test;

import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.reference.CtPackageReference;
import spoon.reflect.reference.CtTypeReference;
import jbprocess.bpel.BpelOutputResolver;
import jbprocess.bpel.xpath.XpathFactory;
import jbprocess.bpel.xpath.impl.XpathFactoryImpl;
import jbprocess.wsdl.WsdlOutputResolver;

public class BpelScannerUnitTester extends SchemaOutputResolver
implements BpelOutputResolver, WsdlOutputResolver {
  TestLauncher launcher;
  jbprocessor.BpelScanner scanner;
 
  public BpelScannerUnitTester(){

  }
 
  @Before
  public void setUp() throws Exception {
    String[] args = new String[2];
    args[0] = "--verbose";
    args[1] = "--input=.\\\\src\\\\typetests;.\\\\src\\\\typetests2;";
   
    launcher = new TestLauncher(args);
    launcher.addProcessor("jbprocessor.BpelProcessor");
    //launcher.run();
    XpathFactory xpathFactory = new XpathFactoryImpl();
    scanner = new BpelScanner(launcher.getFactory(),xpathFactory,Collections.EMPTY_LIST);
  }
 
  public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
    File file = new File(suggestedFileName);
    File dir = file.getParentFile();
    if (dir != null)
      dir.mkdir();
    return new StreamResult(file);
  }
 
 
 
  public Result createBpelOutput(String namespaceUri, String suggestedFileName) throws IOException {
    // TODO Auto-generated method stub
    return null;
  }

  public Result createWsdlOutput(String namespaceUri, String suggestedFileName) throws IOException {
    // TODO Auto-generated method stub
    return null;
  }

  private void clearMappings(){
    scanner.nodeDataStack.contextStack.clear();
    scanner.schemaNsPrefixMap.clear();
    scanner.wsdlNsPrefixMap.clear();
   
    scanner.wsdlDefMap.clear();
    scanner.wsdlOutputStreamMap.clear();
    scanner.typePortTypeMap.clear();
    scanner.typeSoapBindingMap.clear();
    scanner.wsdlTypesSchemaMap.clear();
    scanner.wsdlDependencies.clear();
    scanner.methodOperationMap.clear();   
    scanner.xsdSchemaMap.clear();
    scanner.schemaDependencies.clear();
    scanner.xsdWriterMap.clear();
    scanner.typeXSDComplexTypeMap.clear()
    scanner.packageBpelProcessesMap.clear();
    scanner.packageBpelNamespaceMap.clear();
    scanner.fieldPartnerLinkMap.clear();
    scanner.fieldVariableMap.clear();   
  }
 
  @Test
  public void PackageMappingTest1(){
    try {
      clearMappings();
     
      CtPackage ctPackage = launcher.getFactory().Package().getOrCreate("com.example.bpel");
      @SuppressWarnings("unused")
      CtPackageReference ctPackRef = launcher.getFactory().Package().createReference(ctPackage);
     
      scanner.createPackageToWsdlMapping(ctPackage);     
      assertTrue(scanner.wsdlDefMap.entrySet().size() == 1);
      String targetNamespace = scanner.wsdlDefMap.get(ctPackage).getTargetNamespace();
      assertTrue(targetNamespace.equals(ctPackage.getQualifiedName()));
     
      clearMappings();
     
      scanner.createPackageToSchemaMapping(ctPackage);
      assertTrue(scanner.xsdSchemaMap.entrySet().size() == 1);
      targetNamespace = scanner.xsdSchemaMap.get(ctPackage).getTargetNamespace();
      assertTrue(targetNamespace != null);
      assertTrue(targetNamespace.equals(ctPackage.getQualifiedName()));
     
      clearMappings();
     
      scanner.createPackageToBpelProcessesMapping(ctPackage);
      HashSet<org.eclipse.bpel.model.Process> processes = scanner.packageBpelProcessesMap.get(ctPackage);
      assertTrue(processes != null && processes.size() == 0);
      targetNamespace = scanner.packageBpelNamespaceMap.get(ctPackage);
      assertTrue(targetNamespace != null && targetNamespace.equals(ctPackage.getQualifiedName()));
     
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());
      assertTrue(false);
    }   
  }
 
  @Test
  public void VisitPackageTest1(){
    try {
      clearMappings();
     
      CtPackage testPackage = launcher.getFactory().Package().getOrCreate("com.example.bpel");
      scanner.visitCtPackage(testPackage)
      assertTrue(scanner.wsdlDefMap.entrySet().size() == 1);
      assertTrue(scanner.xsdSchemaMap.entrySet().size() == 1);
      assertTrue(scanner.wsdlDefMap.get(testPackage) != null);
      assertTrue(scanner.xsdSchemaMap.get(testPackage) != null);
      assertTrue(scanner.wsdlDefMap.get(testPackage).getTargetNamespace().equals(testPackage.getQualifiedName()));
      assertTrue(scanner.xsdSchemaMap.get(testPackage).getTargetNamespace().equals(testPackage.getQualifiedName()));
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());
      assertTrue(false);
    }   
  }
  @Test
  public void VisitPackageReferenceTest1(){
    try
      clearMappings();
     
      CtPackage testPackage = launcher.getFactory().Package().getOrCreate("com.example.bpel");
      CtPackageReference ref = launcher.getFactory().Package().createReference(testPackage);
      scanner.visitCtPackageReference(ref);
     
      assertTrue(scanner.wsdlDefMap.entrySet().size() == 1);
      assertTrue(scanner.xsdSchemaMap.entrySet().size() == 1);
      assertTrue(scanner.wsdlDefMap.get(testPackage) != null);
      assertTrue(scanner.xsdSchemaMap.get(testPackage) != null);
      assertTrue(scanner.wsdlDefMap.get(testPackage).getTargetNamespace().equals(testPackage.getQualifiedName()));
      assertTrue(scanner.xsdSchemaMap.get(testPackage).getTargetNamespace().equals(testPackage.getQualifiedName()));
     
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());
      assertTrue(false);
    }   
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void VisitPrimitiveTypeReferenceTest1(){
    try {
      clearMappings();
     
      CtTypeReference typeRef = launcher.getFactory().Type().createReference(java.lang.String.class.getName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName != null);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("string"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(int.class.getCanonicalName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("integer"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(Integer.class.getName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("integer"));     
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(short.class.getCanonicalName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("short"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(Short.class.getName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("short"));     
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(long.class.getCanonicalName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("long"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(Long.class.getName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("long"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(float.class.getCanonicalName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("float"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(Float.class.getName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("float"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(double.class.getCanonicalName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("double"));

      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(Double.class.getName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("double"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(boolean.class.getCanonicalName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("boolean"));
     
      clearMappings();
     
      typeRef = launcher.getFactory().Type().createReference(Boolean.class.getName());
      scanner.visitCtTypeReference(typeRef);
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getNamespaceURI().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001));
      assertTrue(scanner.nodeDataStack.peek().xsdTypeQName.getLocalPart().equals("boolean"));
     
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());
      e.printStackTrace();
      assertTrue(false);
    }   
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void ClassMappingTest1(){
    try {
      clearMappings();
     
      CtPackage ctPackage = launcher.getFactory().Package().getOrCreate("com.example");
      CtClass ctClass = launcher.getFactory().Class().create("com.example.TestClass1");
      ctPackage.getTypes().add(ctClass);
      scanner.visitCtPackage(ctPackage)// calls visitCtClass which creates the complex type mapping
     
      XSDComplexTypeDefinition complexType = scanner.typeXSDComplexTypeMap.get(ctClass);
      assertTrue(complexType != null);
      assertTrue(complexType.getName().equals(ctClass.getSimpleName()));
      assertTrue(complexType.getTargetNamespace().equals(ctPackage.getQualifiedName()));
           
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());
      assertTrue(false);
    }   
  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void ClassFieldMappingTest1(){
    try {

      clearMappings();
     
      CtPackage ctPackage = launcher.getFactory().Package().getOrCreate("com.example");
      CtClass ctClass = launcher.getFactory().Class().create("com.example.TestClass1");
      ctPackage.getTypes().add(ctClass);
      HashSet<ModifierKind> modifiers = new HashSet<ModifierKind>();
      modifiers.add(ModifierKind.PUBLIC);
      CtTypeReference ctTypeRef = launcher.getFactory().Type().createReference(String.class.getName());
      @SuppressWarnings("unused")
      CtField ctField = launcher.getFactory().Field().create(ctClass, modifiers, ctTypeRef, "myField");
      scanner.visitCtPackage(ctPackage);     
     
      XSDComplexTypeDefinition complexType = scanner.typeXSDComplexTypeMap.get(ctClass);
      XSDParticle parentParticle = (XSDParticle)complexType.getContent();
      XSDModelGroup parentModelGroup = (XSDModelGroup)parentParticle.getContent();
      XSDParticle localElementParticle = (XSDParticle)parentModelGroup.getContents().get(0);
      XSDElementDeclaration elementdecl = (XSDElementDeclaration)localElementParticle.getContent();
      assertTrue(elementdecl.getName().equals("myField"));
      assertTrue(elementdecl.getTargetNamespace().equals(ctPackage.getQualifiedName()));     
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());
      assertTrue(false);
    }     
  }
 
 
  @Test
  public void InterfaceComplexTypeMappingTest1(){
    try {
     
      clearMappings();
     
      CtPackage ctPackage = launcher.getFactory().Package().getOrCreate("com.example");
      //CtClass ctClass = launcher.getFactory().Class().create("com.example.TestClass1");
      CtInterface ctInterface = launcher.getFactory().Interface().create("com.example.TestClass1");
      ctPackage.getTypes().add(ctInterface);
      scanner.visitCtPackage(ctPackage)// calls visitCtInterface which creates the complex type mapping
     
      XSDComplexTypeDefinition complexType = scanner.typeXSDComplexTypeMap.get(ctInterface);
      assertTrue(complexType != null);
      assertTrue(complexType.getName().equals(ctInterface.getSimpleName()));
      assertTrue(complexType.getTargetNamespace().equals(ctPackage.getQualifiedName()));
           
    }catch(Exception e){
      System.err.println("Exception: " + e.getMessage());
      assertTrue(false);
    }     
  }
}
TOP

Related Classes of jbprocessor.BpelScannerUnitTester

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.