Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Refactoring of framework test runner so it can have a single unit test running through it all #618

Merged
merged 10 commits into from
Aug 20, 2024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ temp/
# Ignore the OpenAPI spec that is automatically placed into the
# resources folder to be served up by the OpenAPI servlet
**/dev.galasa.framework.api.openapi.servlet/**/openapi.yaml

run-test.sh
30 changes: 10 additions & 20 deletions build-locally.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,16 @@ blue=$(tput setaf 25)
# Headers and Logging
#
#-----------------------------------------------------------------------------------------
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}%s${reset}\n" "$@"
}
info() { printf "${white}➜ %s${reset}\n" "$@"
}
success() { printf "${green}✔ %s${reset}\n" "$@"
}
error() { printf "${red}✖ %s${reset}\n" "$@"
}
warn() { printf "${tan}➜ %s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
underline() { printf "${underline}${bold}%s${reset}\n" "$@" ;}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@" ;}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@" ;}
debug() { printf "${white}%s${reset}\n" "$@" ;}
info() { printf "${white}➜ %s${reset}\n" "$@" ;}
success() { printf "${green}✔ %s${reset}\n" "$@" ;}
error() { printf "${red}✖ %s${reset}\n" "$@" ;}
warn() { printf "${tan}➜ %s${reset}\n" "$@" ;}
bold() { printf "${bold}%s${reset}\n" "$@" ;}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@" ;}

#-----------------------------------------------------------------------------------------
# Functions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package dev.galasa.framework;
techcobweb marked this conversation as resolved.
Show resolved Hide resolved

import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.osgi.framework.BundleContext;
techcobweb marked this conversation as resolved.
Show resolved Hide resolved

import dev.galasa.framework.spi.FrameworkException;

public class BundleManager implements IBundleManager {

@Override
public boolean isBundleActive(BundleContext bundleContext, String bundleSymbolicName) {
return BundleManagement.isBundleActive(bundleContext, bundleSymbolicName);
}

@Override
public void loadAllGherkinManagerBundles(RepositoryAdmin repositoryAdmin, BundleContext bundleContext)
throws FrameworkException {
BundleManagement.loadAllGherkinManagerBundles(repositoryAdmin, bundleContext);
}

@Override
public void loadBundle(RepositoryAdmin repositoryAdmin, BundleContext bundleContext, String bundleSymbolicName)
throws FrameworkException {
BundleManagement.loadBundle(repositoryAdmin, bundleContext, bundleSymbolicName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.util.stream.Stream;

import dev.galasa.ResultArchiveStoreContentType;

public class FileSystem implements IFileSystem {
public FileSystem() {
}
Expand Down Expand Up @@ -64,4 +67,14 @@ public String probeContentType(Path path) throws IOException {
}
return contentType;
}

@Override
public Path createFile(Path path, FileAttribute<?>... attrs) throws IOException {
return Files.createFile(path, attrs);
}

@Override
public void write(Path path, byte[] bytes) throws IOException {
Files.write(path,bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
import dev.galasa.framework.spi.creds.ICredentialsService;
import dev.galasa.framework.spi.creds.ICredentialsStore;

// I know that the IFramework class isn't strictly necessary, but it does seem to make a
// difference to whether the OSGi framework can load it or not.
// Something about it only looking for implementations of IFramework explicitly, rather than
// looking for all the things which are super-interfaces of IFramework also.
// So we leave in the IFramework below, and supress any warning we might get.
@SuppressWarnings("unused")
@Component(immediate = true, scope = ServiceScope.SINGLETON)
public class Framework implements IFramework {
public class Framework implements IFramework, IShutableFramework {

private final static Log logger = LogFactory.getLog(Framework.class);

Expand Down Expand Up @@ -391,6 +397,11 @@ public void installLogCapture() {

}

@Override
public void shutdown() throws FrameworkException {
shutdown(null);
}

public void shutdown(Log shutdownLogger) throws FrameworkException {
if (isShutdown()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,75 +65,6 @@ public FrameworkInitialisation(
getBundleContext(), new FileSystem(), new SystemEnvironment());
}



private static BundleContext getBundleContext() {
return FrameworkUtil.getBundle(FrameworkInitialisation.class).getBundleContext();
}

/**
* Obtain the location of the galasa home directory
* @return a String representing the location of the users Galasa home directory
*/
public String getGalasaHome(Environment env) {
// 1st: If GALASA_HOME is set as a system property then use that,
// 2nd: If GALASA_HOME is set as a system environment variable, then use that.
// 3rd: otherwise we use the calling users' home folder.
String GALASA_HOME = "GALASA_HOME";
String home = env.getProperty(GALASA_HOME);
if( (home == null) || (home.trim().isEmpty())) {
home = env.getenv(GALASA_HOME);
if( (home == null) || (home.trim().isEmpty())) {
String userHome = env.getProperty(USER_HOME);
if ( userHome != null ) {
logger.info("System property "+USER_HOME+" ("+userHome+") used to set value of home location.");
home = userHome + "/.galasa";
} else {
// On windows, the USERPROFILE environment variable will be set to C:/User/Name or similar.
String userProfile = env.getenv("USERPROFILE");
if (userProfile != null) {
logger.info("System property USERPROFILE ("+userProfile+") used to set value of home location.");
home = userProfile + "/.galasa";
} else {
// Just in case, fall back on a trusty default.
logger.info("Defaulting home to ~/.galasa");
home = "~/.galasa";
}
}

} else {
logger.info("Environment variable GALASA_HOME used to set value of home location.");
}
} else {
logger.info("System property GALASA_HOME used to set value of home location.");
// The system property value may be surrounded by " characters.
// If so, strip them off.
// We allow this because a path with strings in would be split
// into separate system properties otherwise.
home = stripLeadingAndTrailingQuotes(home);
}
logger.info("Galasa home location is "+home);

return home;
}

/**
* String the first double-quote and the last double-quote off
* the begining and end of a string.
* @param input
* @return The stripped (or unaltered) string.
*/
String stripLeadingAndTrailingQuotes(String input ) {
String output = input ;
if (output.startsWith("\"")) {
output = output.replaceFirst("\"", "");
}
if (output.endsWith("\"")) {
output = output.substring(0,output.length()-1);
}
return output;
}

public FrameworkInitialisation(
Properties bootstrapProperties,
Properties overrideProperties,
Expand Down Expand Up @@ -228,6 +159,76 @@ public FrameworkInitialisation(
}
}


private static BundleContext getBundleContext() {
return FrameworkUtil.getBundle(FrameworkInitialisation.class).getBundleContext();
}

/**
* Obtain the location of the galasa home directory
* @return a String representing the location of the users Galasa home directory
*/
public String getGalasaHome(Environment env) {
// 1st: If GALASA_HOME is set as a system property then use that,
// 2nd: If GALASA_HOME is set as a system environment variable, then use that.
// 3rd: otherwise we use the calling users' home folder.
String GALASA_HOME = "GALASA_HOME";
String home = env.getProperty(GALASA_HOME);
if( (home == null) || (home.trim().isEmpty())) {
home = env.getenv(GALASA_HOME);
if( (home == null) || (home.trim().isEmpty())) {
String userHome = env.getProperty(USER_HOME);
if ( userHome != null ) {
logger.info("System property "+USER_HOME+" ("+userHome+") used to set value of home location.");
home = userHome + "/.galasa";
} else {
// On windows, the USERPROFILE environment variable will be set to C:/User/Name or similar.
String userProfile = env.getenv("USERPROFILE");
if (userProfile != null) {
logger.info("System property USERPROFILE ("+userProfile+") used to set value of home location.");
home = userProfile + "/.galasa";
} else {
// Just in case, fall back on a trusty default.
logger.info("Defaulting home to ~/.galasa");
home = "~/.galasa";
}
}

} else {
logger.info("Environment variable GALASA_HOME used to set value of home location.");
}
} else {
logger.info("System property GALASA_HOME used to set value of home location.");
// The system property value may be surrounded by " characters.
// If so, strip them off.
// We allow this because a path with strings in would be split
// into separate system properties otherwise.
home = stripLeadingAndTrailingQuotes(home);
}
logger.info("Galasa home location is "+home);

return home;
}

/**
* String the first double-quote and the last double-quote off
* the begining and end of a string.
* @param input
* @return The stripped (or unaltered) string.
*/
String stripLeadingAndTrailingQuotes(String input ) {
String output = input ;
if (output.startsWith("\"")) {
output = output.replaceFirst("\"", "");
}
if (output.endsWith("\"")) {
output = output.substring(0,output.length()-1);
}
return output;
}



/**
* Submit the run and return the run name.
*
Expand Down Expand Up @@ -400,6 +401,10 @@ public void registerEventsService(@NotNull IEventsService eventsService) throws
return this.framework;
}

public @NotNull IShutableFramework getShutableFramework() {
return this.framework;
}

public void shutdownFramework() {
try {
this.framework.shutdown(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public GenericMethodWrapper(Method excecutionMethod, Class<?> testClass, Type ty
* @param testMethod the test method if the execution method is @Before or @After
* @throws TestRunException The failure thrown by the test run
*/
public void invoke(@NotNull TestRunManagers managers, Object testClassObject, GenericMethodWrapper testMethod) throws TestRunException {
public void invoke(@NotNull ITestRunManagers managers, Object testClassObject, GenericMethodWrapper testMethod) throws TestRunException {
try {
String methodType = ",type=" + type.toString();
Result ignored = managers.anyReasonTestMethodShouldBeIgnored(new GalasaMethod(this.excecutionMethod, null));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package dev.galasa.framework;

import java.lang.annotation.Annotation;

/**
* Something which can extract annotations from something can be an AnnotationExtractor
*/
public interface IAnnotationExtractor {
/**
* @param <A> The type of testclass we are extracting something from.
* This will vary wildly based on what the users' test class is.
* @param <B> The annotation type we want to extract.
* Having this as a generic type allows this logic to be used for
* any of the annotations we want to extract.
* B must extend Annotation.
* @param testClass The test class we want annotations extracted from.
* @param annotationClass The annotation class we want to use.
* @return An instance of the annotationClass, extracted from the test class.
*/
public <A,B extends Annotation> B getAnnotation (Class<A> testClass, Class<B> annotationClass);
techcobweb marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package dev.galasa.framework;

import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.osgi.framework.BundleContext;

import dev.galasa.framework.spi.FrameworkException;

/**
* Allows for some control over loading extra bundles, and seeing if bundles are already loaded.
*/
public interface IBundleManager {
/**
* Is the supplied active in the OSGi framework
* @param bundleContext
* @param bundleSymbolicName
* @return true if it is ib the or false
techcobweb marked this conversation as resolved.
Show resolved Hide resolved
*/
public boolean isBundleActive(BundleContext bundleContext, String bundleSymbolicName);

public void loadAllGherkinManagerBundles(RepositoryAdmin repositoryAdmin, BundleContext bundleContext) throws FrameworkException;

public void loadBundle(RepositoryAdmin repositoryAdmin, BundleContext bundleContext, String bundleSymbolicName) throws FrameworkException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.util.stream.Stream;

import dev.galasa.ResultArchiveStoreContentType;

public interface IFileSystem {

void createDirectories(Path folderPath ) throws IOException;
Expand All @@ -29,4 +32,8 @@ public interface IFileSystem {
String probeContentType(Path artifactPath) throws IOException;

InputStream newInputStream(Path folderPath) throws IOException;

Path createFile(Path path, FileAttribute<?>... attrs) throws IOException;

void write(Path rasProperties, byte[] bytes) throws IOException ;
}
Loading