Bug 560968: [RJ-Servi] Adapt rj.servi tests to Maven based build

  - Update tests to JUnit 5 jupiter
  - Fix execution with Maven Surefire
  - Use environment variable STATET_TEST_FILES with renv property files

Change-Id: Ifd625f952efe6d4496e8fbdfd8788383d7c283e0
diff --git a/servi/org.eclipse.statet.rj.servi-tests/META-INF/MANIFEST.MF b/servi/org.eclipse.statet.rj.servi-tests/META-INF/MANIFEST.MF
index 46cf5a7..af562e2 100644
--- a/servi/org.eclipse.statet.rj.servi-tests/META-INF/MANIFEST.MF
+++ b/servi/org.eclipse.statet.rj.servi-tests/META-INF/MANIFEST.MF
@@ -6,4 +6,6 @@
 Bundle-Name: StatET RJ - RServi - Tests  (Incubation)
 Fragment-Host: org.eclipse.statet.rj.servi
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
-Require-Bundle: org.junit;bundle-version="4.12.0"
+Import-Package: org.junit.jupiter.api;version="5.5.1",
+ org.junit.jupiter.api.condition;version="5.5.1",
+ org.apache.commons.pool2;version="[2.6.2,3.0.0)"
diff --git a/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/AbstractServiTest.java b/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/AbstractServiTest.java
index c74201f..3b6a144 100644
--- a/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/AbstractServiTest.java
+++ b/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/AbstractServiTest.java
@@ -14,32 +14,103 @@
 
 package org.eclipse.statet.rj.servi.pool;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
+import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
-
-import org.junit.Assume;
-import org.junit.BeforeClass;
+import java.util.Properties;
 
 import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+import org.eclipse.statet.jcommons.lang.Nullable;
 import org.eclipse.statet.jcommons.status.StatusException;
 
 import org.eclipse.statet.rj.data.RDataUtils;
 import org.eclipse.statet.rj.data.RObject;
 import org.eclipse.statet.rj.data.UnexpectedRDataException;
+import org.eclipse.statet.rj.renv.core.BasicREnvConfiguration;
+import org.eclipse.statet.rj.renv.core.BasicREnvManager;
+import org.eclipse.statet.rj.renv.core.REnv;
+import org.eclipse.statet.rj.renv.core.REnvConfiguration;
 import org.eclipse.statet.rj.servi.RServi;
 
 
 @NonNullByDefault
-public class AbstractServiTest {
+public abstract class AbstractServiTest {
 	
 	
-	@BeforeClass
-	public static void checkR() {
-		Assume.assumeTrue(System.getenv("R_HOME") != null);
+	private static class TestREnvConfiguration extends BasicREnvConfiguration {
+		
+		
+		public TestREnvConfiguration(final REnv rEnv, final Path stateDataRootDirectoryPath)
+				throws IOException {
+			super(rEnv, stateDataRootDirectoryPath);
+			setFlags((LOCAL | SPEC_SETUP));
+			
+			final Properties p= new Properties();
+			final Path propertiesFile= stateDataRootDirectoryPath.resolve("renv.properties");
+			try (final BufferedReader reader= Files.newBufferedReader(propertiesFile,
+					StandardCharsets.UTF_8 )) {
+				p.load(reader);
+			}
+			load(p);
+			resolvePaths();
+		}
+		
 	}
 	
+	private static class TestREnvManager extends BasicREnvManager {
+		
+		
+		private final Path basePath;
+		
+		
+		public TestREnvManager() {
+			final String pathString= System.getenv("STATET_TEST_FILES");
+			this.basePath= Paths.get(pathString, "rj.rservi", "renvs");
+			if (!Files.isDirectory(this.basePath)) {
+				throw new RuntimeException("Configuration folder 'rj.rservi/renvs' in STATET_TEST_FILES is missing.");
+			}
+		}
+		
+		public synchronized REnv getTestREnv(final String id) throws IOException {
+			REnv rEnv= get(id, null);
+			if (rEnv == null) {
+				rEnv= newEnv(id);
+				final TestREnvConfiguration rEnvConfig= new TestREnvConfiguration(rEnv, this.basePath.resolve(id));
+				addEnv(rEnvConfig);
+			}
+			return rEnv;
+		}
+		
+		public REnvConfiguration getTestConfig(final String id) throws IOException {
+			final REnv rEnv= getTestREnv(id);
+			return nonNullAssert(rEnv.get(REnvConfiguration.class));
+		}
+		
+	}
+	
+	protected static void reportErrors(final List<Throwable> errors) throws Exception {
+		if (errors.isEmpty()) {
+			return;
+		}
+		final StringBuilder sb= new StringBuilder(String.format("%1$d exception(s) occurred:", errors.size()));
+		for (int i= 0; i < errors.size(); i++) {
+			final Throwable e= errors.get(i);
+			sb.append(String.format("\n  [%1$d] %2$s: %3$s", i, e.getClass().getName(), e.getMessage()));
+		}
+		throw new Exception(sb.toString(), errors.get(0));
+	}
+	
+	
+	private @Nullable TestREnvManager rEnvManager;
 	
 	private final List<RServi> servis= new ArrayList<>();
 	
@@ -51,6 +122,15 @@
 	}
 	
 	
+	protected REnvConfiguration getEnvConfiguration(final String id) throws IOException {
+		TestREnvManager rEnvManager= this.rEnvManager;
+		if (rEnvManager == null) {
+			rEnvManager= new TestREnvManager();
+			this.rEnvManager= rEnvManager;
+		}
+		return rEnvManager.getTestConfig(id);
+	}
+	
 	protected void disposeServis(final List<Throwable> exceptions) {
 		try {
 			for (final RServi servi : this.servis) {
diff --git a/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/JMPoolTest.java b/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/JMPoolTest.java
index a0fc7d3..59f88b2 100644
--- a/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/JMPoolTest.java
+++ b/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/JMPoolTest.java
@@ -14,35 +14,34 @@
 
 package org.eclipse.statet.rj.servi.pool;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.concurrent.TimeUnit;
 
-import javax.management.OperationsException;
 import javax.security.auth.login.LoginException;
 
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runners.model.MultipleFailureException;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
 
 import org.eclipse.statet.jcommons.status.Status;
 import org.eclipse.statet.jcommons.status.StatusException;
 
-import org.eclipse.statet.rj.RjException;
 import org.eclipse.statet.rj.data.RDataUtils;
-import org.eclipse.statet.rj.data.UnexpectedRDataException;
 import org.eclipse.statet.rj.server.util.RJContext;
 import org.eclipse.statet.rj.servi.RServi;
 import org.eclipse.statet.rj.servi.RServiUtils;
 import org.eclipse.statet.rj.servi.jmx.PoolStatusMX;
+import org.eclipse.statet.rj.servi.node.RServiNodeConfig;
 
 
+@EnabledIfEnvironmentVariable(named= "STATET_TEST_FILES", matches= ".+")
 public class JMPoolTest extends AbstractServiTest {
 	
 	
@@ -51,7 +50,7 @@
 	private static List<JMPoolServer> allServers= new ArrayList<>();
 	private static JMPoolServer sharedServer;
 	
-	@AfterClass
+	@AfterAll
 	public static void disposeServers() throws Exception {
 		final List<Throwable> exceptions= new ArrayList<>();
 		try {
@@ -71,7 +70,7 @@
 			Thread.sleep(1000);
 		}
 		
-		MultipleFailureException.assertEmpty(exceptions);
+		reportErrors(exceptions);
 	}
 	
 	
@@ -82,17 +81,21 @@
 	}
 	
 	
-	@Before
-	public void initServer() throws RjException {
+	@BeforeEach
+	public void initServer() throws Exception {
 		JMPoolServer server= sharedServer;
 		if (server == null) {
 			final RJContext context= ServiTests.getRJContext();
 			
+			final RServiNodeConfig nodeConfig= new RServiNodeConfig();
+			nodeConfig.load(getEnvConfiguration("default"));
+			
 			server= new JMPoolServer("JMPoolTest", context);
 			final NetConfig netConfig= new NetConfig();
 			netConfig.setRegistryEmbed(true);
 			netConfig.setRegistryPort(ServiTests.getRegistryPort());
 			server.setNetConfig(netConfig);
+			server.setNodeConfig(nodeConfig);
 			
 			allServers.add(server);
 			JMPoolTest.sharedServer= server;
@@ -100,7 +103,7 @@
 		this.server= server;
 	}
 	
-	@After
+	@AfterEach
 	public void stopServer() throws Exception {
 		final List<Throwable> exceptions= new ArrayList<>();
 		disposeServis(exceptions);
@@ -118,7 +121,7 @@
 			exceptions.add(e);
 		}
 		
-		MultipleFailureException.assertEmpty(exceptions);
+		reportErrors(exceptions);
 	}
 	
 	
@@ -130,8 +133,7 @@
 	
 	
 	@Test
-	public void test1() throws RjException, OperationsException,
-			NoSuchElementException, LoginException, StatusException, UnexpectedRDataException {
+	public void test1() throws Exception {
 		final PoolConfig poolConfig= new PoolConfig();
 		this.server.setPoolConfig(poolConfig);
 		this.server.start();
@@ -142,8 +144,7 @@
 	}
 	
 	@Test
-	public void closed() throws RjException, OperationsException,
-			NoSuchElementException, LoginException, StatusException, UnexpectedRDataException {
+	public void closed() throws Exception {
 		final PoolConfig poolConfig= new PoolConfig();
 		this.server.setPoolConfig(poolConfig);
 		this.server.start();
@@ -166,9 +167,7 @@
 	}
 	
 	@Test
-	public void IdleNodes_default() throws RjException, OperationsException,
-			NoSuchElementException, LoginException, StatusException, UnexpectedRDataException,
-			InterruptedException {
+	public void IdleNodes_default() throws Exception {
 		final PoolConfig poolConfig= new PoolConfig();
 		this.server.setPoolConfig(poolConfig);
 		this.server.start();
@@ -186,8 +185,7 @@
 	}
 	
 	@Test
-	public void IdleNodes_ensureMin() throws RjException, OperationsException,
-			NoSuchElementException, LoginException, StatusException, UnexpectedRDataException,
+	public void IdleNodes_ensureMin() throws Exception,
 			InterruptedException {
 		final PoolConfig poolConfig= new PoolConfig();
 		this.server.setPoolConfig(poolConfig);
@@ -202,8 +200,7 @@
 	}
 	
 	@Test
-	public void TotalNodes_borrowMax() throws RjException, OperationsException,
-			NoSuchElementException, LoginException, StatusException, UnexpectedRDataException,
+	public void TotalNodes_borrowMax() throws Exception,
 			InterruptedException {
 		final PoolConfig poolConfig= new PoolConfig();
 		poolConfig.setMaxTotalCount(2);
@@ -224,9 +221,7 @@
 	}
 	
 	@Test
-	public void TotalNodes_borrowTimeout() throws RjException, OperationsException,
-			NoSuchElementException, LoginException, StatusException, UnexpectedRDataException,
-				InterruptedException {
+	public void TotalNodes_borrowTimeout() throws Exception {
 		final PoolConfig poolConfig= new PoolConfig();
 		poolConfig.setMaxTotalCount(2);
 		this.server.setPoolConfig(poolConfig);
@@ -254,9 +249,7 @@
 	}
 	
 	@Test
-	public void UsageCount_borrowMax() throws RjException, OperationsException,
-			NoSuchElementException, LoginException, StatusException, UnexpectedRDataException,
-				InterruptedException {
+	public void UsageCount_borrowMax() throws Exception {
 		final PoolConfig poolConfig= new PoolConfig();
 		poolConfig.setMaxTotalCount(1);
 		poolConfig.setMaxUsageCount(1);
@@ -285,7 +278,7 @@
 				return;
 			}
 			if (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - t) > WAIT_IDLE_SEC) {
-				assertEquals("num idle", expected, poolStatus.getNumIdling());
+				assertEquals(expected, poolStatus.getNumIdling(), "num idle");
 			}
 			Thread.sleep(100);
 		}
diff --git a/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/LocalNodeTest.java b/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/LocalNodeTest.java
index 653b7b7..508c784 100644
--- a/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/LocalNodeTest.java
+++ b/servi/org.eclipse.statet.rj.servi-tests/src/org/eclipse/statet/rj/servi/pool/LocalNodeTest.java
@@ -14,8 +14,8 @@
 
 package org.eclipse.statet.rj.servi.pool;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -24,10 +24,10 @@
 import javax.management.OperationsException;
 import javax.security.auth.login.LoginException;
 
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runners.model.MultipleFailureException;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
 
 import org.eclipse.statet.jcommons.rmi.RMIRegistry;
 import org.eclipse.statet.jcommons.rmi.RMIRegistryManager;
@@ -47,6 +47,7 @@
 import org.eclipse.statet.rj.servi.node.RServiNodeManager;
 
 
+@EnabledIfEnvironmentVariable(named= "STATET_TEST_FILES", matches= ".+")
 public class LocalNodeTest extends AbstractServiTest {
 	
 	
@@ -57,21 +58,24 @@
 	}
 	
 	
-	@Before
-	public void initNode() throws RjException, StatusException {
+	@BeforeEach
+	public void initNode() throws Exception {
 		final RJContext context= ServiTests.getRJContext();
 		final RMIRegistry rmiRegistry= RMIRegistryManager.INSTANCE.getEmbeddedPrivateRegistry(
 				new NullProgressMonitor() );
 		
+		final RServiNodeConfig nodeConfig= new RServiNodeConfig();
+		nodeConfig.load(getEnvConfiguration("default"));
+		
 		final RServiNodeFactory nodeFactory= RServiImpl.createLocalNodeFactory(
 				"LocalNodeTest", context );
 		nodeFactory.setRegistry(rmiRegistry);
-		nodeFactory.setConfig(new RServiNodeConfig());
+		nodeFactory.setConfig(nodeConfig);
 		
 		this.localR= RServiImpl.createNodeManager("LocalNodeTest", rmiRegistry, nodeFactory);
 	}
 	
-	@After
+	@AfterEach
 	public void dispose() throws Exception {
 		final List<Throwable> exceptions= new ArrayList<>();
 		disposeServis(exceptions);
@@ -87,7 +91,7 @@
 			this.localR= null;
 		}
 		
-		MultipleFailureException.assertEmpty(exceptions);
+		reportErrors(exceptions);
 	}
 	
 	private RServi getServi(final String id) throws NoSuchElementException, LoginException, StatusException {