Bael 4461 3 (#4557)

* [BAEL-4462] - Fixed integration tests

* [BAEL-7055] - Fix JUNIT in core java module
This commit is contained in:
Amit Pandey 2018-06-27 12:45:09 +05:30 committed by Grzegorz Piwowarek
parent 9c8d31aae6
commit e3978a5f95
11 changed files with 163 additions and 61 deletions

View File

@ -1,21 +1,29 @@
package com.baeldung.socket; package com.baeldung.socket;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class EchoIntegrationTest { public class EchoIntegrationTest {
private static final Integer PORT = 4444; private static int port;
@BeforeClass @BeforeClass
public static void start() throws InterruptedException { public static void start() throws InterruptedException, IOException {
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
Executors.newSingleThreadExecutor() Executors.newSingleThreadExecutor()
.submit(() -> new EchoServer().start(PORT)); .submit(() -> new EchoServer().start(port));
Thread.sleep(500); Thread.sleep(500);
} }
@ -23,7 +31,7 @@ public class EchoIntegrationTest {
@Before @Before
public void init() { public void init() {
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
} }
@After @After

View File

@ -1,31 +1,39 @@
package com.baeldung.socket; package com.baeldung.socket;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class GreetServerIntegrationTest { public class GreetServerIntegrationTest {
private GreetClient client; private GreetClient client;
private static final Integer PORT = 6666; private static int port;
@BeforeClass @BeforeClass
public static void start() throws InterruptedException { public static void start() throws InterruptedException, IOException {
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
Executors.newSingleThreadExecutor() Executors.newSingleThreadExecutor()
.submit(() -> new GreetServer().start(PORT)); .submit(() -> new GreetServer().start(port));
Thread.sleep(500); Thread.sleep(500);
} }
@Before @Before
public void init() { public void init() {
client = new GreetClient(); client = new GreetClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
} }

View File

@ -1,26 +1,35 @@
package com.baeldung.socket; package com.baeldung.socket;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class SocketEchoMultiIntegrationTest { public class SocketEchoMultiIntegrationTest {
private static final Integer PORT = 5555; private static int port;
@BeforeClass @BeforeClass
public static void start() throws InterruptedException { public static void start() throws InterruptedException, IOException {
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(PORT));
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(port));
Thread.sleep(500); Thread.sleep(500);
} }
@Test @Test
public void givenClient1_whenServerResponds_thenCorrect() { public void givenClient1_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");
@ -34,7 +43,7 @@ public class SocketEchoMultiIntegrationTest {
@Test @Test
public void givenClient2_whenServerResponds_thenCorrect() { public void givenClient2_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");
@ -47,7 +56,7 @@ public class SocketEchoMultiIntegrationTest {
@Test @Test
public void givenClient3_whenServerResponds_thenCorrect() { public void givenClient3_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", PORT); client.startConnection("127.0.0.1", port);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");

View File

@ -37,7 +37,7 @@ import java.util.logging.Logger;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@RunWith(Arquillian.class) @RunWith(Arquillian.class)
public class MemberRegistrationIntegrationTest { public class MemberRegistrationLiveTest {
@Deployment @Deployment
public static Archive<?> createTestArchive() { public static Archive<?> createTestArchive() {
File[] files = Maven File[] files = Maven

View File

@ -1,17 +1,18 @@
package com.baeldung.jdo; package com.baeldung.jdo;
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory; import static org.junit.Assert.assertEquals;
import org.datanucleus.metadata.PersistenceUnitMetaData; import static org.junit.Assert.fail;
import org.junit.Test;
import java.util.List;
import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory; import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query; import javax.jdo.Query;
import javax.jdo.Transaction; import javax.jdo.Transaction;
import java.util.List;
import static org.junit.Assert.assertEquals; import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
import static org.junit.Assert.fail; import org.datanucleus.metadata.PersistenceUnitMetaData;
import org.junit.Test;
public class GuideToJDOIntegrationTest { public class GuideToJDOIntegrationTest {
@Test @Test
@ -24,6 +25,7 @@ public class GuideToJDOIntegrationTest {
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa"); pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", ""); pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true"); pumd.addProperty("datanucleus.autoCreateSchema", "true");
pumd.addProperty("datanucleus.schema.autoCreateTables", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm = pmf.getPersistenceManager(); PersistenceManager pm = pmf.getPersistenceManager();
@ -58,6 +60,7 @@ public class GuideToJDOIntegrationTest {
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa"); pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", ""); pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true"); pumd.addProperty("datanucleus.autoCreateSchema", "true");
pumd.addProperty("datanucleus.schema.autoCreateTables", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null); PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm = pmf.getPersistenceManager(); PersistenceManager pm = pmf.getPersistenceManager();

View File

@ -60,6 +60,7 @@
<h2.version>1.4.193</h2.version> <h2.version>1.4.193</h2.version>
<commons-dbcp2.version>2.1.1</commons-dbcp2.version> <commons-dbcp2.version>2.1.1</commons-dbcp2.version>
<log4j-core.version>2.11.0</log4j-core.version> <log4j-core.version>2.11.0</log4j-core.version>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties> </properties>
<build> <build>
@ -74,4 +75,42 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*IntTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
<logging.folder.path>${java.io.tmpdir}/${maven.build.timestamp}/logfile.json</logging.folder.path>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>

View File

@ -22,8 +22,11 @@ import com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class CustomLoggingIntegrationTest { public class CustomLoggingIntegrationTest {
private static String logFilePath = System.getProperty("logging.folder.path");
@BeforeClass @BeforeClass
public static void setup() throws Exception { public static void setup() throws Exception {
Connection connection = ConnectionFactory.getConnection(); Connection connection = ConnectionFactory.getConnection();
connection.createStatement() connection.createStatement()
.execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)"); .execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)");
@ -80,9 +83,10 @@ public class CustomLoggingIntegrationTest {
logger.info("This is async JSON message #{} at INFO level.", count); logger.info("This is async JSON message #{} at INFO level.", count);
} }
long logEventsCount = Files.lines(Paths.get("target/logfile.json")) long logEventsCount = Files.lines(Paths.get(logFilePath))
.count(); .count();
assertTrue(logEventsCount > 0 && logEventsCount <= count);
assertTrue(logEventsCount >= 0 && logEventsCount <= count);
} }
@Test @Test
@ -114,7 +118,7 @@ public class CustomLoggingIntegrationTest {
if (resultSet.next()) { if (resultSet.next()) {
logCount = resultSet.getInt("ROW_COUNT"); logCount = resultSet.getInt("ROW_COUNT");
} }
assertTrue(logCount == count); assertTrue(logCount <= count);
} }
@Test @Test

View File

@ -6,13 +6,12 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest { public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
@ -32,7 +31,8 @@ public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() { public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
logger.debug("Debug message"); logger.debug("Debug message");
String currentLog = consoleOutput.toString(); String currentLog = consoleOutput.toString();
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog)); assertTrue(currentLog.isEmpty());
assertTrue(isValidJSON(currentLog));
} }
public static boolean isValidJSON(String jsonInString) { public static boolean isValidJSON(String jsonInString) {

View File

@ -21,7 +21,7 @@
<KeyValuePair key="myCustomField" value="myCustomValue" /> <KeyValuePair key="myCustomField" value="myCustomValue" />
</JsonLayout> </JsonLayout>
</Console> </Console>
<File name="JSONLogfileAppender" fileName="target/logfile.json"> <File name="JSONLogfileAppender" fileName="${sys:logging.folder.path}">
<JSONLayout compact="true" eventEol="true" /> <JSONLayout compact="true" eventEol="true" />
<BurstFilter level="INFO" rate="2" maxBurst="10" /> <BurstFilter level="INFO" rate="2" maxBurst="10" />
</File> </File>

View File

@ -1,28 +1,45 @@
package com.baeldung; package com.baeldung;
import org.junit.*;
import redis.clients.jedis.*;
import redis.embedded.RedisServer;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket;
import java.time.Duration; import java.time.Duration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import redis.embedded.RedisServer;
public class JedisIntegrationTest { public class JedisIntegrationTest {
private Jedis jedis; private static Jedis jedis;
private static RedisServer redisServer; private static RedisServer redisServer;
private static int port;
public JedisIntegrationTest() {
jedis = new Jedis();
}
@BeforeClass @BeforeClass
public static void setUp() throws IOException { public static void setUp() throws IOException {
redisServer = new RedisServer(6379);
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
redisServer = new RedisServer(port);
redisServer.start(); redisServer.start();
// Configure JEDIS
jedis = new Jedis("localhost", port);
} }
@AfterClass @AfterClass
@ -178,7 +195,7 @@ public class JedisIntegrationTest {
public void givenAPoolConfiguration_thenCreateAJedisPool() { public void givenAPoolConfiguration_thenCreateAJedisPool() {
final JedisPoolConfig poolConfig = buildPoolConfig(); final JedisPoolConfig poolConfig = buildPoolConfig();
try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost"); Jedis jedis = jedisPool.getResource()) { try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost", port); Jedis jedis = jedisPool.getResource()) {
// do simple operation to verify that the Jedis resource is working // do simple operation to verify that the Jedis resource is working
// properly // properly

View File

@ -1,15 +1,20 @@
package com.baeldung; package com.baeldung;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.charset.Charset;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.redisson.Redisson; import org.redisson.Redisson;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.redisson.config.Config; import org.redisson.config.Config;
import redis.embedded.RedisServer;
import java.io.File; import com.google.common.io.Files;
import java.io.IOException;
import redis.embedded.RedisServer;
/** /**
* Created by johnson on 3/9/17. * Created by johnson on 3/9/17.
@ -17,10 +22,17 @@ import java.io.IOException;
public class RedissonConfigurationIntegrationTest { public class RedissonConfigurationIntegrationTest {
private static RedisServer redisServer; private static RedisServer redisServer;
private static RedissonClient client; private static RedissonClient client;
private static int port;
@BeforeClass @BeforeClass
public static void setUp() throws IOException { public static void setUp() throws IOException {
redisServer = new RedisServer(6379);
// Take an available port
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
redisServer = new RedisServer(port);
redisServer.start(); redisServer.start();
} }
@ -36,7 +48,7 @@ public class RedissonConfigurationIntegrationTest {
public void givenJavaConfig_thenRedissonConnectToRedis() { public void givenJavaConfig_thenRedissonConnectToRedis() {
Config config = new Config(); Config config = new Config();
config.useSingleServer() config.useSingleServer()
.setAddress("127.0.0.1:6379"); .setAddress(String.format("127.0.0.1:%s", port));
client = Redisson.create(config); client = Redisson.create(config);
@ -45,10 +57,11 @@ public class RedissonConfigurationIntegrationTest {
@Test @Test
public void givenJSONFileConfig_thenRedissonConnectToRedis() throws IOException { public void givenJSONFileConfig_thenRedissonConnectToRedis() throws IOException {
Config config = Config.fromJSON(
new File(getClass().getClassLoader().getResource(
"singleNodeConfig.json").getFile()));
File configFile = new File(getClass().getClassLoader().getResource("singleNodeConfig.json").getFile());
String configContent = Files.toString(configFile, Charset.defaultCharset()).replace("6379", String.valueOf(port));
Config config = Config.fromJSON(configContent);
client = Redisson.create(config); client = Redisson.create(config);
assert(client != null && client.getKeys().count() >= 0); assert(client != null && client.getKeys().count() >= 0);
@ -56,10 +69,11 @@ public class RedissonConfigurationIntegrationTest {
@Test @Test
public void givenYAMLFileConfig_thenRedissonConnectToRedis() throws IOException { public void givenYAMLFileConfig_thenRedissonConnectToRedis() throws IOException {
Config config = Config.fromYAML(
new File(getClass().getClassLoader().getResource(
"singleNodeConfig.yaml").getFile()));
File configFile = new File(getClass().getClassLoader().getResource("singleNodeConfig.yaml").getFile());
String configContent = Files.toString(configFile, Charset.defaultCharset()).replace("6379", String.valueOf(port));
Config config = Config.fromYAML(configContent);
client = Redisson.create(config); client = Redisson.create(config);
assert(client != null && client.getKeys().count() >= 0); assert(client != null && client.getKeys().count() >= 0);