Skip to content

Commit

Permalink
Remove some usages of commons-lang
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenGBrown committed Dec 4, 2013
1 parent 3133998 commit eb8f2c1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import java.io.IOException;
import java.io.Serializable;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import com.google.common.base.Objects;

/**
* Parser that is able to find a position in the console log file of a build.
Expand Down Expand Up @@ -65,10 +64,34 @@ static final class Result {
*/
boolean endOfFile;

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hashCode(lineNumber, atNewLine, endOfFile);
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof ConsoleLogParser.Result) {
ConsoleLogParser.Result other = (ConsoleLogParser.Result) obj;
return lineNumber == other.lineNumber && atNewLine == other.atNewLine
&& endOfFile == other.endOfFile;
}
return false;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this,
ToStringStyle.SHORT_PREFIX_STYLE);
return Objects.toStringHelper(this).add("lineNumber", lineNumber)
.add("atNewLine", atNewLine).add("endOfFile", endOfFile).toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
import java.util.Collection;

import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.hamcrest.CustomMatcher;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -101,64 +98,86 @@ public void setUp() throws Exception {
*/
@Test
public void testSeekStart() throws Exception {
assertThat(seek(0), is(resultWith(lineNumber(0), atNewLine())));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 0;
result.atNewLine = true;
assertThat(seek(0), is(result));
}

/**
* @throws Exception
*/
@Test
public void testSeekWithinLine() throws Exception {
assertThat(seek(1), is(resultWith(lineNumber(0))));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 0;
assertThat(seek(1), is(result));
}

/**
* @throws Exception
*/
@Test
public void testSeekNextLine() throws Exception {
assertThat(seek(2), is(resultWith(lineNumber(1), atNewLine())));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 1;
result.atNewLine = true;
assertThat(seek(2), is(result));
}

/**
* @throws Exception
*/
@Test
public void testSeekEnd() throws Exception {
assertThat(seek(logLength), is(resultWith(lineNumber(5), atNewLine())));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 5;
result.atNewLine = true;
assertThat(seek(logLength), is(result));
}

/**
* @throws Exception
*/
@Test
public void testSeekPastEnd() throws Exception {
assertThat(seek(logLength + 1),
is(resultWith(lineNumber(5), atNewLine(), endOfFile())));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 5;
result.atNewLine = true;
result.endOfFile = true;
assertThat(seek(logLength + 1), is(result));
}

/**
* @throws Exception
*/
@Test
public void testSeekStartNegative() throws Exception {
assertThat(seek(-logLength), is(resultWith(lineNumber(0), atNewLine())));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 0;
result.atNewLine = true;
assertThat(seek(-logLength), is(result));
}

/**
* @throws Exception
*/
@Test
public void testSeekWithinLineNegative() throws Exception {
assertThat(seek(1 - logLength), is(resultWith(lineNumber(0))));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 0;
assertThat(seek(1 - logLength), is(result));
}

/**
* @throws Exception
*/
@Test
public void testSeekPastStartNegative() throws Exception {
assertThat(seek(-logLength - 1), is(resultWith(lineNumber(0), atNewLine())));
ConsoleLogParser.Result result = new ConsoleLogParser.Result();
result.lineNumber = 0;
result.atNewLine = true;
assertThat(seek(-logLength - 1), is(result));
}

private ConsoleLogParserImpl.Result seek(long pos) throws Exception {
Expand All @@ -168,50 +187,4 @@ private ConsoleLogParserImpl.Result seek(long pos) throws Exception {
}
return parser.seek(build);
}

private static Matcher<ConsoleLogParserImpl.Result> resultWith(
ResultProperty... properties) {
final ConsoleLogParserImpl.Result expectedResult = new ConsoleLogParserImpl.Result();
for (ResultProperty property : properties) {
property.set(expectedResult);
}
return new CustomMatcher<ConsoleLogParserImpl.Result>(
expectedResult.toString()) {
@Override
public boolean matches(Object item) {
return EqualsBuilder.reflectionEquals(item, expectedResult);
}
};
}

private static interface ResultProperty {
void set(ConsoleLogParserImpl.Result result);
}

private static ResultProperty lineNumber(final int lineNumber) {
return new ResultProperty() {
@Override
public void set(ConsoleLogParserImpl.Result result) {
result.lineNumber = lineNumber;
}
};
}

private static ResultProperty atNewLine() {
return new ResultProperty() {
@Override
public void set(ConsoleLogParserImpl.Result result) {
result.atNewLine = true;
}
};
}

private static ResultProperty endOfFile() {
return new ResultProperty() {
@Override
public void set(ConsoleLogParserImpl.Result result) {
result.endOfFile = true;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright (c) 2013 Steven G. Brown
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.timestamper.annotator;

import hudson.plugins.timestamper.annotator.ConsoleLogParser.Result;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;

import org.junit.Test;

/**
* Unit test for the {@link Result} class.
*
* @author Steven G. Brown
*/
public class ConsoleLogParserResultTest {

/**
*/
@Test
public void testResultEqualsAndHashCode() {
EqualsVerifier.forClass(ConsoleLogParser.Result.class)
.suppress(Warning.NONFINAL_FIELDS).verify();
}
}

0 comments on commit eb8f2c1

Please sign in to comment.