From 28c81f909ba1f17abf247e44e3e6364df79d13c6 Mon Sep 17 00:00:00 2001 From: Tomek Szmytka Date: Mon, 27 Jul 2020 22:23:38 +0200 Subject: [PATCH] #185 Calculate hash based on line content. Test --- .../ansicolor/action/LineIdentifier.java | 7 +++-- .../action/ShortlogActionCreator.java | 13 +++++--- .../ansicolor/action/LineIdentifierTest.java | 31 +++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 src/test/java/hudson/plugins/ansicolor/action/LineIdentifierTest.java diff --git a/src/main/java/hudson/plugins/ansicolor/action/LineIdentifier.java b/src/main/java/hudson/plugins/ansicolor/action/LineIdentifier.java index 11dc6e2..062c402 100644 --- a/src/main/java/hudson/plugins/ansicolor/action/LineIdentifier.java +++ b/src/main/java/hudson/plugins/ansicolor/action/LineIdentifier.java @@ -5,9 +5,12 @@ import java.security.NoSuchAlgorithmException; import java.util.Base64; +import static java.nio.charset.StandardCharsets.UTF_8; + public class LineIdentifier implements Serializable { private static final String ALGORITHM = "SHA-256"; - private MessageDigest messageDigest; + private static final long serialVersionUID = 1; + private transient MessageDigest messageDigest; private MessageDigest getMessageDigest() { if (messageDigest == null) { @@ -22,7 +25,7 @@ private MessageDigest getMessageDigest() { public String hash(String lineContent, long lineNo) { final String key = String.join("|", lineContent, String.valueOf(lineNo)); - return Base64.getEncoder().encodeToString(getMessageDigest().digest(key.getBytes())); + return Base64.getEncoder().encodeToString(getMessageDigest().digest(key.getBytes(UTF_8))); } public boolean isEqual(String lineContent, long lineNo, String other) { diff --git a/src/main/java/hudson/plugins/ansicolor/action/ShortlogActionCreator.java b/src/main/java/hudson/plugins/ansicolor/action/ShortlogActionCreator.java index 20e1ff7..2aaa041 100644 --- a/src/main/java/hudson/plugins/ansicolor/action/ShortlogActionCreator.java +++ b/src/main/java/hudson/plugins/ansicolor/action/ShortlogActionCreator.java @@ -9,6 +9,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.Map; @@ -17,11 +18,13 @@ import java.util.logging.Logger; import java.util.stream.Collectors; +import static java.nio.charset.StandardCharsets.UTF_8; + public class ShortlogActionCreator { private static final Logger LOGGER = Logger.getLogger(ShortlogActionCreator.class.getName()); private static final int CONSOLE_TAIL_DEFAULT = 150; private static final int BUFFER_SIZE = 16 * 1024; - private static final byte[] EOL = System.lineSeparator().getBytes(); + private static final byte[] EOL = System.lineSeparator().getBytes(UTF_8); private final LineIdentifier lineIdentifier; @@ -44,7 +47,7 @@ private ActionContext findStartActionAt(File logFile, Collection seriali int read; int totalRead = 0; String currentStartAction = ""; - byte[] partialLine = new byte[]{}; + String partialLine = ""; while ((read = inputStream.read(buf)) != -1) { final String newAction = findActionInBuffer(serializedActions, buf); if (!newAction.isEmpty()) { @@ -54,10 +57,10 @@ private ActionContext findStartActionAt(File logFile, Collection seriali final int startInBuff = shortlogStart > totalRead ? (int) (shortlogStart - totalRead) : 0; final int eolPos = indexOfEol(buf, startInBuff); if (eolPos != -1) { - return new ActionContext(currentStartAction, new String(partialLine) + new String(buf, startInBuff, eolPos - startInBuff + EOL.length)); + return new ActionContext(currentStartAction, partialLine + new String(buf, startInBuff, eolPos - startInBuff + EOL.length, UTF_8)); } else { // line extends to the next buffer - partialLine = Arrays.copyOfRange(buf, startInBuff, buf.length - 1); + partialLine = new String(Arrays.copyOfRange(buf, startInBuff, buf.length - 1), UTF_8); } } totalRead += read; @@ -72,7 +75,7 @@ private String findActionInBuffer(Collection serializedActions, byte[] b int preamblePos = 0; while (preamblePos < buf.length && (preamblePos = ConsoleNote.findPreamble(buf, preamblePos, buf.length - preamblePos)) != -1) { final int begin = preamblePos; - final Optional startAction = serializedActions.stream().filter(sa -> buf.length - begin > sa.length() && sa.equals(new String(buf, begin, sa.length()))).findFirst(); + final Optional startAction = serializedActions.stream().filter(sa -> buf.length - begin > sa.length() && sa.equals(new String(buf, begin, sa.length(), UTF_8))).findFirst(); if (startAction.isPresent()) { return startAction.get(); } diff --git a/src/test/java/hudson/plugins/ansicolor/action/LineIdentifierTest.java b/src/test/java/hudson/plugins/ansicolor/action/LineIdentifierTest.java new file mode 100644 index 0000000..3e439a7 --- /dev/null +++ b/src/test/java/hudson/plugins/ansicolor/action/LineIdentifierTest.java @@ -0,0 +1,31 @@ +package hudson.plugins.ansicolor.action; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class LineIdentifierTest { + + private LineIdentifier lineIdentifier; + + @Before + public void setUp() throws Exception { + lineIdentifier = new LineIdentifier(); + } + + @Test + public void canHashLine() { + assertEquals("ojq32twB56Mha38FSpsOvwxZDdkOKa/SveGHDC4tgHY=", lineIdentifier.hash("test line 123", 735)); + } + + @Test + public void canDetermineIsEqualPositive() { + assertTrue(lineIdentifier.isEqual("\u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m", 67, "1mo5/lFK3s3+qaz1vK3o62k+EAJkd8Q0j3dMRH8Wkh4=")); + } + + @Test + public void canDetermineIsEqualNegative() { + assertFalse(lineIdentifier.isEqual("\u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m", 67, "bogus")); + } +}