Skip to content

Commit

Permalink
Fixed compilation error due to libraries incompatibilities
Browse files Browse the repository at this point in the history
- usage of `data.convertToString().split(context, delimiter, MINUS_ONE);` instead of `data.convertToString().split(delimiter, -1);`
- avoid to extend BuffererdTokenir test cases from `org.logstash.RubyTestBase` which was introduced in #13159
- JDK 8 compatibilities:
  - `Arrays.asList` vs `List.of`
  - `assertThrows` method from JUnit5 not available in JUnit4 so reimplemented in the test
  • Loading branch information
andsel committed Oct 17, 2024
1 parent 5b864ab commit 363b89b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
import org.jruby.runtime.builtin.IRubyObject;
import org.junit.Before;
import org.junit.Test;
import org.logstash.RubyTestBase;
import org.logstash.RubyUtil;

import java.util.List;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.logstash.RubyUtil.RUBY;

@SuppressWarnings("unchecked")
public final class BufferedTokenizerExtTest extends RubyTestBase {
public final class BufferedTokenizerExtTest {

private BufferedTokenizerExt sut;
private ThreadContext context;
Expand All @@ -52,7 +51,7 @@ public void setUp() {
public void shouldTokenizeASingleToken() {
RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("foo\n"));

assertEquals(List.of("foo"), tokens);
assertEquals(Arrays.asList("foo"), tokens);
}

@Test
Expand All @@ -61,14 +60,14 @@ public void shouldMergeMultipleToken() {
assertTrue(tokens.isEmpty());

tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("bar\n"));
assertEquals(List.of("foobar"), tokens);
assertEquals(Arrays.asList("foobar"), tokens);
}

@Test
public void shouldTokenizeMultipleToken() {
RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("foo\nbar\n"));

assertEquals(List.of("foo", "bar"), tokens);
assertEquals(Arrays.asList("foo", "bar"), tokens);
}

@Test
Expand All @@ -77,15 +76,15 @@ public void shouldIgnoreEmptyPayload() {
assertTrue(tokens.isEmpty());

tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("foo\nbar"));
assertEquals(List.of("foo"), tokens);
assertEquals(Arrays.asList("foo"), tokens);
}

@Test
public void shouldTokenizeEmptyPayloadWithNewline() {
RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("\n"));
assertEquals(List.of(""), tokens);
assertEquals(Arrays.asList(""), tokens);

tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("\n\n\n"));
assertEquals(List.of("", "", ""), tokens);
assertEquals(Arrays.asList("", "", ""), tokens);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
import org.jruby.runtime.builtin.IRubyObject;
import org.junit.Before;
import org.junit.Test;
import org.logstash.RubyTestBase;
import org.logstash.RubyUtil;

import java.util.List;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.logstash.RubyUtil.RUBY;

@SuppressWarnings("unchecked")
public final class BufferedTokenizerExtWithDelimiterTest extends RubyTestBase {
public final class BufferedTokenizerExtWithDelimiterTest {

private BufferedTokenizerExt sut;
private ThreadContext context;
Expand All @@ -52,7 +51,7 @@ public void setUp() {
public void shouldTokenizeMultipleToken() {
RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("foo||b|r||"));

assertEquals(List.of("foo", "b|r"), tokens);
assertEquals(Arrays.asList("foo", "b|r"), tokens);
}

@Test
Expand All @@ -61,6 +60,6 @@ public void shouldIgnoreEmptyPayload() {
assertTrue(tokens.isEmpty());

tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("foo||bar"));
assertEquals(List.of("foo"), tokens);
assertEquals(Arrays.asList("foo"), tokens);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
import org.jruby.runtime.builtin.IRubyObject;
import org.junit.Before;
import org.junit.Test;
import org.logstash.RubyTestBase;
import org.logstash.RubyUtil;

import java.util.List;
import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
import static org.logstash.RubyUtil.RUBY;

@SuppressWarnings("unchecked")
public final class BufferedTokenizerExtWithSizeLimitTest extends RubyTestBase {
public final class BufferedTokenizerExtWithSizeLimitTest {

private BufferedTokenizerExt sut;
private ThreadContext context;
Expand All @@ -52,7 +51,7 @@ public void setUp() {
public void givenTokenWithinSizeLimitWhenExtractedThenReturnTokens() {
RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("foo\nbar\n"));

assertEquals(List.of("foo", "bar"), tokens);
assertEquals(Arrays.asList("foo", "bar"), tokens);
}

@Test
Expand All @@ -71,7 +70,7 @@ public void givenExtractedThrownLimitErrorWhenFeedFreshDataThenReturnTokenStarti
assertThat(thrownException.getMessage(), containsString("input buffer full"));

RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("\nanother"));
assertEquals("After buffer full error should resume from the end of line", List.of("kaboom"), tokens);
assertEquals("After buffer full error should resume from the end of line", Arrays.asList("kaboom"), tokens);
}

@Test
Expand All @@ -84,7 +83,7 @@ public void givenExtractInvokedWithDifferentFramingAfterBufferFullErrorTWhenFeed
assertThat(thrownException.getMessage(), containsString("input buffer full"));

RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("aa\nbbbb\nccc"));
assertEquals(List.of("bbbb"), tokens);
assertEquals(Arrays.asList("bbbb"), tokens);
}

@Test
Expand All @@ -105,6 +104,17 @@ public void giveMultipleSegmentsThatGeneratesMultipleBufferFullErrorsThenIsAbleT

// now should resemble processing on c and d
RubyArray<RubyString> tokens = (RubyArray<RubyString>) sut.extract(context, RubyUtil.RUBY.newString("ccc\nddd\n"));
assertEquals(List.of("ccccc", "ddd"), tokens);
assertEquals(Arrays.asList("ccccc", "ddd"), tokens);
}

private static <T extends Throwable> Exception assertThrows(Class<T> excpClass, Runnable action) {
try {
action.run();
fail("Expected an exception");
return new IllegalStateException("Can't be reached");
} catch (Exception t) {
assertTrue(excpClass.isAssignableFrom(t.getClass()));
return t;
}
}
}

0 comments on commit 363b89b

Please sign in to comment.