Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
apollo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
apollo
Commits
9e3aec54
Unverified
Commit
9e3aec54
authored
Sep 16, 2018
by
Jason Song
Committed by
GitHub
Sep 16, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1470 from nobodyiam/config-source
add getSourceType api
parents
ffc99dd2
17236f1e
Changes
16
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
268 additions
and
41 deletions
+268
-41
apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java
...ient/src/main/java/com/ctrip/framework/apollo/Config.java
+8
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigFile.java
.../src/main/java/com/ctrip/framework/apollo/ConfigFile.java
+16
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/enums/ConfigSourceType.java
...va/com/ctrip/framework/apollo/enums/ConfigSourceType.java
+18
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java
.../ctrip/framework/apollo/internals/AbstractConfigFile.java
+20
-4
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigRepository.java
...om/ctrip/framework/apollo/internals/ConfigRepository.java
+8
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java
...a/com/ctrip/framework/apollo/internals/DefaultConfig.java
+24
-8
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
...framework/apollo/internals/LocalFileConfigRepository.java
+14
-4
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java
...ip/framework/apollo/internals/RemoteConfigRepository.java
+19
-12
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java
...va/com/ctrip/framework/apollo/internals/SimpleConfig.java
+15
-5
apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java
...st/java/com/ctrip/framework/apollo/ConfigServiceTest.java
+16
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java
.../framework/apollo/internals/DefaultConfigManagerTest.java
+6
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java
...m/ctrip/framework/apollo/internals/DefaultConfigTest.java
+42
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/JsonConfigFileTest.java
.../ctrip/framework/apollo/internals/JsonConfigFileTest.java
+21
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java
...ework/apollo/internals/LocalFileConfigRepositoryTest.java
+16
-5
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java
...ramework/apollo/internals/RemoteConfigRepositoryTest.java
+2
-0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java
...om/ctrip/framework/apollo/internals/SimpleConfigTest.java
+23
-3
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
;
package
com
.
ctrip
.
framework
.
apollo
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
com.google.common.base.Function
;
import
com.google.common.base.Function
;
import
java.util.Date
;
import
java.util.Date
;
...
@@ -203,4 +204,11 @@ public interface Config {
...
@@ -203,4 +204,11 @@ public interface Config {
* @return the property value
* @return the property value
*/
*/
public
<
T
>
T
getProperty
(
String
key
,
Function
<
String
,
T
>
function
,
T
defaultValue
);
public
<
T
>
T
getProperty
(
String
key
,
Function
<
String
,
T
>
function
,
T
defaultValue
);
/**
* Return the config's source type, i.e. where is the config loaded from
*
* @return the config's source type
*/
public
ConfigSourceType
getSourceType
();
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigFile.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
;
package
com
.
ctrip
.
framework
.
apollo
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
/**
/**
* @author Jason Song(song_s@ctrip.com)
* @author Jason Song(song_s@ctrip.com)
...
@@ -36,4 +37,19 @@ public interface ConfigFile {
...
@@ -36,4 +37,19 @@ public interface ConfigFile {
* @param listener the config file change listener
* @param listener the config file change listener
*/
*/
void
addChangeListener
(
ConfigFileChangeListener
listener
);
void
addChangeListener
(
ConfigFileChangeListener
listener
);
/**
* Remove the change listener
*
* @param listener the specific config change listener to remove
* @return true if the specific config change listener is found and removed
*/
public
boolean
removeChangeListener
(
ConfigChangeListener
listener
);
/**
* Return the config's source type, i.e. where is the config loaded from
*
* @return the config's source type
*/
public
ConfigSourceType
getSourceType
();
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/enums/ConfigSourceType.java
0 → 100644
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
enums
;
/**
* To indicate the config's source type, i.e. where is the config loaded from
*/
public
enum
ConfigSourceType
{
REMOTE
(
"Loaded from remote config service"
),
LOCAL
(
"Loaded from local cache"
),
NONE
(
"Load failed"
);
private
final
String
description
;
ConfigSourceType
(
String
description
)
{
this
.
description
=
description
;
}
public
String
getDescription
()
{
return
description
;
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
com.ctrip.framework.apollo.ConfigChangeListener
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.ExecutorService
;
...
@@ -25,10 +27,12 @@ import com.google.common.collect.Lists;
...
@@ -25,10 +27,12 @@ import com.google.common.collect.Lists;
public
abstract
class
AbstractConfigFile
implements
ConfigFile
,
RepositoryChangeListener
{
public
abstract
class
AbstractConfigFile
implements
ConfigFile
,
RepositoryChangeListener
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AbstractConfigFile
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
AbstractConfigFile
.
class
);
private
static
ExecutorService
m_executorService
;
private
static
ExecutorService
m_executorService
;
protected
ConfigRepository
m_configRepository
;
protected
final
ConfigRepository
m_configRepository
;
protected
String
m_namespace
;
protected
final
String
m_namespace
;
protected
AtomicReference
<
Properties
>
m_configProperties
;
protected
final
AtomicReference
<
Properties
>
m_configProperties
;
private
List
<
ConfigFileChangeListener
>
m_listeners
=
Lists
.
newCopyOnWriteArrayList
();
private
final
List
<
ConfigFileChangeListener
>
m_listeners
=
Lists
.
newCopyOnWriteArrayList
();
private
volatile
ConfigSourceType
m_sourceType
=
ConfigSourceType
.
NONE
;
static
{
static
{
m_executorService
=
Executors
.
newCachedThreadPool
(
ApolloThreadFactory
m_executorService
=
Executors
.
newCachedThreadPool
(
ApolloThreadFactory
...
@@ -45,6 +49,7 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
...
@@ -45,6 +49,7 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
private
void
initialize
()
{
private
void
initialize
()
{
try
{
try
{
m_configProperties
.
set
(
m_configRepository
.
getConfig
());
m_configProperties
.
set
(
m_configRepository
.
getConfig
());
m_sourceType
=
m_configRepository
.
getSourceType
();
}
catch
(
Throwable
ex
)
{
}
catch
(
Throwable
ex
)
{
Tracer
.
logError
(
ex
);
Tracer
.
logError
(
ex
);
logger
.
warn
(
"Init Apollo Config File failed - namespace: {}, reason: {}."
,
logger
.
warn
(
"Init Apollo Config File failed - namespace: {}, reason: {}."
,
...
@@ -74,6 +79,7 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
...
@@ -74,6 +79,7 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
String
oldValue
=
getContent
();
String
oldValue
=
getContent
();
update
(
newProperties
);
update
(
newProperties
);
m_sourceType
=
m_configRepository
.
getSourceType
();
String
newValue
=
getContent
();
String
newValue
=
getContent
();
...
@@ -97,6 +103,16 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
...
@@ -97,6 +103,16 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
}
}
}
}
@Override
public
boolean
removeChangeListener
(
ConfigChangeListener
listener
)
{
return
m_listeners
.
remove
(
listener
);
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
m_sourceType
;
}
private
void
fireConfigChange
(
final
ConfigFileChangeEvent
changeEvent
)
{
private
void
fireConfigChange
(
final
ConfigFileChangeEvent
changeEvent
)
{
for
(
final
ConfigFileChangeListener
listener
:
m_listeners
)
{
for
(
final
ConfigFileChangeListener
listener
:
m_listeners
)
{
m_executorService
.
submit
(
new
Runnable
()
{
m_executorService
.
submit
(
new
Runnable
()
{
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigRepository.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.Properties
;
import
java.util.Properties
;
/**
/**
...
@@ -29,4 +30,11 @@ public interface ConfigRepository {
...
@@ -29,4 +30,11 @@ public interface ConfigRepository {
* @param listener the listener to remove
* @param listener the listener to remove
*/
*/
public
void
removeChangeListener
(
RepositoryChangeListener
listener
);
public
void
removeChangeListener
(
RepositoryChangeListener
listener
);
/**
* Return the config's source type, i.e. where is the config loaded from
*
* @return the config's source type
*/
public
ConfigSourceType
getSourceType
();
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.util.Collections
;
import
java.util.Collections
;
...
@@ -30,10 +31,12 @@ import com.google.common.util.concurrent.RateLimiter;
...
@@ -30,10 +31,12 @@ import com.google.common.util.concurrent.RateLimiter;
public
class
DefaultConfig
extends
AbstractConfig
implements
RepositoryChangeListener
{
public
class
DefaultConfig
extends
AbstractConfig
implements
RepositoryChangeListener
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
DefaultConfig
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
DefaultConfig
.
class
);
private
final
String
m_namespace
;
private
final
String
m_namespace
;
private
Properties
m_resourceProperties
;
private
final
Properties
m_resourceProperties
;
private
AtomicReference
<
Properties
>
m_configProperties
;
private
final
AtomicReference
<
Properties
>
m_configProperties
;
private
ConfigRepository
m_configRepository
;
private
final
ConfigRepository
m_configRepository
;
private
RateLimiter
m_warnLogRateLimiter
;
private
final
RateLimiter
m_warnLogRateLimiter
;
private
volatile
ConfigSourceType
m_sourceType
=
ConfigSourceType
.
NONE
;
/**
/**
* Constructor.
* Constructor.
...
@@ -52,7 +55,7 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
...
@@ -52,7 +55,7 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
private
void
initialize
()
{
private
void
initialize
()
{
try
{
try
{
m_configProperties
.
set
(
m_configRepository
.
getConfig
());
updateConfig
(
m_configRepository
.
getConfig
(),
m_configRepository
.
getSourceType
());
}
catch
(
Throwable
ex
)
{
}
catch
(
Throwable
ex
)
{
Tracer
.
logError
(
ex
);
Tracer
.
logError
(
ex
);
logger
.
warn
(
"Init Apollo Local Config failed - namespace: {}, reason: {}."
,
logger
.
warn
(
"Init Apollo Local Config failed - namespace: {}, reason: {}."
,
...
@@ -105,6 +108,11 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
...
@@ -105,6 +108,11 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
return
stringPropertyNames
(
properties
);
return
stringPropertyNames
(
properties
);
}
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
m_sourceType
;
}
private
Set
<
String
>
stringPropertyNames
(
Properties
properties
)
{
private
Set
<
String
>
stringPropertyNames
(
Properties
properties
)
{
//jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历.
//jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历.
Map
<
String
,
String
>
h
=
new
HashMap
<>();
Map
<
String
,
String
>
h
=
new
HashMap
<>();
...
@@ -123,10 +131,12 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
...
@@ -123,10 +131,12 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
if
(
newProperties
.
equals
(
m_configProperties
.
get
()))
{
if
(
newProperties
.
equals
(
m_configProperties
.
get
()))
{
return
;
return
;
}
}
ConfigSourceType
sourceType
=
m_configRepository
.
getSourceType
();
Properties
newConfigProperties
=
new
Properties
();
Properties
newConfigProperties
=
new
Properties
();
newConfigProperties
.
putAll
(
newProperties
);
newConfigProperties
.
putAll
(
newProperties
);
Map
<
String
,
ConfigChange
>
actualChanges
=
updateAndCalcConfigChanges
(
newConfigProperties
);
Map
<
String
,
ConfigChange
>
actualChanges
=
updateAndCalcConfigChanges
(
newConfigProperties
,
sourceType
);
//check double checked result
//check double checked result
if
(
actualChanges
.
isEmpty
())
{
if
(
actualChanges
.
isEmpty
())
{
...
@@ -138,7 +148,13 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
...
@@ -138,7 +148,13 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
Tracer
.
logEvent
(
"Apollo.Client.ConfigChanges"
,
m_namespace
);
Tracer
.
logEvent
(
"Apollo.Client.ConfigChanges"
,
m_namespace
);
}
}
private
Map
<
String
,
ConfigChange
>
updateAndCalcConfigChanges
(
Properties
newConfigProperties
)
{
private
void
updateConfig
(
Properties
newConfigProperties
,
ConfigSourceType
sourceType
)
{
m_configProperties
.
set
(
newConfigProperties
);
m_sourceType
=
sourceType
;
}
private
Map
<
String
,
ConfigChange
>
updateAndCalcConfigChanges
(
Properties
newConfigProperties
,
ConfigSourceType
sourceType
)
{
List
<
ConfigChange
>
configChanges
=
List
<
ConfigChange
>
configChanges
=
calcPropertyChanges
(
m_namespace
,
m_configProperties
.
get
(),
newConfigProperties
);
calcPropertyChanges
(
m_namespace
,
m_configProperties
.
get
(),
newConfigProperties
);
...
@@ -153,7 +169,7 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
...
@@ -153,7 +169,7 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
}
}
//2. update m_configProperties
//2. update m_configProperties
m_configProperties
.
set
(
newConfigProperties
);
updateConfig
(
newConfigProperties
,
sourceType
);
clearConfigCache
();
clearConfigCache
();
//3. use getProperty to update configChange's new value and calc the final changes
//3. use getProperty to update configChange's new value and calc the final changes
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
...
@@ -38,6 +39,8 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -38,6 +39,8 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
private
volatile
Properties
m_fileProperties
;
private
volatile
Properties
m_fileProperties
;
private
volatile
ConfigRepository
m_upstream
;
private
volatile
ConfigRepository
m_upstream
;
private
volatile
ConfigSourceType
m_sourceType
=
ConfigSourceType
.
LOCAL
;
/**
/**
* Constructor.
* Constructor.
*
*
...
@@ -104,6 +107,11 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -104,6 +107,11 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
upstreamConfigRepository
.
addChangeListener
(
this
);
upstreamConfigRepository
.
addChangeListener
(
this
);
}
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
m_sourceType
;
}
@Override
@Override
public
void
onRepositoryChange
(
String
namespace
,
Properties
newProperties
)
{
public
void
onRepositoryChange
(
String
namespace
,
Properties
newProperties
)
{
if
(
newProperties
.
equals
(
m_fileProperties
))
{
if
(
newProperties
.
equals
(
m_fileProperties
))
{
...
@@ -111,7 +119,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -111,7 +119,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
}
}
Properties
newFileProperties
=
new
Properties
();
Properties
newFileProperties
=
new
Properties
();
newFileProperties
.
putAll
(
newProperties
);
newFileProperties
.
putAll
(
newProperties
);
updateFileProperties
(
newFileProperties
);
updateFileProperties
(
newFileProperties
,
m_upstream
.
getSourceType
()
);
this
.
fireRepositoryChange
(
namespace
,
newProperties
);
this
.
fireRepositoryChange
(
namespace
,
newProperties
);
}
}
...
@@ -129,6 +137,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -129,6 +137,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
try
{
try
{
transaction
.
addData
(
"Basedir"
,
m_baseDir
.
getAbsolutePath
());
transaction
.
addData
(
"Basedir"
,
m_baseDir
.
getAbsolutePath
());
m_fileProperties
=
this
.
loadFromLocalCacheFile
(
m_baseDir
,
m_namespace
);
m_fileProperties
=
this
.
loadFromLocalCacheFile
(
m_baseDir
,
m_namespace
);
m_sourceType
=
ConfigSourceType
.
LOCAL
;
transaction
.
setStatus
(
Transaction
.
SUCCESS
);
transaction
.
setStatus
(
Transaction
.
SUCCESS
);
}
catch
(
Throwable
ex
)
{
}
catch
(
Throwable
ex
)
{
Tracer
.
logEvent
(
"ApolloConfigException"
,
ExceptionUtil
.
getDetailMessage
(
ex
));
Tracer
.
logEvent
(
"ApolloConfigException"
,
ExceptionUtil
.
getDetailMessage
(
ex
));
...
@@ -140,6 +149,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -140,6 +149,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
}
}
if
(
m_fileProperties
==
null
)
{
if
(
m_fileProperties
==
null
)
{
m_sourceType
=
ConfigSourceType
.
NONE
;
throw
new
ApolloConfigException
(
throw
new
ApolloConfigException
(
"Load config from local config failed!"
,
exception
);
"Load config from local config failed!"
,
exception
);
}
}
...
@@ -150,8 +160,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -150,8 +160,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
return
false
;
return
false
;
}
}
try
{
try
{
Properties
properties
=
m_upstream
.
getConfig
();
updateFileProperties
(
m_upstream
.
getConfig
(),
m_upstream
.
getSourceType
());
updateFileProperties
(
properties
);
return
true
;
return
true
;
}
catch
(
Throwable
ex
)
{
}
catch
(
Throwable
ex
)
{
Tracer
.
logError
(
ex
);
Tracer
.
logError
(
ex
);
...
@@ -162,7 +171,8 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -162,7 +171,8 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
return
false
;
return
false
;
}
}
private
synchronized
void
updateFileProperties
(
Properties
newProperties
)
{
private
synchronized
void
updateFileProperties
(
Properties
newProperties
,
ConfigSourceType
sourceType
)
{
this
.
m_sourceType
=
sourceType
;
if
(
newProperties
.
equals
(
m_fileProperties
))
{
if
(
newProperties
.
equals
(
m_fileProperties
))
{
return
;
return
;
}
}
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -47,21 +48,22 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
...
@@ -47,21 +48,22 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RemoteConfigRepository
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RemoteConfigRepository
.
class
);
private
static
final
Joiner
STRING_JOINER
=
Joiner
.
on
(
ConfigConsts
.
CLUSTER_NAMESPACE_SEPARATOR
);
private
static
final
Joiner
STRING_JOINER
=
Joiner
.
on
(
ConfigConsts
.
CLUSTER_NAMESPACE_SEPARATOR
);
private
static
final
Joiner
.
MapJoiner
MAP_JOINER
=
Joiner
.
on
(
"&"
).
withKeyValueSeparator
(
"="
);
private
static
final
Joiner
.
MapJoiner
MAP_JOINER
=
Joiner
.
on
(
"&"
).
withKeyValueSeparator
(
"="
);
private
ConfigServiceLocator
m_serviceLocator
;
private
static
final
Escaper
pathEscaper
=
UrlEscapers
.
urlPathSegmentEscaper
();
private
HttpUtil
m_httpUtil
;
private
static
final
Escaper
queryParamEscaper
=
UrlEscapers
.
urlFormParameterEscaper
();
private
ConfigUtil
m_configUtil
;
private
RemoteConfigLongPollService
remoteConfigLongPollService
;
private
final
ConfigServiceLocator
m_serviceLocator
;
private
final
HttpUtil
m_httpUtil
;
private
final
ConfigUtil
m_configUtil
;
private
final
RemoteConfigLongPollService
remoteConfigLongPollService
;
private
volatile
AtomicReference
<
ApolloConfig
>
m_configCache
;
private
volatile
AtomicReference
<
ApolloConfig
>
m_configCache
;
private
final
String
m_namespace
;
private
final
String
m_namespace
;
private
final
static
ScheduledExecutorService
m_executorService
;
private
final
static
ScheduledExecutorService
m_executorService
;
private
AtomicReference
<
ServiceDTO
>
m_longPollServiceDto
;
private
final
AtomicReference
<
ServiceDTO
>
m_longPollServiceDto
;
private
AtomicReference
<
ApolloNotificationMessages
>
m_remoteMessages
;
private
final
AtomicReference
<
ApolloNotificationMessages
>
m_remoteMessages
;
private
RateLimiter
m_loadConfigRateLimiter
;
private
final
RateLimiter
m_loadConfigRateLimiter
;
private
AtomicBoolean
m_configNeedForceRefresh
;
private
final
AtomicBoolean
m_configNeedForceRefresh
;
private
SchedulePolicy
m_loadConfigFailSchedulePolicy
;
private
final
SchedulePolicy
m_loadConfigFailSchedulePolicy
;
private
Gson
gson
;
private
final
Gson
gson
;
private
static
final
Escaper
pathEscaper
=
UrlEscapers
.
urlPathSegmentEscaper
();
private
static
final
Escaper
queryParamEscaper
=
UrlEscapers
.
urlFormParameterEscaper
();
static
{
static
{
m_executorService
=
Executors
.
newScheduledThreadPool
(
1
,
m_executorService
=
Executors
.
newScheduledThreadPool
(
1
,
...
@@ -105,6 +107,11 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
...
@@ -105,6 +107,11 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
//remote config doesn't need upstream
//remote config doesn't need upstream
}
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
ConfigSourceType
.
REMOTE
;
}
private
void
schedulePeriodicRefresh
()
{
private
void
schedulePeriodicRefresh
()
{
logger
.
debug
(
"Schedule periodic refresh with interval: {} {}"
,
logger
.
debug
(
"Schedule periodic refresh with interval: {} {}"
,
m_configUtil
.
getRefreshInterval
(),
m_configUtil
.
getRefreshIntervalTimeUnit
());
m_configUtil
.
getRefreshInterval
(),
m_configUtil
.
getRefreshIntervalTimeUnit
());
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -24,6 +25,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
...
@@ -24,6 +25,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
private
final
String
m_namespace
;
private
final
String
m_namespace
;
private
final
ConfigRepository
m_configRepository
;
private
final
ConfigRepository
m_configRepository
;
private
volatile
Properties
m_configProperties
;
private
volatile
Properties
m_configProperties
;
private
volatile
ConfigSourceType
m_sourceType
=
ConfigSourceType
.
NONE
;
/**
/**
* Constructor.
* Constructor.
...
@@ -39,7 +41,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
...
@@ -39,7 +41,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
private
void
initialize
()
{
private
void
initialize
()
{
try
{
try
{
m_configProperties
=
m_configRepository
.
getConfig
(
);
updateConfig
(
m_configRepository
.
getConfig
(),
m_configRepository
.
getSourceType
()
);
}
catch
(
Throwable
ex
)
{
}
catch
(
Throwable
ex
)
{
Tracer
.
logError
(
ex
);
Tracer
.
logError
(
ex
);
logger
.
warn
(
"Init Apollo Simple Config failed - namespace: {}, reason: {}"
,
m_namespace
,
logger
.
warn
(
"Init Apollo Simple Config failed - namespace: {}, reason: {}"
,
m_namespace
,
...
@@ -69,6 +71,11 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
...
@@ -69,6 +71,11 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
return
m_configProperties
.
stringPropertyNames
();
return
m_configProperties
.
stringPropertyNames
();
}
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
m_sourceType
;
}
@Override
@Override
public
synchronized
void
onRepositoryChange
(
String
namespace
,
Properties
newProperties
)
{
public
synchronized
void
onRepositoryChange
(
String
namespace
,
Properties
newProperties
)
{
if
(
newProperties
.
equals
(
m_configProperties
))
{
if
(
newProperties
.
equals
(
m_configProperties
))
{
...
@@ -77,9 +84,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
...
@@ -77,9 +84,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
Properties
newConfigProperties
=
new
Properties
();
Properties
newConfigProperties
=
new
Properties
();
newConfigProperties
.
putAll
(
newProperties
);
newConfigProperties
.
putAll
(
newProperties
);
List
<
ConfigChange
>
List
<
ConfigChange
>
changes
=
calcPropertyChanges
(
namespace
,
m_configProperties
,
newConfigProperties
);
changes
=
calcPropertyChanges
(
namespace
,
m_configProperties
,
newConfigProperties
);
Map
<
String
,
ConfigChange
>
changeMap
=
Maps
.
uniqueIndex
(
changes
,
Map
<
String
,
ConfigChange
>
changeMap
=
Maps
.
uniqueIndex
(
changes
,
new
Function
<
ConfigChange
,
String
>()
{
new
Function
<
ConfigChange
,
String
>()
{
@Override
@Override
...
@@ -88,11 +93,16 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
...
@@ -88,11 +93,16 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
}
}
});
});
m_configProperties
=
newConfigProperties
;
updateConfig
(
newConfigProperties
,
m_configRepository
.
getSourceType
())
;
clearConfigCache
();
clearConfigCache
();
this
.
fireConfigChange
(
new
ConfigChangeEvent
(
m_namespace
,
changeMap
));
this
.
fireConfigChange
(
new
ConfigChangeEvent
(
m_namespace
,
changeMap
));
Tracer
.
logEvent
(
"Apollo.Client.ConfigChanges"
,
m_namespace
);
Tracer
.
logEvent
(
"Apollo.Client.ConfigChanges"
,
m_namespace
);
}
}
private
void
updateConfig
(
Properties
newConfigProperties
,
ConfigSourceType
sourceType
)
{
m_configProperties
=
newConfigProperties
;
m_sourceType
=
sourceType
;
}
}
}
apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java
View file @
9e3aec54
...
@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo;
...
@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.Set
;
import
java.util.Set
;
import
org.junit.Before
;
import
org.junit.Before
;
...
@@ -101,6 +102,11 @@ public class ConfigServiceTest {
...
@@ -101,6 +102,11 @@ public class ConfigServiceTest {
public
Set
<
String
>
getPropertyNames
()
{
public
Set
<
String
>
getPropertyNames
()
{
return
null
;
return
null
;
}
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
null
;
}
}
}
private
static
class
MockConfigFile
implements
ConfigFile
{
private
static
class
MockConfigFile
implements
ConfigFile
{
...
@@ -137,6 +143,16 @@ public class ConfigServiceTest {
...
@@ -137,6 +143,16 @@ public class ConfigServiceTest {
public
void
addChangeListener
(
ConfigFileChangeListener
listener
)
{
public
void
addChangeListener
(
ConfigFileChangeListener
listener
)
{
}
}
@Override
public
boolean
removeChangeListener
(
ConfigChangeListener
listener
)
{
return
false
;
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
null
;
}
}
}
public
static
class
MockConfigFactory
implements
ConfigFactory
{
public
static
class
MockConfigFactory
implements
ConfigFactory
{
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java
View file @
9e3aec54
...
@@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals;
...
@@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.Set
;
import
java.util.Set
;
...
@@ -103,6 +104,11 @@ public class DefaultConfigManagerTest {
...
@@ -103,6 +104,11 @@ public class DefaultConfigManagerTest {
public
Set
<
String
>
getPropertyNames
()
{
public
Set
<
String
>
getPropertyNames
()
{
return
null
;
return
null
;
}
}
@Override
public
ConfigSourceType
getSourceType
()
{
return
null
;
}
};
};
}
}
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java
View file @
9e3aec54
...
@@ -9,6 +9,7 @@ import static org.mockito.Mockito.times;
...
@@ -9,6 +9,7 @@ import static org.mockito.Mockito.times;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Sets
;
import
com.google.common.collect.Sets
;
import
java.io.File
;
import
java.io.File
;
...
@@ -50,6 +51,7 @@ public class DefaultConfigTest {
...
@@ -50,6 +51,7 @@ public class DefaultConfigTest {
private
String
someNamespace
;
private
String
someNamespace
;
private
ConfigRepository
configRepository
;
private
ConfigRepository
configRepository
;
private
Properties
someProperties
;
private
Properties
someProperties
;
private
ConfigSourceType
someSourceType
;
@Before
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
...
@@ -99,6 +101,8 @@ public class DefaultConfigTest {
...
@@ -99,6 +101,8 @@ public class DefaultConfigTest {
someProperties
.
setProperty
(
someKey
,
someLocalFileValue
);
someProperties
.
setProperty
(
someKey
,
someLocalFileValue
);
someProperties
.
setProperty
(
anotherKey
,
someLocalFileValue
);
someProperties
.
setProperty
(
anotherKey
,
someLocalFileValue
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
someSourceType
=
ConfigSourceType
.
LOCAL
;
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
//set up resource file
//set up resource file
File
resourceFile
=
new
File
(
someResourceDir
,
someNamespace
+
".properties"
);
File
resourceFile
=
new
File
(
someResourceDir
,
someNamespace
+
".properties"
);
...
@@ -122,6 +126,7 @@ public class DefaultConfigTest {
...
@@ -122,6 +126,7 @@ public class DefaultConfigTest {
assertEquals
(
someLocalFileValue
,
anotherKeyValue
);
assertEquals
(
someLocalFileValue
,
anotherKeyValue
);
assertEquals
(
someResourceValue
,
lastKeyValue
);
assertEquals
(
someResourceValue
,
lastKeyValue
);
assertEquals
(
someSourceType
,
defaultConfig
.
getSourceType
());
}
}
@Test
@Test
...
@@ -599,6 +604,8 @@ public class DefaultConfigTest {
...
@@ -599,6 +604,8 @@ public class DefaultConfigTest {
.
of
(
someKey
,
someLocalFileValue
,
anotherKey
,
someLocalFileValue
,
keyToBeDeleted
,
.
of
(
someKey
,
someLocalFileValue
,
anotherKey
,
someLocalFileValue
,
keyToBeDeleted
,
keyToBeDeletedValue
,
yetAnotherKey
,
yetAnotherValue
));
keyToBeDeletedValue
,
yetAnotherKey
,
yetAnotherValue
));
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
someSourceType
=
ConfigSourceType
.
LOCAL
;
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
//set up resource file
//set up resource file
File
resourceFile
=
new
File
(
someResourceDir
,
someNamespace
+
".properties"
);
File
resourceFile
=
new
File
(
someResourceDir
,
someNamespace
+
".properties"
);
...
@@ -607,6 +614,8 @@ public class DefaultConfigTest {
...
@@ -607,6 +614,8 @@ public class DefaultConfigTest {
DefaultConfig
defaultConfig
=
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someNamespace
,
configRepository
);
new
DefaultConfig
(
someNamespace
,
configRepository
);
assertEquals
(
someSourceType
,
defaultConfig
.
getSourceType
());
final
SettableFuture
<
ConfigChangeEvent
>
configChangeFuture
=
SettableFuture
.
create
();
final
SettableFuture
<
ConfigChangeEvent
>
configChangeFuture
=
SettableFuture
.
create
();
ConfigChangeListener
someListener
=
new
ConfigChangeListener
()
{
ConfigChangeListener
someListener
=
new
ConfigChangeListener
()
{
@Override
@Override
...
@@ -625,6 +634,9 @@ public class DefaultConfigTest {
...
@@ -625,6 +634,9 @@ public class DefaultConfigTest {
newProperties
.
putAll
(
ImmutableMap
newProperties
.
putAll
(
ImmutableMap
.
of
(
someKey
,
someKeyNewValue
,
anotherKey
,
anotherKeyNewValue
,
newKey
,
newValue
));
.
of
(
someKey
,
someKeyNewValue
,
anotherKey
,
anotherKeyNewValue
,
newKey
,
newValue
));
ConfigSourceType
anotherSourceType
=
ConfigSourceType
.
REMOTE
;
when
(
configRepository
.
getSourceType
()).
thenReturn
(
anotherSourceType
);
defaultConfig
.
onRepositoryChange
(
someNamespace
,
newProperties
);
defaultConfig
.
onRepositoryChange
(
someNamespace
,
newProperties
);
ConfigChangeEvent
changeEvent
=
configChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
ConfigChangeEvent
changeEvent
=
configChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
...
@@ -654,6 +666,8 @@ public class DefaultConfigTest {
...
@@ -654,6 +666,8 @@ public class DefaultConfigTest {
assertEquals
(
null
,
newKeyChange
.
getOldValue
());
assertEquals
(
null
,
newKeyChange
.
getOldValue
());
assertEquals
(
newValue
,
newKeyChange
.
getNewValue
());
assertEquals
(
newValue
,
newKeyChange
.
getNewValue
());
assertEquals
(
PropertyChangeType
.
ADDED
,
newKeyChange
.
getChangeType
());
assertEquals
(
PropertyChangeType
.
ADDED
,
newKeyChange
.
getChangeType
());
assertEquals
(
anotherSourceType
,
defaultConfig
.
getSourceType
());
}
}
@Test
@Test
...
@@ -825,6 +839,34 @@ public class DefaultConfigTest {
...
@@ -825,6 +839,34 @@ public class DefaultConfigTest {
},
Lists
.<
String
>
newArrayList
()),
Lists
.
newArrayList
());
},
Lists
.<
String
>
newArrayList
()),
Lists
.
newArrayList
());
}
}
@Test
public
void
testLoadFromRepositoryFailedAndThenRecovered
()
{
String
someKey
=
"someKey"
;
String
someValue
=
"someValue"
;
String
someDefaultValue
=
"someDefaultValue"
;
ConfigSourceType
someSourceType
=
ConfigSourceType
.
REMOTE
;
when
(
configRepository
.
getConfig
()).
thenThrow
(
mock
(
RuntimeException
.
class
));
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someNamespace
,
configRepository
);
verify
(
configRepository
,
times
(
1
)).
addChangeListener
(
defaultConfig
);
assertEquals
(
ConfigSourceType
.
NONE
,
defaultConfig
.
getSourceType
());
assertEquals
(
someDefaultValue
,
defaultConfig
.
getProperty
(
someKey
,
someDefaultValue
));
someProperties
=
new
Properties
();
someProperties
.
setProperty
(
someKey
,
someValue
);
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
defaultConfig
.
onRepositoryChange
(
someNamespace
,
someProperties
);
assertEquals
(
someSourceType
,
defaultConfig
.
getSourceType
());
assertEquals
(
someValue
,
defaultConfig
.
getProperty
(
someKey
,
someDefaultValue
));
}
private
void
checkDatePropertyWithFormat
(
Config
config
,
Date
expected
,
String
propertyName
,
String
format
,
Date
private
void
checkDatePropertyWithFormat
(
Config
config
,
Date
expected
,
String
propertyName
,
String
format
,
Date
defaultValue
)
{
defaultValue
)
{
assertEquals
(
expected
,
config
.
getDateProperty
(
propertyName
,
format
,
defaultValue
));
assertEquals
(
expected
,
config
.
getDateProperty
(
propertyName
,
format
,
defaultValue
));
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/JsonConfigFileTest.java
View file @
9e3aec54
...
@@ -6,6 +6,7 @@ import static org.junit.Assert.assertNull;
...
@@ -6,6 +6,7 @@ import static org.junit.Assert.assertNull;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.Properties
;
import
java.util.Properties
;
import
org.junit.Before
;
import
org.junit.Before
;
...
@@ -26,6 +27,8 @@ public class JsonConfigFileTest {
...
@@ -26,6 +27,8 @@ public class JsonConfigFileTest {
@Mock
@Mock
private
ConfigRepository
configRepository
;
private
ConfigRepository
configRepository
;
private
ConfigSourceType
someSourceType
;
@Before
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
someNamespace
=
"someName"
;
someNamespace
=
"someName"
;
...
@@ -38,7 +41,10 @@ public class JsonConfigFileTest {
...
@@ -38,7 +41,10 @@ public class JsonConfigFileTest {
String
someValue
=
"someValue"
;
String
someValue
=
"someValue"
;
someProperties
.
setProperty
(
key
,
someValue
);
someProperties
.
setProperty
(
key
,
someValue
);
someSourceType
=
ConfigSourceType
.
LOCAL
;
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
JsonConfigFile
configFile
=
new
JsonConfigFile
(
someNamespace
,
configRepository
);
JsonConfigFile
configFile
=
new
JsonConfigFile
(
someNamespace
,
configRepository
);
...
@@ -46,6 +52,7 @@ public class JsonConfigFileTest {
...
@@ -46,6 +52,7 @@ public class JsonConfigFileTest {
assertEquals
(
someNamespace
,
configFile
.
getNamespace
());
assertEquals
(
someNamespace
,
configFile
.
getNamespace
());
assertTrue
(
configFile
.
hasContent
());
assertTrue
(
configFile
.
hasContent
());
assertEquals
(
someValue
,
configFile
.
getContent
());
assertEquals
(
someValue
,
configFile
.
getContent
());
assertEquals
(
someSourceType
,
configFile
.
getSourceType
());
}
}
@Test
@Test
...
@@ -66,6 +73,7 @@ public class JsonConfigFileTest {
...
@@ -66,6 +73,7 @@ public class JsonConfigFileTest {
assertFalse
(
configFile
.
hasContent
());
assertFalse
(
configFile
.
hasContent
());
assertNull
(
configFile
.
getContent
());
assertNull
(
configFile
.
getContent
());
assertEquals
(
ConfigSourceType
.
NONE
,
configFile
.
getSourceType
());
}
}
@Test
@Test
...
@@ -76,18 +84,26 @@ public class JsonConfigFileTest {
...
@@ -76,18 +84,26 @@ public class JsonConfigFileTest {
String
anotherValue
=
"anotherValue"
;
String
anotherValue
=
"anotherValue"
;
someProperties
.
setProperty
(
key
,
someValue
);
someProperties
.
setProperty
(
key
,
someValue
);
someSourceType
=
ConfigSourceType
.
LOCAL
;
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
JsonConfigFile
configFile
=
new
JsonConfigFile
(
someNamespace
,
configRepository
);
JsonConfigFile
configFile
=
new
JsonConfigFile
(
someNamespace
,
configRepository
);
assertEquals
(
someValue
,
configFile
.
getContent
());
assertEquals
(
someValue
,
configFile
.
getContent
());
assertEquals
(
someSourceType
,
configFile
.
getSourceType
());
Properties
anotherProperties
=
new
Properties
();
Properties
anotherProperties
=
new
Properties
();
anotherProperties
.
setProperty
(
key
,
anotherValue
);
anotherProperties
.
setProperty
(
key
,
anotherValue
);
ConfigSourceType
anotherSourceType
=
ConfigSourceType
.
REMOTE
;
when
(
configRepository
.
getSourceType
()).
thenReturn
(
anotherSourceType
);
configFile
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
configFile
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
assertEquals
(
anotherValue
,
configFile
.
getContent
());
assertEquals
(
anotherValue
,
configFile
.
getContent
());
assertEquals
(
anotherSourceType
,
configFile
.
getSourceType
());
}
}
@Test
@Test
...
@@ -97,16 +113,21 @@ public class JsonConfigFileTest {
...
@@ -97,16 +113,21 @@ public class JsonConfigFileTest {
String
someValue
=
"someValue"
;
String
someValue
=
"someValue"
;
someProperties
.
setProperty
(
key
,
someValue
);
someProperties
.
setProperty
(
key
,
someValue
);
someSourceType
=
ConfigSourceType
.
LOCAL
;
when
(
configRepository
.
getConfig
()).
thenThrow
(
new
RuntimeException
(
"someError"
));
when
(
configRepository
.
getConfig
()).
thenThrow
(
new
RuntimeException
(
"someError"
));
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
JsonConfigFile
configFile
=
new
JsonConfigFile
(
someNamespace
,
configRepository
);
JsonConfigFile
configFile
=
new
JsonConfigFile
(
someNamespace
,
configRepository
);
assertFalse
(
configFile
.
hasContent
());
assertFalse
(
configFile
.
hasContent
());
assertNull
(
configFile
.
getContent
());
assertNull
(
configFile
.
getContent
());
assertEquals
(
ConfigSourceType
.
NONE
,
configFile
.
getSourceType
());
configFile
.
onRepositoryChange
(
someNamespace
,
someProperties
);
configFile
.
onRepositoryChange
(
someNamespace
,
someProperties
);
assertTrue
(
configFile
.
hasContent
());
assertTrue
(
configFile
.
hasContent
());
assertEquals
(
someValue
,
configFile
.
getContent
());
assertEquals
(
someValue
,
configFile
.
getContent
());
assertEquals
(
someSourceType
,
configFile
.
getSourceType
());
}
}
}
}
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java
View file @
9e3aec54
...
@@ -9,6 +9,7 @@ import static org.mockito.Mockito.times;
...
@@ -9,6 +9,7 @@ import static org.mockito.Mockito.times;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
...
@@ -38,6 +39,7 @@ public class LocalFileConfigRepositoryTest {
...
@@ -38,6 +39,7 @@ public class LocalFileConfigRepositoryTest {
private
static
String
someCluster
=
"someCluster"
;
private
static
String
someCluster
=
"someCluster"
;
private
String
defaultKey
;
private
String
defaultKey
;
private
String
defaultValue
;
private
String
defaultValue
;
private
ConfigSourceType
someSourceType
;
@Before
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
...
@@ -49,8 +51,10 @@ public class LocalFileConfigRepositoryTest {
...
@@ -49,8 +51,10 @@ public class LocalFileConfigRepositoryTest {
defaultKey
=
"defaultKey"
;
defaultKey
=
"defaultKey"
;
defaultValue
=
"defaultValue"
;
defaultValue
=
"defaultValue"
;
someProperties
.
setProperty
(
defaultKey
,
defaultValue
);
someProperties
.
setProperty
(
defaultKey
,
defaultValue
);
someSourceType
=
ConfigSourceType
.
REMOTE
;
upstreamRepo
=
mock
(
ConfigRepository
.
class
);
upstreamRepo
=
mock
(
ConfigRepository
.
class
);
when
(
upstreamRepo
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
upstreamRepo
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
upstreamRepo
.
getSourceType
()).
thenReturn
(
someSourceType
);
MockInjector
.
reset
();
MockInjector
.
reset
();
MockInjector
.
setInstance
(
ConfigUtil
.
class
,
new
MockConfigUtil
());
MockInjector
.
setInstance
(
ConfigUtil
.
class
,
new
MockConfigUtil
());
...
@@ -95,7 +99,7 @@ public class LocalFileConfigRepositoryTest {
...
@@ -95,7 +99,7 @@ public class LocalFileConfigRepositoryTest {
Properties
properties
=
localRepo
.
getConfig
();
Properties
properties
=
localRepo
.
getConfig
();
assertEquals
(
someValue
,
properties
.
getProperty
(
someKey
));
assertEquals
(
someValue
,
properties
.
getProperty
(
someKey
));
assertEquals
(
ConfigSourceType
.
LOCAL
,
localRepo
.
getSourceType
());
}
}
@Test
@Test
...
@@ -112,12 +116,12 @@ public class LocalFileConfigRepositoryTest {
...
@@ -112,12 +116,12 @@ public class LocalFileConfigRepositoryTest {
Properties
properties
=
localRepo
.
getConfig
();
Properties
properties
=
localRepo
.
getConfig
();
assertEquals
(
defaultValue
,
properties
.
getProperty
(
defaultKey
));
assertEquals
(
defaultValue
,
properties
.
getProperty
(
defaultKey
));
assertEquals
(
someSourceType
,
localRepo
.
getSourceType
());
}
}
@Test
@Test
public
void
testLoadConfigWithNoLocalFile
()
throws
Exception
{
public
void
testLoadConfigWithNoLocalFile
()
throws
Exception
{
LocalFileConfigRepository
LocalFileConfigRepository
localFileConfigRepository
=
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
,
true
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
,
true
);
...
@@ -126,6 +130,7 @@ public class LocalFileConfigRepositoryTest {
...
@@ -126,6 +130,7 @@ public class LocalFileConfigRepositoryTest {
assertThat
(
assertThat
(
"LocalFileConfigRepository's properties should be the same as fallback repo's when there is no local cache"
,
"LocalFileConfigRepository's properties should be the same as fallback repo's when there is no local cache"
,
result
.
entrySet
(),
equalTo
(
someProperties
.
entrySet
()));
result
.
entrySet
(),
equalTo
(
someProperties
.
entrySet
()));
assertEquals
(
someSourceType
,
localFileConfigRepository
.
getSourceType
());
}
}
@Test
@Test
...
@@ -146,7 +151,7 @@ public class LocalFileConfigRepositoryTest {
...
@@ -146,7 +151,7 @@ public class LocalFileConfigRepositoryTest {
assertThat
(
assertThat
(
"LocalFileConfigRepository should persist local cache files and return that afterwards"
,
"LocalFileConfigRepository should persist local cache files and return that afterwards"
,
someProperties
.
entrySet
(),
equalTo
(
anotherProperties
.
entrySet
()));
someProperties
.
entrySet
(),
equalTo
(
anotherProperties
.
entrySet
()));
assertEquals
(
someSourceType
,
localRepo
.
getSourceType
());
}
}
@Test
@Test
...
@@ -155,6 +160,9 @@ public class LocalFileConfigRepositoryTest {
...
@@ -155,6 +160,9 @@ public class LocalFileConfigRepositoryTest {
LocalFileConfigRepository
localFileConfigRepository
=
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
new
LocalFileConfigRepository
(
someNamespace
,
upstreamRepo
);
assertEquals
(
someSourceType
,
localFileConfigRepository
.
getSourceType
());
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
,
true
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
,
true
);
localFileConfigRepository
.
addChangeListener
(
someListener
);
localFileConfigRepository
.
addChangeListener
(
someListener
);
...
@@ -163,6 +171,9 @@ public class LocalFileConfigRepositoryTest {
...
@@ -163,6 +171,9 @@ public class LocalFileConfigRepositoryTest {
Properties
anotherProperties
=
new
Properties
();
Properties
anotherProperties
=
new
Properties
();
anotherProperties
.
put
(
"anotherKey"
,
"anotherValue"
);
anotherProperties
.
put
(
"anotherKey"
,
"anotherValue"
);
ConfigSourceType
anotherSourceType
=
ConfigSourceType
.
NONE
;
when
(
upstreamRepo
.
getSourceType
()).
thenReturn
(
anotherSourceType
);
localFileConfigRepository
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
localFileConfigRepository
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
final
ArgumentCaptor
<
Properties
>
captor
=
ArgumentCaptor
.
forClass
(
Properties
.
class
);
final
ArgumentCaptor
<
Properties
>
captor
=
ArgumentCaptor
.
forClass
(
Properties
.
class
);
...
@@ -170,7 +181,7 @@ public class LocalFileConfigRepositoryTest {
...
@@ -170,7 +181,7 @@ public class LocalFileConfigRepositoryTest {
verify
(
someListener
,
times
(
1
)).
onRepositoryChange
(
eq
(
someNamespace
),
captor
.
capture
());
verify
(
someListener
,
times
(
1
)).
onRepositoryChange
(
eq
(
someNamespace
),
captor
.
capture
());
assertEquals
(
anotherProperties
,
captor
.
getValue
());
assertEquals
(
anotherProperties
,
captor
.
getValue
());
assertEquals
(
anotherSourceType
,
localFileConfigRepository
.
getSourceType
());
}
}
public
static
class
MockConfigUtil
extends
ConfigUtil
{
public
static
class
MockConfigUtil
extends
ConfigUtil
{
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java
View file @
9e3aec54
...
@@ -12,6 +12,7 @@ import static org.mockito.Mockito.times;
...
@@ -12,6 +12,7 @@ import static org.mockito.Mockito.times;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.lang.reflect.Type
;
import
java.lang.reflect.Type
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -105,6 +106,7 @@ public class RemoteConfigRepositoryTest {
...
@@ -105,6 +106,7 @@ public class RemoteConfigRepositoryTest {
Properties
config
=
remoteConfigRepository
.
getConfig
();
Properties
config
=
remoteConfigRepository
.
getConfig
();
assertEquals
(
configurations
,
config
);
assertEquals
(
configurations
,
config
);
assertEquals
(
ConfigSourceType
.
REMOTE
,
remoteConfigRepository
.
getSourceType
());
remoteConfigLongPollService
.
stopLongPollingRefresh
();
remoteConfigLongPollService
.
stopLongPollingRefresh
();
}
}
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java
View file @
9e3aec54
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.enums.ConfigSourceType
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
...
@@ -28,6 +30,7 @@ public class SimpleConfigTest {
...
@@ -28,6 +30,7 @@ public class SimpleConfigTest {
private
String
someNamespace
;
private
String
someNamespace
;
@Mock
@Mock
private
ConfigRepository
configRepository
;
private
ConfigRepository
configRepository
;
private
ConfigSourceType
someSourceType
;
@Before
@Before
public
void
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
...
@@ -41,22 +44,28 @@ public class SimpleConfigTest {
...
@@ -41,22 +44,28 @@ public class SimpleConfigTest {
String
someValue
=
"someValue"
;
String
someValue
=
"someValue"
;
someProperties
.
setProperty
(
someKey
,
someValue
);
someProperties
.
setProperty
(
someKey
,
someValue
);
someSourceType
=
ConfigSourceType
.
LOCAL
;
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
SimpleConfig
config
=
new
SimpleConfig
(
someNamespace
,
configRepository
);
SimpleConfig
config
=
new
SimpleConfig
(
someNamespace
,
configRepository
);
assertEquals
(
someValue
,
config
.
getProperty
(
someKey
,
null
));
assertEquals
(
someValue
,
config
.
getProperty
(
someKey
,
null
));
assertEquals
(
someSourceType
,
config
.
getSourceType
());
}
}
@Test
@Test
public
void
testLoadConfigFromConfigRepositoryError
()
throws
Exception
{
public
void
testLoadConfigFromConfigRepositoryError
()
throws
Exception
{
when
(
configRepository
.
getConfig
()).
thenThrow
(
Throwable
.
class
);
String
someKey
=
"someKey"
;
String
anyValue
=
"anyValue"
+
Math
.
random
();
when
(
configRepository
.
getConfig
()).
thenThrow
(
mock
(
RuntimeException
.
class
));
Config
config
=
new
SimpleConfig
(
someNamespace
,
configRepository
);
Config
config
=
new
SimpleConfig
(
someNamespace
,
configRepository
);
String
someKey
=
"someKey"
;
String
anyValue
=
"anyValue"
+
Math
.
random
();
assertEquals
(
anyValue
,
config
.
getProperty
(
someKey
,
anyValue
));
assertEquals
(
anyValue
,
config
.
getProperty
(
someKey
,
anyValue
));
assertEquals
(
ConfigSourceType
.
NONE
,
config
.
getSourceType
());
}
}
@Test
@Test
...
@@ -74,7 +83,10 @@ public class SimpleConfigTest {
...
@@ -74,7 +83,10 @@ public class SimpleConfigTest {
String
someValueNew
=
"someValueNew"
;
String
someValueNew
=
"someValueNew"
;
anotherProperties
.
putAll
(
ImmutableMap
.
of
(
someKey
,
someValueNew
,
newKey
,
newValue
));
anotherProperties
.
putAll
(
ImmutableMap
.
of
(
someKey
,
someValueNew
,
newKey
,
newValue
));
someSourceType
=
ConfigSourceType
.
LOCAL
;
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
when
(
configRepository
.
getSourceType
()).
thenReturn
(
someSourceType
);
final
SettableFuture
<
ConfigChangeEvent
>
configChangeFuture
=
SettableFuture
.
create
();
final
SettableFuture
<
ConfigChangeEvent
>
configChangeFuture
=
SettableFuture
.
create
();
ConfigChangeListener
someListener
=
new
ConfigChangeListener
()
{
ConfigChangeListener
someListener
=
new
ConfigChangeListener
()
{
...
@@ -85,8 +97,14 @@ public class SimpleConfigTest {
...
@@ -85,8 +97,14 @@ public class SimpleConfigTest {
};
};
SimpleConfig
config
=
new
SimpleConfig
(
someNamespace
,
configRepository
);
SimpleConfig
config
=
new
SimpleConfig
(
someNamespace
,
configRepository
);
assertEquals
(
someSourceType
,
config
.
getSourceType
());
config
.
addChangeListener
(
someListener
);
config
.
addChangeListener
(
someListener
);
ConfigSourceType
anotherSourceType
=
ConfigSourceType
.
REMOTE
;
when
(
configRepository
.
getSourceType
()).
thenReturn
(
anotherSourceType
);
config
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
config
.
onRepositoryChange
(
someNamespace
,
anotherProperties
);
ConfigChangeEvent
changeEvent
=
configChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
ConfigChangeEvent
changeEvent
=
configChangeFuture
.
get
(
500
,
TimeUnit
.
MILLISECONDS
);
...
@@ -108,5 +126,7 @@ public class SimpleConfigTest {
...
@@ -108,5 +126,7 @@ public class SimpleConfigTest {
assertEquals
(
null
,
newKeyChange
.
getOldValue
());
assertEquals
(
null
,
newKeyChange
.
getOldValue
());
assertEquals
(
newValue
,
newKeyChange
.
getNewValue
());
assertEquals
(
newValue
,
newKeyChange
.
getNewValue
());
assertEquals
(
PropertyChangeType
.
ADDED
,
newKeyChange
.
getChangeType
());
assertEquals
(
PropertyChangeType
.
ADDED
,
newKeyChange
.
getChangeType
());
assertEquals
(
anotherSourceType
,
config
.
getSourceType
());
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment