Skip to content

Commit c72b6bb

Browse files
committed
[Test] Covered Java class SettingPortRange with JUnit test copied from Ruby specs
1 parent 80e252b commit c72b6bb

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.logstash.settings;
21+
22+
import org.jruby.RubyFixnum;
23+
import org.jruby.RubyRange;
24+
import org.jruby.runtime.ThreadContext;
25+
import org.junit.Test;
26+
import org.logstash.RubyUtil;
27+
28+
import static org.junit.Assert.*;
29+
30+
// Porting to Java of logstash-core/spec/logstash/settings/port_range_spec.rb
31+
public class SettingPortRangeTest {
32+
33+
private static void assertThrowsError(String expectedErrorMessage, Runnable action) {
34+
try {
35+
action.run();
36+
fail("An error must be thrown");
37+
} catch (IllegalArgumentException ex) {
38+
assertTrue(ex.getMessage().contains(expectedErrorMessage));
39+
}
40+
}
41+
42+
@Test
43+
public void givenPortRangeCreatedWithSingleIntegerValue_thenCoercesTheValueToRange() {
44+
SettingPortRange sut = new SettingPortRange("test", 9_000);
45+
assertEquals(new Range<>(9_000, 9_000), sut.value());
46+
}
47+
48+
@Test
49+
public void givenPortRangeCreatedWithSingleIntegerValue_thenCanUpdateTheRange() {
50+
SettingPortRange sut = new SettingPortRange("test", 9_000);
51+
sut.set(10_000);
52+
assertEquals(new Range<>(10_000, 10_000), sut.value());
53+
}
54+
55+
@Test
56+
public void givenPortRangeCreatedWithStringValue_thenCoercesTheValueToRange() {
57+
SettingPortRange sut = new SettingPortRange("test", "9000-10000");
58+
assertEquals(new Range<>(9_000, 10_000), sut.value());
59+
sut = new SettingPortRange("test", " 9000-10000 ");
60+
assertEquals(new Range<>(9_000, 10_000), sut.value());
61+
}
62+
63+
@Test
64+
public void givenPortRangeCreatedWithStringValue_whenUpperPortIsOutOfRange_thenThrowsAnError() {
65+
assertThrowsError("valid options are within the range of 1-65535",
66+
() -> new SettingPortRange("test", "9000-95000")
67+
);
68+
}
69+
70+
@Test
71+
public void givenPortRangeCreatedWithStringValue_thenCanUpdateTheRange() {
72+
SettingPortRange sut = new SettingPortRange("test", "9000-10000");
73+
sut.set("500-1000");
74+
assertEquals(new Range<>(500, 1000), sut.value());
75+
}
76+
77+
@Test
78+
public void givenPortRangeCreatedWithGarbageString_thenThrowsAnError() {
79+
assertThrowsError("Could not coerce [fsdfnsdkjnfjs](type: class java.lang.String) into a port range",
80+
() -> new SettingPortRange("test", "fsdfnsdkjnfjs")
81+
);
82+
}
83+
84+
@Test
85+
public void givenPortRange_whenUpdatedWithGarbageString_thenThrowsAnError() {
86+
final SettingPortRange sut = new SettingPortRange("test", 10_000);
87+
assertThrowsError("Could not coerce [dsfnsdknfksdnfjksdnfjns](type: class java.lang.String) into a port range",
88+
() -> sut.set("dsfnsdknfksdnfjksdnfjns")
89+
);
90+
}
91+
92+
@Test
93+
public void givenPortRangeCreatedWithUnknownType_thenThrowsAnError() {
94+
assertThrowsError("Could not coerce [0.1](type: class java.lang.Double) into a port range",
95+
() -> new SettingPortRange("test", 0.1)
96+
);
97+
}
98+
99+
@Test
100+
public void givenPortRange_whenUpdatedWithUnknownType_thenThrowsAnError() {
101+
final SettingPortRange sut = new SettingPortRange("test", 10_000);
102+
assertThrowsError("Could not coerce [0.1](type: class java.lang.Double) into a port range",
103+
() -> sut.set(0.1)
104+
);
105+
}
106+
107+
@Test
108+
public void givenPortRangeCreatedWithRangeValue_thenCoercesTheValueToRange() {
109+
SettingPortRange sut = new SettingPortRange("test", new Range<>(9_000, 10_000));
110+
assertEquals(new Range<>(9_000, 10_000), sut.value());
111+
}
112+
113+
@Test
114+
public void givenPortRangeCreatedWithRubyRangeValue_thenCoercesTheValueToRange() {
115+
ThreadContext ctx = RubyUtil.RUBY.getCurrentContext();
116+
RubyRange rubyRange = RubyRange.newExclusiveRange(ctx, new RubyFixnum(RubyUtil.RUBY, 9_000), new RubyFixnum(RubyUtil.RUBY, 10_000));
117+
SettingPortRange sut = new SettingPortRange("test", rubyRange);
118+
assertEquals(new Range<>(9_000, 10_000), sut.value());
119+
}
120+
121+
@Test
122+
public void givenPortRangeCreatedWithRangeValue_thenCanUpdateTheRange() {
123+
SettingPortRange sut = new SettingPortRange("test", new Range<>(9_000, 10_000));
124+
sut.set(new Range<>(500, 1_000));
125+
assertEquals(new Range<>(500, 1_000), sut.value());
126+
}
127+
128+
@Test
129+
public void givenPortRangeCreatedWithOutOfRangeUpperPort_thenThrowsAnError() {
130+
assertThrowsError("valid options are within the range of 1-65535",
131+
() -> new SettingPortRange("test", new Range<>(9_000, 90_000))
132+
);
133+
}
134+
135+
@Test
136+
public void givenPortRangeCreatedWithOutOfRangePort_thenThrowsAnError() {
137+
assertThrowsError("valid options are within the range of 1-65535",
138+
() -> new SettingPortRange("test", new Range<>(-1_000, 1_000))
139+
);
140+
}
141+
}

0 commit comments

Comments
 (0)