Browse Source

Avoid Locale-dependant InvalidPathException in RelationalOperator, fixes #600

pull/601/head
René Kriegler 5 years ago
parent
commit
698dbfae27
  1. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java
  2. 39
      json-path/src/test/java/com/jayway/jsonpath/internal/filter/RelationalOperatorTest.java

7
json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java

@ -2,6 +2,8 @@ package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.InvalidPathException;
import java.util.Locale;
public enum RelationalOperator {
GTE(">="),
@ -40,9 +42,10 @@ public enum RelationalOperator {
this.operatorString = operatorString;
}
public static RelationalOperator fromString(String operatorString){
public static RelationalOperator fromString(String operatorString) {
String upperCaseOperatorString = operatorString.toUpperCase(Locale.ROOT);
for (RelationalOperator operator : RelationalOperator.values()) {
if(operator.operatorString.equals(operatorString.toUpperCase()) ){
if(operator.operatorString.equals(upperCaseOperatorString) ){
return operator;
}
}

39
json-path/src/test/java/com/jayway/jsonpath/internal/filter/RelationalOperatorTest.java

@ -0,0 +1,39 @@
package com.jayway.jsonpath.internal.filter;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
public class RelationalOperatorTest {
Locale locale;
@Before
public void saveDefaultLocale() {
locale = Locale.getDefault();
}
@After
public void restoreDefaultLocale() {
Locale.setDefault(locale);
}
@Test
public void testFromStringWithEnglishLocale() {
Locale.setDefault(Locale.ENGLISH);
assertEquals(RelationalOperator.IN, RelationalOperator.fromString("in"));
assertEquals(RelationalOperator.IN, RelationalOperator.fromString("IN"));
}
@Test
public void testFromStringWithTurkishLocale() {
Locale.setDefault(new Locale("tr", "TR"));
assertEquals(RelationalOperator.IN, RelationalOperator.fromString("in"));
assertEquals(RelationalOperator.IN, RelationalOperator.fromString("IN"));
}
}
Loading…
Cancel
Save