-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
245 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/ahunigel/json/JsonPropertySourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.ahunigel.json; | ||
|
||
import com.github.ahunigel.util.ResourceUtil; | ||
import org.springframework.boot.env.PropertySourceLoader; | ||
import org.springframework.core.env.PropertySource; | ||
import org.springframework.core.io.support.EncodedResource; | ||
import org.springframework.core.io.support.PropertySourceFactory; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Created by nigel on 2020/3/21. | ||
* | ||
* @author nigel | ||
*/ | ||
public class JsonPropertySourceFactory implements PropertySourceFactory { | ||
private PropertySourceLoader sourceLoader = new JsonPropertySourceLoader(); | ||
|
||
@Override | ||
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { | ||
return ResourceUtil.loadPropertySource(name, resource, sourceLoader); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/github/ahunigel/json/JsonPropertySourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.ahunigel.json; | ||
|
||
import org.springframework.boot.env.OriginTrackedMapPropertySource; | ||
import org.springframework.boot.env.PropertySourceLoader; | ||
import org.springframework.core.env.PropertySource; | ||
import org.springframework.core.io.Resource; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by nigel on 2020/3/22. | ||
* | ||
* @author nigel | ||
*/ | ||
public class JsonPropertySourceLoader implements PropertySourceLoader { | ||
@Override | ||
public String[] getFileExtensions() { | ||
return new String[]{"json"}; | ||
} | ||
|
||
@Override | ||
public List<PropertySource<?>> load(String name, Resource resource) throws IOException { | ||
List<Map<String, ?>> loaded = new OriginTrackedJsonLoader(resource).loadAsYaml(); | ||
if (loaded.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
List<PropertySource<?>> propertySources = new ArrayList<>(loaded.size()); | ||
for (int i = 0; i < loaded.size(); i++) { | ||
propertySources.add(new OriginTrackedMapPropertySource( | ||
name + (loaded.size() != 1 ? " (document #" + i + ")" : ""), | ||
loaded.get(i))); | ||
} | ||
|
||
return propertySources; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/github/ahunigel/json/OriginTrackedJsonLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.github.ahunigel.json; | ||
|
||
import com.github.ahunigel.yaml.YamlResourceUtil; | ||
import com.google.common.io.Files; | ||
import org.springframework.boot.json.JsonParser; | ||
import org.springframework.boot.json.JsonParserFactory; | ||
import org.springframework.core.io.Resource; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by nigel on 2020/3/22. | ||
* | ||
* @author nigel | ||
*/ | ||
public class OriginTrackedJsonLoader { | ||
private final Resource resource; | ||
|
||
public OriginTrackedJsonLoader(Resource resource) { | ||
this.resource = resource; | ||
} | ||
|
||
public List<Map<String, ?>> load() throws IOException { | ||
JsonParser parser = JsonParserFactory.getJsonParser(); | ||
String json = Files.toString(resource.getFile(), StandardCharsets.UTF_8); | ||
Map<String, ?> map = parser.parseMap(json); | ||
return Collections.singletonList(map); | ||
} | ||
|
||
public List<Map<String, ?>> loadAsYaml() { | ||
Map<String, ?> properties = (Map) YamlResourceUtil.toProperties(resource); | ||
if (properties.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
return Collections.singletonList(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/com/github/ahunigel/json/JsonPropertySourceFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.ahunigel.json; | ||
|
||
import com.github.ahunigel.TestApp; | ||
import com.github.ahunigel.TestDevice; | ||
import lombok.SneakyThrows; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Created by nigel on 2020/3/21. | ||
* | ||
* @author nigel | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = {TestApp.class, TestDevice.class}) | ||
@AutoConfigureWebMvc | ||
@PropertySource(value = {"classpath:test.json", "classpath:test-device.json"}, factory = JsonPropertySourceFactory.class) | ||
public class JsonPropertySourceFactoryTest { | ||
@Autowired | ||
private Environment environment; | ||
@Autowired | ||
private TestDevice testDevice; | ||
@Value("${nz.int}") | ||
private int nzInt; | ||
@Value("${nz.float}") | ||
private float nzFloat; | ||
@Value("${nz.string}") | ||
private String nzString; | ||
|
||
@SneakyThrows | ||
@Test | ||
public void testBean() { | ||
TestDevice expectDevice = new TestDevice(); | ||
expectDevice.setIp("192.168.168.1"); | ||
expectDevice.setPort(8080); | ||
expectDevice.setPortSsl(8443); | ||
expectDevice.setUserName("nigel"); | ||
expectDevice.setPassword("zheng"); | ||
expectDevice.setMainApi("app/index.php"); | ||
|
||
assertThat(testDevice).isEqualTo(expectDevice); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
public void testEnv() { | ||
assertThat(environment.getProperty("nz.int")).isNotNull().isEqualTo("5"); | ||
assertThat(environment.getProperty("nz.float")).isNotNull().isEqualTo("6.0"); | ||
assertThat(environment.getProperty("nz.string")).isNotNull().isEqualTo("nigel"); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
public void testValueAnnotation() { | ||
assertThat(nzInt).isNotZero().isEqualTo(5); | ||
assertThat(nzFloat).isNotNaN().isEqualTo(6.0f); | ||
assertThat(nzString).isNotNull().isEqualTo("nigel"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"test.device": { | ||
"ip": "192.168.168.1", | ||
"port": 8080, | ||
"portSSL": 8443, | ||
"username": "nigel", | ||
"password": "zheng", | ||
"mainApi": "app/index.php" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test: | ||
device: | ||
ip: 192.168.168.1 | ||
port: 8080 | ||
portSSL: 8443 | ||
username: nigel | ||
password: zheng | ||
mainApi: app/index.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"nz": { | ||
"int": 5, | ||
"float": 6.0, | ||
"string": "nigel" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
nz: | ||
int: 5 | ||
float: 6.0 | ||
string: nigel |