Browse Source

refactor: Design Smells

pull/1008/head
Vaibhav Ramchandani 11 months ago
parent
commit
5b33010609
  1. 13
      json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java
  2. 10
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java
  3. 57
      json-path/src/test/java/com/jayway/jsonpath/Configurations.java
  4. 3
      json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue191.java
  5. 3
      json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java
  6. 2
      json-path/src/test/java/com/jayway/jsonpath/internal/function/KeySetFunctionTest.java
  7. 2
      json-path/src/test/java/com/jayway/jsonpath/internal/function/SequentialPathFunctionTest.java

13
json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java

@ -3,7 +3,6 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.InvalidPathException;
public class CharacterIndex { public class CharacterIndex {
private static final char OPEN_PARENTHESIS = '('; private static final char OPEN_PARENTHESIS = '(';
private static final char CLOSE_PARENTHESIS = ')'; private static final char CLOSE_PARENTHESIS = ')';
private static final char CLOSE_SQUARE_BRACKET = ']'; private static final char CLOSE_SQUARE_BRACKET = ']';
@ -29,6 +28,7 @@ public class CharacterIndex {
this.endPosition = charSequence.length() - 1; this.endPosition = charSequence.length() - 1;
} }
public int length() { public int length() {
return endPosition + 1; return endPosition + 1;
} }
@ -320,4 +320,15 @@ public class CharacterIndex {
skipBlanksAtEnd(); skipBlanksAtEnd();
return this; return this;
} }
public void readWhitespace() {
while (inBounds()) {
char c = currentChar();
if (!Character.isWhitespace(c)) {
break;
}
incrementPosition(1);
}
}
} }

10
json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java

@ -85,13 +85,7 @@ public class PathCompiler {
} }
private void readWhitespace() { private void readWhitespace() {
while (path.inBounds()) { path.readWhitespace();
char c = path.currentChar();
if (!isWhitespace(c)) {
break;
}
path.incrementPosition(1);
}
} }
private Boolean isPathContext(char c) { private Boolean isPathContext(char c) {
@ -101,7 +95,7 @@ public class PathCompiler {
//[$ | @] //[$ | @]
private RootPathToken readContextToken() { private RootPathToken readContextToken() {
readWhitespace(); path.readWhitespace();
if (!isPathContext(path.currentChar())) { if (!isPathContext(path.currentChar())) {
throw new InvalidPathException("Path must start with '$' or '@'"); throw new InvalidPathException("Path must start with '$' or '@'");

57
json-path/src/test/java/com/jayway/jsonpath/Configurations.java

@ -16,37 +16,37 @@ import java.util.Arrays;
public class Configurations { public class Configurations {
public static final Configuration JSON_ORG_CONFIGURATION = Configuration private static final Configuration JSON_ORG_CONFIGURATION = Configuration
.builder() .builder()
.mappingProvider(new JsonOrgMappingProvider()) .mappingProvider(new JsonOrgMappingProvider())
.jsonProvider(new JsonOrgJsonProvider()) .jsonProvider(new JsonOrgJsonProvider())
.build(); .build();
public static final Configuration GSON_CONFIGURATION = Configuration private static final Configuration GSON_CONFIGURATION = Configuration
.builder() .builder()
.mappingProvider(new GsonMappingProvider()) .mappingProvider(new GsonMappingProvider())
.jsonProvider(new GsonJsonProvider()) .jsonProvider(new GsonJsonProvider())
.build(); .build();
public static final Configuration JACKSON_CONFIGURATION = Configuration private static final Configuration JACKSON_CONFIGURATION = Configuration
.builder() .builder()
.mappingProvider(new JacksonMappingProvider()) .mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider()) .jsonProvider(new JacksonJsonProvider())
.build(); .build();
public static final Configuration JACKSON_JSON_NODE_CONFIGURATION = Configuration private static final Configuration JACKSON_JSON_NODE_CONFIGURATION = Configuration
.builder() .builder()
.mappingProvider(new JacksonMappingProvider()) .mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonNodeJsonProvider()) .jsonProvider(new JacksonJsonNodeJsonProvider())
.build(); .build();
public static final Configuration JSON_SMART_CONFIGURATION = Configuration private static final Configuration JSON_SMART_CONFIGURATION = Configuration
.builder() .builder()
.mappingProvider(new JsonSmartMappingProvider()) .mappingProvider(new JsonSmartMappingProvider())
.jsonProvider(new JsonSmartJsonProvider()) .jsonProvider(new JsonSmartJsonProvider())
.build(); .build();
public static final Configuration JAKARTA_CONFIGURATION = Configuration private static final Configuration JAKARTA_CONFIGURATION = Configuration
.builder() .builder()
.mappingProvider(new JakartaMappingProvider()) .mappingProvider(new JakartaMappingProvider())
.jsonProvider(new JakartaJsonProvider()) .jsonProvider(new JakartaJsonProvider())
@ -54,21 +54,46 @@ public class Configurations {
public static Iterable<Configuration> configurations() { public static Iterable<Configuration> configurations() {
return Arrays.asList( return Arrays.asList(
JSON_SMART_CONFIGURATION JSON_SMART_CONFIGURATION,
,GSON_CONFIGURATION GSON_CONFIGURATION,
,JACKSON_CONFIGURATION JACKSON_CONFIGURATION,
,JACKSON_JSON_NODE_CONFIGURATION JACKSON_JSON_NODE_CONFIGURATION,
,JSON_ORG_CONFIGURATION JSON_ORG_CONFIGURATION,
,JAKARTA_CONFIGURATION JAKARTA_CONFIGURATION
); );
} }
public static Iterable<Configuration> objectMappingConfigurations() { public static Iterable<Configuration> objectMappingConfigurations() {
return Arrays.asList( return Arrays.asList(
GSON_CONFIGURATION GSON_CONFIGURATION,
,JACKSON_CONFIGURATION JACKSON_CONFIGURATION,
,JACKSON_JSON_NODE_CONFIGURATION JACKSON_JSON_NODE_CONFIGURATION,
,JAKARTA_CONFIGURATION JAKARTA_CONFIGURATION
); );
} }
// Public getter methods for accessing configurations
public static Configuration getJsonOrgConfiguration() {
return JSON_ORG_CONFIGURATION;
}
public static Configuration getGsonConfiguration() {
return GSON_CONFIGURATION;
}
public static Configuration getJacksonConfiguration() {
return JACKSON_CONFIGURATION;
}
public static Configuration getJacksonJsonNodeConfiguration() {
return JACKSON_JSON_NODE_CONFIGURATION;
}
public static Configuration getJsonSmartConfiguration() {
return JSON_SMART_CONFIGURATION;
}
public static Configuration getJakartaConfiguration() {
return JAKARTA_CONFIGURATION;
}
} }

3
json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue191.java

@ -16,7 +16,8 @@ import java.io.InputStream;
*/ */
public class Issue191 { public class Issue191 {
private Configuration conf = Configurations.GSON_CONFIGURATION; private Configuration conf = Configurations.getGsonConfiguration();
@Test @Test
public void testResultSetNumericComputation() { public void testResultSetNumericComputation() {

3
json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java

@ -52,7 +52,8 @@ public class JSONEntityPathFunctionTest extends BaseFunctionTest {
private Configuration conf = Configurations.JSON_SMART_CONFIGURATION;
private Configuration conf= Configurations.getJsonSmartConfiguration();
@Test @Test
public void testLengthOfTextArray() { public void testLengthOfTextArray() {

2
json-path/src/test/java/com/jayway/jsonpath/internal/function/KeySetFunctionTest.java

@ -14,7 +14,7 @@ import java.util.HashSet;
*/ */
public class KeySetFunctionTest extends BaseFunctionTest { public class KeySetFunctionTest extends BaseFunctionTest {
private Configuration conf = Configurations.JACKSON_CONFIGURATION; private Configuration conf = Configurations.getJsonSmartConfiguration();
@Test @Test
public void testKeySet() throws Exception { public void testKeySet() throws Exception {

2
json-path/src/test/java/com/jayway/jsonpath/internal/function/SequentialPathFunctionTest.java

@ -15,7 +15,7 @@ import org.junit.jupiter.api.Test;
*/ */
public class SequentialPathFunctionTest extends BaseFunctionTest { public class SequentialPathFunctionTest extends BaseFunctionTest {
private Configuration conf = Configurations.JACKSON_CONFIGURATION; private Configuration conf = Configurations.getJsonSmartConfiguration();
@Test @Test
public void testFirstOfNumbers() throws Exception { public void testFirstOfNumbers() throws Exception {

Loading…
Cancel
Save