Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
superrain51
apollo
提交
76528a11
apollo
项目概览
superrain51
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
76528a11
编写于
4月 11, 2016
作者:
Y
Yiming Liu
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #73 from nobodyiam/config-client-refactor
refactor config client - add config repository
上级
b78fadf0
933b532b
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
441 addition
and
299 deletion
+441
-299
apollo-client/src/main/java/com/ctrip/apollo/internals/ConfigRepository.java
...ain/java/com/ctrip/apollo/internals/ConfigRepository.java
+3
-1
apollo-client/src/main/java/com/ctrip/apollo/internals/DefaultConfig.java
...c/main/java/com/ctrip/apollo/internals/DefaultConfig.java
+17
-109
apollo-client/src/main/java/com/ctrip/apollo/internals/LocalFileConfigRepository.java
...com/ctrip/apollo/internals/LocalFileConfigRepository.java
+131
-0
apollo-client/src/main/java/com/ctrip/apollo/internals/RemoteConfigRepository.java
...va/com/ctrip/apollo/internals/RemoteConfigRepository.java
+21
-21
apollo-client/src/main/java/com/ctrip/apollo/internals/SimpleConfig.java
...rc/main/java/com/ctrip/apollo/internals/SimpleConfig.java
+36
-0
apollo-client/src/main/java/com/ctrip/apollo/spi/DefaultConfigFactory.java
.../main/java/com/ctrip/apollo/spi/DefaultConfigFactory.java
+14
-9
apollo-client/src/test/java/com/ctrip/apollo/AllTests.java
apollo-client/src/test/java/com/ctrip/apollo/AllTests.java
+5
-3
apollo-client/src/test/java/com/ctrip/apollo/internals/DefaultConfigManagerTest.java
.../com/ctrip/apollo/internals/DefaultConfigManagerTest.java
+0
-1
apollo-client/src/test/java/com/ctrip/apollo/internals/DefaultConfigTest.java
...st/java/com/ctrip/apollo/internals/DefaultConfigTest.java
+12
-108
apollo-client/src/test/java/com/ctrip/apollo/internals/LocalFileConfigRepositoryTest.java
...ctrip/apollo/internals/LocalFileConfigRepositoryTest.java
+130
-0
apollo-client/src/test/java/com/ctrip/apollo/internals/RemoteConfigRepositoryTest.java
...om/ctrip/apollo/internals/RemoteConfigRepositoryTest.java
+16
-38
apollo-client/src/test/java/com/ctrip/apollo/internals/SimpleConfigTest.java
...est/java/com/ctrip/apollo/internals/SimpleConfigTest.java
+48
-0
apollo-client/src/test/java/com/ctrip/apollo/spi/DefaultConfigFactoryManagerTest.java
...com/ctrip/apollo/spi/DefaultConfigFactoryManagerTest.java
+0
-1
apollo-client/src/test/java/com/ctrip/apollo/spi/DefaultConfigFactoryTest.java
...t/java/com/ctrip/apollo/spi/DefaultConfigFactoryTest.java
+6
-6
apollo-client/src/test/java/com/ctrip/apollo/spi/DefaultConfigRegistryTest.java
.../java/com/ctrip/apollo/spi/DefaultConfigRegistryTest.java
+2
-2
未找到文件。
apollo-client/src/main/java/com/ctrip/apollo/internals/Config
Loader
.java
→
apollo-client/src/main/java/com/ctrip/apollo/internals/Config
Repository
.java
浏览文件 @
76528a11
...
...
@@ -5,6 +5,8 @@ import java.util.Properties;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
interface
Config
Loader
{
public
interface
Config
Repository
{
public
Properties
loadConfig
();
public
void
setFallback
(
ConfigRepository
fallbackConfigRepository
);
}
apollo-client/src/main/java/com/ctrip/apollo/internals/DefaultConfig.java
浏览文件 @
76528a11
...
...
@@ -2,36 +2,36 @@ package com.ctrip.apollo.internals;
import
com.ctrip.apollo.Config
;
import
com.ctrip.apollo.core.utils.ClassLoaderUtil
;
import
com.ctrip.apollo.util.ConfigUtil
;
import
com.dianping.cat.Cat
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.util.Properties
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
DefaultConfig
implements
Config
,
ConfigLoader
{
public
class
DefaultConfig
implements
Config
{
private
final
String
m_namespace
;
private
final
File
m_baseDir
;
private
Properties
m_resourceProperties
;
private
Properties
m_fileProperties
;
private
ConfigLoader
m_fallbackLoader
;
private
ConfigUtil
m_configUtil
;
private
Properties
m_configProperties
;
private
ConfigRepository
m_configRepository
;
public
DefaultConfig
(
File
baseDir
,
String
namespace
,
ConfigLoader
fallbackLoader
,
ConfigUtil
configUtil
)
{
public
DefaultConfig
(
String
namespace
,
ConfigRepository
configRepository
)
{
m_namespace
=
namespace
;
m_baseDir
=
baseDir
;
m_resourceProperties
=
loadFromResource
(
m_namespace
);
m_fallbackLoader
=
fallbackLoader
;
m_configUtil
=
configUtil
;
this
.
initLocalConfig
();
m_configRepository
=
configRepository
;
initialize
();
}
private
void
initialize
()
{
try
{
m_configProperties
=
m_configRepository
.
loadConfig
();
}
catch
(
Throwable
ex
)
{
throw
new
RuntimeException
(
String
.
format
(
"Init Apollo Local Config failed - namespace: %s"
,
m_namespace
),
ex
);
}
}
@Override
...
...
@@ -41,7 +41,7 @@ public class DefaultConfig implements Config, ConfigLoader {
// step 2: check local cached properties file
if
(
value
==
null
)
{
value
=
m_
file
Properties
.
getProperty
(
key
);
value
=
m_
config
Properties
.
getProperty
(
key
);
}
/**
...
...
@@ -87,97 +87,5 @@ public class DefaultConfig implements Config, ConfigLoader {
return
properties
;
}
void
initLocalConfig
()
{
m_fileProperties
=
this
.
loadFromLocalCacheFile
(
m_baseDir
,
m_namespace
);
//TODO check if local file is expired
if
(
m_fileProperties
!=
null
)
{
return
;
}
if
(
m_fallbackLoader
!=
null
)
{
m_fileProperties
=
m_fallbackLoader
.
loadConfig
();
}
if
(
m_fileProperties
==
null
)
{
throw
new
RuntimeException
(
String
.
format
(
"Init Apollo Local Config failed - namespace: %s"
,
m_namespace
));
}
persistLocalCacheFile
(
m_baseDir
,
m_namespace
);
}
private
Properties
loadFromLocalCacheFile
(
File
baseDir
,
String
namespace
)
{
if
(
baseDir
==
null
)
{
return
null
;
}
File
file
=
assembleLocalCacheFile
(
baseDir
,
namespace
);
Properties
properties
=
null
;
if
(
file
.
isFile
()
&&
file
.
canRead
())
{
InputStream
in
=
null
;
try
{
in
=
new
FileInputStream
(
file
);
properties
=
new
Properties
();
properties
.
load
(
in
);
}
catch
(
IOException
e
)
{
Cat
.
logError
(
e
);
}
finally
{
try
{
if
(
in
!=
null
)
{
in
.
close
();
}
}
catch
(
IOException
e
)
{
// ignore
}
}
}
else
{
//TODO error handling
}
return
properties
;
}
void
persistLocalCacheFile
(
File
baseDir
,
String
namespace
)
{
if
(
baseDir
==
null
)
{
return
;
}
File
file
=
assembleLocalCacheFile
(
baseDir
,
namespace
);
OutputStream
out
=
null
;
try
{
out
=
new
FileOutputStream
(
file
);
m_fileProperties
.
store
(
out
,
"Persisted by DefaultConfig"
);
}
catch
(
FileNotFoundException
ex
)
{
Cat
.
logError
(
ex
);
}
catch
(
IOException
ex
)
{
Cat
.
logError
(
ex
);
}
finally
{
if
(
out
!=
null
)
{
try
{
out
.
close
();
}
catch
(
IOException
e
)
{
//ignore
}
}
}
}
File
assembleLocalCacheFile
(
File
baseDir
,
String
namespace
)
{
String
fileName
=
String
.
format
(
"%s-%s-%s.properties"
,
m_configUtil
.
getAppId
(),
m_configUtil
.
getCluster
(),
namespace
);
return
new
File
(
baseDir
,
fileName
);
}
@Override
public
Properties
loadConfig
()
{
Properties
result
=
new
Properties
();
result
.
putAll
(
m_fileProperties
);
return
result
;
}
public
ConfigLoader
getFallbackLoader
()
{
return
m_fallbackLoader
;
}
}
apollo-client/src/main/java/com/ctrip/apollo/internals/LocalFileConfigRepository.java
0 → 100644
浏览文件 @
76528a11
package
com.ctrip.apollo.internals
;
import
com.google.common.base.Preconditions
;
import
com.ctrip.apollo.util.ConfigUtil
;
import
com.dianping.cat.Cat
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.util.Properties
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
LocalFileConfigRepository
implements
ConfigRepository
{
private
final
String
m_namespace
;
private
final
File
m_baseDir
;
private
final
ConfigUtil
m_configUtil
;
private
Properties
m_fileProperties
;
private
ConfigRepository
m_fallback
;
public
LocalFileConfigRepository
(
File
baseDir
,
String
namespace
,
ConfigUtil
configUtil
)
{
m_baseDir
=
baseDir
;
m_namespace
=
namespace
;
m_configUtil
=
configUtil
;
}
@Override
public
Properties
loadConfig
()
{
if
(
m_fileProperties
==
null
)
{
initLocalConfig
();
}
Properties
result
=
new
Properties
();
result
.
putAll
(
m_fileProperties
);
return
result
;
}
@Override
public
void
setFallback
(
ConfigRepository
fallbackConfigRepository
)
{
m_fallback
=
fallbackConfigRepository
;
}
void
initLocalConfig
()
{
if
(
m_fallback
!=
null
)
{
try
{
m_fileProperties
=
m_fallback
.
loadConfig
();
//TODO register change listener
persistLocalCacheFile
(
m_baseDir
,
m_namespace
);
return
;
}
catch
(
Throwable
ex
)
{
Cat
.
logError
(
ex
);
}
}
try
{
m_fileProperties
=
this
.
loadFromLocalCacheFile
(
m_baseDir
,
m_namespace
);
}
catch
(
IOException
ex
)
{
throw
new
RuntimeException
(
"Loading config from local cache file failed"
,
ex
);
}
}
private
Properties
loadFromLocalCacheFile
(
File
baseDir
,
String
namespace
)
throws
IOException
{
Preconditions
.
checkNotNull
(
baseDir
,
"Basedir cannot be null"
);
File
file
=
assembleLocalCacheFile
(
baseDir
,
namespace
);
Properties
properties
=
null
;
if
(
file
.
isFile
()
&&
file
.
canRead
())
{
InputStream
in
=
null
;
try
{
in
=
new
FileInputStream
(
file
);
properties
=
new
Properties
();
properties
.
load
(
in
);
}
catch
(
IOException
e
)
{
Cat
.
logError
(
e
);
throw
e
;
}
finally
{
try
{
if
(
in
!=
null
)
{
in
.
close
();
}
}
catch
(
IOException
e
)
{
// ignore
}
}
}
else
{
//TODO error handling
}
return
properties
;
}
void
persistLocalCacheFile
(
File
baseDir
,
String
namespace
)
{
if
(
baseDir
==
null
)
{
return
;
}
File
file
=
assembleLocalCacheFile
(
baseDir
,
namespace
);
OutputStream
out
=
null
;
try
{
out
=
new
FileOutputStream
(
file
);
m_fileProperties
.
store
(
out
,
"Persisted by DefaultConfig"
);
}
catch
(
FileNotFoundException
ex
)
{
Cat
.
logError
(
ex
);
}
catch
(
IOException
ex
)
{
Cat
.
logError
(
ex
);
}
finally
{
if
(
out
!=
null
)
{
try
{
out
.
close
();
}
catch
(
IOException
e
)
{
//ignore
}
}
}
}
File
assembleLocalCacheFile
(
File
baseDir
,
String
namespace
)
{
String
fileName
=
String
.
format
(
"%s-%s-%s.properties"
,
m_configUtil
.
getAppId
(),
m_configUtil
.
getCluster
(),
namespace
);
return
new
File
(
baseDir
,
fileName
);
}
}
apollo-client/src/main/java/com/ctrip/apollo/internals/RemoteConfig.java
→
apollo-client/src/main/java/com/ctrip/apollo/internals/RemoteConfig
Repository
.java
浏览文件 @
76528a11
...
...
@@ -2,7 +2,6 @@ package com.ctrip.apollo.internals;
import
com.google.common.collect.Maps
;
import
com.ctrip.apollo.Config
;
import
com.ctrip.apollo.core.dto.ApolloConfig
;
import
com.ctrip.apollo.core.dto.ServiceDTO
;
import
com.ctrip.apollo.util.ConfigUtil
;
...
...
@@ -23,44 +22,45 @@ import java.util.Properties;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
RemoteConfig
implements
Config
,
ConfigLoader
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RemoteConfig
.
class
);
public
class
RemoteConfig
Repository
implements
ConfigRepository
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RemoteConfig
Repository
.
class
);
private
RestTemplate
m_restTemplate
;
private
ConfigServiceLocator
m_serviceLocator
;
private
String
m_namespace
;
private
ConfigUtil
m_configUtil
;
private
Properties
m_remoteProperties
;
private
String
m_namespace
;
public
RemoteConfig
(
RestTemplate
restTemplate
,
ConfigServiceLocator
locator
,
String
namespace
,
ConfigUtil
configUtil
)
{
this
.
m_restTemplate
=
restTemplate
;
this
.
m_serviceLocator
=
locator
;
this
.
m_namespace
=
namespace
;
this
.
m_configUtil
=
configUtil
;
this
.
initialize
();
}
@Override
public
String
getProperty
(
String
key
,
String
defaultValue
)
{
return
this
.
m_remoteProperties
.
getProperty
(
key
,
defaultValue
);
public
RemoteConfigRepository
(
RestTemplate
restTemplate
,
ConfigServiceLocator
serviceLocator
,
ConfigUtil
configUtil
,
String
namespace
)
{
m_restTemplate
=
restTemplate
;
m_serviceLocator
=
serviceLocator
;
m_configUtil
=
configUtil
;
m_namespace
=
namespace
;
}
@Override
public
Properties
loadConfig
()
{
if
(
m_remoteProperties
==
null
)
{
return
null
;
initRemoteConfig
()
;
}
Properties
result
=
new
Properties
();
result
.
putAll
(
m_remoteProperties
);
return
result
;
}
private
void
initialize
()
{
@Override
public
void
setFallback
(
ConfigRepository
fallbackConfigRepository
)
{
//remote config doesn't need fallback
}
private
void
initRemoteConfig
()
{
ApolloConfig
apolloConfig
=
this
.
loadApolloConfig
();
m_remoteProperties
=
new
Properties
();
m_remoteProperties
.
putAll
(
apolloConfig
.
getConfigurations
());
}
private
ApolloConfig
loadApolloConfig
()
{
String
appId
=
m_configUtil
.
getAppId
();
String
cluster
=
m_configUtil
.
getCluster
();
...
...
@@ -82,9 +82,10 @@ public class RemoteConfig implements Config, ConfigLoader {
}
}
private
ApolloConfig
getRemoteConfig
(
RestTemplate
restTemplate
,
String
uri
,
String
appId
,
String
cluster
,
String
namespace
,
ApolloConfig
previousConfig
)
{
String
appId
,
String
cluster
,
String
namespace
,
ApolloConfig
previousConfig
)
{
logger
.
info
(
"Loading config from {}, appId={}, cluster={}, namespace={}"
,
uri
,
appId
,
cluster
,
namespace
);
...
...
@@ -136,5 +137,4 @@ public class RemoteConfig implements Config, ConfigLoader {
}
return
services
.
get
(
0
).
getHomepageUrl
();
}
}
apollo-client/src/main/java/com/ctrip/apollo/internals/SimpleConfig.java
0 → 100644
浏览文件 @
76528a11
package
com.ctrip.apollo.internals
;
import
com.ctrip.apollo.Config
;
import
java.util.Properties
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
SimpleConfig
implements
Config
{
private
String
m_namespace
;
private
ConfigRepository
m_configRepository
;
private
Properties
m_configProperties
;
public
SimpleConfig
(
String
namespace
,
ConfigRepository
configRepository
)
{
m_namespace
=
namespace
;
m_configRepository
=
configRepository
;
this
.
initialize
();
}
private
void
initialize
()
{
try
{
m_configProperties
=
m_configRepository
.
loadConfig
();
}
catch
(
Throwable
ex
)
{
throw
new
RuntimeException
(
String
.
format
(
"Init Apollo Remote Config failed - namespace: %s"
,
m_namespace
),
ex
);
}
}
@Override
public
String
getProperty
(
String
key
,
String
defaultValue
)
{
return
this
.
m_configProperties
.
getProperty
(
key
,
defaultValue
);
}
}
apollo-client/src/main/java/com/ctrip/apollo/spi/DefaultConfigFactory.java
浏览文件 @
76528a11
...
...
@@ -2,9 +2,8 @@ package com.ctrip.apollo.spi;
import
com.ctrip.apollo.Config
;
import
com.ctrip.apollo.core.utils.ClassLoaderUtil
;
import
com.ctrip.apollo.internals.ConfigServiceLocator
;
import
com.ctrip.apollo.internals.DefaultConfig
;
import
com.ctrip.apollo.internals.RemoteConfig
;
import
com.ctrip.apollo.internals.*
;
import
com.ctrip.apollo.internals.RemoteConfigRepository
;
import
com.ctrip.apollo.util.ConfigUtil
;
import
org.springframework.web.client.RestTemplate
;
...
...
@@ -29,14 +28,20 @@ public class DefaultConfigFactory implements ConfigFactory {
@Override
public
Config
create
(
String
namespace
)
{
RemoteConfig
remoteConfig
=
this
.
createRemoteConfig
(
namespace
);
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
m_baseDir
,
namespace
,
remoteConfig
,
ConfigUtil
.
getInstance
());
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
namespace
,
createLocalConfigRepository
(
namespace
));
return
defaultConfig
;
}
public
RemoteConfig
createRemoteConfig
(
String
namespace
)
{
return
new
RemoteConfig
(
new
RestTemplate
(),
new
ConfigServiceLocator
(),
namespace
,
ConfigUtil
.
getInstance
());
LocalFileConfigRepository
createLocalConfigRepository
(
String
namespace
)
{
LocalFileConfigRepository
localFileConfigLoader
=
new
LocalFileConfigRepository
(
m_baseDir
,
namespace
,
ConfigUtil
.
getInstance
());
localFileConfigLoader
.
setFallback
(
createRemoteConfigRepository
(
namespace
));
return
localFileConfigLoader
;
}
RemoteConfigRepository
createRemoteConfigRepository
(
String
namespace
)
{
return
new
RemoteConfigRepository
(
new
RestTemplate
(),
new
ConfigServiceLocator
(),
ConfigUtil
.
getInstance
(),
namespace
);
}
}
apollo-client/src/test/java/com/ctrip/apollo/AllTests.java
浏览文件 @
76528a11
...
...
@@ -3,7 +3,9 @@ package com.ctrip.apollo;
import
com.ctrip.apollo.internals.DefaultConfigManagerTest
;
import
com.ctrip.apollo.internals.DefaultConfigTest
;
import
com.ctrip.apollo.internals.RemoteConfigTest
;
import
com.ctrip.apollo.internals.LocalFileConfigRepositoryTest
;
import
com.ctrip.apollo.internals.RemoteConfigRepositoryTest
;
import
com.ctrip.apollo.internals.SimpleConfigTest
;
import
com.ctrip.apollo.spi.DefaultConfigFactoryManagerTest
;
import
com.ctrip.apollo.spi.DefaultConfigFactoryTest
;
import
com.ctrip.apollo.spi.DefaultConfigRegistryTest
;
...
...
@@ -15,8 +17,8 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith
(
Suite
.
class
)
@SuiteClasses
({
ConfigServiceTest
.
class
,
DefaultConfigRegistryTest
.
class
,
DefaultConfigFactoryManagerTest
.
class
,
DefaultConfig
FactoryTest
.
class
,
DefaultConfigManagerTest
.
class
,
DefaultConfig
Test
.
class
,
RemoteConfigTest
.
class
DefaultConfig
ManagerTest
.
class
,
DefaultConfigTest
.
class
,
LocalFileConfigRepository
Test
.
class
,
RemoteConfig
RepositoryTest
.
class
,
SimpleConfigTest
.
class
,
DefaultConfigFactory
Test
.
class
})
public
class
AllTests
{
...
...
apollo-client/src/test/java/com/ctrip/apollo/internals/DefaultConfigManagerTest.java
浏览文件 @
76528a11
...
...
@@ -11,7 +11,6 @@ import org.unidal.lookup.ComponentTestCase;
import
static
org
.
hamcrest
.
core
.
IsEqual
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* @author Jason Song(song_s@ctrip.com)
...
...
apollo-client/src/test/java/com/ctrip/apollo/internals/DefaultConfigTest.java
浏览文件 @
76528a11
...
...
@@ -4,59 +4,37 @@ import com.google.common.base.Charsets;
import
com.google.common.io.Files
;
import
com.ctrip.apollo.core.utils.ClassLoaderUtil
;
import
com.ctrip.apollo.util.ConfigUtil
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.unidal.lookup.ComponentTestCase
;
import
java.io.File
;
import
java.util.Properties
;
import
static
org
.
hamcrest
.
core
.
IsEqual
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
DefaultConfigTest
extends
ComponentTestCase
{
private
File
someBaseDir
;
public
class
DefaultConfigTest
{
private
File
someResourceDir
;
private
String
someNamespace
;
private
Config
Loader
fallbackLoader
;
private
Config
Repository
configRepository
;
private
Properties
someProperties
;
private
ConfigUtil
someConfigUtil
;
@Before
public
void
setUp
()
throws
Exception
{
super
.
setUp
();
someBaseDir
=
new
File
(
"src/test/resources/config-cache"
);
someBaseDir
.
mkdir
();
someResourceDir
=
new
File
(
ClassLoaderUtil
.
getClassPath
()
+
"/META-INF/config"
);
someResourceDir
.
mkdir
();
someNamespace
=
"someName"
;
someProperties
=
new
Properties
();
someProperties
.
setProperty
(
"defaultKey"
,
"defaultValue"
);
fallbackLoader
=
mock
(
RemoteConfig
.
class
);
when
(
fallbackLoader
.
loadConfig
()).
thenReturn
(
someProperties
);
String
someAppId
=
"someApp"
;
String
someCluster
=
"someCluster"
;
someConfigUtil
=
mock
(
ConfigUtil
.
class
);
when
(
someConfigUtil
.
getAppId
()).
thenReturn
(
someAppId
);
when
(
someConfigUtil
.
getCluster
()).
thenReturn
(
someCluster
);
configRepository
=
mock
(
ConfigRepository
.
class
);
}
@Override
@After
public
void
tearDown
()
throws
Exception
{
recursiveDelete
(
someBaseDir
);
recursiveDelete
(
someResourceDir
);
}
...
...
@@ -73,47 +51,6 @@ public class DefaultConfigTest extends ComponentTestCase {
file
.
delete
();
}
private
String
assembleLocalCacheFileName
()
{
return
String
.
format
(
"%s-%s-%s.properties"
,
someConfigUtil
.
getAppId
(),
someConfigUtil
.
getCluster
(),
someNamespace
);
}
@Test
public
void
testGetPropertyWithLocalFile
()
throws
Exception
{
File
file
=
new
File
(
someBaseDir
,
assembleLocalCacheFileName
());
String
someKey
=
"someKey"
;
String
someValue
=
"someValue"
;
Files
.
write
(
someKey
+
"="
+
someValue
,
file
,
Charsets
.
UTF_8
);
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someBaseDir
,
someNamespace
,
fallbackLoader
,
someConfigUtil
);
file
.
delete
();
assertEquals
(
someValue
,
defaultConfig
.
getProperty
(
someKey
,
null
));
}
@Test
public
void
testGetPropertyWithLocalResource
()
throws
Exception
{
File
file
=
new
File
(
someResourceDir
,
someNamespace
+
".properties"
);
file
.
deleteOnExit
();
Files
.
createParentDirs
(
file
);
String
someKey
=
"someKey"
;
String
someValue
=
"someValue"
;
Files
.
write
(
someKey
+
"="
+
someValue
,
file
,
Charsets
.
UTF_8
);
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someBaseDir
,
someNamespace
,
fallbackLoader
,
someConfigUtil
);
file
.
delete
();
assertEquals
(
someValue
,
defaultConfig
.
getProperty
(
someKey
,
null
));
}
@Test
public
void
testGetPropertyWithAllPropertyHierarchy
()
throws
Exception
{
String
someKey
=
"someKey"
;
...
...
@@ -128,11 +65,11 @@ public class DefaultConfigTest extends ComponentTestCase {
//set up system property
System
.
setProperty
(
someKey
,
someSystemPropertyValue
);
//set up
local file
File
localCacheFile
=
new
File
(
someBaseDir
,
assembleLocalCacheFileName
()
);
Files
.
write
(
someKey
+
"="
+
someLocalFileValue
,
localCacheFile
,
Charsets
.
UTF_8
);
Files
.
append
(
System
.
getProperty
(
"line.separator"
),
localCacheFile
,
Charsets
.
UTF_8
);
Files
.
append
(
anotherKey
+
"="
+
someLocalFileValue
,
localCacheFile
,
Charsets
.
UTF_8
);
//set up
config repo
someProperties
=
new
Properties
(
);
someProperties
.
setProperty
(
someKey
,
someLocalFileValue
);
someProperties
.
setProperty
(
anotherKey
,
someLocalFileValue
);
when
(
configRepository
.
loadConfig
()).
thenReturn
(
someProperties
);
//set up resource file
File
resourceFile
=
new
File
(
someResourceDir
,
someNamespace
+
".properties"
);
...
...
@@ -143,52 +80,19 @@ public class DefaultConfigTest extends ComponentTestCase {
Files
.
append
(
lastKey
+
"="
+
someResourceValue
,
resourceFile
,
Charsets
.
UTF_8
);
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someBaseDir
,
someNamespace
,
fallbackLoader
,
someConfigUtil
);
defaultConfig
=
new
DefaultConfig
(
someNamespace
,
configRepository
);
String
someKeyValue
=
defaultConfig
.
getProperty
(
someKey
,
null
);
String
anotherKeyValue
=
defaultConfig
.
getProperty
(
anotherKey
,
null
);
String
lastKeyValue
=
defaultConfig
.
getProperty
(
lastKey
,
null
);
localCacheFile
.
delete
();
resourceFile
.
delete
(
);
//clean up
System
.
clearProperty
(
someKey
);
assertEquals
(
someSystemPropertyValue
,
someKeyValue
);
assertEquals
(
someLocalFileValue
,
anotherKeyValue
);
assertEquals
(
someResourceValue
,
lastKeyValue
);
}
@Test
public
void
testInitLocalConfigWithNoLocalFile
()
throws
Exception
{
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someBaseDir
,
someNamespace
,
fallbackLoader
,
someConfigUtil
);
Properties
result
=
defaultConfig
.
loadConfig
();
assertThat
(
"Default config's properties should be the same as fallback loader's when there is no local cache"
,
result
.
entrySet
(),
equalTo
(
someProperties
.
entrySet
()));
}
@Test
public
void
testInitLocalConfigWithNoLocalFileMultipleTimes
()
throws
Exception
{
Properties
anotherProperties
=
new
Properties
();
anotherProperties
.
setProperty
(
"anotherKey"
,
"anotherValue"
);
ConfigLoader
anotherLoader
=
mock
(
RemoteConfig
.
class
);
when
(
anotherLoader
.
loadConfig
()).
thenReturn
(
anotherProperties
);
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someBaseDir
,
someNamespace
,
fallbackLoader
,
someConfigUtil
);
DefaultConfig
anotherConfig
=
new
DefaultConfig
(
someBaseDir
,
someNamespace
,
anotherLoader
,
someConfigUtil
);
assertThat
(
"Default config should persist local cache files and return that afterwards"
,
defaultConfig
.
loadConfig
().
entrySet
(),
equalTo
(
anotherConfig
.
loadConfig
().
entrySet
()));
}
}
apollo-client/src/test/java/com/ctrip/apollo/internals/LocalFileConfigRepositoryTest.java
0 → 100644
浏览文件 @
76528a11
package
com.ctrip.apollo.internals
;
import
com.google.common.base.Charsets
;
import
com.google.common.io.Files
;
import
com.ctrip.apollo.util.ConfigUtil
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.io.File
;
import
java.util.Properties
;
import
static
org
.
hamcrest
.
core
.
IsEqual
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* Created by Jason on 4/9/16.
*/
public
class
LocalFileConfigRepositoryTest
{
private
File
someBaseDir
;
private
String
someNamespace
;
private
ConfigRepository
fallbackRepo
;
private
Properties
someProperties
;
private
ConfigUtil
someConfigUtil
;
@Before
public
void
setUp
()
throws
Exception
{
someBaseDir
=
new
File
(
"src/test/resources/config-cache"
);
someBaseDir
.
mkdir
();
someNamespace
=
"someName"
;
someProperties
=
new
Properties
();
someProperties
.
setProperty
(
"defaultKey"
,
"defaultValue"
);
fallbackRepo
=
mock
(
ConfigRepository
.
class
);
when
(
fallbackRepo
.
loadConfig
()).
thenReturn
(
someProperties
);
String
someAppId
=
"someApp"
;
String
someCluster
=
"someCluster"
;
someConfigUtil
=
mock
(
ConfigUtil
.
class
);
when
(
someConfigUtil
.
getAppId
()).
thenReturn
(
someAppId
);
when
(
someConfigUtil
.
getCluster
()).
thenReturn
(
someCluster
);
}
@After
public
void
tearDown
()
throws
Exception
{
recursiveDelete
(
someBaseDir
);
}
//helper method to clean created files
private
void
recursiveDelete
(
File
file
)
{
if
(!
file
.
exists
())
{
return
;
}
if
(
file
.
isDirectory
())
{
for
(
File
f
:
file
.
listFiles
())
{
recursiveDelete
(
f
);
}
}
file
.
delete
();
}
private
String
assembleLocalCacheFileName
()
{
return
String
.
format
(
"%s-%s-%s.properties"
,
someConfigUtil
.
getAppId
(),
someConfigUtil
.
getCluster
(),
someNamespace
);
}
@Test
public
void
testLoadConfig
()
throws
Exception
{
}
@Test
public
void
testLoadConfigWithLocalFile
()
throws
Exception
{
File
file
=
new
File
(
someBaseDir
,
assembleLocalCacheFileName
());
String
someKey
=
"someKey"
;
String
someValue
=
"someValue"
;
Files
.
write
(
someKey
+
"="
+
someValue
,
file
,
Charsets
.
UTF_8
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someBaseDir
,
someNamespace
,
someConfigUtil
);
Properties
properties
=
localRepo
.
loadConfig
();
assertEquals
(
someValue
,
properties
.
getProperty
(
someKey
));
}
@Test
public
void
testLoadConfigWithNoLocalFile
()
throws
Exception
{
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someBaseDir
,
someNamespace
,
someConfigUtil
);
localFileConfigRepository
.
setFallback
(
fallbackRepo
);
Properties
result
=
localFileConfigRepository
.
loadConfig
();
assertThat
(
"LocalFileConfigRepository's properties should be the same as fallback repo's when there is no local cache"
,
result
.
entrySet
(),
equalTo
(
someProperties
.
entrySet
()));
}
@Test
public
void
testLoadConfigWithNoLocalFileMultipleTimes
()
throws
Exception
{
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someBaseDir
,
someNamespace
,
someConfigUtil
);
localRepo
.
setFallback
(
fallbackRepo
);
Properties
someProperties
=
localRepo
.
loadConfig
();
LocalFileConfigRepository
anotherLocalRepoWithNoFallback
=
new
LocalFileConfigRepository
(
someBaseDir
,
someNamespace
,
someConfigUtil
);
Properties
anotherProperties
=
anotherLocalRepoWithNoFallback
.
loadConfig
();
assertThat
(
"LocalFileConfigRepository should persist local cache files and return that afterwards"
,
someProperties
.
entrySet
(),
equalTo
(
anotherProperties
.
entrySet
()));
}
}
\ No newline at end of file
apollo-client/src/test/java/com/ctrip/apollo/internals/RemoteConfigTest.java
→
apollo-client/src/test/java/com/ctrip/apollo/internals/RemoteConfig
Repository
Test.java
浏览文件 @
76528a11
...
...
@@ -22,18 +22,18 @@ import java.util.Map;
import
java.util.Properties
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
M
atchers
.
any
;
import
static
org
.
mockito
.
M
atchers
.
anyMap
;
import
static
org
.
mockito
.
M
atchers
.
anyString
;
import
static
org
.
mockito
.
M
atchers
.
eq
;
import
static
org
.
mockito
.
M
ockito
.
any
;
import
static
org
.
mockito
.
M
ockito
.
anyMap
;
import
static
org
.
mockito
.
M
ockito
.
anyString
;
import
static
org
.
mockito
.
M
ockito
.
eq
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
*
@author Jason Song(song_s@ctrip.com)
*
Created by Jason on 4/9/16.
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
RemoteConfigTest
{
public
class
RemoteConfig
Repository
Test
{
@Mock
private
RestTemplate
restTemplate
;
@Mock
...
...
@@ -47,7 +47,6 @@ public class RemoteConfigTest {
@Before
public
void
setUp
()
throws
Exception
{
someNamespace
=
"someName"
;
String
someServerUrl
=
"http://someServer"
;
mockConfigServiceLocator
(
someServerUrl
);
...
...
@@ -58,11 +57,9 @@ public class RemoteConfigTest {
}
@Test
public
void
test
GetProperty
()
throws
Exception
{
public
void
test
LoadConfig
()
throws
Exception
{
String
someKey
=
"someKey"
;
String
someValue
=
"someValue"
;
String
someKeyNotExisted
=
"key-not-existed"
;
String
someDefaultValue
=
"someDefault"
;
Map
<
String
,
String
>
configurations
=
Maps
.
newHashMap
();
configurations
.
put
(
someKey
,
someValue
);
ApolloConfig
someApolloConfig
=
assembleApolloConfig
(
configurations
);
...
...
@@ -70,12 +67,13 @@ public class RemoteConfigTest {
when
(
someResponse
.
getStatusCode
()).
thenReturn
(
HttpStatus
.
OK
);
when
(
someResponse
.
getBody
()).
thenReturn
(
someApolloConfig
);
when
(
restTemplate
.
exchange
(
anyString
(),
eq
(
HttpMethod
.
GET
),
any
(
HttpEntity
.
class
),
eq
(
ApolloConfig
.
class
),
anyMap
())).
thenReturn
(
someResponse
);
eq
(
ApolloConfig
.
class
),
anyMap
())).
thenReturn
(
someResponse
);
RemoteConfig
remoteConfig
=
new
RemoteConfig
(
restTemplate
,
configServiceLocator
,
someNamespace
,
someConfigUtil
);
RemoteConfigRepository
remoteConfigRepository
=
new
RemoteConfigRepository
(
restTemplate
,
configServiceLocator
,
someConfigUtil
,
someNamespace
);
Properties
config
=
remoteConfigRepository
.
loadConfig
();
assertEquals
(
someValue
,
remoteConfig
.
getProperty
(
someKey
,
null
));
assertEquals
(
someDefaultValue
,
remoteConfig
.
getProperty
(
someKeyNotExisted
,
someDefaultValue
));
assertEquals
(
configurations
,
config
);
}
@Test
(
expected
=
RuntimeException
.
class
)
...
...
@@ -83,30 +81,10 @@ public class RemoteConfigTest {
when
(
someResponse
.
getStatusCode
()).
thenReturn
(
HttpStatus
.
INTERNAL_SERVER_ERROR
);
when
(
restTemplate
.
exchange
(
anyString
(),
eq
(
HttpMethod
.
GET
),
any
(
HttpEntity
.
class
),
eq
(
ApolloConfig
.
class
),
anyMap
())).
thenReturn
(
someResponse
);
new
RemoteConfig
(
restTemplate
,
configServiceLocator
,
someNamespace
,
someConfigUtil
);
}
@Test
public
void
testLoadConfig
()
throws
Exception
{
String
someKey
=
"someKey"
;
String
someValue
=
"someValue"
;
Map
<
String
,
String
>
configurations
=
Maps
.
newHashMap
();
configurations
.
put
(
someKey
,
someValue
);
ApolloConfig
someApolloConfig
=
assembleApolloConfig
(
configurations
);
when
(
someResponse
.
getStatusCode
()).
thenReturn
(
HttpStatus
.
OK
);
when
(
someResponse
.
getBody
()).
thenReturn
(
someApolloConfig
);
when
(
restTemplate
.
exchange
(
anyString
(),
eq
(
HttpMethod
.
GET
),
any
(
HttpEntity
.
class
),
eq
(
ApolloConfig
.
class
),
anyMap
())).
thenReturn
(
someResponse
);
RemoteConfig
remoteConfig
=
new
RemoteConfig
(
restTemplate
,
configServiceLocator
,
someNamespace
,
someConfigUtil
);
eq
(
ApolloConfig
.
class
),
anyMap
())).
thenReturn
(
someResponse
);
Properties
config
=
remoteConfig
.
loadConfig
();
assertEquals
(
configurations
,
config
);
RemoteConfigRepository
remoteConfigRepository
=
new
RemoteConfigRepository
(
restTemplate
,
configServiceLocator
,
someConfigUtil
,
someNamespace
);
remoteConfigRepository
.
loadConfig
();
}
private
ApolloConfig
assembleApolloConfig
(
Map
<
String
,
String
>
configurations
)
{
...
...
@@ -114,7 +92,7 @@ public class RemoteConfigTest {
String
someClusterName
=
"cluster"
;
long
someReleaseId
=
1
;
ApolloConfig
apolloConfig
=
new
ApolloConfig
(
someAppId
,
someClusterName
,
someNamespace
,
someReleaseId
);
new
ApolloConfig
(
someAppId
,
someClusterName
,
someNamespace
,
someReleaseId
);
apolloConfig
.
setConfigurations
(
configurations
);
...
...
apollo-client/src/test/java/com/ctrip/apollo/internals/SimpleConfigTest.java
0 → 100644
浏览文件 @
76528a11
package
com.ctrip.apollo.internals
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
java.util.Properties
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
SimpleConfigTest
{
private
String
someNamespace
;
@Mock
private
ConfigRepository
configRepository
;
@Before
public
void
setUp
()
throws
Exception
{
someNamespace
=
"someName"
;
}
@Test
public
void
testGetProperty
()
throws
Exception
{
Properties
someProperties
=
new
Properties
();
String
someKey
=
"someKey"
;
String
someValue
=
"someValue"
;
someProperties
.
setProperty
(
someKey
,
someValue
);
when
(
configRepository
.
loadConfig
()).
thenReturn
(
someProperties
);
SimpleConfig
config
=
new
SimpleConfig
(
someNamespace
,
configRepository
);
assertEquals
(
someValue
,
config
.
getProperty
(
someKey
,
null
));
}
@Test
(
expected
=
RuntimeException
.
class
)
public
void
testLoadConfigFromConfigRepositoryError
()
throws
Exception
{
when
(
configRepository
.
loadConfig
()).
thenThrow
(
Throwable
.
class
);
new
SimpleConfig
(
someNamespace
,
configRepository
);
}
}
apollo-client/src/test/java/com/ctrip/apollo/spi/DefaultConfigFactoryManagerTest.java
浏览文件 @
76528a11
...
...
@@ -10,7 +10,6 @@ import static org.hamcrest.core.IsEqual.equalTo;
import
static
org
.
hamcrest
.
core
.
IsInstanceOf
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* @author Jason Song(song_s@ctrip.com)
...
...
apollo-client/src/test/java/com/ctrip/apollo/spi/DefaultConfigFactoryTest.java
浏览文件 @
76528a11
...
...
@@ -2,7 +2,7 @@ package com.ctrip.apollo.spi;
import
com.ctrip.apollo.Config
;
import
com.ctrip.apollo.internals.DefaultConfig
;
import
com.ctrip.apollo.internals.
RemoteConfig
;
import
com.ctrip.apollo.internals.
LocalFileConfigRepository
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -12,6 +12,7 @@ import java.util.Properties;
import
static
org
.
hamcrest
.
core
.
Is
.
is
;
import
static
org
.
hamcrest
.
core
.
IsInstanceOf
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
static
org
.
mockito
.
Mockito
.
mock
;
...
...
@@ -38,17 +39,16 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
String
someValue
=
"someValue"
;
someProperties
.
setProperty
(
someKey
,
someValue
);
RemoteConfig
someRemoteConfig
=
mock
(
RemoteConfig
.
class
);
when
(
some
RemoteConfig
.
loadConfig
()).
thenReturn
(
someProperties
);
LocalFileConfigRepository
someLocalConfigRepo
=
mock
(
LocalFileConfigRepository
.
class
);
when
(
some
LocalConfigRepo
.
loadConfig
()).
thenReturn
(
someProperties
);
doReturn
(
some
RemoteConfig
).
when
(
defaultConfigFactory
).
createRemoteConfig
(
someNamespace
);
doReturn
(
some
LocalConfigRepo
).
when
(
defaultConfigFactory
).
createLocalConfigRepository
(
someNamespace
);
Config
result
=
defaultConfigFactory
.
create
(
someNamespace
);
assertThat
(
"DefaultConfigFactory should create DefaultConfig"
,
result
,
is
(
instanceOf
(
DefaultConfig
.
class
)));
assertThat
(
"DefaultConfigFactory should set remote config as the fallback loader"
,
((
DefaultConfig
)
result
).
getFallbackLoader
(),
instanceOf
(
RemoteConfig
.
class
));
assertEquals
(
someValue
,
result
.
getProperty
(
someKey
,
null
));
}
}
apollo-client/src/test/java/com/ctrip/apollo/spi/DefaultConfigRegistryTest.java
浏览文件 @
76528a11
...
...
@@ -4,11 +4,11 @@ import com.ctrip.apollo.Config;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.MockitoAnnotations
;
import
org.unidal.lookup.ComponentTestCase
;
import
static
org
.
hamcrest
.
core
.
IsEqual
.
equalTo
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* @author Jason Song(song_s@ctrip.com)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录