Skip to content
Permalink
Browse files

[JENKINS-29032] java 1.6 compatibility

  • Loading branch information...
nilleb committed Aug 15, 2015
1 parent 6183148 commit 54085aa8db1beddcdc4342fee671c09c080771bd
@@ -1,5 +1,8 @@
package hudson.plugins.mstest;

import org.apache.commons.io.FileUtils;
import org.springframework.util.FileCopyUtils;

import java.io.*;
//import java.nio.charset.Charset;
//import java.nio.charset.StandardCharsets;
@@ -30,22 +33,29 @@ public void fix() throws IOException
String line = in.readLine();
boolean replace = false;
while (line != null) {
String newline = stripIllegalEntities(stripIllegalCharacters(line));
String newline = stripIllegalEntities(line);
if (line.length() != newline.length())
replace = true;
out.println(newline);
line = in.readLine();
}
in.close();
out.close();
if (replace)
java.nio.file.Files.move(
if (replace) {
inFile.delete();
outfile.renameTo(inFile);
/*java.nio.file.Files.move(
outfile.toPath(),
inFile.toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING,
java.nio.file.LinkOption.NOFOLLOW_LINKS);
java.nio.file.LinkOption.NOFOLLOW_LINKS);*/
}
else
outfile.delete();
// java.nio.file.Files.delete(outfile.toPath());
}

/*
private String stripIllegalCharacters(String line)
{
String xml10pattern = "[^"
@@ -60,16 +70,21 @@ private String stripIllegalCharacters(String line)
+ "\ud800\udc00-\udbff\udfff"
+ "]+";
return line.replaceAll(xml10pattern, "").replaceAll(xml11pattern, "");
}
}*/

private String stripIllegalEntities(String line)
{
final String pattern = "(?<entity>&#x(?<char>[0-9A-Fa-f]{1,4});)";
final String pattern = "(&#x([0-9A-Fa-f]{1,4});)";
/* Java 1.7+
final String pattern = "(?<entity>&#x(?<char>[0-9A-Fa-f]{1,4});)";*/
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(line);

while (m.find()) {
long c = Long.parseLong(m.group("char"), 16);
String charGroup = m.group(2);
/* Java 1.7+
String charGroupJava17 = m.group("char");*/
long c = Long.parseLong(charGroup, 16);
if (!isAllowed(c)) {
line = new StringBuilder(line).replace(m.start(1), m.end(1), "").toString();
m = p.matcher(line);
@@ -209,7 +209,7 @@ public void testInconclusive() throws Exception {
private String readXmlAsString(String resourceName) throws IOException {
String xmlString = "";

BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(resourceName)));
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(resourceName), com.google.common.base.Charsets.UTF_8));
String line = reader.readLine();
while (line != null) {
xmlString += line + "\n";
@@ -8,12 +8,12 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.FileCopyUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.*;
/* Java 1.7+
import java.nio.file.Files;
*/

/**
* Unit tests for MSTestTransformer class
@@ -60,7 +60,9 @@ public void testInvalidXmlCharacters() throws Exception {
if (testFile.exists())
testFile.delete();
InputStream testStream = this.getClass().getResourceAsStream("JENKINS-23531-xmlentities-forged.trx");
Files.copy(testStream, testFile.toPath());
FileCopyUtils.copy(testStream, new FileOutputStream(testFile));
/* Java 1.7+
Files.copy(testStream, testFile.toPath());*/
MSTestReportConverter converter = new MSTestReportConverter();
transformer = new MSTestTransformer(testPath, converter, buildListener);
transformer.invoke(parentFile, virtualChannel);
@@ -83,7 +85,9 @@ public void testInvalidXmlCharacters_VishalMane() throws Exception {
if (testFile.exists())
testFile.delete();
InputStream testStream = this.getClass().getResourceAsStream("SYSTEM_AD-JENKINS 2015-07-08 10_53_01.trx");
Files.copy(testStream, testFile.toPath());
FileCopyUtils.copy(testStream, new FileOutputStream(testFile));
/* Java 1.7+
Files.copy(testStream, testFile.toPath()); */
MSTestReportConverter converter = new MSTestReportConverter();
transformer = new MSTestTransformer(testPath, converter, buildListener);
transformer.invoke(parentFile, virtualChannel);
@@ -106,7 +110,9 @@ public void testCharset() throws Exception {
if (testFile.exists())
testFile.delete();
InputStream testStream = this.getClass().getResourceAsStream("JENKINS-23531-charset.trx");
Files.copy(testStream, testFile.toPath());
FileCopyUtils.copy(testStream, new FileOutputStream(testFile));
/* Java 1.7+
Files.copy(testStream, testFile.toPath());*/
MSTestReportConverter converter = new MSTestReportConverter();
transformer = new MSTestTransformer(testPath, converter, buildListener);
transformer.invoke(parentFile, virtualChannel);

0 comments on commit 54085aa

Please sign in to comment.
You can’t perform that action at this time.