-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ut for ExternalDataSourceProperties
- Loading branch information
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
.../com/alibaba/nacos/config/server/service/datasource/ExternalDataSourcePropertiesTest.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,113 @@ | ||
package com.alibaba.nacos.config.server.service.datasource; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
|
||
public class ExternalDataSourcePropertiesTest { | ||
|
||
public static final String JDBC_URL = | ||
"jdbc:mysql://127.0.0.1:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC"; | ||
public static final String PASSWORD = "nacos"; | ||
public static final String USERNAME = "nacos_devtest"; | ||
|
||
@Test | ||
public void external_datasource_normaly() { | ||
HikariDataSource expectedDataSource = new HikariDataSource(); | ||
expectedDataSource.setJdbcUrl(JDBC_URL); | ||
expectedDataSource.setUsername(USERNAME); | ||
expectedDataSource.setPassword(PASSWORD); | ||
MockEnvironment environment = new MockEnvironment(); | ||
environment.setProperty("db.num", "1"); | ||
environment.setProperty("db.user", USERNAME); | ||
environment.setProperty("db.password", PASSWORD); | ||
environment.setProperty("db.url.0", JDBC_URL); | ||
List<HikariDataSource> dataSources = new ExternalDataSourceProperties().build(environment, (dataSource -> { | ||
Assert.assertEquals(dataSource.getJdbcUrl(), expectedDataSource.getJdbcUrl()); | ||
Assert.assertEquals(dataSource.getUsername(), expectedDataSource.getUsername()); | ||
Assert.assertEquals(dataSource.getPassword(), expectedDataSource.getPassword()); | ||
|
||
})); | ||
Assert.assertEquals(dataSources.size(), 1); | ||
} | ||
|
||
@Test | ||
public void external_datasource_to_assert_multi_jdbc_url() { | ||
|
||
HikariDataSource expectedDataSource = new HikariDataSource(); | ||
expectedDataSource.setJdbcUrl(JDBC_URL); | ||
expectedDataSource.setUsername(USERNAME); | ||
expectedDataSource.setPassword(PASSWORD); | ||
MockEnvironment environment = new MockEnvironment(); | ||
environment.setProperty("db.num", "2"); | ||
environment.setProperty("db.user", USERNAME); | ||
environment.setProperty("db.password", PASSWORD); | ||
environment.setProperty("db.url.0", JDBC_URL); | ||
environment.setProperty("db.url.1", JDBC_URL); | ||
List<HikariDataSource> dataSources = new ExternalDataSourceProperties().build(environment, (dataSource -> { | ||
Assert.assertEquals(dataSource.getJdbcUrl(), expectedDataSource.getJdbcUrl()); | ||
Assert.assertEquals(dataSource.getUsername(), expectedDataSource.getUsername()); | ||
Assert.assertEquals(dataSource.getPassword(), expectedDataSource.getPassword()); | ||
|
||
})); | ||
Assert.assertEquals(dataSources.size(), 2); | ||
} | ||
|
||
@Test | ||
public void external_datasource_to_assert_multi_password_and_username() { | ||
|
||
HikariDataSource expectedDataSource = new HikariDataSource(); | ||
expectedDataSource.setJdbcUrl(JDBC_URL); | ||
expectedDataSource.setUsername(USERNAME); | ||
expectedDataSource.setPassword(PASSWORD); | ||
MockEnvironment environment = new MockEnvironment(); | ||
environment.setProperty("db.num", "2"); | ||
environment.setProperty("db.user.0", USERNAME); | ||
environment.setProperty("db.user.1", USERNAME); | ||
environment.setProperty("db.password.0", PASSWORD); | ||
environment.setProperty("db.password.1", PASSWORD); | ||
environment.setProperty("db.url.0", JDBC_URL); | ||
environment.setProperty("db.url.1", JDBC_URL); | ||
List<HikariDataSource> dataSources = new ExternalDataSourceProperties().build(environment, (dataSource -> { | ||
Assert.assertEquals(dataSource.getJdbcUrl(), expectedDataSource.getJdbcUrl()); | ||
Assert.assertEquals(dataSource.getUsername(), expectedDataSource.getUsername()); | ||
Assert.assertEquals(dataSource.getPassword(), expectedDataSource.getPassword()); | ||
|
||
})); | ||
Assert.assertEquals(dataSources.size(), 2); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void external_datasource_to_assert_illegal_argument() { | ||
|
||
MockEnvironment environment = new MockEnvironment(); | ||
new ExternalDataSourceProperties().build(environment, null); | ||
|
||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void external_datasource_to_assert_illegal_argument_2() { | ||
|
||
HikariDataSource expectedDataSource = new HikariDataSource(); | ||
expectedDataSource.setJdbcUrl(JDBC_URL); | ||
expectedDataSource.setUsername(USERNAME); | ||
expectedDataSource.setPassword(PASSWORD); | ||
MockEnvironment environment = new MockEnvironment(); | ||
// error num of db | ||
environment.setProperty("db.num", "2"); | ||
environment.setProperty("db.user", USERNAME); | ||
environment.setProperty("db.password", PASSWORD); | ||
environment.setProperty("db.url.0", JDBC_URL); | ||
List<HikariDataSource> dataSources = new ExternalDataSourceProperties().build(environment, (dataSource -> { | ||
Assert.assertEquals(dataSource.getJdbcUrl(), expectedDataSource.getJdbcUrl()); | ||
Assert.assertEquals(dataSource.getUsername(), expectedDataSource.getUsername()); | ||
Assert.assertEquals(dataSource.getPassword(), expectedDataSource.getPassword()); | ||
|
||
})); | ||
} | ||
|
||
} |