Skip to content

Commit

Permalink
Merge pull request #4 from stornado/develop
Browse files Browse the repository at this point in the history
fix(core): fix PathUtils test case
  • Loading branch information
stornado authored Aug 19, 2020
2 parents a5be281 + 2605fd7 commit 9f50ed8
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 189 deletions.
175 changes: 92 additions & 83 deletions ngast-core/src/main/java/com/zxytech/ngast/util/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,103 +9,112 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public final class PathUtils {
private static final String DATA_DIR = "data/";

private PathUtils() {}

public static Path get(String first, String... more) {
return getEnvFilePath(of(first, more));
}

public static Path get(URI uri) {
return getEnvFilePath(of(uri));
}

public static Path of(String first, String... more) {
return Paths.get(first, more);
}

public static Path of(URI uri) {
return Paths.get(uri);
}

/**
* 根据 rdm.profiles={env} 配置值 从data/{env}/目录获取对应文件路径
*
* <p>如: ngast.environment=test
*
* <p>则:
*
* <pre>
* PathUtils.getEnvFilePath("order/purchased-order.json") = "data/test/order/purchased-order.json"
* PathUtils.getEnvFilePath("data/order/purchased-order.json") = "data/test/order/purchased-order.json"
* </pre>
*
* @param filePath
* @return
*/
public static String getEnvFilePath(String filePath) {
return getAbsolutePath(filePath, PropertiesUtils.getAsString(NGAST_ENVIRONMENT_KEY_NAME));
}

public static Path getEnvFilePath(Path filePath) {
return of(
getAbsolutePath(
filePath.toString(), PropertiesUtils.getAsString(NGAST_ENVIRONMENT_KEY_NAME)));
}

private static String getClasspathFilePath(String fileName) throws FileNotFoundException {
URL fileUrl = ClassLoader.getSystemClassLoader().getResource(fileName);
if (fileUrl != null) {
return fileUrl.getPath();

private static final String DATA_DIR = "data/";

private PathUtils() {
}

public static Path get(String first, String... more) {
return getEnvFilePath(of(first, more));
}

public static Path get(URI uri) {
return getEnvFilePath(of(uri));
}
fileUrl = Thread.currentThread().getContextClassLoader().getResource(fileName);
if (fileUrl != null) {
return fileUrl.getPath();

public static Path of(String first, String... more) {
return Paths.get(first, more);
}

public static Path of(URI uri) {
return Paths.get(uri);
}

throw new FileNotFoundException(fileName);
}
/**
* 根据 rdm.profiles={env} 配置值 从data/{env}/目录获取对应文件路径
*
* <p>如: ngast.environment=test
*
* <p>则:
*
* <pre>
* PathUtils.getEnvFilePath("order/purchased-order.json") = "data/test/order/purchased-order.json"
* PathUtils.getEnvFilePath("data/order/purchased-order.json") = "data/test/order/purchased-order.json"
* </pre>
*
* @param filePath
* @return
*/
public static String getEnvFilePath(String filePath) {
return getAbsolutePath(filePath, PropertiesUtils.getAsString(NGAST_ENVIRONMENT_KEY_NAME));
}

private static String getExistClasspathPath(String filePath) {
File file = null;
try {
file = new File(getClasspathFilePath(filePath));
} catch (FileNotFoundException ignored) {
public static Path getEnvFilePath(Path filePath) {
return of(
getAbsolutePath(
filePath.toString(), PropertiesUtils.getAsString(NGAST_ENVIRONMENT_KEY_NAME)));
}
if (file != null && file.exists()) {
return file.getAbsolutePath();

private static String getClasspathFilePath(String fileName) throws FileNotFoundException {
URL fileUrl = ClassLoader.getSystemClassLoader().getResource(fileName);
if (fileUrl != null) {
return fileUrl.getPath();
}
fileUrl = Thread.currentThread().getContextClassLoader().getResource(fileName);
if (fileUrl != null) {
return fileUrl.getPath();
}

throw new FileNotFoundException(fileName);
}
return null;
}

private static String getAbsolutePath(String path, String env) {
if (log.isDebugEnabled()) {
log.debug("{} {}", path, env);
private static String getExistClasspathPath(String filePath) {
File file = null;
try {
file = new File(getClasspathFilePath(filePath));
} catch (FileNotFoundException ignored) {
}
if (file != null && file.exists()) {
return file.getAbsolutePath();
}
return null;
}
String envNameLowerCase = StringUtils.isBlank(env) ? "" : StringUtils.trim(env).toLowerCase();

for (String absPath :
Arrays.asList(
getExistClasspathPath(
path.replaceFirst("^/?data", StringUtils.join(DATA_DIR, envNameLowerCase))),
getExistClasspathPath(StringUtils.join(DATA_DIR, envNameLowerCase, "/", path)),
getExistClasspathPath(path),
getExistClasspathPath(path.replaceFirst("^/?data", DATA_DIR)),
getExistClasspathPath(StringUtils.join(DATA_DIR, path)))) {
if (StringUtils.isNotBlank(absPath)) {
return absPath;
}

private static String getAbsolutePath(String path, String env) {
if (log.isDebugEnabled()) {
log.debug("{} {}", path, env);
}
String envNameLowerCase =
StringUtils.isBlank(env) ? "" : StringUtils.trim(env).toLowerCase();

for (String absPath :
Arrays.asList(
getExistClasspathPath(
path.replaceFirst("^/?data", StringUtils.join(DATA_DIR, envNameLowerCase))),
getExistClasspathPath(StringUtils.join(DATA_DIR, envNameLowerCase, "/", path)),
getExistClasspathPath(path),
getExistClasspathPath(path.replaceFirst("^/?data", DATA_DIR)),
getExistClasspathPath(StringUtils.join(DATA_DIR, path)))) {

if (StringUtils.isNotBlank(absPath) && isFileExists(absPath)) {
return absPath;
}
}

if (log.isWarnEnabled()) {
log.warn("Env: {} FileNotFound {}", env, path);
}
return path;
}

if (log.isWarnEnabled()) {
log.warn("Env: {} FileNotFound {}", env, path);
private static boolean isFileExists(@NonNull String path) {
return new File(path).exists();
}
return path;
}
}
160 changes: 60 additions & 100 deletions ngast-core/src/test/java/com/zxytech/ngast/util/PropertiesUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,73 @@
import static org.testng.Assert.assertNotEquals;

import com.zxytech.ngast.testng.NgastAnnotationTransformer;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Slf4j
@Listeners(NgastAnnotationTransformer.class)
public class PropertiesUtilsTest {
public static final String NGAST_PROPERTIES_FILE_PATH =
FilenameUtils.concat(
"/Users/ryan/Github/ngast/ngast-core/src/test/resources", "ngast.properties");

public static final String PROPS_KEY_STRING = "props.key.string";
public static final String PROPS_VALUE_STRING = "ABCabc123";
public static final String PROPS_STRING = "props.key.string=ABCabc123";
public static final String PROPS_KEY_INTEGER = "props.key.integer";
public static final Integer PROPS_VALUE_INTEGER = 123;
public static final String PROPS_INTEGER = "props.key.integer=123";

public static final String PROPS_KEY_LONG = "props.key.long";
public static final Long PROPS_VALUE_LONG = 123L;
public static final String PROPS_LONG = "props.key.long=123";

public static final String PROPS_KEY_FLOAT = "props.key.float";
public static final Float PROPS_VALUE_FLOAT = 9.9F;
public static final String PROPS_FLOAT = "props.key.float=9.9";

public static final String PROPS_KEY_DOUBLE = "props.key.double";
public static final Double PROPS_VALUE_DOUBLE = 9.9;
public static final String PROPS_DOUBLE = "props.key.double=9.9";

public static final String PROPS_KEY_BIG_DECIMAL = "props.key.big.decimal";
public static final BigDecimal PROPS_VALUE_BIG_DECIMAL = new BigDecimal("9.9");
public static final String PROPS_BIG_DECIMAL = "props.key.big.decimal=9.9";

@BeforeClass
public void setUp() {
File ngastProperties = new File(NGAST_PROPERTIES_FILE_PATH);
try {
FileUtils.writeLines(
ngastProperties,
Arrays.asList(
PROPS_STRING,
PROPS_INTEGER,
PROPS_LONG,
PROPS_FLOAT,
PROPS_DOUBLE,
PROPS_BIG_DECIMAL));
} catch (IOException e) {
log.error("create {} failed", NGAST_PROPERTIES_FILE_PATH, e);

public static final String PROPS_KEY_STRING = "props.key.string";
public static final String PROPS_VALUE_STRING = "ABCabc123";
public static final String PROPS_KEY_INTEGER = "props.key.integer";
public static final Integer PROPS_VALUE_INTEGER = 123;

public static final String PROPS_KEY_LONG = "props.key.long";
public static final Long PROPS_VALUE_LONG = 123L;

public static final String PROPS_KEY_FLOAT = "props.key.float";
public static final Float PROPS_VALUE_FLOAT = 9.9F;

public static final String PROPS_KEY_DOUBLE = "props.key.double";
public static final Double PROPS_VALUE_DOUBLE = 9.9;

public static final String PROPS_KEY_BIG_DECIMAL = "props.key.big.decimal";
public static final BigDecimal PROPS_VALUE_BIG_DECIMAL = new BigDecimal("9.9");


@Test
public void testGetProperty() {
assertEquals(PropertiesUtils.getProperty(PROPS_KEY_STRING), PROPS_VALUE_STRING);
}

@Test
public void testGetAsString() {
assertEquals(PropertiesUtils.getAsString(PROPS_KEY_STRING), PROPS_VALUE_STRING);
}

@Test
public void testGetAsInteger() {
assertEquals(PropertiesUtils.getAsInteger(PROPS_KEY_INTEGER), PROPS_VALUE_INTEGER);
}

@Test
public void testGetAsLong() {
assertEquals(PropertiesUtils.getAsLong(PROPS_KEY_LONG), PROPS_VALUE_LONG);
}

@Test
public void testGetAsFloat() {
assertEquals(PropertiesUtils.getAsFloat(PROPS_KEY_FLOAT), PROPS_VALUE_FLOAT);
}

@Test
public void testGetAsDouble() {
assertEquals(PropertiesUtils.getAsDouble(PROPS_KEY_DOUBLE), PROPS_VALUE_DOUBLE);
}

@Test
public void testGetAsBigDecimal() {
assertEquals(PropertiesUtils.getAsBigDecimal(PROPS_KEY_BIG_DECIMAL),
PROPS_VALUE_BIG_DECIMAL);

assertNotEquals(
BigDecimal.valueOf(PropertiesUtils.getAsFloat(PROPS_KEY_BIG_DECIMAL)),
PROPS_VALUE_BIG_DECIMAL);
assertEquals(
BigDecimal.valueOf(PropertiesUtils.getAsDouble(PROPS_KEY_BIG_DECIMAL)),
PROPS_VALUE_BIG_DECIMAL);
}
}

// @AfterClass
// public void tearDown() {
// try {
// FileUtils.forceDeleteOnExit(new File(NGAST_PROPERTIES_FILE_PATH));
// } catch (IOException e) {
// e.printStackTrace();
// }
// }

@Test
public void testGetProperty() {
assertEquals(PropertiesUtils.getProperty(PROPS_KEY_STRING), PROPS_VALUE_STRING);
}

@Test
public void testGetAsString() {
assertEquals(PropertiesUtils.getAsString(PROPS_KEY_STRING), PROPS_VALUE_STRING);
}

@Test
public void testGetAsInteger() {
assertEquals(PropertiesUtils.getAsInteger(PROPS_KEY_INTEGER), PROPS_VALUE_INTEGER);
}

@Test
public void testGetAsLong() {
assertEquals(PropertiesUtils.getAsLong(PROPS_KEY_LONG), PROPS_VALUE_LONG);
}

@Test
public void testGetAsFloat() {
assertEquals(PropertiesUtils.getAsFloat(PROPS_KEY_FLOAT), PROPS_VALUE_FLOAT);
}

@Test
public void testGetAsDouble() {
assertEquals(PropertiesUtils.getAsDouble(PROPS_KEY_DOUBLE), PROPS_VALUE_DOUBLE);
}

@Test
public void testGetAsBigDecimal() {
assertEquals(PropertiesUtils.getAsBigDecimal(PROPS_KEY_BIG_DECIMAL), PROPS_VALUE_BIG_DECIMAL);

assertNotEquals(
BigDecimal.valueOf(PropertiesUtils.getAsFloat(PROPS_KEY_BIG_DECIMAL)),
PROPS_VALUE_BIG_DECIMAL);
assertEquals(
BigDecimal.valueOf(PropertiesUtils.getAsDouble(PROPS_KEY_BIG_DECIMAL)),
PROPS_VALUE_BIG_DECIMAL);
}
}
6 changes: 6 additions & 0 deletions ngast-core/src/test/resources/ngast-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
props.key.string=ABCabc123
props.key.integer=123
props.key.long=123
props.key.float=9.9
props.key.double=9.9
props.key.big.decimal=9.9
8 changes: 2 additions & 6 deletions ngast-core/src/test/resources/ngast.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
props.key.string=ABCabc123
props.key.integer=123
props.key.long=123
props.key.float=9.9
props.key.double=9.9
props.key.big.decimal=9.9
# 配置环境
ngast.environment=test

0 comments on commit 9f50ed8

Please sign in to comment.