Search in sources :

Example 1 with Method

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method 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:
     *
     * 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>
     *
     * <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>
     *
     * 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>
     *
     * <li>for all primitives apart from UTF (exception expected)<ul>
     * <li>scenario(prim(NONE), IllegalArgumentException.class, Byte, "bytes[0]"),</li>
     * <li>scenario(prim(NULL), IllegalArgumentException.class, Byte, "bytes[1]"),</li>
     * </ul></li>
     *
     * <li>for UTF (null expected)<ul>
     * <li>scenario(prim(NONE), null, UTF, "strings[0]"),</li>
     * <li>scenario(prim(NULL), null, UTF, "strings[1]"),</li>
     * </ul></li>
     * </ul>
     */
private static Collection<Object[]> expandPrimitiveArrayScenario(Portable input, PrimitivePortable result, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<Object[]>();
    // group A:
    for (Method method : getPrimitiveArrays()) {
        String path = pathToExplode.replace("primitiveArray", method.field);
        Object resultToMatch = result != null ? result.getPrimitiveArray(method) : null;
        Object resultToMatchAny = resultToMatch;
        if (resultToMatchAny != null && Array.getLength(resultToMatchAny) == 0) {
            resultToMatchAny = null;
        }
        scenarios.addAll(asList(scenario(input, resultToMatch, method, path, parent), scenario(input, resultToMatchAny, method, path + "[any]", parent)));
        scenarios.addAll(asList(scenario(input, resultToMatch, Generic, path, parent), scenario(input, resultToMatchAny, Generic, path + "[any]", parent)));
    }
    // group B:
    for (Method method : getPrimitives(true)) {
        String path = pathToExplode.replace("primitiveArray", method.field).replace("_", "s");
        if (result == null || result.getPrimitiveArray(method) == null || Array.getLength(result.getPrimitiveArray(method)) == 0) {
            if (method.equals(UTF)) {
                scenarios.addAll(asList(scenario(input, null, method, path + "[0]", parent), scenario(input, null, Generic, path + "[0]", parent)));
            } else {
                // IMPORTANT: the difference between generic and non-generic primitive call for null
                scenarios.addAll(asList(scenario(input, IllegalArgumentException.class, method, path + "[0]", parent), scenario(input, null, Generic, path + "[0]", parent)));
            }
        } else {
            scenarios.addAll(asList(scenario(input, Array.get(result.getPrimitiveArray(method), 0), method, path + "[0]", parent), scenario(input, Array.get(result.getPrimitiveArray(method), 1), method, path + "[1]", parent), scenario(input, Array.get(result.getPrimitiveArray(method), 2), method, path + "[2]", parent)));
            scenarios.addAll(asList(scenario(input, Array.get(result.getPrimitiveArray(method), 0), Generic, path + "[0]", parent), scenario(input, Array.get(result.getPrimitiveArray(method), 1), Generic, path + "[1]", parent), scenario(input, Array.get(result.getPrimitiveArray(method), 2), Generic, path + "[2]", parent)));
        }
    }
    return scenarios;
}
Also used : ArrayList(java.util.ArrayList) Method(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method)

Example 2 with Method

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method 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 3 with Method

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

the class DefaultPortableReaderSpecTest method expandPrimitiveScenarioWrongMethodType.

/**
     * Expands test cases for primitive non-array data types.
     * Word "primitive_" is replaced by each primitive type and the scenario is to call a wrong method type for the field.
     *
     * So, for example, for primitive byte field we call methods for other data types.
     * <ul>
     * <li>scenario(input, result.byte_, Short, adjustedPath + "byte_"),</li>
     * <li>scenario(input, result.byte_, Int, adjustedPath + "byte_"),</li>
     * <li>scenario(input, result.byte_, Long, adjustedPath + "byte_"),</li>
     * <li>plus all other combinations that are incorrect (also with read*array method family)</li>
     * </ul>
     */
@SuppressWarnings("SameParameterValue")
private static Collection<Object[]> expandPrimitiveScenarioWrongMethodType(Portable input, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<Object[]>();
    String tokenToReplace = "primitive_";
    for (Method fieldMethod : getPrimitives(true)) {
        for (Method possibleMethod : getPrimitives(true)) {
            // type-specific method case
            String adjustedPath = pathToExplode.replace(tokenToReplace, fieldMethod.field);
            if (fieldMethod != possibleMethod) {
                scenarios.add(scenario(input, IllegalArgumentException.class, possibleMethod, adjustedPath, parent));
            }
            scenarios.add(scenario(input, IllegalArgumentException.class, Method.getArrayMethodFor(possibleMethod), adjustedPath, parent));
        }
    }
    return scenarios;
}
Also used : ArrayList(java.util.ArrayList) Method(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method)

Example 4 with Method

use of com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method 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)

Example 5 with Method

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

the class DefaultPortableReaderSpecTest method expandPrimitiveArrayScenarioWrongMethodType.

/**
     * Expands test cases for primitive array data types.
     * Word "primitive_" is replaced by each primitive type and the scenario is to call a wrong method type for the field.
     *
     * So, for example, for primitive byte field we call methods for other data types.
     * <ul>
     * <li>scenario(input, result.bytes, Short, adjustedPath + "bytes"),</li>
     * <li>scenario(input, result.bytes, Int, adjustedPath + "bytes"),</li>
     * <li>scenario(input, result.bytes, Long, adjustedPath + "bytes"),</li>
     * <li>plus all other combinations that are incorrect (also with read*array method family)</li>
     * </ul>
     */
@SuppressWarnings("SameParameterValue")
private static Collection<Object[]> expandPrimitiveArrayScenarioWrongMethodType(Portable input, String pathToExplode, String parent) {
    List<Object[]> scenarios = new ArrayList<Object[]>();
    String tokenToReplace = "primitiveArray";
    for (Method fieldMethod : getPrimitiveArrays()) {
        for (Method possibleMethod : getPrimitives(true)) {
            // type-specific method case
            String adjustedPath = pathToExplode.replace(tokenToReplace, fieldMethod.field);
            if (fieldMethod != Method.getArrayMethodFor(possibleMethod)) {
                scenarios.add(scenario(input, IllegalArgumentException.class, Method.getArrayMethodFor(possibleMethod), adjustedPath, parent));
            }
            scenarios.add(scenario(input, IllegalArgumentException.class, possibleMethod, adjustedPath, parent));
        }
    }
    return scenarios;
}
Also used : ArrayList(java.util.ArrayList) Method(com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method)

Aggregations

Method (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.Method)5 ArrayList (java.util.ArrayList)5 PrimitivePortable (com.hazelcast.nio.serialization.impl.DefaultPortableReaderTestStructure.PrimitivePortable)2 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1