Skip to content

Commit

Permalink
changes as per suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
bncriju committed Sep 13, 2024
1 parent 82bc5e6 commit c78762a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
public class XQueryImplUtil {

public static Object executeMatchesFunction(String input, String pattern, String flags){
String XQueryExpression = String.format("matches('%s', '%s', '%s')", input, pattern, flags);
return evaluateXQueryExpression(XQueryExpression);
flags = flags == null ? "" : flags;
String xQueryExpression = String.format("matches('%s', '%s', '%s')", input, pattern, flags);
return evaluateXQueryExpression(xQueryExpression);
}

public static Object executeReplaceFunction(String input, String pattern, String replacement, String flags) {
String XQueryExpression = String.format("replace('%s', '%s', '%s', '%s')", input, pattern, replacement, flags);
return evaluateXQueryExpression(XQueryExpression);
flags = flags == null ? "" : flags;
String xQueryExpression = String.format("replace('%s', '%s', '%s', '%s')", input, pattern, replacement, flags);
return evaluateXQueryExpression(xQueryExpression);
}

static Object evaluateXQueryExpression (String expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import org.mockito.MockedStatic;

import java.security.InvalidParameterException;
import java.util.regex.PatternSyntaxException;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatNoException;
import static org.mockito.Mockito.*;

class MatchesFunctionTest {
Expand All @@ -39,11 +37,11 @@ void invokeNull() {

@Test
void invokeUnsupportedFlags() {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("foobar", "fo.bar", "g"));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", "p"));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", "X"));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", " "));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", "iU"));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("foobar", "fo.bar", "g"));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", "p"));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", "X"));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", " "));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("abracadabra", "bra", "iU"));
}

@Test
Expand All @@ -63,9 +61,9 @@ void invokeWithoutFlagsNotMatch() {
FunctionTestUtil.assertResult(MatchesFunction.matchFunctionWithFlags("h", "(.)\3",null), false);
FunctionTestUtil.assertResult(MatchesFunction.matchFunctionWithFlags("h", "(.)\2",null), false);
FunctionTestUtil.assertResult(MatchesFunction.matchFunctionWithFlags("input", "\3",null), false);
FunctionTestUtil.assertResult(MatchesFunction.matchFunctionWithFlags("fo\nbar", "(?iU)(?iU)(ab)[|cd]",null), false);
FunctionTestUtil.assertResult(MatchesFunction.matchFunctionWithFlags("fo\nbar", "(?x)(?i)hello world","i"), false);
FunctionTestUtil.assertResult(MatchesFunction.matchFunctionWithFlags("fo\nbar", "(?xi)hello world",null), false);
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("fo\nbar", "(?iU)(?iU)(ab)[|cd]",null));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() ->MatchesFunction.matchFunctionWithFlags("fo\nbar", "(?x)(?i)hello world","i"));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() ->MatchesFunction.matchFunctionWithFlags("fo\nbar", "(?xi)hello world",null));
}

@Test
Expand Down Expand Up @@ -98,20 +96,20 @@ void invokeWithAllFlags() {

@Test
void checkForPatternTest() {
assertThatExceptionOfType(PatternSyntaxException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("foobar", "(abc|def(ghi", "i"));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> MatchesFunction.matchFunctionWithFlags("foobar", "(abc|def(ghi", "i"));
}

/*@Test
@Test
void checkMatchFunctionWithFlagsInvocation() {
MatchesFunction matchesFunctionSpied = spy(MatchesFunction.INSTANCE);
matchesFunctionSpied.invoke("input", "pattern");
matchesFunctionSpied.invoke("input", "pattern",null);
verify(matchesFunctionSpied, times(1)).invoke("input", "pattern", null);
try (MockedStatic<MatchesFunction> matchesFunctionMocked = mockStatic(MatchesFunction.class)) {
matchesFunctionSpied.invoke("input", "pattern");
matchesFunctionSpied.invoke("input", "pattern",null);
matchesFunctionMocked.verify(() -> MatchesFunction.matchFunctionWithFlags("input", "pattern", null));
matchesFunctionSpied.invoke("input", "pattern", "flags");
matchesFunctionMocked.verify(() -> MatchesFunction.matchFunctionWithFlags("input", "pattern", "flags"));
}
}*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class XQueryImplUtilTest {

Expand All @@ -36,19 +37,28 @@ void executeMatchesFunctionTest() {

input = "fo\nbar";
pattern = "o.b";
flags = "";
flags = null;
retrieved = XQueryImplUtil.executeMatchesFunction(input, pattern, flags);
expected = false;
assertThat(retrieved).isNotNull().isEqualTo(expected);

input = "test";
input = "TEST";
pattern = "test";
flags = "n";
flags = "i";
retrieved = XQueryImplUtil.executeMatchesFunction(input, pattern, flags);
expected = true;
assertThat(retrieved).isNotNull().isEqualTo(expected);
}

@Test
void executeMatchesFunctionInvokingException(){
String input = "test";
String pattern = "^test";
String flags = "g";
assertThatThrownBy(() -> XQueryImplUtil.executeMatchesFunction(input, pattern,
flags)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Unrecognized flag");
}

@Test
void executeReplaceFunctionTest() {
String input = "testString";
Expand Down

0 comments on commit c78762a

Please sign in to comment.