Package archmapper.main.test

Source Code of archmapper.main.test.MappingHelperTest

package archmapper.main.test;

import static org.junit.Assert.*;
import static org.easymock.EasyMock.*;

import org.easymock.IMocksControl;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.junit.Test;

import archmapper.main.annotations.BelongsToComponent;
import archmapper.main.model.architecture.Component;
import archmapper.main.model.architecture.Connector;
import archmapper.main.model.architecture.Port;
import archmapper.main.model.archmapping.ClassDefinition;
import archmapper.main.model.archmapping.ComponentMapping;
import archmapper.main.model.archmapping.ConnectorMapping;
import archmapper.main.model.archmapping.FileDefinition;
import archmapper.main.model.archmapping.InterfaceDefinition;
import archmapper.main.model.archmapping.PortMapping;

public class MappingHelperTest extends MappingTestBase {
 
  @Test
  public void testNoExceptionsWithEmptyModels() {
    assertNull(mappingHelper.getComponentTypeMapping("test"));
    assertFalse(mappingHelper.isExternalComponent("test"));
    assertFalse(mappingHelper.mayClassReferenceOtherClass("className", "componentName",
        "otherClassName", "otherComponentName"));
  }
 
  @Test
  public void testNoExceptionsWithOnlyArchitectureMapping() {
    // create an architecture mapping and nothing else
    createArchitectureMapping();
   
    ComponentMapping comp1Mapping = archMapping.getComponentMapping("Comp1");
   
    ClassDefinition classDef = (ClassDefinition) mappingHelper.getImplementationArtifactDefinitionByClassname(comp1Mapping,
        "de.superpackage.ClassName1");

    assertNotNull(classDef);
   
    assertEquals("de.superpackage",
        mappingHelper.getDefaultPackage(comp1Mapping));
   
    assertNull(mappingHelper.getComponentTypeOf(comp1Mapping));
    assertNull(mappingHelper.getConnectorTypeOf(archMapping.getConnectorMapping("Connector1")));
   
    FileDefinition fileDef = comp1Mapping.getFileDefinition().get(0);
   
    assertEquals("file1", mappingHelper.getFilename(fileDef));
    assertNull(mappingHelper.getFileTypeForFileDefinition(fileDef));
   
    assertEquals("de.superpackage", mappingHelper.getPackageOfImplementationArtifact(classDef));
    assertEquals("de.superpackage.ClassName1", mappingHelper.getQualifiedName(classDef));
   
    assertEquals("", mappingHelper.getTemplateTextForFile(fileDef));
   
    assertFalse(mappingHelper.isClassSingleton(classDef));
    assertFalse(mappingHelper.isExternalComponent("Comp1"));
  }
 
  @Test
  public void testNoExceptionsWithoutStyleMapping() {
    createArchitecture();
    createArchitectureMapping();
   
    ComponentMapping comp1Mapping = archMapping.getComponentMapping("Comp1");
    ComponentMapping comp2Mapping = archMapping.getComponentMapping("Comp2");
    ConnectorMapping conn1Mapping = archMapping.getConnectorMapping("Connector1");
   
    assertNull(mappingHelper.getComponentTypeMapping("Comp1"));
    assertEquals("CompType1", mappingHelper.getComponentTypeOf(comp1Mapping));
    assertEquals("ConnType", mappingHelper.getConnectorTypeOf(conn1Mapping));
   
    ClassDefinition class1Def = comp1Mapping.getClassDefinition().get(0);
   
    Port p = architecture.getComponentByName("Comp1").getPortByName("p");
    assertTrue(mappingHelper.isClassUsingPort(class1Def,     
        p, comp1Mapping.getPortMapping("p")));
    assertTrue(mappingHelper.isClassExposedByPort(class1Def,
        p, comp1Mapping.getPortMapping("p")));
   
    assertEquals("com.testpackage.Class2", mappingHelper.getQualifiedName(comp2Mapping.getClassDefinition().get(0)));
   
    assertFalse(mappingHelper.isExternalComponent("Comp1"));
   
    assertFalse(mappingHelper.mayClassReferenceOtherClass("de.superpackage.ClassName1", "Comp1",
        "com.testpackage.Class2", "Comp2"));
   
  }
 
  @Test
  public void testComponentTypeMapping() {
    createArchitecture();
    createArchitectureMapping();
    createStyleMapping();
   
    assertTrue(mappingHelper.isExternalComponent("Comp1"));
    assertTrue(mappingHelper.isClassSingleton(archMapping.getComponentMapping("Comp2").
        getClassDefinition().get(0)));
   
    assertEquals("TestTemplate", mappingHelper.getTemplateTextForFile(archMapping.getComponentMapping("Comp1").getFileDefinition().get(0)));

  }
 
  @Test
  public void testPortTypeMapping() {
    createArchitecture();
    createArchitectureMapping();
    createStyleMapping();
   
    assertTrue(mappingHelper.mayClassReferenceOtherClass("de.superpackage.ClassName1", "Comp1",
        "com.testpackage.Class2", "Comp2"));
    assertFalse(mappingHelper.mayClassReferenceOtherClass("com.testpackage.Class2", "Comp2",
        "de.superpackage.ClassName1", "Comp1"));
   
    // classes from the same component may know each other, even though they are not
    // declared in the mappings
    assertTrue(mappingHelper.mayClassReferenceOtherClass("de.superpackage.ClassName1", "Comp1",
        "superclass", "Comp1"));
  }
 
  @Test
  public void testCommunicationIntegrityTests() {
    loadFiles();
   
    ComponentMapping comp1Mapping = archMapping.getComponentMapping("Comp1");
    PortMapping pMapping = comp1Mapping.getPortMapping("p");
    Component comp1 = architecture.getComponentByName("Comp1");
    Port p = comp1.getPortByName("p");
   
    assertTrue(mappingHelper.isClassExposedByPort(comp1Mapping.getInterfaceDefinition().get(0),
        p, pMapping));
    assertFalse(mappingHelper.isClassExposedByPort(comp1Mapping.getClassDefinition().get(0),
        p, pMapping));
   
    assertTrue(mappingHelper.isClassUsingPort(comp1Mapping.getClassDefinition().get(0),
        p, pMapping));
    assertFalse(mappingHelper.isClassUsingPort(comp1Mapping.getInterfaceDefinition().get(0),
        p, pMapping));
   
    assertTrue(mappingHelper.isClassExposedByPort(comp1Mapping.getInterfaceDefinition().get(1),
        p, pMapping));
    assertFalse(mappingHelper.isClassUsingPort(comp1Mapping.getInterfaceDefinition().get(1),
        p, pMapping));
   
    assertTrue(mappingHelper.mayClassReferenceOtherClass
        ("Comp1Interface3", "Comp1", "Comp2If1", "Comp2"));
   
    Component comp2 = architecture.getComponentByName("Comp2");
    ComponentMapping comp2Mapping = archMapping.getComponentMapping("Comp2");
    Port p2 = comp2.getPortByName("p2");
   
    assertFalse(mappingHelper.isClassUsingPort(comp2Mapping.getClassDefinition().get(0), p2, null));
    assertTrue(mappingHelper.isClassExposedByPort(comp2Mapping.getClassDefinition().get(0), p2, null));
   
    // every class is exposed by the port, so we can also test with null
    // (which will be the case for classes defined by classNamePattern)
    assertTrue(mappingHelper.isClassExposedByPort(null, p2, null));
   
    assertTrue(mappingHelper.isClassUsingPort(comp2Mapping.getInterfaceDefinition().get(0),
        p2, null));
   
   
    // Test with implemented Connector
   
    Component comp3 = architecture.getComponentByName("Comp3");
    ComponentMapping comp3Mapping = archMapping.getComponentMapping("Comp3");
    ClassDefinition classComp3Def = comp3Mapping.getClassDefinition().get(0);
    ClassDefinition class2Comp3Def = comp3Mapping.getClassDefinition().get(1);
   
    Connector connWithImpl = architecture.getConnectorByName("ConnWithImpl");
    ConnectorMapping connWithImplMapping = archMapping.getConnectorMapping("ConnWithImpl");
    ClassDefinition connClassDef = connWithImplMapping.getClassDefinition().get(0);
   
    // One class in the component may reference the connector class, one not
    assertTrue(mappingHelper.mayClassInComponentReferenceClassInConnector(classComp3Def,
        comp3, comp3Mapping, connClassDef, connWithImpl, connWithImplMapping));
    assertTrue(mappingHelper.mayClassReferenceOtherClass("Comp3Class1",
        "Comp3", "ConnWithImplClass1", "ConnWithImpl"));
    assertFalse(mappingHelper.mayClassInComponentReferenceClassInConnector(class2Comp3Def,
        comp3, comp3Mapping, connClassDef, connWithImpl, connWithImplMapping));
    assertFalse(mappingHelper.mayClassReferenceOtherClass("Comp3Class2",
        "Comp3", "ConnWithImplClass1", "ConnWithImpl"));
   
    // The connector class may not reference the classes in the component, since
    // they are not exposed by the port
    assertFalse(mappingHelper.mayClassInConnectorReferenceClassInComponent(connClassDef,
        connWithImpl, connWithImplMapping, classComp3Def, comp3, comp3Mapping));
   
    InterfaceDefinition comp2If = comp2Mapping.getInterfaceDefinition().get(1);
   
    assertTrue(mappingHelper.mayClassInConnectorReferenceClassInComponent(connClassDef,
        connWithImpl, connWithImplMapping, comp2If, comp2, comp2Mapping));
    assertFalse(mappingHelper.mayClassInComponentReferenceClassInConnector(comp2If,
        comp2, comp2Mapping, connClassDef, connWithImpl, connWithImplMapping));
   
    // The connected components over the implemented connector also reference
    // each other directly
    assertTrue(mappingHelper.mayClassInComponentReferenceClassInOtherComponent(
        classComp3Def, comp3, comp3Mapping, comp2If, comp2, comp2Mapping));
  }
 
  @Test
  public void testGetAnnotationValue() {
    IMocksControl control = createStrictControl();
   
    ITypeBinding binding = control.createMock(ITypeBinding.class);
    IAnnotationBinding anno = control.createMock(IAnnotationBinding.class);
    ITypeBinding typeBinding = control.createMock(ITypeBinding.class);
    IMemberValuePairBinding memberValue = control.createMock(IMemberValuePairBinding.class);
   
    expect(binding.getAnnotations()).andReturn(new IAnnotationBinding[] {anno});
    expect(anno.getAnnotationType()).andReturn(typeBinding);
    expect(typeBinding.getQualifiedName()).andReturn("java.lang.String");
    expect(anno.getAllMemberValuePairs()).andReturn(new IMemberValuePairBinding[] {memberValue});
    expect(memberValue.getName()).andReturn("value");
    expect(memberValue.getValue()).andReturn("Test-Annotation");
   
    control.replay();
   
    assertEquals("Test-Annotation",
        mappingHelper.getAnnotationValue(binding, String.class));
   
    control.verify();
  }
 
  @Test
  public void testGetComponentName() {
    loadFiles();
   
    ITypeBinding binding = createMock(ITypeBinding.class);
    // This binding should have no annotations...
    // First test is a file annotation
    expect(binding.getAnnotations()).andReturn(new IAnnotationBinding[] {}).anyTimes();
   
    expect(binding.getQualifiedName()).andReturn("Comp1Class1");
   
    replay(binding);
    assertEquals("Comp1", mappingHelper.getComponentName(binding));
   
    reset(binding);
   
    // Now test with a filename pattern
    expect(binding.getQualifiedName()).andReturn("de.superpackage.TestClass");
    expect(binding.getAnnotations()).andReturn(new IAnnotationBinding[] {}).anyTimes();
   
    replay(binding);
    assertEquals("Comp2", mappingHelper.getComponentName(binding));
    verify(binding);
   
    reset(binding);
   
    // Test with an interface definition
    expect(binding.getAnnotations()).andReturn(new IAnnotationBinding[] {}).anyTimes();
    expect(binding.getQualifiedName()).andReturn("Comp1Interface3");
   
    replay(binding);
    assertEquals("Comp1", mappingHelper.getComponentName(binding));
   
    reset(binding);
   
    // Last test is with an annotation value
    IAnnotationBinding annoBinding = createMock(IAnnotationBinding.class);
    ITypeBinding annoTypeBinding = createMock(ITypeBinding.class);
    IMemberValuePairBinding memberValuePairBinding = createMock(IMemberValuePairBinding.class);
    IPackageBinding packageBinding = createMock(IPackageBinding.class);
   
    expect(binding.getQualifiedName()).andReturn("de.supertest.TestClass");
    expect(binding.getAnnotations()).andReturn(new IAnnotationBinding[] {annoBinding});
    expect(annoBinding.getAnnotationType()).andReturn(annoTypeBinding);
    expect(annoTypeBinding.getQualifiedName()).andReturn(BelongsToComponent.class.getCanonicalName());
    expect(annoBinding.getAllMemberValuePairs()).andReturn(new IMemberValuePairBinding[]
                             { memberValuePairBinding });
    expect(memberValuePairBinding.getName()).andReturn("value");
    expect(memberValuePairBinding.getValue()).andReturn("Comp1");
   
    expect(binding.getName()).andReturn("TestClass");
    expect(binding.getPackage()).andReturn(packageBinding);
    expect(packageBinding.getName()).andReturn("de.supertest");
    expect(binding.getAnnotations()).andReturn(new IAnnotationBinding[] {}).anyTimes();
   
    replay(binding, annoBinding, annoTypeBinding, memberValuePairBinding);
    assertEquals("Comp1", mappingHelper.getComponentName(binding));
  }
 
 
  @Test
  public void testGetSuperClassAndSuperInterface() {
    loadFiles();
   
    ClassDefinition class1Def = archMapping.getComponentMapping("Comp2").getClassDefinition().get(1);
    assertEquals("java.util.ArrayList", mappingHelper.getSuperClass(class1Def));
    assertEquals("java.util.List", mappingHelper.getSuperInterfaces(class1Def).get(0));
    assertEquals("java.util.Map", mappingHelper.getSuperInterfaces(class1Def).get(1));
   
    ClassDefinition class2Def = archMapping.getComponentMapping("Comp2").getClassDefinition().get(2);
    assertEquals("java.util.HashMap", mappingHelper.getSuperClass(class2Def));
    assertEquals("Testinterface", mappingHelper.getSuperInterfaces(class2Def).get(0));
    assertEquals(3, mappingHelper.getSuperInterfaces(class2Def).size());
  }
}
TOP

Related Classes of archmapper.main.test.MappingHelperTest

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.