2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.data.jaxen;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.mock;
17 import com.google.common.collect.ImmutableList;
18 import org.jaxen.Context;
19 import org.jaxen.Function;
20 import org.jaxen.FunctionCallException;
21 import org.junit.Test;
23 public class ReMatchXPathFunctionTest {
26 public void testRematchFunction() throws Exception {
27 // re-match() uses regex processing from yang-parser-impl which has been thoroughly tested within
28 // the Bug5410Test unit test class, so here is just a basic test
29 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
30 final Function rematchFunction = yangFunctionContext.getFunction(null, null, "re-match");
32 final Context mockedContext = mock(Context.class);
34 boolean rematchResult = (boolean) rematchFunction.call(mockedContext, ImmutableList.of("abbc", "[abc]{1,4}"));
35 assertTrue(rematchResult);
36 rematchResult = (boolean) rematchFunction.call(mockedContext, ImmutableList.of("abbcc", "[abc]{1,4}"));
37 assertFalse(rematchResult);
41 public void shouldFailOnInvalidNumberOfArguments() throws Exception {
42 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
43 final Function rematchFunction = yangFunctionContext.getFunction(null, null, "re-match");
45 final Context mockedContext = mock(Context.class);
48 rematchFunction.call(mockedContext, ImmutableList.of("abbc", "[abc]{1,4}", "should not be here"));
49 fail("Function call should have failed on invalid number of arguments.");
50 } catch (final FunctionCallException ex) {
51 assertEquals("re-match() takes two arguments: string subject, string pattern.", ex.getMessage());
56 public void shouldFailOnInvalidTypeOfArgument() throws Exception {
57 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
58 final Function rematchFunction = yangFunctionContext.getFunction(null, null, "re-match");
60 final Context mockedContext = mock(Context.class);
63 rematchFunction.call(mockedContext, ImmutableList.of(100, "[abc]{1,4}"));
64 fail("Function call should have failed on invalid type of the subject argument.");
65 } catch (final FunctionCallException ex) {
66 assertEquals("First argument of re-match() should be a String.", ex.getMessage());
70 rematchFunction.call(mockedContext, ImmutableList.of("abbc", 100));
71 fail("Function call should have failed on invalid type of the pattern argument.");
72 } catch (final FunctionCallException ex) {
73 assertEquals("Second argument of re-match() should be a String.", ex.getMessage());