Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/zachary_moore/filters/BaseFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BaseFilter {
* @return {@link ArrayList<T>} of given class type from JSONFile
*/
<T> List<T> getAllOfType(JSONFile file, Class<T> clazz) {
Object baseObject = file.getObject();
Object baseObject = file.getChild();
if (baseObject instanceof JSONObject) {
return accumulateType((JSONObject) baseObject, clazz);
} else if (baseObject instanceof JSONArray){
Expand All @@ -42,7 +42,7 @@ <T> List<T> getAllOfType(JSONFile file, Class<T> clazz) {
*/
<T> List<WrappedPrimitive<T>> getAllOfWrappedType(JSONFile file, Class<T> clazz) {
ArrayList<WrappedPrimitive<T>> wrappedTypeList = new ArrayList<>();
Object baseObject = file.getObject();
Object baseObject = file.getChild();
if (baseObject instanceof JSONObject) {
wrappedTypeList.addAll(accumulateWrappedTypeFromEntrySet(((JSONObject) baseObject).toMap().entrySet(), clazz));
} else if (baseObject instanceof JSONArray) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/zachary_moore/filters/FilterMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

public class FilterMapper {
Expand Down Expand Up @@ -50,6 +51,8 @@ public static List<?> filter(JSONFile jsonFile, LintImplementation lintImplement
return filters.filterToObjects(jsonFile);
} else if (lintImplementation.getClazz() == JSONArray.class) {
return filters.filterToArrays(jsonFile);
} else if (lintImplementation.getClazz() == JSONFile.class) {
return Collections.singletonList(jsonFile);
} else {
return null;
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/zachary_moore/lint/BaseJSONAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.zachary_moore.lint;

import com.zachary_moore.objects.JSONArray;
import com.zachary_moore.objects.JSONObject;
import com.zachary_moore.objects.WrappedObject;
import com.zachary_moore.objects.WrappedPrimitive;
import com.zachary_moore.objects.*;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -213,4 +210,15 @@ protected <T> boolean isParentOfType(WrappedObject object, Class<T> clazz) {
protected boolean reduceBooleans(Boolean... booleans) {
return Arrays.stream(booleans).filter(b -> !b).count() == 0;
}

protected int getLineNumber(String offendingText, WrappedObject originatingObject) {
if (originatingObject == null) {
return -1;
}
if (originatingObject instanceof JSONFile) {
return ((JSONFile) originatingObject).getLineNumber(offendingText);
} else {
return getLineNumber(offendingText, originatingObject.getParentObject());
}
}
}
75 changes: 62 additions & 13 deletions src/main/java/com/zachary_moore/objects/JSONFile.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,98 @@
package com.zachary_moore.objects;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONException;
import org.json.JSONTokener;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JSONFile {
public class JSONFile implements WrappedObject {

private org.json.JSONObject jsonObject;
private org.json.JSONArray jsonArray;
private JSONObject wrappedJsonObject;
private JSONArray wrappedJsonArray;
private String filePath;
private String fileExtension;
private List<String> linesInFile;

public JSONFile(File file) throws IOException {
filePath = file.getCanonicalPath();
fileExtension = FilenameUtils.getExtension(filePath);
initializeLineNumberData(file);
initializeWrappedObjects(file);
}

private void initializeWrappedObjects(File file) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
JSONTokener jsonTokener = new JSONTokener(bufferedReader);
try {
jsonObject = new org.json.JSONObject(jsonTokener);
this.wrappedJsonObject = new JSONObject(null,
this,
new org.json.JSONObject(jsonTokener));
} catch (JSONException e) {
jsonTokener.back();
jsonArray = new org.json.JSONArray(jsonTokener);
this.wrappedJsonArray = new JSONArray(null,
this,
new org.json.JSONArray(jsonTokener));
}
bufferedReader.close();
}

/**
* @return either {@link JSONObject} or {@link JSONArray} based on input file to constructor
*/
public Object getObject() {
if (jsonObject != null) {
return new JSONObject(null, null, jsonObject);
} else if (jsonArray != null) {
return new JSONArray(null, null, jsonArray);
} else {
throw new RuntimeException("Could not parse either a JSONArray or JSONObject from file");
}
public WrappedObject getChild() {
return wrappedJsonObject != null ? wrappedJsonObject : wrappedJsonArray;
}

@Override
public String getOriginatingKey() {
return null;
}

@Override
public WrappedObject getParentObject() {
return null;
}

@Override
public void parseAndReplaceWithWrappers() { }

@Override
public boolean isPrimitive() {
return false;
}

public String getFilePath() {
return filePath;
}

public String getFileExtension() {
return fileExtension;
}

private void initializeLineNumberData(File file) throws IOException{
linesInFile = new ArrayList<>();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null) {
linesInFile.add(line);
}
}

public int getLineNumber(String offendingText) {
for (int i = 0; i < linesInFile.size(); i++) {
if (linesInFile.get(i).contains(offendingText)) {
return i + 1;
}
}
return -1;
}

@Override
public int hashCode() {
return this.filePath.hashCode();
Expand Down
53 changes: 40 additions & 13 deletions src/test/java/com/zachary_moore/objects/JSONFileShould.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,56 @@

import java.io.File;
import java.io.IOException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;


public class JSONFileShould {

private File jsonObjectFile;
private File jsonArrayFile;

public JSONFileShould() {
jsonObjectFile = new File(getClass()
.getClassLoader()
.getResource("test-2.json")
.getFile());
jsonArrayFile = new File(getClass()
.getClassLoader()
.getResource("array-file.json")
.getFile());
}

@Test
public void JSONFileGivesJSONArray() throws IOException {
JSONFile jsonFile = new JSONFile(
new File(getClass()
.getClassLoader()
.getResource("array-file.json")
.getFile()));
assert(jsonFile.getObject() instanceof JSONArray);
assert(((JSONArray)jsonFile.getObject()).length() == 3);
JSONFile jsonFile = new JSONFile(jsonArrayFile);
assert(jsonFile.getChild() instanceof JSONArray);
assert(((JSONArray)jsonFile.getChild()).length() == 3);
}

@Test
public void JSONFileGivesJSONObject() throws IOException {
JSONFile jsonFile = new JSONFile(
new File(getClass()
.getClassLoader()
.getResource("test-2.json")
.getFile()));
assert(jsonFile.getObject() instanceof JSONObject);
JSONFile jsonFile = new JSONFile(jsonObjectFile);
assert(jsonFile.getChild() instanceof JSONObject);
}

@Test
public void JSONFileGivesFilePath() throws IOException {
JSONFile jsonFile = new JSONFile(jsonObjectFile);
assert(jsonFile.getFilePath().equals(jsonObjectFile.getAbsolutePath()));
}

@Test
public void JSONFileGivesFileExtension() throws IOException {
JSONFile jsonFile = new JSONFile(jsonObjectFile);
assert(jsonFile.getFileExtension().equals("json"));
}

@Test
public void JSONFileChildShouldHaveParent() throws IOException {
JSONFile jsonFile = new JSONFile(jsonObjectFile);
assertNotNull(jsonFile.getChild().getParentObject());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.zachary_moore.overall;

import com.zachary_moore.lint.LintImplementation;
import com.zachary_moore.lint.LintLevel;
import com.zachary_moore.lint.LintRegister;
import com.zachary_moore.lint.LintRule;
import com.zachary_moore.objects.JSONFile;
import com.zachary_moore.objects.JSONObject;
import com.zachary_moore.objects.WrappedPrimitive;
import com.zachary_moore.runner.LintRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JSONFileLineNumberShould {

private LintRule.Builder builder;
private LintRegister lintRegister;

@BeforeEach
public void setUp() {
this.builder = new LintRule.Builder().setIssueId("");
this.lintRegister = new LintRegister();
}

@Test
public void basicLineNumberShouldReturnCorrect() throws Exception {
LintRule lintRule = this.builder.setImplementation(new LintImplementation<WrappedPrimitive<String>>() {
@Override
public Class<?> getClazz() {
return String.class;
}

@Override
public boolean shouldReport(WrappedPrimitive<String> wrappedPrimitive) {
if (wrappedPrimitive.equals("test")) {
setReportMessage("Error occurs on line " + getLineNumber(wrappedPrimitive.getValue(), wrappedPrimitive));
return true;
}
return false;
}
}).setLevel(LintLevel.ERROR)
.build();
this.lintRegister.register(lintRule);
LintRunner lintRunner =
new LintRunner(this.lintRegister,
"./src/test/resources/test-2.json");

Map<LintRule, Map<JSONFile, List<String>>> lintOutput = lintRunner.lint();
assert(lintRunner.analyzeLintAndGiveExitCode() == 1);
assert(lintOutput.get(lintRule).size() == 1);


for (Map.Entry<JSONFile, List<String>> entry : lintOutput.get(lintRule).entrySet()) {
for (int i = 0; i < entry.getValue().size(); i++) {
if (i == 0) {
assert(entry.getValue().get(i).equals("Error occurs on line 2"));
}
}
}
}
}
Loading