@Test public void testMatrixParameters() { String segment = "segment"; String segmentMatrixParam = segment + ";param=value"; String segmentValue = AbstractURLSegment.getActualSegment(segment); assertEquals(segment, segmentValue); Map<String, String> matrixParams = AbstractURLSegment.getSegmentMatrixParameters(segment); assertTrue(matrixParams.size() == 0); segmentValue = AbstractURLSegment.getActualSegment(segmentMatrixParam); assertEquals(segment, segmentValue); matrixParams = AbstractURLSegment.getSegmentMatrixParameters(segmentMatrixParam); assertEquals(1, matrixParams.size()); assertNotNull(matrixParams.get("param")); String segmentMatrixParamsQuotes = segment + ";param=value;param1='hello world'"; matrixParams = AbstractURLSegment.getSegmentMatrixParameters(segmentMatrixParamsQuotes); assertEquals(2, matrixParams.size()); assertEquals("value", matrixParams.get("param")); assertEquals("'hello world'", matrixParams.get("param1")); }
/** * Extract method parameter value from matrix parameters. * * @param pageParameters PageParameters for the current request. * @param matrixParam the {@link MatrixParam} annotation used for the current parameter. * @param argClass the type of the current method parameter. * @return the value obtained from query parameters and converted to argClass. */ private Object extractParameterFromMatrixParams( PageParameters pageParameters, MatrixParam matrixParam, Class<?> argClass) { int segmentIndex = matrixParam.segmentIndex(); String variableName = matrixParam.parameterName(); String rawsSegment = pageParameters.get(segmentIndex).toString(); Map<String, String> matrixParameters = AbstractURLSegment.getSegmentMatrixParameters(rawsSegment); if (matrixParameters.get(variableName) == null) return null; return toObject(argClass, matrixParameters.get(variableName)); }