Search in sources :

Example 1 with PrimitivePortable

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable in project hazelcast by hazelcast.

the class DefaultPortableReaderSpecTest method fromPortableArrayAnyToPortableArrayAnyToPrimitiveScenarios.

// ----------------------------------------------------------------------------------------------------------
// from PORTABLE_ARRAY[any] via PORTABLE_ARRAY[any] to further PRIMITIVE access
// ----------------------------------------------------------------------------------------------------------
private static void fromPortableArrayAnyToPortableArrayAnyToPrimitiveScenarios(List<Object[]> result) {
    String p = "fromPortableArrayAnyToPortableArrayAnyToPrimitiveScenarios";
    // =============================================
    // INPUT mixed
    // =============================================
    PrimitivePortable p1 = prim(1, NONE);
    PrimitivePortable p10 = prim(10, FULL);
    PrimitivePortable p20 = prim(20, FULL);
    NestedGroupPortable input = nested(new Portable[] { new GroupPortable(new Portable[0]), group(p1, p10), new GroupPortable((Portable[]) null), group(new PrimitivePortable[] { p20 }) });
    Class failure = IllegalArgumentException.class;
    result.addAll(asList(scenario(input, failure, ByteArray, "portables[any].portables[any].byte_", p), scenario(input, failure, ShortArray, "portables[any].portables[any].short_", p), scenario(input, failure, IntArray, "portables[any].portables[any].int_", p), scenario(input, failure, LongArray, "portables[any].portables[any].long_", p), scenario(input, failure, CharArray, "portables[any].portables[any].char_", p), scenario(input, failure, FloatArray, "portables[any].portables[any].float_", p), scenario(input, failure, DoubleArray, "portables[any].portables[any].double_", p), scenario(input, failure, BooleanArray, "portables[any].portables[any].boolean_", p)));
    List expectedUtfArray = list(null, p1.string_, p10.string_, null, p20.string_);
    result.add(scenario(input, expectedUtfArray, UTFArray, "portables[any].portables[any].string_", p));
    result.addAll(asList(scenario(input, list(null, p1.byte_, p10.byte_, null, p20.byte_), Generic, "portables[any].portables[any].byte_", p), scenario(input, list(null, p1.short_, p10.short_, null, p20.short_), Generic, "portables[any].portables[any].short_", p), scenario(input, list(null, p1.int_, p10.int_, null, p20.int_), Generic, "portables[any].portables[any].int_", p), scenario(input, list(null, p1.long_, p10.long_, null, p20.long_), Generic, "portables[any].portables[any].long_", p), scenario(input, list(null, p1.char_, p10.char_, null, p20.char_), Generic, "portables[any].portables[any].char_", p), scenario(input, list(null, p1.float_, p10.float_, null, p20.float_), Generic, "portables[any].portables[any].float_", p), scenario(input, list(null, p1.double_, p10.double_, null, p20.double_), Generic, "portables[any].portables[any].double_", p), scenario(input, list(null, p1.boolean_, p10.boolean_, null, p20.boolean_), Generic, "portables[any].portables[any].boolean_", p), scenario(input, list(null, p1.string_, p10.string_, null, p20.string_), Generic, "portables[any].portables[any].string_", p)));
    // =============================================
    // INPUT empty
    // =============================================
    NestedGroupPortable inputEmpty = nested(new Portable[0]);
    result.addAll(asList(scenario(inputEmpty, null, ByteArray, "portables[any].portables[any].byte_", p), scenario(inputEmpty, null, ShortArray, "portables[any].portables[any].short_", p), scenario(inputEmpty, null, IntArray, "portables[any].portables[any].int_", p), scenario(inputEmpty, null, LongArray, "portables[any].portables[any].long_", p), scenario(inputEmpty, null, CharArray, "portables[any].portables[any].char_", p), scenario(inputEmpty, null, FloatArray, "portables[any].portables[any].float_", p), scenario(inputEmpty, null, DoubleArray, "portables[any].portables[any].double_", p), scenario(inputEmpty, null, BooleanArray, "portables[any].portables[any].boolean_", p), scenario(inputEmpty, null, UTFArray, "portables[any].portables[any].string_", p)));
    result.addAll(asList(scenario(inputEmpty, null, Generic, "portables[any].portables[any].byte_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].short_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].int_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].long_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].char_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].float_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].double_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].boolean_", p), scenario(inputEmpty, null, Generic, "portables[any].portables[any].string_", p)));
    // =============================================
    // INPUT null
    // =============================================
    NestedGroupPortable inputNull = nested((Portable[]) null);
    result.addAll(asList(scenario(inputNull, null, ByteArray, "portables[any].portables[any].byte_", p), scenario(inputNull, null, ShortArray, "portables[any].portables[any].short_", p), scenario(inputNull, null, IntArray, "portables[any].portables[any].int_", p), scenario(inputNull, null, LongArray, "portables[any].portables[any].long_", p), scenario(inputNull, null, CharArray, "portables[any].portables[any].char_", p), scenario(inputNull, null, FloatArray, "portables[any].portables[any].float_", p), scenario(inputNull, null, DoubleArray, "portables[any].portables[any].double_", p), scenario(inputNull, null, BooleanArray, "portables[any].portables[any].boolean_", p), scenario(inputNull, null, UTFArray, "portables[any].portables[any].string_", p)));
    result.addAll(asList(scenario(inputNull, null, Generic, "portables[any].portables[any].byte_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].short_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].int_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].long_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].char_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].float_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].double_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].boolean_", p), scenario(inputNull, null, Generic, "portables[any].portables[any].string_", p)));
}
Also used : GroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.GroupPortable) Portable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method.Portable) Portable(com.hazelcast.nio.serialization.Portable) PrimitivePortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable) NestedGroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable) PrimitivePortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable) NestedGroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) GroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.GroupPortable) NestedGroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable)

Example 2 with PrimitivePortable

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable in project hazelcast by hazelcast.

the class DefaultPortableReaderSpecTest method reader.

//
// Hazelcast init Utilities
//
public PortableReader reader(Portable portable) throws IOException {
    Config config = new Config();
    config.getSerializationConfig().addPortableFactory(TestPortableFactory.ID, new TestPortableFactory());
    HazelcastInstanceProxy hz = (HazelcastInstanceProxy) createHazelcastInstance(config);
    IMap<String, Object> map = hz.getMap("stealingMap");
    // avoid the case where there's no class definition
    if (portable instanceof PrimitivePortable) {
        map.put(P_NON_EMPTY.toString(), P_NON_EMPTY);
    }
    if (portable instanceof GroupPortable) {
        map.put(G_NON_EMPTY.toString(), G_NON_EMPTY);
    }
    if (portable instanceof NestedGroupPortable) {
        map.put(N_NON_EMPTY.toString(), N_NON_EMPTY);
    }
    map.put(portable.toString(), portable);
    EntryStealingProcessor processor = new EntryStealingProcessor(portable.toString());
    map.executeOnEntries(processor);
    SerializationServiceV1 ss = (SerializationServiceV1) hz.getSerializationService();
    return ss.createPortableReader(processor.stolenEntryData);
}
Also used : TestPortableFactory(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.TestPortableFactory) Config(com.hazelcast.config.Config) PrimitivePortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable) NestedGroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable) SerializationServiceV1(com.hazelcast.internal.serialization.impl.SerializationServiceV1) HazelcastInstanceProxy(com.hazelcast.instance.HazelcastInstanceProxy) GroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.GroupPortable) NestedGroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable)

Example 3 with PrimitivePortable

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable in project hazelcast by hazelcast.

the class DefaultPortableReaderSpecTest method fromPortableToPortableToPrimitiveScenarios.

// ----------------------------------------------------------------------------------------------------------
// from PORTABLE via PORTABLE to further access
// ----------------------------------------------------------------------------------------------------------
private static void fromPortableToPortableToPrimitiveScenarios(List<Object[]> result) {
    String p = "fromPortableToPortableToPrimitiveScenarios";
    // FULLy initialised primitive objects accessed from portable stored in array
    NestedGroupPortable nestedFullGroup = nested(group(prim(1, FULL), prim(10, FULL), prim(100, FULL)));
    result.addAll(asList(scenario(nestedFullGroup, (nestedFullGroup.portable), Portable, "portable", p), scenario(nestedFullGroup, ((GroupPortable) (nestedFullGroup.portable)).portable, Portable, "portable.portable", p)));
    result.addAll(asList(scenario(nestedFullGroup, (nestedFullGroup.portable), Generic, "portable", p), scenario(nestedFullGroup, ((GroupPortable) (nestedFullGroup.portable)).portable, Generic, "portable.portable", p)));
    result.addAll(expandPrimitiveScenario(nestedFullGroup, ((GroupPortable) nestedFullGroup.portable).portable, "portable.portable.primitiveUTF_", p));
    result.addAll(expandPrimitiveArrayScenario(nestedFullGroup, (PrimitivePortable) ((GroupPortable) nestedFullGroup.portable).portable, "portable.portable.primitiveArray", p));
    NestedGroupPortable nestedFullEmptyNullGroup = nested(group(prim(1, FULL), prim(10, NONE), prim(100, NULL)));
    result.addAll(expandPrimitiveScenario(nestedFullEmptyNullGroup, ((GroupPortable) nestedFullEmptyNullGroup.portable).portable, "portable.portable.primitiveUTF_", p));
    result.addAll(expandPrimitiveArrayScenario(nestedFullEmptyNullGroup, (PrimitivePortable) ((GroupPortable) nestedFullEmptyNullGroup.portable).portable, "portable.portable.primitiveArray", p));
    // empty or null portable array de-referenced further
    NestedGroupPortable nestedNullArrayGroup = nested(new GroupPortable((Portable[]) null));
    result.addAll(asList(scenario(nestedNullArrayGroup, (nestedNullArrayGroup.portable), Portable, "portable", p), scenario(nestedNullArrayGroup, null, Portable, "portable.portable", p)));
    result.addAll(asList(scenario(nestedNullArrayGroup, (nestedNullArrayGroup.portable), Generic, "portable", p), scenario(nestedNullArrayGroup, null, Generic, "portable.portable", p)));
    result.addAll(expandPrimitiveScenario(nestedNullArrayGroup, null, "portable.portable.primitiveUTF_", p));
    result.addAll(expandPrimitiveArrayScenario(nestedNullArrayGroup, null, "portable.portable.primitiveArray", p));
    NestedGroupPortable nestedNull = nested(new Portable[0]);
    result.addAll(asList(scenario(nestedNull, null, Portable, "portable", p), scenario(nestedNull, null, Portable, "portable.portable", p)));
    result.addAll(asList(scenario(nestedNull, null, Generic, "portable", p), scenario(nestedNull, null, Generic, "portable.portable", p)));
    result.addAll(expandPrimitiveScenario(nestedNull, null, "portable.portable.primitiveUTF_", p));
    result.addAll(expandPrimitiveArrayScenario(nestedNull, null, "portable.portable.primitiveArray", p));
}
Also used : PrimitivePortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable) NestedGroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable) GroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.GroupPortable) NestedGroupPortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable)

Example 4 with PrimitivePortable

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable in project hazelcast by hazelcast.

the class DefaultPortableReaderSpecTest method expandPortableArrayPrimitiveScenario.

/**
     * Expands test cases for that navigate from portable array to a primitive field.
     * Word portableArray is replaced to: portables[0], portables[1], portables[2], portables[any]
     * Word "primitive_" is replaced by each primitive type and the scenario is expanded to for each type:
     *
     * A.) The contract is that input should somewhere on the path contain an array of Portable[] which contains objects of type
     * PrimitivePortable. For example: "portableArray.primitive_" will be expanded two-fold, the portable array and primitive
     * types will be expanded as follows:
     * <ul>
     * <li>portables[0].byte, portables[0].short, portables[0].char, ... for all primitive types</li>
     * <li>portables[1].byte, portables[1].short, portables[1].char, ...</li>
     * <li>portables[2].byte, portables[2].short, portables[2].char, ...</li>
     * </ul>
     *
     * B.) Then the [any] case will be expanded too:
     * <ul>
     * <li>portables[any].byte, portables[any].short, portables[any].char, ... for all primitive types</li>
     * </ul>
     *
     * The expected result should be the object that contains the portable array - that's the general contract.
     * The result for assertion will be automatically calculated
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static Collection<Object[]> expandPortableArrayPrimitiveScenario(Portable input, GroupPortable result, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<Object[]>();
    // expansion of the portable array using the following quantifiers
    for (String token : asList("0", "1", "2", "any")) {
        String tokenToReplace = "primitive_";
        if (pathToExplode.contains("primitiveUTF_")) {
            tokenToReplace = "primitiveUTF_";
        }
        String path = pathToExplode.replace("portableArray", "portables[" + token + "]");
        if (token.equals("any")) {
            // expansion of the primitive fields
            for (Method method : getPrimitives(tokenToReplace.contains("UTF"))) {
                List resultToMatch = new ArrayList();
                int portableCount = 0;
                try {
                    portableCount = result.portables.length;
                } catch (NullPointerException ignored) {
                }
                for (int i = 0; i < portableCount; i++) {
                    PrimitivePortable portable = (PrimitivePortable) result.portables[i];
                    resultToMatch.add(portable.getPrimitive(method));
                }
                if (result == null || result.portables == null || result.portables.length == 0) {
                    resultToMatch = null;
                }
                scenarios.addAll(asList(scenario(input, resultToMatch, getArrayMethodFor(method), path.replace(tokenToReplace, method.field), parent), scenario(input, resultToMatch, Generic, path.replace(tokenToReplace, method.field), parent)));
            }
        } else {
            // expansion of the primitive fields
            for (Method method : getPrimitives(tokenToReplace.contains("UTF"))) {
                Object resultToMatch = null;
                try {
                    PrimitivePortable portable = (PrimitivePortable) result.portables[Integer.parseInt(token)];
                    resultToMatch = portable.getPrimitive(method);
                } catch (NullPointerException ignored) {
                } catch (IndexOutOfBoundsException ignored) {
                }
                if (method != UTF) {
                    if (result == null || result.portables == null || result.portables.length == 0) {
                        resultToMatch = IllegalArgumentException.class;
                    }
                }
                scenarios.addAll(asList(scenario(input, resultToMatch, method, path.replace(tokenToReplace, method.field), parent), // IMPORTANT: the difference between generic and non-generic primitive call for null
                scenario(input, resultToMatch == IllegalArgumentException.class ? null : resultToMatch, Generic, path.replace(tokenToReplace, method.field), parent)));
            }
        }
    }
    return scenarios;
}
Also used : PrimitivePortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Method(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method)

Example 5 with PrimitivePortable

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable in project hazelcast by hazelcast.

the class DefaultPortableReaderSpecTest method expandPrimitiveScenario.

/**
     * Expands test cases for primitive non-array data types.
     * Word "primitive_" from the pathToExplode is replaced by each primitive type and the scenario is expanded to:
     * <ul>
     * <li>scenario(input, result.byte_, Byte, adjustedPath + "byte_"),</li>
     * <li>scenario(input, result.short_, Short, adjustedPath + "short_"),</li>
     * <li>scenario(input, result.int_, Int, adjustedPath + "int_"),</li>
     * <li>scenario(input, result.long_, Long, adjustedPath + "long_"),</li>
     * <li>scenario(input, result.float_, Float, adjustedPath + "float_"),</li>
     * <li>scenario(input, result.double_, Double, adjustedPath + "double_"),</li>
     * <li>scenario(input, result.boolean_, Boolean, adjustedPath + "boolean_"),</li>
     * <li>scenario(input, result.char_, Char, adjustedPath + "char_"),</li>
     * <li>scenario(input, result.string_, UTF, adjustedPath + "string_"),</li>
     * </ul>
     */
private static Collection<Object[]> expandPrimitiveScenario(Portable input, Object result, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<Object[]>();
    Object adjustedResult;
    String tokenToReplace = "primitive_";
    if (pathToExplode.contains("primitiveUTF_")) {
        tokenToReplace = "primitiveUTF_";
    }
    for (Method method : getPrimitives(tokenToReplace.contains("UTF"))) {
        if (result instanceof PrimitivePortable) {
            adjustedResult = ((PrimitivePortable) result).getPrimitive(method);
        } else if (result == null && method != UTF) {
            adjustedResult = IllegalArgumentException.class;
        } else {
            adjustedResult = result;
        }
        // type-specific method case
        scenarios.add(scenario(input, adjustedResult, method, pathToExplode.replace(tokenToReplace, method.field), parent));
        // generic method case
        scenarios.add(scenario(input, adjustedResult == IllegalArgumentException.class ? null : adjustedResult, Generic, pathToExplode.replace(tokenToReplace, method.field), parent));
    }
    return scenarios;
}
Also used : PrimitivePortable(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable) ArrayList(java.util.ArrayList) Method(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method)

Aggregations

PrimitivePortable (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable)6 GroupPortable (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.GroupPortable)4 NestedGroupPortable (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.NestedGroupPortable)4 ArrayList (java.util.ArrayList)4 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 Portable (com.hazelcast.nio.serialization.Portable)2 Method (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method)2 Portable (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method.Portable)2 Config (com.hazelcast.config.Config)1 HazelcastInstanceProxy (com.hazelcast.instance.HazelcastInstanceProxy)1 SerializationServiceV1 (com.hazelcast.internal.serialization.impl.SerializationServiceV1)1 TestPortableFactory (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.TestPortableFactory)1