Refactor asDirectoryName(:Class<?>) to qualify type based on declaring types and include a UUID in the pathname.

This commit is contained in:
John Blum
2022-10-13 15:49:29 -07:00
parent 6b5e7d56e0
commit 30428f75d0
2 changed files with 74 additions and 2 deletions

View File

@@ -30,6 +30,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -114,7 +115,7 @@ public abstract class IntegrationTestsSupport {
protected static final long DEFAULT_WAIT_DURATION = TimeUnit.SECONDS.toMillis(30);
protected static final long DEFAULT_WAIT_INTERVAL = 500L; // milliseconds
protected static final String DATE_TIME_PATTERN = "yyyy-MM-dd-hh-mm-ss";
protected static final String DATE_TIME_PATTERN = "yyyy-MM-dd-HH-mm-ss";
protected static final String DIRECTORY_DELETE_ON_EXIT_PROPERTY = "spring.data.gemfire.test.directory.delete-on-exit";
protected static final String DIRECTORY_NAME_FORMAT = "%1$s-%2$s";
protected static final String GEMFIRE_LOG_FILE = "gemfire-server.log";
@@ -511,8 +512,25 @@ public abstract class IntegrationTestsSupport {
}
protected static @NonNull String asDirectoryName(@NonNull Class<?> type) {
return String.format(DIRECTORY_NAME_FORMAT, asApplicationName(type),
String baseDirectoryName = String.format(DIRECTORY_NAME_FORMAT, asQualifiedDirectoryName(type),
LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
return baseDirectoryName.concat(File.separator).concat(UUID.randomUUID().toString());
}
private static @NonNull String asQualifiedDirectoryName(@NonNull Class<?> type) {
String qualifiedDirectoryName = asApplicationName(type);
Class<?> declaringType = type.getDeclaringClass();
while (declaringType != null) {
qualifiedDirectoryName = asApplicationName(declaringType).concat(".").concat(qualifiedDirectoryName);
declaringType = declaringType.getDeclaringClass();
}
return qualifiedDirectoryName;
}
protected static @NonNull File createDirectory(@NonNull String pathname) {

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2017-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.springframework.data.gemfire.tests.integration;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.Test;
/**
* Unit Tests for {@link IntegrationTestsSupport}.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @since 1.0.0
*/
public class IntegrationTestsSupportUnitTests {
@Test
public void asDirectoryNameIsCorrect() {
LocalDateTime now = LocalDateTime.now();
String directoryName = IntegrationTestsSupport.asDirectoryName(OuterType.InnerType.class);
assertThat(directoryName).isNotBlank();
assertThat(directoryName).startsWith(String.format("%s.%s.%s-%s%s",
IntegrationTestsSupportUnitTests.class.getSimpleName(), OuterType.class.getSimpleName(), OuterType.InnerType.class.getSimpleName(),
DateTimeFormatter.ofPattern(IntegrationTestsSupport.DATE_TIME_PATTERN).format(now),
File.separator));
}
interface OuterType {
interface InnerType { }
}
}