Search in sources :

Example 1 with PrimitiveFields

use of com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitiveFields 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_, adjustedPath + "byte_"),</li>
 * <li>scenario(input, result.short_, adjustedPath + "short_"),</li>
 * <li>scenario(input, result.int_, adjustedPath + "int_"),</li>
 * <li>scenario(input, result.long_, adjustedPath + "long_"),</li>
 * <li>scenario(input, result.float_, adjustedPath + "float_"),</li>
 * <li>scenario(input, result.double_, adjustedPath + "double_"),</li>
 * <li>scenario(input, result.boolean_, adjustedPath + "boolean_"),</li>
 * <li>scenario(input, result.char_, adjustedPath + "char_"),</li>
 * <li>scenario(input, result.string_, adjustedPath + "string_"),</li>
 * </ul>
 */
private static Collection<Object[]> expandPrimitiveScenario(Portable input, Object result, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<>();
    Object adjustedResult;
    for (PrimitiveFields primitiveFields : getPrimitives()) {
        if (result instanceof PrimitivePortable) {
            adjustedResult = ((PrimitivePortable) result).getPrimitive(primitiveFields);
        } else {
            adjustedResult = result;
        }
        // generic method case
        scenarios.add(scenario(input, adjustedResult, pathToExplode.replace("primitive_", primitiveFields.field), parent));
    }
    return scenarios;
}
Also used : PrimitiveFields(com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitiveFields) PrimitivePortable(com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitivePortable) ArrayList(java.util.ArrayList)

Example 2 with PrimitiveFields

use of com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitiveFields in project hazelcast by hazelcast.

the class DefaultPortableReaderSpecTest method expandPrimitiveArrayScenario.

/**
 * Expands test cases for primitive array data types.
 * Word "primitiveArray" is replaced by each primitive array type and the scenario is expanded to for each type:
 * <p>
 * group A:
 * <ul>
 * <li>scenario(prim(FULL), prim(FULL).bytes, ByteArray, "bytes"),</li>
 * <li>scenario(prim(NONE), prim(NONE).bytes, ByteArray, "bytes"),</li>
 * <li>scenario(prim(NULL), prim(NULL).bytes, ByteArray, "bytes"),</li>
 * <p>
 * <li>scenario(prim(FULL), prim(FULL).bytes, ByteArray, "bytes[any]"),</li>
 * <li>scenario(prim(NONE), prim(NONE).bytes, ByteArray, "bytes[any]"),</li>
 * <li>scenario(prim(NULL), prim(NULL).bytes, ByteArray, "bytes[any]"),</li>
 * </ul>
 * <p>
 * group B:
 * <ul>
 * <li>scenario(prim(FULL), prim(FULL).bytes[0], Byte, "bytes[0]"),</li>
 * <li>scenario(prim(FULL), prim(FULL).bytes[1], Byte, "bytes[1]"),</li>
 * <li>scenario(prim(FULL), prim(FULL).bytes[2], Byte, "bytes[2]"),</li>
 * <p>
 * <li>for all primitives <ul>
 * <li>scenario(prim(NONE), null, Byte, "bytes[0]"),</li>
 * <li>scenario(prim(NULL), null, Byte, "bytes[1]"),</li>
 * </ul></li>
 * <p>
 * </ul>
 */
private static Collection<Object[]> expandPrimitiveArrayScenario(Portable input, PrimitivePortable result, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<>();
    // group A:
    for (PrimitiveFields primitiveFields : getPrimitiveArrays()) {
        String path = pathToExplode.replace("primitiveArray", primitiveFields.field);
        Object resultToMatch = result != null ? result.getPrimitiveArray(primitiveFields) : null;
        Object resultToMatchAny = resultToMatch;
        if (resultToMatchAny != null && Array.getLength(resultToMatchAny) == 0) {
            resultToMatchAny = null;
        }
        scenarios.addAll(asList(scenario(input, resultToMatch, path, parent), scenario(input, resultToMatchAny, path + "[any]", parent)));
    }
    // group B:
    for (PrimitiveFields primitiveFields : getPrimitives()) {
        String path = pathToExplode.replace("primitiveArray", primitiveFields.field).replace("_", "s");
        if (result == null || result.getPrimitiveArray(primitiveFields) == null || Array.getLength(result.getPrimitiveArray(primitiveFields)) == 0) {
            scenarios.add(scenario(input, null, path + "[0]", parent));
        } else {
            scenarios.addAll(asList(scenario(input, Array.get(result.getPrimitiveArray(primitiveFields), 0), path + "[0]", parent), scenario(input, Array.get(result.getPrimitiveArray(primitiveFields), 1), path + "[1]", parent), scenario(input, Array.get(result.getPrimitiveArray(primitiveFields), 2), path + "[2]", parent)));
        }
    }
    return scenarios;
}
Also used : PrimitiveFields(com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitiveFields) ArrayList(java.util.ArrayList)

Example 3 with PrimitiveFields

use of com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitiveFields 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:
 * <p>
 * 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>
 * <p>
 * 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>
 * <p>
 * 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" })
private static Collection<Object[]> expandPortableArrayPrimitiveScenario(Portable input, GroupPortable result, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<>();
    // expansion of the portable array using the following quantifiers
    for (String token : asList("0", "1", "2", "any")) {
        String path = pathToExplode.replace("portableArray", "portables[" + token + "]");
        if (token.equals("any")) {
            // expansion of the primitive fields
            for (PrimitiveFields primitiveFields : getPrimitives()) {
                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(primitiveFields));
                }
                if (result == null || result.portables == null || result.portables.length == 0) {
                    resultToMatch = null;
                }
                scenarios.add(scenario(input, resultToMatch, path.replace("primitive_", primitiveFields.field), parent));
            }
        } else {
            // expansion of the primitive fields
            for (PrimitiveFields primitiveFields : getPrimitives()) {
                Object resultToMatch = null;
                try {
                    PrimitivePortable portable = (PrimitivePortable) result.portables[Integer.parseInt(token)];
                    resultToMatch = portable.getPrimitive(primitiveFields);
                } catch (NullPointerException ignored) {
                } catch (IndexOutOfBoundsException ignored) {
                }
                if (result == null || result.portables == null || result.portables.length == 0) {
                    resultToMatch = null;
                }
                scenarios.add(scenario(input, resultToMatch, path.replace("primitive_", primitiveFields.field), parent));
            }
        }
    }
    return scenarios;
}
Also used : PrimitiveFields(com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitiveFields) PrimitivePortable(com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitivePortable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List)

Aggregations

PrimitiveFields (com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitiveFields)3 ArrayList (java.util.ArrayList)3 PrimitivePortable (com.hazelcast.internal.serialization.impl.portable.portablereader.DefaultPortableReaderTestStructure.PrimitivePortable)2 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1