forked from External/mage
[app-wiring-refactor]: Define external configuration
This commit is contained in:
parent
e3733dfae7
commit
cf3dd2d94c
14 changed files with 1083 additions and 2 deletions
123
Mage.Common/src/test/java/mage/remote/ConnectionTest.java
Normal file
123
Mage.Common/src/test/java/mage/remote/ConnectionTest.java
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
package mage.remote;
|
||||
|
||||
import mage.utils.FluentBuilder;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ConnectionTest {
|
||||
|
||||
static class ConnectionBuilder extends FluentBuilder<Connection, ConnectionBuilder> {
|
||||
|
||||
public int port;
|
||||
public String host;
|
||||
public String parameter;
|
||||
|
||||
private ConnectionBuilder() {
|
||||
super(ConnectionBuilder::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection makeValue() {
|
||||
final Connection result = new Connection(parameter);
|
||||
result.setHost(host);
|
||||
result.setPort(port);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private ConnectionBuilder baseBuilder() {
|
||||
return new ConnectionBuilder();
|
||||
}
|
||||
|
||||
class TestsTemplate {
|
||||
final ConnectionBuilder testeeBuilder;
|
||||
|
||||
TestsTemplate(ConnectionBuilder testeeBuilder) {
|
||||
this.testeeBuilder = testeeBuilder;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("produce the expected scheme")
|
||||
void scheme() throws Exception {
|
||||
final URI testee = make(testeeBuilder);
|
||||
assertThat(testee.getScheme()).isEqualTo("bisocket");
|
||||
}
|
||||
|
||||
URI make(ConnectionBuilder builder) {
|
||||
try {
|
||||
return new URI(builder.build().getURI());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("generate the expected port")
|
||||
void port() {
|
||||
final int expected = RandomUtils.nextInt(1000, 65000);
|
||||
final int port = make(testeeBuilder.with(c -> c.port = expected)).getPort();
|
||||
|
||||
assertThat(port).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("generate the expected serialisation parameter")
|
||||
void serialisation() {
|
||||
final String query = make(testeeBuilder).getQuery();
|
||||
|
||||
assertThat(query).contains("serializationtype=jboss");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("generate the expected threadpool parameter")
|
||||
void threadpool() {
|
||||
final String parameter = RandomStringUtils.randomAlphanumeric(12);
|
||||
final String query = make(testeeBuilder.with(c -> c.parameter = parameter)).getQuery();
|
||||
|
||||
assertThat(query).contains("onewayThreadPool=mage.remote.CustomThreadPool" + parameter);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("getUri when host is localhost should")
|
||||
class LocalhostTest extends TestsTemplate {
|
||||
|
||||
LocalhostTest() {
|
||||
super(baseBuilder().with(c -> c.host = "localhost"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("generate an ipv4 as host")
|
||||
void ipv4Gen() {
|
||||
final String host = make(testeeBuilder).getHost();
|
||||
|
||||
assertThat(host).matches("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");
|
||||
}
|
||||
}
|
||||
|
||||
private final String randomHost = RandomStringUtils.randomAlphabetic(15);
|
||||
|
||||
@Nested
|
||||
@DisplayName("getUri when host is not localhost should")
|
||||
class StandardHostTest extends TestsTemplate {
|
||||
StandardHostTest() {
|
||||
super(baseBuilder().with(c -> c.host = randomHost));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("generate the selected host as host")
|
||||
void hostGen() {
|
||||
final String host = make(testeeBuilder).getHost();
|
||||
|
||||
assertThat(host).isEqualTo(randomHost);
|
||||
}
|
||||
}
|
||||
}
|
||||
116
Mage.Common/src/test/java/mage/utils/FluentBuilderTest.java
Normal file
116
Mage.Common/src/test/java/mage/utils/FluentBuilderTest.java
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
package mage.utils;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||
|
||||
public class FluentBuilderTest {
|
||||
|
||||
|
||||
private ABuilder baseBuilder() {
|
||||
return new ABuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("build with default parameters")
|
||||
void testDefault() {
|
||||
final A actual = baseBuilder().build();
|
||||
|
||||
verifyAB(actual, null, 0);
|
||||
}
|
||||
|
||||
private void verifyAB(A actual, String a, int b) {
|
||||
assertThat(actual.getA()).isEqualTo(a);
|
||||
assertThat(actual.getB()).isEqualTo(b);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("chain with clause and add new parameters")
|
||||
void testBaseChain() {
|
||||
final A actual = baseBuilder().with(a -> a.a = "hello").build();
|
||||
|
||||
verifyAB(actual, "hello", 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("chain multiple with clauses and add new parameters")
|
||||
void testMultiChain() {
|
||||
final A actual = baseBuilder().with(a -> a.a = "world").with(a -> a.b = 6).build();
|
||||
|
||||
verifyAB(actual, "world", 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("chain multiple with clauses and override latest writes")
|
||||
void testMultiChainOverride() {
|
||||
final A actual = baseBuilder().with(a -> a.a = "world").with(a -> a.b = 4).with(a -> a.a = "foobar").build();
|
||||
|
||||
verifyAB(actual, "foobar", 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("not mutate the state of previous builder in the chain")
|
||||
void testImmutability() {
|
||||
final ABuilder builder1 = baseBuilder().with(a -> a.a = "world");
|
||||
final ABuilder builder2 = builder1.with(a -> {
|
||||
a.a = "hello";
|
||||
a.b = 42;
|
||||
});
|
||||
|
||||
verifyAB(builder1.build(), "world", 0);
|
||||
verifyAB(builder2.build(), "hello", 42);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("produce different objects")
|
||||
void differentObjects() {
|
||||
final ABuilder builder = baseBuilder().with(a -> {
|
||||
a.a = "hello";
|
||||
a.b = 42;
|
||||
});
|
||||
final A a1 = builder.build();
|
||||
final A a2 = builder.build();
|
||||
|
||||
assertThat(a1).isNotSameAs(a2);
|
||||
verifyAB(a1, "hello", 42);
|
||||
verifyAB(a2, "hello", 42);
|
||||
}
|
||||
|
||||
static class A {
|
||||
public final String a;
|
||||
private int b;
|
||||
|
||||
public A(String a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public String getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public int getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public void setB(int b) {
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
|
||||
static class ABuilder extends FluentBuilder<A, ABuilder> {
|
||||
public String a;
|
||||
public int b;
|
||||
|
||||
private ABuilder() {
|
||||
super(ABuilder::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected A makeValue() {
|
||||
final A result = new A(a);
|
||||
result.setB(b);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue