Skip to content

Commit

Permalink
added ability to validate
Browse files Browse the repository at this point in the history
  • Loading branch information
judovana committed Jul 31, 2024
1 parent a15ad56 commit a774505
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
63 changes: 62 additions & 1 deletion system/jcstress/Generate.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -89,6 +103,11 @@ public class Generate {


public static void main(String... args) throws Exception {
if (getOutputStyle().equals(OutputType.VALIDATE)){
String xml = readPlaylistArg(args);
validate(xml);
return;
}
String jar = readArg(args);
setAndPrintSetup();
setUsedJvm();
Expand Down Expand Up @@ -216,6 +235,18 @@ private static String readArg(String[] args) throws MalformedURLException {
return jar;
}

private static String readPlaylistArg(String[] args) throws MalformedURLException {
String jar = "playlist.xml";
if (args.length > 0) {
jar = args[0];
}
jarFile = new File(jar);
if (!jarFile.exists()) {
throw new RuntimeException(jar + " does not exists");
}
return jar;
}

private static TestDetails getJcstressTests(String clazz) throws Exception {
Class cl = jarFileClasses.loadClass(clazz);
int arbiters = getMethodsAnnotatedWith(cl, new String[]{"Arbiter"}).size();
Expand Down Expand Up @@ -391,6 +422,10 @@ private static OutputType getOutputStyle() {
return OutputType.STATS;
} else if ("regexes".equals(output)) {
return OutputType.REGEXES;
} else if ("generate".equals(output)) {
return OutputType.GENERATE;
} else if ("validate".equals(output)) {
return OutputType.VALIDATE;
} else {
return OutputType.GENERATE;
}
Expand Down Expand Up @@ -734,8 +769,34 @@ public static void calculateStats(List<GroupWithCases> results, List<GroupWithCa
System.out.println("Avg differecne from ideal: " + (100 - (forAvgPercentAvg / results.size())) + "%");
}

public static void validate(String xml) throws Exception {
System.err.println("Checking: " + xml);
wellFormed(xml);
validByXsd(xml);
}

public static void wellFormed(String xml) throws ParserConfigurationException, SAXException, IOException {
System.err.println("Well formed?");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(xml));
System.err.println("Well formed!");
}

public static void validByXsd(String xml) throws ParserConfigurationException, SAXException, IOException {
String url = "https://raw.githubusercontent.com/adoptium/TKG/master/resources/playlist.xsd";
System.err.println("Valid by " + url + " ?");
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new URL(url));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xml)));
System.err.println("Valid!");
}

private enum OutputType {
GENERATE, DO, TEST, STATS, REGEXES
GENERATE, DO, TEST, STATS, REGEXES, VALIDATE
}

private interface TestDetails {
Expand Down
1 change: 1 addition & 0 deletions system/jcstress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ In order of importance and reasonability
* test - will fork and kill `java jcstress.jar` and estimate time of all groups. If interrupted, will print at least what was calculated up to that time
* stats - will print amount of tests in each group
* regexes - will print just final regexes
* validate - will validate given (instead of jar) playlist.xml
* **generate - default, will print playlist.xml to stdout**
* LIMIT - number. **default is 100**. Every group smaller then LIMIT will be merged to bigger subset.
* SMALL_GROUPS - true/false. **default is false**. After natural grouping is done, all remaining groups smaller then LIMIT are merged to artificial groups
Expand Down

0 comments on commit a774505

Please sign in to comment.