Skip to content

Commit

Permalink
GEODE-6731: Make TestUtil delegate to ResourceUtils
Browse files Browse the repository at this point in the history
* Make TestUtil delegate to ResourceUtils.
* Add deprecation annotations and javadocs.
* Use static import for all uses of ResourceUtils.
  • Loading branch information
kirklund committed May 7, 2019
1 parent 6defb1b commit 2a8259f
Show file tree
Hide file tree
Showing 76 changed files with 595 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.apache.geode.session.tests;

import static org.apache.geode.test.util.ResourceUtils.getResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand Down Expand Up @@ -47,7 +48,6 @@

import org.apache.geode.internal.logging.LogService;
import org.apache.geode.management.internal.configuration.utils.ZipUtils;
import org.apache.geode.util.test.TestUtil;

/**
* Base class for handling downloading and configuring J2EE containers.
Expand Down Expand Up @@ -145,7 +145,7 @@ public ContainerInstall(String name, String downloadURL, ConnectionType connType

clearPreviousInstall(installDir);

String resource = TestUtil.getResourcePath(getClass(), "/" + downloadURL);
String resource = getResource(getClass(), "/" + downloadURL).getPath();
URL url = Paths.get(resource).toUri().toURL();
logger.info("Installing container from URL " + url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.apache.geode.distributed.ConfigurationProperties.LOG_LEVEL;
import static org.apache.geode.distributed.ConfigurationProperties.SECURITY_MANAGER;
import static org.apache.geode.test.util.ResourceUtils.getResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand All @@ -36,29 +37,33 @@
import org.apache.geode.management.internal.cli.util.CommandStringBuilder;
import org.apache.geode.test.junit.categories.LoggingTest;
import org.apache.geode.test.junit.categories.SecurityTest;
import org.apache.geode.test.junit.rules.RequiresGeodeHome;
import org.apache.geode.test.junit.rules.gfsh.GfshExecution;
import org.apache.geode.test.junit.rules.gfsh.GfshRule;
import org.apache.geode.util.test.TestUtil;

/**
* This test class expects a "security.json" file to be visible on the member classpath. The
* security.json is consumed by {@link org.apache.geode.examples.security.ExampleSecurityManager}
* and should contain each username/password combination present (even though not all will actually
* be consumed by the Security Manager).
* security.json is consumed by {@link ExampleSecurityManager} and should contain each
* username/password combination present (even though not all will actually be consumed by the
* Security Manager).
*
* <p>
* Each password shares the string below for easier log scanning.
*/
@Category({SecurityTest.class, LoggingTest.class})
public class LogsAndDescribeConfigAreFullyRedactedAcceptanceTest {

private static String sharedPasswordString = "abcdefg";
private static final String sharedPasswordString = "abcdefg";

private File propertyFile;
private File securityPropertyFile;

@Rule
public GfshRule gfsh = new GfshRule();

@Rule
public RequiresGeodeHome geodeHome = new RequiresGeodeHome();

@Before
public void createDirectoriesAndFiles() throws Exception {
propertyFile = gfsh.getTemporaryFolder().newFile("geode.properties");
Expand All @@ -85,36 +90,32 @@ public void createDirectoriesAndFiles() throws Exception {
startSecureLocatorAndServer();
}

private void startSecureLocatorAndServer() throws Exception {
try {
// The json is in the root resource directory.
String securityJson = TestUtil.getResourcePath(
LogsAndDescribeConfigAreFullyRedactedAcceptanceTest.class, "/security.json");
// We want to add the folder to the classpath, so we strip off the filename.
securityJson = securityJson.substring(0, securityJson.length() - "security.json".length());
String startLocatorCmd =
new CommandStringBuilder("start locator").addOption("name", "test-locator")
.addOption("properties-file", propertyFile.getAbsolutePath())
.addOption("security-properties-file", securityPropertyFile.getAbsolutePath())
.addOption("J", "-Dsecure-username-jd=user-jd")
.addOption("J", "-Dsecure-password-jd=password-jd")
.addOption("classpath", securityJson).getCommandString();

String startServerCmd = new CommandStringBuilder("start server")
.addOption("name", "test-server").addOption("user", "viaStartMemberOptions")
.addOption("password", sharedPasswordString + "-viaStartMemberOptions")
.addOption("properties-file", propertyFile.getAbsolutePath())
.addOption("security-properties-file", securityPropertyFile.getAbsolutePath())
.addOption("J", "-Dsecure-username-jd=user-jd")
.addOption("J", "-Dsecure-password-jd=" + sharedPasswordString + "-password-jd")
.addOption("server-port", "0")
.addOption("classpath", securityJson).getCommandString();

gfsh.execute(startLocatorCmd, startServerCmd);
} catch (Exception e) {
throw new Exception(
"Cluster could not start, failing beyond the intended scope of this test.", e);
}
private void startSecureLocatorAndServer() {
// The json is in the root resource directory.
String securityJson =
getResource(LogsAndDescribeConfigAreFullyRedactedAcceptanceTest.class,
"/security.json").getPath();
// We want to add the folder to the classpath, so we strip off the filename.
securityJson = securityJson.substring(0, securityJson.length() - "security.json".length());
String startLocatorCmd =
new CommandStringBuilder("start locator").addOption("name", "test-locator")
.addOption("properties-file", propertyFile.getAbsolutePath())
.addOption("security-properties-file", securityPropertyFile.getAbsolutePath())
.addOption("J", "-Dsecure-username-jd=user-jd")
.addOption("J", "-Dsecure-password-jd=password-jd")
.addOption("classpath", securityJson).getCommandString();

String startServerCmd = new CommandStringBuilder("start server")
.addOption("name", "test-server").addOption("user", "viaStartMemberOptions")
.addOption("password", sharedPasswordString + "-viaStartMemberOptions")
.addOption("properties-file", propertyFile.getAbsolutePath())
.addOption("security-properties-file", securityPropertyFile.getAbsolutePath())
.addOption("J", "-Dsecure-username-jd=user-jd")
.addOption("J", "-Dsecure-password-jd=" + sharedPasswordString + "-password-jd")
.addOption("server-port", "0")
.addOption("classpath", securityJson).getCommandString();

gfsh.execute(startLocatorCmd, startServerCmd);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package org.apache.geode.management.internal.rest;

import static org.apache.geode.test.util.ResourceUtils.copyDirectoryResource;
import static org.apache.geode.test.util.ResourceUtils.getResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand All @@ -29,7 +31,6 @@

import org.apache.geode.internal.GemFireVersion;
import org.apache.geode.test.junit.rules.RequiresGeodeHome;
import org.apache.geode.test.util.ResourceUtils;

public class GradleBuildWithGeodeCoreAcceptanceTest {

Expand All @@ -41,7 +42,7 @@ public class GradleBuildWithGeodeCoreAcceptanceTest {

@Test
public void testBasicGradleBuild() throws Exception {
URL projectDir = ResourceUtils.getResource("/gradle-test-projects/management");
URL projectDir = getResource("/gradle-test-projects/management");
assertThat(projectDir).isNotNull();

String projectGroup = System.getProperty("projectGroup");
Expand All @@ -51,7 +52,7 @@ public void testBasicGradleBuild() throws Exception {
String geodeVersion = GemFireVersion.getGemFireVersion();

File buildDir = temp.getRoot();
ResourceUtils.copyDirectoryResource(projectDir, buildDir);
copyDirectoryResource(projectDir, buildDir);

GradleConnector connector = GradleConnector.newConnector();
connector.useBuildDistribution();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.apache.geode.management.internal.rest;

import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand Down Expand Up @@ -45,7 +46,6 @@
import org.apache.geode.test.junit.rules.gfsh.GfshScript;
import org.apache.geode.test.junit.rules.gfsh.internal.ProcessLogger;
import org.apache.geode.test.junit.runners.CategoryWithParameterizedRunnerFactory;
import org.apache.geode.util.test.TestUtil;

@RunWith(Parameterized.class)
@UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)
Expand Down Expand Up @@ -75,8 +75,9 @@ public static void beforeClass() {
* -keystore trusted.keystore -keypass password -storepass password \
* -ext san=ip:127.0.0.1,dns:localhost -storetype jks
*/
trustStorePath = TestUtil.getResourcePath(StandaloneClientManagementAPIAcceptanceTest.class,
"/ssl/trusted.keystore");
trustStorePath =
createTempFileFromResource(StandaloneClientManagementAPIAcceptanceTest.class,
"/ssl/trusted.keystore").getAbsolutePath();
assertThat(trustStorePath).as("java file resource not found").isNotBlank();
}

Expand All @@ -85,7 +86,8 @@ public static void beforeClass() {
public void clientCreatesRegionUsingClusterManagementService() throws Exception {
JarBuilder jarBuilder = new JarBuilder();
String filePath =
TestUtil.getResourcePath(this.getClass(), "/ManagementClientCreateRegion.java");
createTempFileFromResource(this.getClass(), "/ManagementClientCreateRegion.java")
.getAbsolutePath();
assertThat(filePath).as("java file resource not found").isNotBlank();

File outputJar = new File(tempDir.getRoot(), "output.jar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import static org.apache.geode.test.junit.rules.HttpResponseAssert.assertResponse;
import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand All @@ -36,7 +37,6 @@
import org.apache.geode.test.junit.categories.RestAPITest;
import org.apache.geode.test.junit.rules.GeodeDevRestClient;
import org.apache.geode.test.junit.rules.GfshCommandRule;
import org.apache.geode.util.test.TestUtil;

@Category({RestAPITest.class})
public class RestFunctionExecuteDUnitTest {
Expand Down Expand Up @@ -110,8 +110,10 @@ public void connectToServer2() throws Exception {

// find ImplementsFunction.java in the geode-core resource
private static File loadClassToFile() {
String resourcePath = TestUtil.getResourcePath(Function.class.getClassLoader(),
"org/apache/geode/management/internal/deployment/ImplementsFunction.java");
String resourcePath =
createTempFileFromResource(Function.class.getClassLoader(),
"org/apache/geode/management/internal/deployment/ImplementsFunction.java")
.getAbsolutePath();
assertThat(resourcePath).isNotNull();

return new File(resourcePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.apache.geode.distributed.ConfigurationProperties.SSL_TRUSTSTORE_PASSWORD;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_WEB_ALIAS;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_WEB_SERVICE_REQUIRE_AUTHENTICATION;
import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
Expand Down Expand Up @@ -77,7 +78,6 @@
import org.apache.geode.test.dunit.rules.ClusterStartupRule;
import org.apache.geode.test.dunit.rules.MemberVM;
import org.apache.geode.test.junit.categories.RestAPITest;
import org.apache.geode.util.test.TestUtil;

/**
* @since GemFire 8.0
Expand All @@ -99,7 +99,8 @@ public class RestAPIsWithSSLDUnitTest {

private static File findTrustedJKSWithSingleEntry() {
return new File(
TestUtil.getResourcePath(RestAPIsWithSSLDUnitTest.class, "/ssl/trusted.keystore"));
createTempFileFromResource(RestAPIsWithSSLDUnitTest.class, "/ssl/trusted.keystore")
.getAbsolutePath());
}

private static File findTrustStore(Properties props) {
Expand Down Expand Up @@ -249,9 +250,11 @@ public void testSimpleSSLWithMultiKey_KeyStore() throws Exception {

Properties props = new Properties();
props.setProperty(SSL_KEYSTORE,
TestUtil.getResourcePath(getClass(), "/org/apache/geode/internal/net/multiKey.jks"));
createTempFileFromResource(getClass(), "/org/apache/geode/internal/net/multiKey.jks")
.getAbsolutePath());
props.setProperty(SSL_TRUSTSTORE,
TestUtil.getResourcePath(getClass(), "/org/apache/geode/internal/net/multiKeyTrust.jks"));
createTempFileFromResource(getClass(),
"/org/apache/geode/internal/net/multiKeyTrust.jks").getAbsolutePath());
props.setProperty(SSL_KEYSTORE_PASSWORD, "password");
props.setProperty(SSL_TRUSTSTORE_PASSWORD, "password");
props.setProperty(SSL_KEYSTORE_TYPE, "JKS");
Expand All @@ -267,9 +270,11 @@ public void testSimpleSSLWithMultiKey_KeyStore_WithInvalidClientKey() throws Exc

Properties props = new Properties();
props.setProperty(SSL_KEYSTORE,
TestUtil.getResourcePath(getClass(), "/org/apache/geode/internal/net/multiKey.jks"));
createTempFileFromResource(getClass(), "/org/apache/geode/internal/net/multiKey.jks")
.getAbsolutePath());
props.setProperty(SSL_TRUSTSTORE,
TestUtil.getResourcePath(getClass(), "/org/apache/geode/internal/net/multiKeyTrust.jks"));
createTempFileFromResource(getClass(),
"/org/apache/geode/internal/net/multiKeyTrust.jks").getAbsolutePath());
props.setProperty(SSL_KEYSTORE_PASSWORD, "password");
props.setProperty(SSL_TRUSTSTORE_PASSWORD, "password");
props.setProperty(SSL_KEYSTORE_TYPE, "JKS");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.apache.geode;

import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand All @@ -31,7 +32,6 @@
import org.junit.experimental.categories.Category;

import org.apache.geode.test.junit.categories.RestAPITest;
import org.apache.geode.util.test.TestUtil;

@Category({RestAPITest.class})
public class AssemblyContentsIntegrationTest {
Expand All @@ -42,7 +42,8 @@ public class AssemblyContentsIntegrationTest {
@Before
public void loadExpectedAssemblyContent() throws IOException {
String assemblyContent =
TestUtil.getResourcePath(AssemblyContentsIntegrationTest.class, "/assembly_content.txt");
createTempFileFromResource(AssemblyContentsIntegrationTest.class,
"/assembly_content.txt").getAbsolutePath();

expectedAssemblyContent =
Files.lines(Paths.get(assemblyContent)).collect(Collectors.toCollection(TreeSet::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.apache.geode;

import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.junit.Assert.assertTrue;

import java.io.File;
Expand All @@ -34,7 +35,6 @@
import org.junit.experimental.categories.Category;

import org.apache.geode.test.junit.categories.RestAPITest;
import org.apache.geode.util.test.TestUtil;

@Category({RestAPITest.class})
public class BundledJarsJUnitTest {
Expand All @@ -46,7 +46,8 @@ public class BundledJarsJUnitTest {
@Before
public void loadExpectedJars() throws IOException {
String expectedJarFile =
TestUtil.getResourcePath(BundledJarsJUnitTest.class, "/expected_jars.txt");
createTempFileFromResource(BundledJarsJUnitTest.class, "/expected_jars.txt")
.getAbsolutePath();

expectedJars = Files.lines(Paths.get(expectedJarFile)).collect(Collectors.toSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.apache.geode;

import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand All @@ -31,7 +32,6 @@
import org.junit.experimental.categories.Category;

import org.apache.geode.test.junit.categories.RestAPITest;
import org.apache.geode.util.test.TestUtil;

@Category({RestAPITest.class})
public class GeodeDependencyJarIntegrationTest {
Expand All @@ -42,8 +42,8 @@ public class GeodeDependencyJarIntegrationTest {
@Before
public void loadExpectedClassPath() throws IOException {
String dependencyClasspath =
TestUtil.getResourcePath(AssemblyContentsIntegrationTest.class,
"/dependency_classpath.txt");
createTempFileFromResource(AssemblyContentsIntegrationTest.class,
"/dependency_classpath.txt").getAbsolutePath();

expectedClasspathElements =
Files.lines(Paths.get(dependencyClasspath)).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static org.apache.geode.distributed.ConfigurationProperties.SSL_TRUSTSTORE;
import static org.apache.geode.distributed.ConfigurationProperties.SSL_TRUSTSTORE_PASSWORD;
import static org.apache.geode.test.junit.rules.HttpResponseAssert.assertResponse;
import static org.apache.geode.util.test.TestUtil.getResourcePath;
import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;

import java.io.File;

Expand All @@ -44,7 +44,8 @@
public class RestSecurityWithSSLTest {

private static File KEYSTORE_FILE =
new File(getResourcePath(RestSecurityWithSSLTest.class, "/ssl/trusted.keystore"));
new File(createTempFileFromResource(RestSecurityWithSSLTest.class, "/ssl/trusted.keystore")
.getAbsolutePath());

@Rule
public RequiresGeodeHome requiresGeodeHome = new RequiresGeodeHome();
Expand Down
Loading

0 comments on commit 2a8259f

Please sign in to comment.