Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #2: unit test failure under JDK17 #3

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ <h1>
<p><b>1.0.0</b> -- To be determined.</p>
<ul>
<li>Initial release</li>
<li>Fixes <a href="https://github.com/igniterealtime/openfire-exi-plugin/issues/2">issue #2</a>: Unit test fail under JDK 17</li>
</ul>

</body>
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/cl/clayster/exi/EXIFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ String setupResponse(Element setup, IoSession session) throws IOException
try {
// obtener el schemas File del servidor y transformarlo a un elemento XML
Element serverSchemas;
String schemasFileContent = EXIUtils.readFile(EXIUtils.schemasFileLocation);
String schemasFileContent = EXIUtils.readFile(EXIUtils.getSchemasFileLocation());
if (schemasFileContent == null) {
return null;
}
Expand Down Expand Up @@ -332,7 +332,7 @@ private String createCanonicalSchema(Element setup, Element serverSchemas) throw
final MessageDigest md = MessageDigest.getInstance("MD5");
final String schemaId = EXIUtils.bytesToHex(md.digest(canonicalSchema.asXML().getBytes()));

final Path fileName = EXIUtils.exiFolder.resolve(schemaId + ".xsd");
final Path fileName = EXIUtils.getExiFolder().resolve(schemaId + ".xsd");
try (final FileWriter fileWriter = new FileWriter(fileName.toFile()))
{
final XMLWriter writer = new XMLWriter(fileWriter, OutputFormat.createPrettyPrint());
Expand Down Expand Up @@ -402,7 +402,7 @@ void uploadMissingSchema(String content, IoSession session)
content = content.substring(content.indexOf('>') + 1, content.indexOf("</"));
byte[] outputBytes = Base64.decode(content);

final Path filePath = EXIUtils.schemasFolder.resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");
final Path filePath = EXIUtils.getSchemasFolder().resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");
try (final OutputStream out = Files.newOutputStream(filePath)) {
out.write(outputBytes);
}
Expand All @@ -414,7 +414,7 @@ void uploadMissingSchema(String content, IoSession session)
void uploadCompressedMissingSchema(byte[] content, String contentType, String md5Hash, String bytes, IoSession session)
throws IOException, NoSuchAlgorithmException, DocumentException, EXIException, SAXException, TransformerException
{
Path filePath = EXIUtils.schemasFolder.resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");
Path filePath = EXIUtils.getSchemasFolder().resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");

if (!"text".equals(contentType) && md5Hash != null && bytes != null) {
String xml = "";
Expand Down Expand Up @@ -449,12 +449,12 @@ static void addNewSchemaToSchemasFile(Path fileLocation, String md5Hash, String

// Create schemas file if it does not exist yet.
final Document document;
if (!Files.exists(EXIUtils.schemasFileLocation)) { // no more schemas (only the new one)
if (!Files.exists(EXIUtils.getSchemasFileLocation())) { // no more schemas (only the new one)
document = DocumentHelper.createDocument();
document.addElement("setupResponse");
} else {
// obtener el schemas File del servidor y transformarlo a un elemento XML
final String content = String.join("", Files.readAllLines(EXIUtils.schemasFileLocation));
final String content = String.join("", Files.readAllLines(EXIUtils.getSchemasFileLocation()));
document = DocumentHelper.parseText(content);
}

Expand All @@ -472,7 +472,7 @@ static void addNewSchemaToSchemasFile(Path fileLocation, String md5Hash, String
// XEP-0322 wants canonical schemas to be ordered by namespace.
schemas.sort(Comparator.comparing(element -> element.attributeValue("ns")));

try (final FileWriter fileWriter = new FileWriter(EXIUtils.schemasFileLocation.toFile()))
try (final FileWriter fileWriter = new FileWriter(EXIUtils.getSchemasFileLocation().toFile()))
{
final XMLWriter writer = new XMLWriter(fileWriter, OutputFormat.createPrettyPrint());
writer.write(document);
Expand All @@ -483,7 +483,7 @@ static void addNewSchemaToSchemasFile(Path fileLocation, String md5Hash, String
static void addNewSchemaToCanonicalSchema(Path fileLocation, IoSession session) throws IOException, DocumentException
{
// obtener el schemas File del servidor y transformarlo a un elemento XML
final Path canonicalSchema = EXIUtils.exiFolder.resolve(session.getAttribute(EXIUtils.SCHEMA_ID) + ".xsd");
final Path canonicalSchema = EXIUtils.getExiFolder().resolve(session.getAttribute(EXIUtils.SCHEMA_ID) + ".xsd");
EXIUtils.addNewSchemaToCanonicalSchema(canonicalSchema, fileLocation);
session.setAttribute(EXIUtils.CANONICAL_SCHEMA_LOCATION, canonicalSchema.toAbsolutePath().toString());
}
Expand All @@ -492,7 +492,7 @@ static void addNewSchemaToCanonicalSchema(Path fileLocation, IoSession session)

private void saveDownloadedSchema(String content, IoSession session) throws NoSuchAlgorithmException, IOException, DocumentException
{
Path filePath = EXIUtils.schemasFolder.resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");
Path filePath = EXIUtils.getSchemasFolder().resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");

OutputStream out = Files.newOutputStream(filePath);
out.write(content.getBytes());
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/cl/clayster/exi/EXISetupConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public boolean isSessionWideBuffers()
public Path getCanonicalSchemaLocation()
{
if (schemaId != null) {
return EXIUtils.exiFolder.resolve(schemaId + ".xsd");
return EXIUtils.getExiFolder().resolve(schemaId + ".xsd");
} else {
return EXIUtils.defaultCanonicalSchemaLocation;
return EXIUtils.getDefaultCanonicalSchemaLocation();
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public boolean saveConfiguration() throws IOException
Log.warn("Exception while trying to save configuration.", e);
}

Path fileName = EXIUtils.exiFolder.resolve(configurationId + ".xml");
Path fileName = EXIUtils.getExiFolder().resolve(configurationId + ".xml");
if (Files.exists(fileName)) {
return true;
} else {
Expand All @@ -238,7 +238,7 @@ public boolean saveConfiguration() throws IOException
*/
public static EXISetupConfiguration parseQuickConfigId(String configId) throws DocumentException
{
Path fileLocation = EXIUtils.exiFolder.resolve(configId + ".xml");
Path fileLocation = EXIUtils.getExiFolder().resolve(configId + ".xml");
String content = EXIUtils.readFile(fileLocation);
if (content == null)
return null;
Expand Down
55 changes: 45 additions & 10 deletions src/main/java/cl/clayster/exi/EXIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,52 @@ public class EXIUtils
{
private static final Logger Log = LoggerFactory.getLogger(EXIUtils.class);

static Path schemasFolder = Paths.get(JiveGlobals.getHomeDirectory(), "plugins", "exi", "classes");
static Path schemasFileLocation = schemasFolder.resolve("schemas.xml");
static Path exiFolder = schemasFolder.resolve("canonicalSchemas");
static Path defaultCanonicalSchemaLocation = exiFolder.resolve("defaultSchema.xsd");
private static Path schemasFolder;
private static Path schemasFileLocation ;
private static Path exiFolder;
private static Path defaultCanonicalSchemaLocation;
final static String CANONICAL_SCHEMA_LOCATION = "canonicalSchemaLocation";
final static String EXI_CONFIG = "exiConfig";
final static String SCHEMA_ID = "schemaId";
final static String EXI_PROCESSOR = EXIProcessor.class.getName();

final protected static char[] hexArray = "0123456789abcdef".toCharArray();

synchronized static Path getSchemasFolder() {
if (schemasFolder == null) {
schemasFolder = Paths.get(JiveGlobals.getHomeDirectory(), "plugins", "exi", "classes");
}
return schemasFolder;
}

synchronized static void setSchemasFolder(Path folder) {
schemasFolder = folder;
schemasFileLocation = null;
exiFolder = null;
defaultCanonicalSchemaLocation = null;
}

synchronized static Path getSchemasFileLocation() {
if (schemasFileLocation == null) {
schemasFileLocation = getSchemasFolder().resolve("schemas.xml");
}
return schemasFileLocation;
}

synchronized static Path getExiFolder() {
if (exiFolder == null) {
exiFolder = getSchemasFolder().resolve("canonicalSchemas");
}
return exiFolder;
}

synchronized static Path getDefaultCanonicalSchemaLocation() {
if (defaultCanonicalSchemaLocation == null) {
defaultCanonicalSchemaLocation = getExiFolder().resolve("defaultSchema.xsd");
}
return defaultCanonicalSchemaLocation;
}

/**
* Returns a hexadecimal String representation of the given bytes.
*
Expand Down Expand Up @@ -230,12 +265,12 @@ public static byte[] concat(byte[] a, byte[] b)
static void generateSchemasFile() throws IOException
{
try {
Files.createDirectories(EXIUtils.schemasFolder);
Files.createDirectories(EXIUtils.exiFolder);
Files.createDirectories(EXIUtils.getSchemasFolder());
Files.createDirectories(EXIUtils.getExiFolder());

// Read all XSDs
final Set<Path> xsds;
try (final Stream<Path> stream = Files.walk(EXIUtils.schemasFolder, 1)) {
try (final Stream<Path> stream = Files.walk(EXIUtils.getSchemasFolder(), 1)) {
xsds = stream
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".xsd"))
Expand Down Expand Up @@ -271,7 +306,7 @@ static void generateSchemasFile() throws IOException
final Element schemas = schemasFile.addElement("schemas");
schemaElements.forEach(schemas::add);

try (final FileWriter fileWriter = new FileWriter(EXIUtils.schemasFileLocation.toFile()))
try (final FileWriter fileWriter = new FileWriter(EXIUtils.getSchemasFileLocation().toFile()))
{
final XMLWriter writer = new XMLWriter(fileWriter, OutputFormat.createPrettyPrint());
writer.write(schemasFile);
Expand All @@ -291,7 +326,7 @@ static void generateDefaultCanonicalSchema() throws IOException
boolean[] schemasFound = {false, false};
Element setup;
try {
setup = DocumentHelper.parseText(EXIUtils.readFile(EXIUtils.schemasFileLocation)).getRootElement();
setup = DocumentHelper.parseText(EXIUtils.readFile(EXIUtils.getSchemasFileLocation())).getRootElement();
} catch (DocumentException e) {
Log.warn("Exception while trying to generate default canonical schema.", e);
return;
Expand Down Expand Up @@ -331,7 +366,7 @@ static void generateDefaultCanonicalSchema() throws IOException

String content = sb.toString();

BufferedWriter newCanonicalSchemaWriter = new BufferedWriter(new FileWriter(EXIUtils.defaultCanonicalSchemaLocation.toFile()));
BufferedWriter newCanonicalSchemaWriter = new BufferedWriter(new FileWriter(EXIUtils.getDefaultCanonicalSchemaLocation().toFile()));
newCanonicalSchemaWriter.write(content);
newCanonicalSchemaWriter.close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cl/clayster/exi/SchemaIdResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Grammars resolveSchemaId(String schemaId) throws EXIException
return GrammarFactory.newInstance().createGrammars(EXIUtils.defaultCanonicalSchemaLocation);
} else {
*/
final Path schemaIdPath = EXIUtils.exiFolder.resolve(schemaId + ".xsd");
final Path schemaIdPath = EXIUtils.getExiFolder().resolve(schemaId + ".xsd");
if (Files.exists(schemaIdPath)) {
try {
Log.trace("Found schema file for schema ID '{}'. Using it to populate grammar.", schemaId);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/cl/clayster/exi/SchemaResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ public SchemaResolver() throws ParserConfigurationException, IOException, SAXExc
namespaceToPath = new HashMap<>();

// Iterate over all files to record a namespace-to-path mapping.
if (!Files.isDirectory(EXIUtils.schemasFolder)) {
throw new IllegalStateException("Configured schema folder is not a directory: " + EXIUtils.schemasFolder);
if (!Files.isDirectory(EXIUtils.getSchemasFolder())) {
throw new IllegalStateException("Configured schema folder is not a directory: " + EXIUtils.getSchemasFolder());
}

final Set<Path> xsds;
try (final Stream<Path> stream = Files.walk(EXIUtils.schemasFolder, 1)) {
try (final Stream<Path> stream = Files.walk(EXIUtils.getSchemasFolder(), 1)) {
xsds = stream
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".xsd"))
.collect(Collectors.toSet());
}

if (xsds.isEmpty()) {
throw new IllegalStateException("Configured schema folder contains no files: " + EXIUtils.schemasFolder);
throw new IllegalStateException("Configured schema folder contains no files: " + EXIUtils.getSchemasFolder());
}

for (final Path xsd : xsds) {
Expand All @@ -75,8 +75,8 @@ public SchemaResolver() throws ParserConfigurationException, IOException, SAXExc
namespaceToPath.remove("urn:xmpp:exi:cs");

// Add DTDs
this.namespaceToPath.put("-//W3C//DTD XMLSCHEMA 200102//EN", EXIUtils.schemasFolder.resolve("XMLSchema.dtd"));
this.namespaceToPath.put("datatypes", EXIUtils.schemasFolder.resolve("datatypes.dtd"));
this.namespaceToPath.put("-//W3C//DTD XMLSCHEMA 200102//EN", EXIUtils.getSchemasFolder().resolve("XMLSchema.dtd"));
this.namespaceToPath.put("datatypes", EXIUtils.getSchemasFolder().resolve("datatypes.dtd"));
}

public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cl/clayster/exi/UploadSchemaFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void messageReceived(NextFilter nextFilter, IoSession session, Object mes
void uploadCompressedMissingSchema(byte[] content, String contentType, String md5Hash, String bytes, IoSession session)
throws IOException, NoSuchAlgorithmException, DocumentException, EXIException, SAXException, TransformerException
{
Path filePath = EXIUtils.schemasFolder.resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");
Path filePath = EXIUtils.getSchemasFolder().resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");

if (!"text".equals(contentType) && md5Hash != null && bytes != null) {
String xml = "";
Expand All @@ -159,7 +159,7 @@ void uploadCompressedMissingSchema(byte[] content, String contentType, String md
void uploadMissingSchema(String content, IoSession session)
throws IOException, NoSuchAlgorithmException, DocumentException, EXIException, SAXException, TransformerException
{
Path filePath = EXIUtils.schemasFolder.resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");
Path filePath = EXIUtils.getSchemasFolder().resolve(Calendar.getInstance().getTimeInMillis() + ".xsd");
OutputStream out = Files.newOutputStream(filePath);

content = content.substring(content.indexOf('>') + 1, content.indexOf("</"));
Expand Down
18 changes: 3 additions & 15 deletions src/test/java/cl/clayster/exi/EXICodedFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,14 @@
@RunWith(MockitoJUnitRunner.class)
public class EXICodedFilterTest
{
private final static Path oldSchemasFolder = EXIUtils.schemasFolder;
private final static Path oldSchemasFileLocation = EXIUtils.schemasFileLocation;
private final static Path oldExiFolder = EXIUtils.exiFolder;
private final static Path oldDefaultCanonicalSchemaLocation = EXIUtils.defaultCanonicalSchemaLocation;

@BeforeClass
public static void mockFolders() throws Exception {
EXIUtils.schemasFolder = Files.createTempDirectory("unit-test-classes-");
EXIUtils.setSchemasFolder( Files.createTempDirectory("unit-test-classes-") );

// Copy all content to temp folder
try (final Stream<Path> stream = Files.walk(Paths.get("classes"))) {
stream.forEach(source -> {
Path destination = EXIUtils.schemasFolder.resolve(source.getFileName());
Path destination = EXIUtils.getSchemasFolder().resolve(source.getFileName());
try {
Files.copy(source, destination);
} catch (IOException e) {
Expand All @@ -69,20 +64,13 @@ public static void mockFolders() throws Exception {
});
}

EXIUtils.schemasFileLocation = EXIUtils.schemasFolder.resolve("schemas.xml");
EXIUtils.exiFolder = EXIUtils.schemasFolder.resolve("canonicalSchemas");
EXIUtils.defaultCanonicalSchemaLocation = EXIUtils.exiFolder.resolve("defaultSchema.xsd");

EXIUtils.generateSchemasFile();
EXIUtils.generateDefaultCanonicalSchema();
}

@AfterClass
public static void restoreFolders() {
EXIUtils.schemasFolder = oldSchemasFolder;
EXIUtils.schemasFileLocation = oldSchemasFileLocation;
EXIUtils.exiFolder = oldExiFolder;
EXIUtils.defaultCanonicalSchemaLocation = oldDefaultCanonicalSchemaLocation;
EXIUtils.setSchemasFolder(null);
}

/**
Expand Down