forked from striner/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertiesUtilTest.java
More file actions
56 lines (50 loc) · 2.33 KB
/
Copy pathPropertiesUtilTest.java
File metadata and controls
56 lines (50 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package utils;
import common.utils.PropertiesUtil;
import org.junit.Test;
import java.io.IOException;
import java.util.Properties;
public class PropertiesUtilTest {
@Test
public void test1() {
try {
String dbConfigPath = "D:\\git\\codes\\javaCode\\utils\\src\\main\\resources\\db.properties";
Properties pro = PropertiesUtil.fromFile(dbConfigPath);
System.out.println(pro.getProperty("db.driver"));
System.out.println(pro.getProperty("db.url"));
System.out.println(pro.getProperty("db.userName"));
System.out.println(pro.getProperty("db.password"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test2() {
try {
String dbConfigPath = "db.properties";
Properties pro = PropertiesUtil.fromPath(dbConfigPath);
System.out.println(pro.get("db.driver"));
} catch (Exception io) {
io.printStackTrace();
}
}
@Test
public void test3() {
System.out.println(PropertiesUtil.class.getResource(""));
System.out.println(PropertiesUtil.class.getResource("/"));
System.out.println(PropertiesUtil.class.getResource("/db.properties"));
System.out.println("\n --------- \n");
PropertiesUtil propertiesUtil = new PropertiesUtil();
System.out.println(propertiesUtil.getClass());
System.out.println(propertiesUtil.getClass().getClassLoader());
System.out.println(propertiesUtil.getClass().getClassLoader().getResource(""));
System.out.println(propertiesUtil.getClass().getClassLoader().getResource("/")); //不支持,name参数表示的是从classpath下获取的资源
System.out.println(propertiesUtil.getClass().getClassLoader().getResource("db.properties"));
System.out.println(propertiesUtil.getClass().getClassLoader().getResourceAsStream(""));
System.out.println(propertiesUtil.getClass().getClassLoader().getResourceAsStream("db.properties"));
}
@Test
public void test4() throws IOException{
System.out.println(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
System.out.println(PropertiesUtil.fromStream(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties")));
}
}