Search in sources :

Example 1 with TransformerWeighting

use of org.mule.runtime.core.internal.registry.TransformerWeighting in project mule by mulesoft.

the class TransformerWeightingTestCase method testCompareWeightingWithExactMatch.

@Test
public void testCompareWeightingWithExactMatch() throws Exception {
    Transformer trans1 = new MockConverterBuilder().from(DataType.fromType(Serializable.class)).to(DataType.BYTE_ARRAY).build();
    Transformer trans2 = new MockConverterBuilder().from(DataType.fromType(IOException.class)).to(DataType.BYTE_ARRAY).build();
    TransformerWeighting weighting1 = new TransformerWeighting(IOException.class, byte[].class, trans1);
    TransformerWeighting weighting2 = new TransformerWeighting(IOException.class, byte[].class, trans2);
    assertFalse(weighting1.isNotMatch());
    assertFalse(weighting2.isNotMatch());
    assertFalse(weighting1.isExactMatch());
    assertTrue(weighting2.isExactMatch());
    // Weighting2 two is an exact match
    assertEquals(1, weighting2.compareTo(weighting1));
    assertEquals(3, weighting1.getInputWeighting());
    assertEquals(0, weighting2.getInputWeighting());
    assertEquals(0, weighting1.getOutputWeighting());
    assertEquals(0, weighting2.getOutputWeighting());
}
Also used : TransformerWeighting(org.mule.runtime.core.internal.registry.TransformerWeighting) Serializable(java.io.Serializable) Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) IOException(java.io.IOException) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 2 with TransformerWeighting

use of org.mule.runtime.core.internal.registry.TransformerWeighting in project mule by mulesoft.

the class TransformerWeightingTestCase method testCompareWeightingWithNoMatch.

@Test
public void testCompareWeightingWithNoMatch() throws Exception {
    Transformer trans1 = new MockConverterBuilder().from(DataType.fromType(Serializable.class)).to(DataType.BYTE_ARRAY).build();
    Transformer trans2 = new MockConverterBuilder().from(DataType.fromType(FruitBowl.class)).to(DataType.BYTE_ARRAY).build();
    TransformerWeighting weighting1 = new TransformerWeighting(IOException.class, byte[].class, trans1);
    TransformerWeighting weighting2 = new TransformerWeighting(IOException.class, byte[].class, trans2);
    assertFalse(weighting1.isNotMatch());
    assertTrue(weighting2.isNotMatch());
    assertFalse(weighting1.isExactMatch());
    assertFalse(weighting2.isExactMatch());
    // Weighting2 two is not a match
    assertEquals(-1, weighting2.compareTo(weighting1));
    assertEquals(3, weighting1.getInputWeighting());
    assertEquals(-1, weighting2.getInputWeighting());
    assertEquals(0, weighting1.getOutputWeighting());
    assertEquals(0, weighting2.getOutputWeighting());
}
Also used : TransformerWeighting(org.mule.runtime.core.internal.registry.TransformerWeighting) Serializable(java.io.Serializable) FruitBowl(org.mule.tck.testmodels.fruit.FruitBowl) Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 3 with TransformerWeighting

use of org.mule.runtime.core.internal.registry.TransformerWeighting in project mule by mulesoft.

the class PriorityWeightingConverterFilter method filter.

@Override
public List<Converter> filter(List<Converter> converters, DataType source, DataType result) {
    if (converters.size() == 0) {
        return Collections.emptyList();
    }
    List<TransformerWeighting> weightings = getTransformerWeightings(converters, source.getType(), result.getType());
    TransformerWeighting transformerWeighting = weightings.get(weightings.size() - 1);
    int index = weightings.size() - 2;
    List<Converter> heaviestConverter = new LinkedList<>();
    heaviestConverter.add((Converter) transformerWeighting.getTransformer());
    for (; index > -1; --index) {
        if (weightings.get(index).compareTo(transformerWeighting) < 0) {
            break;
        } else {
            heaviestConverter.add((Converter) weightings.get(index).getTransformer());
        }
    }
    return heaviestConverter;
}
Also used : TransformerWeighting(org.mule.runtime.core.internal.registry.TransformerWeighting) Converter(org.mule.runtime.core.api.transformer.Converter) LinkedList(java.util.LinkedList)

Example 4 with TransformerWeighting

use of org.mule.runtime.core.internal.registry.TransformerWeighting in project mule by mulesoft.

the class TransformerWeightingTestCase method testCompareWeightingWithNearMatches.

@Test
public void testCompareWeightingWithNearMatches() throws Exception {
    Transformer trans1 = new MockConverterBuilder().from(DataType.fromType(Serializable.class)).to(DataType.BYTE_ARRAY).build();
    Transformer trans2 = new MockConverterBuilder().from(DataType.fromType(Exception.class)).to(DataType.BYTE_ARRAY).build();
    TransformerWeighting weighting1 = new TransformerWeighting(IOException.class, byte[].class, trans1);
    TransformerWeighting weighting2 = new TransformerWeighting(IOException.class, byte[].class, trans2);
    assertFalse(weighting1.isNotMatch());
    assertFalse(weighting2.isNotMatch());
    assertFalse(weighting1.isExactMatch());
    assertFalse(weighting2.isExactMatch());
    // Weighting2 two is a better match
    assertEquals(1, weighting2.compareTo(weighting1));
    assertEquals(3, weighting1.getInputWeighting());
    assertEquals(1, weighting2.getInputWeighting());
    assertEquals(0, weighting1.getOutputWeighting());
    assertEquals(0, weighting2.getOutputWeighting());
}
Also used : TransformerWeighting(org.mule.runtime.core.internal.registry.TransformerWeighting) Serializable(java.io.Serializable) Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) IOException(java.io.IOException) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 5 with TransformerWeighting

use of org.mule.runtime.core.internal.registry.TransformerWeighting in project mule by mulesoft.

the class TransformerWeightingTestCase method testExactMatch.

@Test
public void testExactMatch() throws Exception {
    Transformer trans = new MockConverterBuilder().from(DataType.fromType(IOException.class)).to(DataType.BYTE_ARRAY).build();
    TransformerWeighting weighting = new TransformerWeighting(IOException.class, byte[].class, trans);
    assertFalse(weighting.isNotMatch());
    assertTrue(weighting.isExactMatch());
}
Also used : TransformerWeighting(org.mule.runtime.core.internal.registry.TransformerWeighting) Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) IOException(java.io.IOException) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

TransformerWeighting (org.mule.runtime.core.internal.registry.TransformerWeighting)8 Test (org.junit.Test)6 Transformer (org.mule.runtime.core.api.transformer.Transformer)6 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)6 SmallTest (org.mule.tck.size.SmallTest)6 Serializable (java.io.Serializable)4 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)2 Converter (org.mule.runtime.core.api.transformer.Converter)2 FruitBowl (org.mule.tck.testmodels.fruit.FruitBowl)1