-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement optional tags for modifiers in test.json (Merge PR #94)
This PR also added support for class modifiers. Co-authored-by: Johannes Stöhr <[email protected]>
- Loading branch information
Showing
5 changed files
with
95 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,12 @@ | |
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
@@ -38,7 +41,7 @@ | |
* their access modifiers, annotations and types and the declared enum values of | ||
* an enum</li> | ||
* </ol> | ||
* | ||
* | ||
* All these elements are tests based on the test.json that specifies the | ||
* structural oracle, i.e. how the solution has to look like in terms of | ||
* structural elements. Note: the file test.json can be automatically generated | ||
|
@@ -54,7 +57,7 @@ | |
* JUnit) If no attributes and no enums should be tested for correctness, remove | ||
* {@link AttributeTestProvider}, otherwise one test will fail (limitation of | ||
* JUnit) | ||
* | ||
* | ||
* @author Stephan Krusche ([email protected]) | ||
* @version 5.0 (11.11.2020) | ||
*/ | ||
|
@@ -112,7 +115,8 @@ protected static Class<?> findClassForTestType(ExpectedClassStructure expectedCl | |
fail(classNameScanMessage); | ||
} | ||
try { | ||
return Class.forName(expectedClassStructure.getQualifiedClassName()); | ||
return Class.forName(expectedClassStructure.getQualifiedClassName(), false, | ||
StructuralTestProvider.class.getClassLoader()); | ||
} catch (@SuppressWarnings("unused") ClassNotFoundException e) { | ||
// Note: this error happens when the ClassNameScanner finds the correct file, | ||
// e.g. 'Course.java', but the class 'Course' was not yet created correctly in | ||
|
@@ -125,7 +129,7 @@ protected static Class<?> findClassForTestType(ExpectedClassStructure expectedCl | |
|
||
/** | ||
* get the expected elements or an empty JSON array | ||
* | ||
* | ||
* @param element the class, attribute, method or constructor JSON | ||
* object element | ||
* @param jsonPropertyKey the key used in JSON | ||
|
@@ -152,18 +156,54 @@ protected static boolean checkModifiers(String[] observedModifiers, JSONArray ex | |
if (Arrays.equals(observedModifiers, new String[] { "" }) && expectedModifiers.length() == 0) { | ||
return true; | ||
} | ||
|
||
/* | ||
* If the number of the modifiers does not match, then the modifiers per se do | ||
* not match either. | ||
* Otherwise check if all expected necessary modifiers are contained in the | ||
* array of the observed ones and if any forbidden modifiers were used. | ||
*/ | ||
if (observedModifiers.length != expectedModifiers.length()) { | ||
return false; | ||
Set<ModifierSpecification> modifierSpecifications = new HashSet<>(); | ||
for (int i = 0; i < expectedModifiers.length(); i++) { | ||
modifierSpecifications.add(ModifierSpecification.getModifierForJsonString(expectedModifiers.getString(i))); | ||
} | ||
Set<String> observedModifiersSet = Set.of(observedModifiers); | ||
Set<String> allowedModifiers = modifierSpecifications.stream().map(ModifierSpecification::getModifier) | ||
.collect(Collectors.toSet()); | ||
boolean hasAllNecessaryModifiers = modifierSpecifications.stream().filter(ModifierSpecification::isRequired) | ||
.map(ModifierSpecification::getModifier).allMatch(observedModifiersSet::contains); | ||
boolean hasForbiddenModifier = observedModifiersSet.stream() | ||
.anyMatch(modifier -> !allowedModifiers.contains(modifier)); | ||
|
||
return hasAllNecessaryModifiers && !hasForbiddenModifier; | ||
} | ||
|
||
private static final class ModifierSpecification { | ||
|
||
private final String modifier; | ||
private final boolean optional; | ||
|
||
private ModifierSpecification(String modifier, boolean optional) { | ||
this.modifier = Objects.requireNonNull(modifier); | ||
this.optional = optional; | ||
} | ||
|
||
String getModifier() { | ||
return modifier; | ||
} | ||
|
||
boolean isRequired() { | ||
return !optional; | ||
} | ||
|
||
static ModifierSpecification getModifierForJsonString(String jsonString) { | ||
String[] sections = jsonString.split(":", -1); | ||
if (sections.length == 1) { | ||
return new ModifierSpecification(jsonString, false); | ||
} else if (sections[0].equals("optional")) { | ||
return new ModifierSpecification(sections[1].trim(), true); | ||
} else { | ||
throw new IllegalArgumentException("Invalid entry for modifier: '" + jsonString + "'"); | ||
} | ||
} | ||
/* | ||
* Otherwise check if all expected modifiers are contained in the array of the | ||
* observed ones. If at least one isn't, then the modifiers don't match. | ||
*/ | ||
return Arrays.asList(observedModifiers).containsAll(expectedModifiers.toList()); | ||
} | ||
|
||
protected static boolean checkAnnotations(Annotation[] observedAnnotations, JSONArray expectedAnnotations) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters