Skip to content

Commit 2e4c950

Browse files
Merge branch 'master' into Mercator
2 parents 03fd0d8 + b09fd15 commit 2e4c950

28 files changed

Lines changed: 1101 additions & 26 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.properties;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.util.Map;
8+
import java.util.Properties;
9+
import java.util.Set;
10+
import java.util.stream.Stream;
11+
12+
import org.junit.Test;
13+
14+
public class MergePropertiesUnitTest {
15+
16+
@Test
17+
public void givenTwoProperties_whenMergedUsingIteration_thenAllPropertiesInResult() {
18+
Properties globalProperties = mergePropertiesByIteratingKeySet(propertiesA(), propertiesB());
19+
20+
testMergedProperties(globalProperties);
21+
}
22+
23+
@Test
24+
public void givenTwoProperties_whenMergedUsingPutAll_thenAllPropertiesInResult() {
25+
Properties globalProperties = mergePropertiesByUsingPutAll(propertiesA(), propertiesB());
26+
27+
testMergedProperties(globalProperties);
28+
}
29+
30+
@Test
31+
public void givenTwoProperties_whenMergedUsingStreamAPI_thenAllPropertiesInResult() {
32+
Properties globalProperties = mergePropertiesByUsingStreamApi(propertiesB(), propertiesA());
33+
34+
testMergedProperties(globalProperties);
35+
}
36+
37+
private Properties mergePropertiesByIteratingKeySet(Properties... properties) {
38+
Properties mergedProperties = new Properties();
39+
for (Properties property : properties) {
40+
Set<String> propertyNames = property.stringPropertyNames();
41+
for (String name : propertyNames) {
42+
String propertyValue = property.getProperty(name);
43+
mergedProperties.setProperty(name, propertyValue);
44+
}
45+
}
46+
return mergedProperties;
47+
}
48+
49+
private Properties mergePropertiesByUsingPutAll(Properties... properties) {
50+
Properties mergedProperties = new Properties();
51+
for (Properties property : properties) {
52+
mergedProperties.putAll(property);
53+
}
54+
return mergedProperties;
55+
}
56+
57+
private Properties mergePropertiesByUsingStreamApi(Properties... properties) {
58+
return Stream.of(properties)
59+
.collect(Properties::new, Map::putAll, Map::putAll);
60+
}
61+
62+
private Properties propertiesA() {
63+
Properties properties = new Properties();
64+
properties.setProperty("application.name", "my-app");
65+
properties.setProperty("application.version", "1.0");
66+
return properties;
67+
}
68+
69+
private Properties propertiesB() {
70+
Properties properties = new Properties();
71+
properties.setProperty("property-1", "sample property");
72+
properties.setProperty("property-2", "another sample property");
73+
return properties;
74+
}
75+
76+
private void testMergedProperties(Properties globalProperties) {
77+
assertThat("There should be 4 properties", globalProperties.size(), equalTo(4));
78+
assertEquals("Property should be", globalProperties.getProperty("application.name"), "my-app");
79+
assertEquals("Property should be", globalProperties.getProperty("application.version"), "1.0");
80+
assertEquals("Property should be", globalProperties.getProperty("property-1"), "sample property");
81+
assertEquals("Property should be", globalProperties.getProperty("property-2"), "another sample property");
82+
}
83+
84+
}

‎core-kotlin/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
<version>3.3.0</version>
6262
<type>pom</type>
6363
</dependency>
64+
<dependency>
65+
<groupId>uy.kohesive.injekt</groupId>
66+
<artifactId>injekt-core</artifactId>
67+
<version>1.16.1</version>
68+
</dependency>
6469
</dependencies>
6570

6671
<properties>
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.baeldung.datamapping
22

33
data class User(
4-
val firstName: String,
5-
val lastName: String,
6-
val street: String,
7-
val houseNumber: String,
8-
val phone: String,
9-
val age: Int,
10-
val password: String)
4+
val firstName: String,
5+
val lastName: String,
6+
val street: String,
7+
val houseNumber: String,
8+
val phone: String,
9+
val age: Int,
10+
val password: String)

‎core-kotlin/src/main/kotlin/com/baeldung/datamapping/UserExtensions.kt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package com.baeldung.datamapping
33
import kotlin.reflect.full.memberProperties
44

55
fun User.toUserView() = UserView(
6-
name = "$firstName $lastName",
7-
address = "$street $houseNumber",
8-
telephone = phone,
9-
age = age
6+
name = "$firstName $lastName",
7+
address = "$street $houseNumber",
8+
telephone = phone,
9+
age = age
1010
)
1111

1212
fun User.toUserViewReflection() = with(::UserView) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.baeldung.datamapping
22

33
data class UserView(
4-
val name: String,
5-
val address: String,
6-
val telephone: String,
7-
val age: Int
4+
val name: String,
5+
val address: String,
6+
val telephone: String,
7+
val age: Int
88
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
import java.util.*
7+
8+
class DelegateInjectionApplication {
9+
companion object : InjektMain() {
10+
private val LOG = LoggerFactory.getLogger(DelegateInjectionApplication::class.java)
11+
@JvmStatic fun main(args: Array<String>) {
12+
DelegateInjectionApplication().run()
13+
}
14+
15+
override fun InjektRegistrar.registerInjectables() {
16+
addFactory {
17+
val value = FactoryInstance("Factory" + UUID.randomUUID().toString())
18+
LOG.info("Constructing instance: {}", value)
19+
value
20+
}
21+
22+
addSingletonFactory {
23+
val value = SingletonInstance("Singleton" + UUID.randomUUID().toString())
24+
LOG.info("Constructing singleton instance: {}", value)
25+
value
26+
}
27+
28+
addSingletonFactory { App() }
29+
}
30+
}
31+
32+
data class FactoryInstance(val value: String)
33+
data class SingletonInstance(val value: String)
34+
35+
class App {
36+
private val instance: FactoryInstance by injectValue()
37+
private val lazyInstance: FactoryInstance by injectLazy()
38+
private val singleton: SingletonInstance by injectValue()
39+
private val lazySingleton: SingletonInstance by injectLazy()
40+
41+
fun run() {
42+
for (i in 1..5) {
43+
LOG.info("Instance {}: {}", i, instance)
44+
}
45+
for (i in 1..5) {
46+
LOG.info("Lazy Instance {}: {}", i, lazyInstance)
47+
}
48+
for (i in 1..5) {
49+
LOG.info("Singleton {}: {}", i, singleton)
50+
}
51+
for (i in 1..5) {
52+
LOG.info("Lazy Singleton {}: {}", i, lazySingleton)
53+
}
54+
}
55+
}
56+
57+
fun run() {
58+
Injekt.get<App>().run()
59+
}
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
7+
class KeyedApplication {
8+
companion object : InjektMain() {
9+
private val LOG = LoggerFactory.getLogger(KeyedApplication::class.java)
10+
@JvmStatic fun main(args: Array<String>) {
11+
KeyedApplication().run()
12+
}
13+
14+
override fun InjektRegistrar.registerInjectables() {
15+
val configs = mapOf(
16+
"google" to Config("googleClientId", "googleClientSecret"),
17+
"twitter" to Config("twitterClientId", "twitterClientSecret")
18+
)
19+
addPerKeyFactory<Config, String> {key -> configs[key]!! }
20+
21+
addSingletonFactory { App() }
22+
}
23+
}
24+
25+
data class Config(val clientId: String, val clientSecret: String)
26+
27+
class App {
28+
fun run() {
29+
LOG.info("Google config: {}", Injekt.get<Config>("google"))
30+
LOG.info("Twitter config: {}", Injekt.get<Config>("twitter"))
31+
}
32+
}
33+
34+
fun run() {
35+
Injekt.get<App>().run()
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
7+
class ModularApplication {
8+
class ConfigModule(private val port: Int) : InjektModule {
9+
override fun InjektRegistrar.registerInjectables() {
10+
addSingleton(Config(port))
11+
}
12+
}
13+
14+
object ServerModule : InjektModule {
15+
override fun InjektRegistrar.registerInjectables() {
16+
addSingletonFactory { Server(Injekt.get()) }
17+
}
18+
}
19+
20+
companion object : InjektMain() {
21+
private val LOG = LoggerFactory.getLogger(Server::class.java)
22+
@JvmStatic fun main(args: Array<String>) {
23+
ModularApplication().run()
24+
}
25+
26+
override fun InjektRegistrar.registerInjectables() {
27+
importModule(ConfigModule(12345))
28+
importModule(ServerModule)
29+
}
30+
}
31+
32+
data class Config(
33+
val port: Int
34+
)
35+
36+
class Server(private val config: Config) {
37+
38+
fun start() {
39+
LOG.info("Starting server on ${config.port}")
40+
}
41+
}
42+
43+
fun run() {
44+
Injekt.get<Server>().start()
45+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
import java.util.*
7+
import java.util.concurrent.Executors
8+
import java.util.concurrent.TimeUnit
9+
10+
class PerThreadApplication {
11+
companion object : InjektMain() {
12+
private val LOG = LoggerFactory.getLogger(PerThreadApplication::class.java)
13+
@JvmStatic fun main(args: Array<String>) {
14+
PerThreadApplication().run()
15+
}
16+
17+
override fun InjektRegistrar.registerInjectables() {
18+
addPerThreadFactory {
19+
val value = FactoryInstance(UUID.randomUUID().toString())
20+
LOG.info("Constructing instance: {}", value)
21+
value
22+
}
23+
24+
addSingletonFactory { App() }
25+
}
26+
}
27+
28+
data class FactoryInstance(val value: String)
29+
30+
class App {
31+
fun run() {
32+
val threadPool = Executors.newFixedThreadPool(5)
33+
34+
for (i in 1..20) {
35+
threadPool.submit {
36+
val instance = Injekt.get<FactoryInstance>()
37+
LOG.info("Value for thread {}: {}", Thread.currentThread().id, instance)
38+
TimeUnit.MILLISECONDS.sleep(100)
39+
}
40+
}
41+
threadPool.awaitTermination(10, TimeUnit.SECONDS)
42+
}
43+
}
44+
45+
fun run() {
46+
Injekt.get<App>().run()
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
7+
class SimpleApplication {
8+
companion object : InjektMain() {
9+
private val LOG = LoggerFactory.getLogger(Server::class.java)
10+
@JvmStatic fun main(args: Array<String>) {
11+
SimpleApplication().run()
12+
}
13+
14+
override fun InjektRegistrar.registerInjectables() {
15+
addSingleton(Config(12345))
16+
addSingletonFactory { Server(Injekt.get()) }
17+
}
18+
}
19+
20+
data class Config(
21+
val port: Int
22+
)
23+
24+
class Server(private val config: Config) {
25+
26+
fun start() {
27+
LOG.info("Starting server on ${config.port}")
28+
}
29+
}
30+
31+
fun run() {
32+
Injekt.get<Server>().start()
33+
}
34+
}

0 commit comments

Comments
 (0)