From f26355de416b5a30d44177f312f7ab684d29a8f4 Mon Sep 17 00:00:00 2001 From: Marcus Handte Date: Mon, 15 Feb 2021 16:57:21 +0100 Subject: [PATCH] Cleanup after test and add tool output to gitignore. --- .gitignore | 5 +- .../test/session/AbstractSessionTest.java | 81 ++++++++++++++---- .../test/rest/AbstractResourceTest.java | 85 +++++++++++++++---- 3 files changed, 137 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index abe7e1eb..20571dfd 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ target/ .factorypath # netbeans -nbproject/private/ +Bnbproject/private/ # intellij .idea/ @@ -52,3 +52,6 @@ Package.resolved #xcode xcuserdata/ /temp/ + +#tools +service-tool/dwd-data/ diff --git a/service-ejb/src/test/java/com/mowesta/service/test/session/AbstractSessionTest.java b/service-ejb/src/test/java/com/mowesta/service/test/session/AbstractSessionTest.java index 43588095..8841497d 100644 --- a/service-ejb/src/test/java/com/mowesta/service/test/session/AbstractSessionTest.java +++ b/service-ejb/src/test/java/com/mowesta/service/test/session/AbstractSessionTest.java @@ -1,8 +1,13 @@ package com.mowesta.service.test.session; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,6 +19,7 @@ import javax.xml.bind.Marshaller; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.security.auth.callback.UsernamePasswordHandler; import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.AfterClass; import org.junit.Assert; import com.mowesta.service.test.EjbDeployment; @@ -23,34 +29,34 @@ import com.mowesta.service.test.EjbDeployment; * utilities that might be useful for testing, including * an "executeAs" method that allows the test to impersonate * as a particular user. - * + * * @author Marcus */ public abstract class AbstractSessionTest { - + /** * The security domain for logins. */ public static final String SECURITY_DOMAIN = "test"; - + /** * The logger. */ protected Logger log = Logger.getLogger(getClass().getName()); - + /** * Creates the deployment. - * + * * @return The deployment. */ @Deployment public static JavaArchive createDeployment() { return EjbDeployment.createEjb(); } - + /** * Prints the xml representation of any api object to standard out. - * + * * @param obj The object to print. */ public void printApiObject(Object obj) { @@ -62,10 +68,10 @@ public abstract class AbstractSessionTest { log.log(Level.SEVERE, "Failed to find UTF-8?!", e); } } - + /** * Prints the xml representation of any api object to the ouptut stream. - * + * * @param obj The object to print. * @param out The ouptut stream to print to. */ @@ -86,18 +92,18 @@ public abstract class AbstractSessionTest { * A password is not required, since the DatabaseServerLoginModule * is configured to return "password" as the password for all * users. - * + * * @param user The user's login. * @param r The runnable to execute. */ public void executeAs(String user, Runnable r) { - executeAs(user, null, r); + executeAs(user, null, r); } - + /** * Tries to login into the system using the specified user * and device secret. Either one of which can be null. - * + * * @param user The user login (which may be null, if device is set). * @param device The device token (which may be null, if user is set). * @param r The runnable to execute @@ -124,8 +130,51 @@ public abstract class AbstractSessionTest { Assert.assertEquals("Login exception during login for '" + user + ":" + device + "'.", null, e); } catch (RuntimeException e) { Assert.assertEquals("Runtime exception during test with '" + user + ":" + device + "'.", null, e); - } + } } - - + + /** + * Remove the infinispan and lucene files and folders after running + * the tests so I do not have to exclude them from commits all the time. + */ + @AfterClass + public static void cleanup() { + deleteRecursive(new File("com.mowesta.service.entity.WeatherForecastContainer")); + deleteRecursive(new File("temp")); + deleteRecursive(new File("meta")); + deleteRecursive(new File("data")); + } + + /** + * Recursively delete all data in the specified folder. + * + * @param file The file. + */ + private static void deleteRecursive(File file) { + Set ignored = new HashSet<>(); + List remaining = new ArrayList<>(); + remaining.add(file); + while (! remaining.isEmpty()) { + File next = remaining.remove(0); + File[] contents = next.listFiles(); + List children = new ArrayList<>(); + if (contents != null) { + for (File f: contents) { + if (!ignored.contains(f)) { + children.add(0, f); + } + } + } + if (! children.isEmpty()) { + remaining.add(0, next); + remaining.addAll(0, children); + } else { + if (! next.delete()) { + ignored.add(next); + next.deleteOnExit(); + } + } + } + } + } diff --git a/service-war/src/test/java/com/mowesta/service/test/rest/AbstractResourceTest.java b/service-war/src/test/java/com/mowesta/service/test/rest/AbstractResourceTest.java index 4e5a92e5..91447b35 100644 --- a/service-war/src/test/java/com/mowesta/service/test/rest/AbstractResourceTest.java +++ b/service-war/src/test/java/com/mowesta/service/test/rest/AbstractResourceTest.java @@ -1,7 +1,15 @@ package com.mowesta.service.test.rest; -import com.mowesta.service.auth.basic.BasicAuthCredential; -import com.mowesta.service.test.WarDeployment; +import java.io.File; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.ws.rs.client.ClientRequestFilter; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.resteasy.client.jaxrs.BasicAuthentication; @@ -10,15 +18,14 @@ import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; import org.jboss.resteasy.client.jaxrs.engines.URLConnectionEngine; import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; +import org.junit.AfterClass; -import javax.ws.rs.client.ClientRequestFilter; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Marshaller; -import java.io.OutputStream; +import com.mowesta.service.auth.basic.BasicAuthCredential; +import com.mowesta.service.test.WarDeployment; /** * Base class to run tests with a generic deployment and utilities. - * + * * @author Marcus */ public class AbstractResourceTest { @@ -27,7 +34,7 @@ public class AbstractResourceTest { * The base url for use in tests. This is the url without protocol. */ public static final String BASE_URL = "localhost:9080/api"; - + /** * The URL to run the tests. This is a combination of the hostname assigned to * the test container at startup (which includes a static port shift by 1000 @@ -38,7 +45,7 @@ public class AbstractResourceTest { /** * Creates the deployment. - * + * * @return The deployment. */ @Deployment @@ -48,7 +55,7 @@ public class AbstractResourceTest { /** * Returns a simple web target for the api url (DRY). - * + * * @return The web target. */ public ResteasyWebTarget getTarget() { @@ -60,7 +67,7 @@ public class AbstractResourceTest { /** * Returns a web target with basic auth set to the specified login and password. - * + * * @param login * The login (username). * @param password @@ -82,7 +89,7 @@ public class AbstractResourceTest { clientRequestContext.getHeaders().putSingle("Authorization", "Bearer " + deviceToken); }); } - + /** * Returns a web target with device authentication enabled using the specified token * and user. @@ -100,13 +107,13 @@ public class AbstractResourceTest { clientRequestContext.getHeaders().add("Authorization", new BasicAuthCredential(login, password).toString()); }); } - - - + + + /** * Prints the xml representation of any api object to standard out. - * + * * @param obj * The object to print. */ @@ -116,7 +123,7 @@ public class AbstractResourceTest { /** * Prints the xml representation of any api object to the ouptut stream. - * + * * @param obj * The object to print. * @param out @@ -133,4 +140,48 @@ public class AbstractResourceTest { } } + /** + * Remove the infinispan and lucene files and folders after running + * the tests so I do not have to exclude them from commits all the time. + */ + @AfterClass + public static void cleanup() { + deleteRecursive(new File("com.mowesta.service.entity.WeatherForecastContainer")); + deleteRecursive(new File("temp")); + deleteRecursive(new File("meta")); + deleteRecursive(new File("data")); + } + + /** + * Recursively delete all data in the specified folder. + * + * @param file The file. + */ + private static void deleteRecursive(File file) { + Set ignored = new HashSet<>(); + List remaining = new ArrayList<>(); + remaining.add(file); + while (! remaining.isEmpty()) { + File next = remaining.remove(0); + File[] contents = next.listFiles(); + List children = new ArrayList<>(); + if (contents != null) { + for (File f: contents) { + if (!ignored.contains(f)) { + children.add(0, f); + } + } + } + if (! children.isEmpty()) { + remaining.add(0, next); + remaining.addAll(0, children); + } else { + if (! next.delete()) { + ignored.add(next); + next.deleteOnExit(); + } + } + } + } + }