diff --git a/apollo-adminservice/pom.xml b/apollo-adminservice/pom.xml index 32555da791235f451eb1920607b6036959ff699e..35e306fb7cf7f3f15fbd71a6c6822ccc43612ce6 100644 --- a/apollo-adminservice/pom.xml +++ b/apollo-adminservice/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 diff --git a/apollo-assembly/pom.xml b/apollo-assembly/pom.xml index e5a14b7024e98df2e139a4cb1b7a9b65bca4d371..8cdbb696dbfea307df7484f56d6e5ba558684955 100644 --- a/apollo-assembly/pom.xml +++ b/apollo-assembly/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 diff --git a/apollo-biz/pom.xml b/apollo-biz/pom.xml index b548a84c7d2aba73e8cb2d75ac902cfb3cae1ea0..571c5328fca425df0cbc14ae8950d14547a08918 100644 --- a/apollo-biz/pom.xml +++ b/apollo-biz/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT 4.0.0 apollo-biz diff --git a/apollo-buildtools/pom.xml b/apollo-buildtools/pom.xml index bbfd77d568dc0fa0c5e3a65357f283bf6b5a298f..784d906a760e6042f3e682d83687bf8be7b38adc 100644 --- a/apollo-buildtools/pom.xml +++ b/apollo-buildtools/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 diff --git a/apollo-client/README.md b/apollo-client/README.md index 3191de5e64add19da579f8fdeefbe0de8a8f6676..60fac72e1cdb0579e1ebf5fff18bb32a08df5627 100644 --- a/apollo-client/README.md +++ b/apollo-client/README.md @@ -88,7 +88,7 @@ If you need this functionality, you could specify the cluster as follows: com.ctrip.framework.apollo apollo-client - 0.6.4 + 0.7.0 ## III. Client Usage diff --git a/apollo-client/pom.xml b/apollo-client/pom.xml index 9d4affec46f67e20264a68fa35ea6e849cfc2ae5..fc9c3e1a752b48d25512f450858fdcb50cecf9ff 100644 --- a/apollo-client/pom.xml +++ b/apollo-client/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 @@ -21,12 +21,12 @@ apollo-core - + - org.unidal.framework - foundation-service + com.google.inject + guice - + org.slf4j @@ -61,19 +61,5 @@ test - - - org.unidal.framework - dal-jdbc - 2.4.0 - true - - - com.dianping.cat - cat-client - - - - diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java index d2e8f4865e3b423305ab78f19e023ce84a696bed..1b1aacb477d7edc07dcb784e53a17ddb66a4b4e6 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java @@ -1,16 +1,11 @@ package com.ctrip.framework.apollo; +import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; -import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.internals.ConfigManager; import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigRegistry; -import com.ctrip.framework.apollo.tracer.Tracer; - -import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.unidal.lookup.ContainerLoader; /** * Entry point for client config use @@ -20,25 +15,14 @@ import org.unidal.lookup.ContainerLoader; public class ConfigService { private static final ConfigService s_instance = new ConfigService(); - private PlexusContainer m_container; private volatile ConfigManager m_configManager; private volatile ConfigRegistry m_configRegistry; - private ConfigService() { - m_container = ContainerLoader.getDefaultContainer(); - } - private ConfigManager getManager() { if (m_configManager == null) { synchronized (this) { if (m_configManager == null) { - try { - m_configManager = m_container.lookup(ConfigManager.class); - } catch (ComponentLookupException ex) { - ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigManager!", ex); - Tracer.logError(exception); - throw exception; - } + m_configManager = ApolloInjector.getInstance(ConfigManager.class); } } } @@ -50,13 +34,7 @@ public class ConfigService { if (m_configRegistry == null) { synchronized (this) { if (m_configRegistry == null) { - try { - m_configRegistry = m_container.lookup(ConfigRegistry.class); - } catch (ComponentLookupException ex) { - ApolloConfigException exception = new ApolloConfigException("Unable to load ConfigRegistry!", ex); - Tracer.logError(exception); - throw exception; - } + m_configRegistry = ApolloInjector.getInstance(ConfigRegistry.class); } } } @@ -127,9 +105,8 @@ public class ConfigService { } // for test only - static void setContainer(PlexusContainer m_container) { + static void reset() { synchronized (s_instance) { - s_instance.m_container = m_container; s_instance.m_configManager = null; s_instance.m_configRegistry = null; } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/build/ApolloInjector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/build/ApolloInjector.java new file mode 100644 index 0000000000000000000000000000000000000000..4c01e396d76c78a34a0ea0a753a1cb4d6e007f55 --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/build/ApolloInjector.java @@ -0,0 +1,51 @@ +package com.ctrip.framework.apollo.build; + +import com.ctrip.framework.apollo.exceptions.ApolloConfigException; +import com.ctrip.framework.apollo.internals.Injector; +import com.ctrip.framework.apollo.tracer.Tracer; +import com.ctrip.framework.foundation.internals.ServiceBootstrap; + +/** + * @author Jason Song(song_s@ctrip.com) + */ +public class ApolloInjector { + private static Injector s_injector; + private static Object lock = new Object(); + + private static Injector getInjector() { + if (s_injector == null) { + synchronized (lock) { + if (s_injector == null) { + try { + s_injector = ServiceBootstrap.loadFirst(Injector.class); + } catch (Throwable ex) { + ApolloConfigException exception = new ApolloConfigException("Unable to initialize Apollo Injector!", ex); + Tracer.logError(exception); + throw exception; + } + } + } + } + + return s_injector; + } + + public static T getInstance(Class clazz) { + try { + return getInjector().getInstance(clazz); + } catch (Throwable ex) { + Tracer.logError(ex); + throw new ApolloConfigException(String.format("Unable to load instance for type %s!", clazz.getName()), ex); + } + } + + public static T getInstance(Class clazz, String name) { + try { + return getInjector().getInstance(clazz, name); + } catch (Throwable ex) { + Tracer.logError(ex); + throw new ApolloConfigException( + String.format("Unable to load instance for type %s and name %s !", clazz.getName(), name), ex); + } + } +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/build/ComponentConfigurator.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/build/ComponentConfigurator.java deleted file mode 100644 index fa50ec0756928bca0a1c8251fa777ca311d990b5..0000000000000000000000000000000000000000 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/build/ComponentConfigurator.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.ctrip.framework.apollo.build; - -import com.ctrip.framework.apollo.internals.ConfigServiceLocator; -import com.ctrip.framework.apollo.internals.DefaultConfigManager; -import com.ctrip.framework.apollo.internals.RemoteConfigLongPollService; -import com.ctrip.framework.apollo.spi.DefaultConfigFactory; -import com.ctrip.framework.apollo.spi.DefaultConfigFactoryManager; -import com.ctrip.framework.apollo.spi.DefaultConfigRegistry; -import com.ctrip.framework.apollo.util.ConfigUtil; -import com.ctrip.framework.apollo.util.http.HttpUtil; - -import org.unidal.lookup.configuration.AbstractResourceConfigurator; -import org.unidal.lookup.configuration.Component; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Jason Song(song_s@ctrip.com) - */ -public class ComponentConfigurator extends AbstractResourceConfigurator { - public static void main(String[] args) { - generatePlexusComponentsXmlFile(new ComponentConfigurator()); - } - - @Override - public List defineComponents() { - List all = new ArrayList<>(); - - all.add(A(DefaultConfigManager.class)); - all.add(A(DefaultConfigFactory.class)); - all.add(A(DefaultConfigRegistry.class)); - all.add(A(DefaultConfigFactoryManager.class)); - all.add(A(ConfigUtil.class)); - all.add(A(HttpUtil.class)); - all.add(A(ConfigServiceLocator.class)); - all.add(A(RemoteConfigLongPollService.class)); - - return all; - } -} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/ds/ApolloDataSourceProvider.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/ds/ApolloDataSourceProvider.java deleted file mode 100644 index 5222674219ca86207f7a3b897afa34434ba16cc2..0000000000000000000000000000000000000000 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/ds/ApolloDataSourceProvider.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.ctrip.framework.apollo.ds; - -import com.ctrip.framework.apollo.ConfigFile; -import com.ctrip.framework.apollo.ConfigService; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; -import com.ctrip.framework.foundation.Foundation; - -import org.codehaus.plexus.logging.LogEnabled; -import org.codehaus.plexus.logging.Logger; -import org.unidal.dal.jdbc.datasource.DataSourceProvider; -import org.unidal.dal.jdbc.datasource.model.entity.DataSourcesDef; -import org.unidal.dal.jdbc.datasource.model.transform.DefaultSaxParser; -import org.unidal.lookup.annotation.Named; - -/** - * Data source provider based on Apollo configuration service. - *

- * - * Use following component definition to replace default - * DataSourceProvider: - *

- *

- *   public List defineComponents() {
- *      List all = new ArrayList<>();
- * 
- *      all.add(A(ApolloDataSourceProvider.class));
- * 
- *      return all;
- *   }
- * 
- * - * WARNING: all defined DataSourceProvider components will - * be taken affect. DO NOT define unused DataSourceProvider - * component. - */ -@Named(type = DataSourceProvider.class, value = "apollo") -public class ApolloDataSourceProvider implements DataSourceProvider, LogEnabled { - private Logger m_logger; - - private DataSourcesDef m_def; - - @Override - public DataSourcesDef defineDatasources() { - if (m_def == null) { - ConfigFile file = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML); - String appId = Foundation.app().getAppId(); - String envType = Foundation.server().getEnvType(); - - if (file != null && file.hasContent()) { - String content = file.getContent(); - - m_logger.info(String.format("Found datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId)); - - try { - m_def = DefaultSaxParser.parse(content); - } catch (Exception e) { - throw new IllegalStateException(String.format("Error when parsing datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId), e); - } - } else { - m_logger.warn(String.format("Can't get datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId)); - m_def = new DataSourcesDef(); - } - } - - return m_def; - } - - @Override - public void enableLogging(Logger logger) { - m_logger = logger; - } -} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java index a3c13a7d0d8e2b17c2595918359b8dd48b30c873..965301c94082ee969d56298123fcae1e0c4f933b 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java @@ -1,15 +1,21 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Function; -import com.google.common.base.Objects; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicLong; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; @@ -20,21 +26,13 @@ import com.ctrip.framework.apollo.tracer.spi.Transaction; import com.ctrip.framework.apollo.util.ConfigUtil; import com.ctrip.framework.apollo.util.function.Functions; import com.ctrip.framework.apollo.util.parser.Parsers; - -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.unidal.lookup.ContainerLoader; - -import java.util.Date; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.atomic.AtomicLong; +import com.google.common.base.Function; +import com.google.common.base.Objects; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; /** * @author Jason Song(song_s@ctrip.com) @@ -65,15 +63,10 @@ public abstract class AbstractConfig implements Config { } public AbstractConfig() { - try { - m_configUtil = ContainerLoader.getDefaultContainer().lookup(ConfigUtil.class); + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); m_configVersion = new AtomicLong(); m_arrayCache = Maps.newConcurrentMap(); allCaches = Lists.newArrayList(); - } catch (ComponentLookupException ex) { - Tracer.logError(ex); - throw new ApolloConfigException("Unable to load component!", ex); - } } @Override diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java index 5b21165c7f935cf881be223c9f37040850ddaf4c..c6bb2ffd1e51adfb9ab18583144aa9cf83b6762d 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigFile.java @@ -1,14 +1,14 @@ package com.ctrip.framework.apollo.internals; -import com.ctrip.framework.apollo.ConfigFile; -import com.ctrip.framework.apollo.tracer.Tracer; -import com.ctrip.framework.apollo.util.ExceptionUtil; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Properties; -import java.util.concurrent.atomic.AtomicReference; +import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.tracer.Tracer; +import com.ctrip.framework.apollo.util.ExceptionUtil; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java index cb8365066892fb4b079f8be9a02d8000b6060ffc..7d378febb46ae053c4af6b91e94d95d6b4a9c422 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfigRepository.java @@ -1,15 +1,14 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.collect.Lists; - -import com.ctrip.framework.apollo.tracer.Tracer; -import com.ctrip.framework.apollo.util.ExceptionUtil; +import java.util.List; +import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; -import java.util.Properties; +import com.ctrip.framework.apollo.tracer.Tracer; +import com.ctrip.framework.apollo.util.ExceptionUtil; +import com.google.common.collect.Lists; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java index eb5654160a77ddd2115768d05a75acf1fcd7e796..fcd13b4424e1d9e0ce22721c2fcba518d42cd47a 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java @@ -1,13 +1,16 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Joiner; -import com.google.common.base.Strings; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.escape.Escaper; -import com.google.common.net.UrlEscapers; -import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.atomic.AtomicReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.dto.ServiceDTO; import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; @@ -18,28 +21,17 @@ import com.ctrip.framework.apollo.util.ExceptionUtil; import com.ctrip.framework.apollo.util.http.HttpRequest; import com.ctrip.framework.apollo.util.http.HttpResponse; import com.ctrip.framework.apollo.util.http.HttpUtil; +import com.google.common.base.Joiner; +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.escape.Escaper; +import com.google.common.net.UrlEscapers; +import com.google.gson.reflect.TypeToken; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.unidal.lookup.annotation.Inject; -import org.unidal.lookup.annotation.Named; - -import java.lang.reflect.Type; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; - -@Named(type = ConfigServiceLocator.class) -public class ConfigServiceLocator implements Initializable { +public class ConfigServiceLocator { private static final Logger logger = LoggerFactory.getLogger(ConfigServiceLocator.class); - @Inject private HttpUtil m_httpUtil; - @Inject private ConfigUtil m_configUtil; private AtomicReference> m_configServices; private Type m_responseType; @@ -55,12 +47,10 @@ public class ConfigServiceLocator implements Initializable { m_configServices = new AtomicReference<>(initial); m_responseType = new TypeToken>() { }.getType(); + m_httpUtil = ApolloInjector.getInstance(HttpUtil.class); + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); this.m_executorService = Executors.newScheduledThreadPool(1, ApolloThreadFactory.create("ConfigServiceLocator", true)); - } - - @Override - public void initialize() throws InitializationException { this.tryUpdateConfigServices(); this.schedulePeriodicRefresh(); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java index 2dca21794e4ff4e1621e94579a4be24665d13bc6..eb7502c5aea772df63243c6a6b1531fdda3fc4c1 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java @@ -1,17 +1,5 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.collect.ImmutableMap; - -import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; -import com.ctrip.framework.apollo.enums.PropertyChangeType; -import com.ctrip.framework.apollo.model.ConfigChange; -import com.ctrip.framework.apollo.model.ConfigChangeEvent; -import com.ctrip.framework.apollo.tracer.Tracer; -import com.ctrip.framework.apollo.util.ExceptionUtil; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; import java.io.InputStream; import java.util.Collections; @@ -22,6 +10,17 @@ import java.util.Properties; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; +import com.ctrip.framework.apollo.enums.PropertyChangeType; +import com.ctrip.framework.apollo.model.ConfigChange; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.ctrip.framework.apollo.tracer.Tracer; +import com.ctrip.framework.apollo.util.ExceptionUtil; +import com.google.common.collect.ImmutableMap; + /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java index 36b360e2748987a0897dbc2245e1fc9fa374ec68..e0de79f8a991ef94e010a48f89c2d5c819c87ed4 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java @@ -1,29 +1,28 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.collect.Maps; +import java.util.Map; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactoryManager; - -import org.unidal.lookup.annotation.Inject; -import org.unidal.lookup.annotation.Named; - -import java.util.Map; +import com.google.common.collect.Maps; /** * @author Jason Song(song_s@ctrip.com) */ -@Named(type = ConfigManager.class) public class DefaultConfigManager implements ConfigManager { - @Inject private ConfigFactoryManager m_factoryManager; private Map m_configs = Maps.newConcurrentMap(); private Map m_configFiles = Maps.newConcurrentMap(); + public DefaultConfigManager() { + m_factoryManager = ApolloInjector.getInstance(ConfigFactoryManager.class); + } + @Override public Config getConfig(String namespace) { Config config = m_configs.get(namespace); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultInjector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultInjector.java new file mode 100644 index 0000000000000000000000000000000000000000..46e935ff7f8355d34f9b9303c24ec01e5f404a10 --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultInjector.java @@ -0,0 +1,65 @@ +package com.ctrip.framework.apollo.internals; + +import com.ctrip.framework.apollo.exceptions.ApolloConfigException; +import com.ctrip.framework.apollo.spi.ConfigFactory; +import com.ctrip.framework.apollo.spi.ConfigFactoryManager; +import com.ctrip.framework.apollo.spi.ConfigRegistry; +import com.ctrip.framework.apollo.spi.DefaultConfigFactory; +import com.ctrip.framework.apollo.spi.DefaultConfigFactoryManager; +import com.ctrip.framework.apollo.spi.DefaultConfigRegistry; +import com.ctrip.framework.apollo.tracer.Tracer; +import com.ctrip.framework.apollo.util.ConfigUtil; +import com.ctrip.framework.apollo.util.http.HttpUtil; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Singleton; + +/** + * Guice injector + * @author Jason Song(song_s@ctrip.com) + */ +public class DefaultInjector implements Injector { + private com.google.inject.Injector m_injector; + + public DefaultInjector() { + try { + m_injector = Guice.createInjector(new ApolloModule()); + } catch (Throwable ex) { + ApolloConfigException exception = new ApolloConfigException("Unable to initialize Guice Injector!", ex); + Tracer.logError(exception); + throw exception; + } + } + + @Override + public T getInstance(Class clazz) { + try { + return m_injector.getInstance(clazz); + } catch (Throwable ex) { + Tracer.logError(ex); + throw new ApolloConfigException( + String.format("Unable to load instance for %s!", clazz.getName()), ex); + } + } + + @Override + public T getInstance(Class clazz, String name) { + //Guice does not support get instance by type and name + return null; + } + + private static class ApolloModule extends AbstractModule { + @Override + protected void configure() { + bind(ConfigManager.class).to(DefaultConfigManager.class).in(Singleton.class); + bind(ConfigFactoryManager.class).to(DefaultConfigFactoryManager.class).in(Singleton.class); + bind(ConfigRegistry.class).to(DefaultConfigRegistry.class).in(Singleton.class); + bind(ConfigFactory.class).to(DefaultConfigFactory.class).in(Singleton.class); + bind(ConfigUtil.class).in(Singleton.class); + bind(HttpUtil.class).in(Singleton.class); + bind(ConfigServiceLocator.class).in(Singleton.class); + bind(RemoteConfigLongPollService.class).in(Singleton.class); + } + } +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/Injector.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/Injector.java new file mode 100644 index 0000000000000000000000000000000000000000..81add45b630bd9985f3dd93b58702a218ec583f6 --- /dev/null +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/Injector.java @@ -0,0 +1,17 @@ +package com.ctrip.framework.apollo.internals; + +/** + * @author Jason Song(song_s@ctrip.com) + */ +public interface Injector { + + /** + * Returns the appropriate instance for the given injection type + */ + T getInstance(Class clazz); + + /** + * Returns the appropriate instance for the given injection type and name + */ + T getInstance(Class clazz, String name); +} diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java index 9111157b9a4baf2ffdcfd027d98549e9269f9943..08c24cd432ea1dc17559971a4e0088b9b6e47b4a 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java @@ -1,22 +1,5 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; - -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; -import com.ctrip.framework.apollo.exceptions.ApolloConfigException; -import com.ctrip.framework.apollo.tracer.Tracer; -import com.ctrip.framework.apollo.tracer.spi.Transaction; -import com.ctrip.framework.apollo.util.ConfigUtil; -import com.ctrip.framework.apollo.util.ExceptionUtil; - -import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.unidal.lookup.ContainerLoader; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -28,6 +11,20 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.ctrip.framework.apollo.build.ApolloInjector; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; +import com.ctrip.framework.apollo.exceptions.ApolloConfigException; +import com.ctrip.framework.apollo.tracer.Tracer; +import com.ctrip.framework.apollo.tracer.spi.Transaction; +import com.ctrip.framework.apollo.util.ConfigUtil; +import com.ctrip.framework.apollo.util.ExceptionUtil; +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; + /** * @author Jason Song(song_s@ctrip.com) */ @@ -35,7 +32,6 @@ public class LocalFileConfigRepository extends AbstractConfigRepository implements RepositoryChangeListener { private static final Logger logger = LoggerFactory.getLogger(LocalFileConfigRepository.class); private static final String CONFIG_DIR = "/config-cache"; - private final PlexusContainer m_container; private final String m_namespace; private File m_baseDir; private final ConfigUtil m_configUtil; @@ -53,13 +49,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository public LocalFileConfigRepository(String namespace, ConfigRepository upstream) { m_namespace = namespace; - m_container = ContainerLoader.getDefaultContainer(); - try { - m_configUtil = m_container.lookup(ConfigUtil.class); - } catch (ComponentLookupException ex) { - Tracer.logError(ex); - throw new ApolloConfigException("Unable to load component!", ex); - } + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); this.setLocalCacheDir(findLocalCacheDir(), false); this.setUpstreamRepository(upstream); this.trySync(); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PropertiesConfigFile.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PropertiesConfigFile.java index 7c1052da6f3c685078096c8e55cad83ba03ed44a..ee9e81027892902a23d2cfdaf6d5fc6ade3af662 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PropertiesConfigFile.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/PropertiesConfigFile.java @@ -1,17 +1,17 @@ package com.ctrip.framework.apollo.internals; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.utils.PropertiesUtil; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.tracer.Tracer; import com.ctrip.framework.apollo.util.ExceptionUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Properties; -import java.util.concurrent.atomic.AtomicReference; - /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java index 9dfd1c5684f5bd76353a8c7a66140b9c01e6f425..73ffbb8a4c2179b1c0d6b7ae4c6f1cee8f7c346c 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java @@ -1,18 +1,19 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Joiner; -import com.google.common.base.Strings; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; -import com.google.common.collect.Multimaps; -import com.google.common.escape.Escaper; -import com.google.common.net.UrlEscapers; -import com.google.common.reflect.TypeToken; -import com.google.common.util.concurrent.RateLimiter; -import com.google.gson.Gson; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; import com.ctrip.framework.apollo.core.dto.ServiceDTO; @@ -28,29 +29,23 @@ import com.ctrip.framework.apollo.util.ExceptionUtil; import com.ctrip.framework.apollo.util.http.HttpRequest; import com.ctrip.framework.apollo.util.http.HttpResponse; import com.ctrip.framework.apollo.util.http.HttpUtil; - -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.unidal.lookup.annotation.Inject; -import org.unidal.lookup.annotation.Named; - -import java.lang.reflect.Type; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; +import com.google.common.base.Joiner; +import com.google.common.base.Strings; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import com.google.common.escape.Escaper; +import com.google.common.net.UrlEscapers; +import com.google.common.reflect.TypeToken; +import com.google.common.util.concurrent.RateLimiter; +import com.google.gson.Gson; /** * @author Jason Song(song_s@ctrip.com) */ -@Named(type = RemoteConfigLongPollService.class) -public class RemoteConfigLongPollService implements Initializable { +public class RemoteConfigLongPollService { private static final Logger logger = LoggerFactory.getLogger(RemoteConfigLongPollService.class); private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR); private static final Joiner.MapJoiner MAP_JOINER = Joiner.on("&").withKeyValueSeparator("="); @@ -65,11 +60,8 @@ public class RemoteConfigLongPollService implements Initializable { private final ConcurrentMap m_notifications; private Type m_responseType; private Gson gson; - @Inject private ConfigUtil m_configUtil; - @Inject private HttpUtil m_httpUtil; - @Inject private ConfigServiceLocator m_serviceLocator; /** @@ -87,10 +79,9 @@ public class RemoteConfigLongPollService implements Initializable { m_responseType = new TypeToken>() { }.getType(); gson = new Gson(); - } - - @Override - public void initialize() throws InitializationException { + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); + m_httpUtil = ApolloInjector.getInstance(HttpUtil.class); + m_serviceLocator = ApolloInjector.getInstance(ConfigServiceLocator.class); m_longPollRateLimiter = RateLimiter.create(m_configUtil.getLongPollQPS()); } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java index efb69866c3260bf5f07e4181eb56648380af8bf3..325ffe1745e2fd186f4d1f0a5ffd4c3a5802fe4b 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java @@ -1,14 +1,19 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Joiner; -import com.google.common.base.Strings; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.escape.Escaper; -import com.google.common.net.UrlEscapers; -import com.google.common.util.concurrent.RateLimiter; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.ctrip.framework.apollo.Apollo; +import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.dto.ApolloConfig; import com.ctrip.framework.apollo.core.dto.ServiceDTO; @@ -22,21 +27,13 @@ import com.ctrip.framework.apollo.util.ExceptionUtil; import com.ctrip.framework.apollo.util.http.HttpRequest; import com.ctrip.framework.apollo.util.http.HttpResponse; import com.ctrip.framework.apollo.util.http.HttpUtil; - -import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.unidal.lookup.ContainerLoader; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; +import com.google.common.base.Joiner; +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.escape.Escaper; +import com.google.common.net.UrlEscapers; +import com.google.common.util.concurrent.RateLimiter; /** * @author Jason Song(song_s@ctrip.com) @@ -45,11 +42,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository { 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.MapJoiner MAP_JOINER = Joiner.on("&").withKeyValueSeparator("="); - private PlexusContainer m_container; - private final ConfigServiceLocator m_serviceLocator; - private final HttpUtil m_httpUtil; - private final ConfigUtil m_configUtil; - private final RemoteConfigLongPollService remoteConfigLongPollService; + private ConfigServiceLocator m_serviceLocator; + private HttpUtil m_httpUtil; + private ConfigUtil m_configUtil; + private RemoteConfigLongPollService remoteConfigLongPollService; private volatile AtomicReference m_configCache; private final String m_namespace; private final static ScheduledExecutorService m_executorService; @@ -71,16 +67,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository { public RemoteConfigRepository(String namespace) { m_namespace = namespace; m_configCache = new AtomicReference<>(); - m_container = ContainerLoader.getDefaultContainer(); - try { - m_configUtil = m_container.lookup(ConfigUtil.class); - m_httpUtil = m_container.lookup(HttpUtil.class); - m_serviceLocator = m_container.lookup(ConfigServiceLocator.class); - remoteConfigLongPollService = m_container.lookup(RemoteConfigLongPollService.class); - } catch (ComponentLookupException ex) { - Tracer.logError(ex); - throw new ApolloConfigException("Unable to load component!", ex); - } + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); + m_httpUtil = ApolloInjector.getInstance(HttpUtil.class); + m_serviceLocator = ApolloInjector.getInstance(ConfigServiceLocator.class); + remoteConfigLongPollService = ApolloInjector.getInstance(RemoteConfigLongPollService.class); m_longPollServiceDto = new AtomicReference<>(); m_loadConfigRateLimiter = RateLimiter.create(m_configUtil.getLoadConfigQPS()); this.trySync(); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java index 6442c02a740ba2d8503ade163b73292ee807a1a7..23633f5307d16c64990f5c7406bfe80eaffdfcfd 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/SimpleConfig.java @@ -1,22 +1,21 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Function; -import com.google.common.collect.Maps; - -import com.ctrip.framework.apollo.model.ConfigChange; -import com.ctrip.framework.apollo.model.ConfigChangeEvent; -import com.ctrip.framework.apollo.tracer.Tracer; -import com.ctrip.framework.apollo.util.ExceptionUtil; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.ctrip.framework.apollo.model.ConfigChange; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.ctrip.framework.apollo.tracer.Tracer; +import com.ctrip.framework.apollo.util.ExceptionUtil; +import com.google.common.base.Function; +import com.google.common.collect.Maps; + /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java index 1e05e4343682fda648118c3ce563913825566b41..221d3c53eca7feda2695c1f3a759ce6b11d26545 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java @@ -1,7 +1,11 @@ package com.ctrip.framework.apollo.spi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.internals.ConfigRepository; import com.ctrip.framework.apollo.internals.DefaultConfig; @@ -14,20 +18,17 @@ import com.ctrip.framework.apollo.internals.YamlConfigFile; import com.ctrip.framework.apollo.internals.YmlConfigFile; import com.ctrip.framework.apollo.util.ConfigUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.unidal.lookup.annotation.Inject; -import org.unidal.lookup.annotation.Named; - /** * @author Jason Song(song_s@ctrip.com) */ -@Named(type = ConfigFactory.class) public class DefaultConfigFactory implements ConfigFactory { private static final Logger logger = LoggerFactory.getLogger(DefaultConfigFactory.class); - @Inject private ConfigUtil m_configUtil; + public DefaultConfigFactory() { + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); + } + @Override public Config create(String namespace) { DefaultConfig defaultConfig = diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManager.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManager.java index c34d5d1f1d22589fc103575f958134224288fc47..58bd7ebd6679129d55cbd7a08e3eb30d65cc5360 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManager.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManager.java @@ -1,24 +1,22 @@ package com.ctrip.framework.apollo.spi; -import com.google.common.collect.Maps; - -import org.unidal.lookup.ContainerHolder; -import org.unidal.lookup.LookupException; -import org.unidal.lookup.annotation.Inject; -import org.unidal.lookup.annotation.Named; - import java.util.Map; +import com.ctrip.framework.apollo.build.ApolloInjector; +import com.google.common.collect.Maps; + /** * @author Jason Song(song_s@ctrip.com) */ -@Named(type = ConfigFactoryManager.class) -public class DefaultConfigFactoryManager extends ContainerHolder implements ConfigFactoryManager { - @Inject +public class DefaultConfigFactoryManager implements ConfigFactoryManager { private ConfigRegistry m_registry; private Map m_factories = Maps.newConcurrentMap(); + public DefaultConfigFactoryManager() { + m_registry = ApolloInjector.getInstance(ConfigRegistry.class); + } + @Override public ConfigFactory getFactory(String namespace) { // step 1: check hacked factory @@ -36,16 +34,14 @@ public class DefaultConfigFactoryManager extends ContainerHolder implements Conf } // step 3: check declared config factory - try { - factory = lookup(ConfigFactory.class, namespace); - } catch (LookupException ex) { - // ignore it + factory = ApolloInjector.getInstance(ConfigFactory.class, namespace); + + if (factory != null) { + return factory; } // step 4: check default config factory - if (factory == null) { - factory = lookup(ConfigFactory.class); - } + factory = ApolloInjector.getInstance(ConfigFactory.class); m_factories.put(namespace, factory); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistry.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistry.java index c95f2f176cc5022bf2b36d5ffff208559191d983..537a6ecd0ab21646a1d9f0fd2d2619bf6f8ece87 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistry.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistry.java @@ -1,27 +1,23 @@ package com.ctrip.framework.apollo.spi; -import com.google.common.collect.Maps; +import java.util.Map; -import org.codehaus.plexus.logging.LogEnabled; -import org.codehaus.plexus.logging.Logger; -import org.unidal.lookup.annotation.Named; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.util.Map; +import com.google.common.collect.Maps; /** * @author Jason Song(song_s@ctrip.com) */ -@Named(type = ConfigRegistry.class) -public class DefaultConfigRegistry implements ConfigRegistry, LogEnabled { +public class DefaultConfigRegistry implements ConfigRegistry { + private static final Logger s_logger = LoggerFactory.getLogger(DefaultConfigRegistry.class); private Map m_instances = Maps.newConcurrentMap(); - private Logger m_logger; - @Override public void register(String namespace, ConfigFactory factory) { if (m_instances.containsKey(namespace)) { - m_logger.warn( - String.format("ConfigFactory(%s) is overridden by %s!", namespace, factory.getClass())); + s_logger.warn("ConfigFactory({}) is overridden by {}!", namespace, factory.getClass()); } m_instances.put(namespace, factory); @@ -33,9 +29,4 @@ public class DefaultConfigRegistry implements ConfigRegistry, LogEnabled { return config; } - - @Override - public void enableLogging(Logger logger) { - m_logger = logger; - } } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java index 34529917834eafa3b94c46fa1ee9990978c119d8..e6fc7f9c2e57643f0906863cf4290ea599bbb080 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java @@ -1,11 +1,7 @@ package com.ctrip.framework.apollo.spring.annotation; -import com.google.common.base.Preconditions; - -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigChangeListener; -import com.ctrip.framework.apollo.ConfigService; -import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; @@ -14,8 +10,11 @@ import org.springframework.core.PriorityOrdered; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.ReflectionUtils; -import java.lang.reflect.Field; -import java.lang.reflect.Method; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.ConfigService; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.google.common.base.Preconditions; /** * Apollo Annotation Processor for Spring Application diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfig.java index 77962215583b948f57bc34340beeb0f53a600f85..731bd6f5839a72922f173566ee37fe6ce552a7fe 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfig.java @@ -1,13 +1,13 @@ package com.ctrip.framework.apollo.spring.annotation; -import com.ctrip.framework.apollo.core.ConfigConsts; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import com.ctrip.framework.apollo.core.ConfigConsts; + /** * Use this annotation to inject Apollo Config Instance. * diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigChangeListener.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigChangeListener.java index 81d09bae82e764ef4b174b0fe7345ec727602d05..02e2a94783c0e6e7d4e671297bde4e7058852dcc 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigChangeListener.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigChangeListener.java @@ -1,13 +1,13 @@ package com.ctrip.framework.apollo.spring.annotation; -import com.ctrip.framework.apollo.core.ConfigConsts; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import com.ctrip.framework.apollo.core.ConfigConsts; + /** * Use this annotation to register Apollo ConfigChangeListener. * diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigRegistrar.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigRegistrar.java index 642d3217ad3965d6fdbb937eed070ef68e01c122..03d55be90a47a93851f4b9288ed78b4029a0fbe4 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigRegistrar.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigRegistrar.java @@ -1,16 +1,15 @@ package com.ctrip.framework.apollo.spring.annotation; -import com.google.common.collect.Lists; - -import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; -import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil; - import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; +import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; +import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil; +import com.google.common.collect.Lists; + /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/EnableApolloConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/EnableApolloConfig.java index 260bd6b0e6b47051dd3251d3c3c845ab89061f7b..9572f970080c4eb46682afc44b6d9b6f4c3ed1b3 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/EnableApolloConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/EnableApolloConfig.java @@ -1,16 +1,16 @@ package com.ctrip.framework.apollo.spring.annotation; -import com.ctrip.framework.apollo.core.ConfigConsts; - -import org.springframework.context.annotation.Import; -import org.springframework.core.Ordered; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.context.annotation.Import; +import org.springframework.core.Ordered; + +import com.ctrip.framework.apollo.core.ConfigConsts; + /** * Use this annotation to register Apollo property sources when using Java Config. * diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySource.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySource.java index 57e4a46bdf40901abed3de412de6cab92ede1315..9a53fd654db83e33af95f489cad3bbd51dfa5153 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySource.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySource.java @@ -1,6 +1,7 @@ package com.ctrip.framework.apollo.spring.config; import java.util.Set; + import org.springframework.core.env.EnumerablePropertySource; import com.ctrip.framework.apollo.Config; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourcesProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourcesProcessor.java index 3ec97339c36694c7ac01ec9893ad05e731d7e723..1cc082be9fa19daca371101803f843526b1839bf 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourcesProcessor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourcesProcessor.java @@ -1,13 +1,13 @@ package com.ctrip.framework.apollo.spring.config; -import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor; -import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil; - import org.springframework.beans.BeansException; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor; +import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil; + /** * Apollo Property Sources processor for Spring XML Based Application * diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/NamespaceHandler.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/NamespaceHandler.java index b4626b6b67668e5c4109ae21f1bc4b568411243b..31dcf0f1506ef0e44fcf3f05639f8b1ea828541e 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/NamespaceHandler.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/NamespaceHandler.java @@ -1,16 +1,15 @@ package com.ctrip.framework.apollo.spring.config; -import com.google.common.base.Splitter; -import com.google.common.base.Strings; - -import com.ctrip.framework.apollo.core.ConfigConsts; - import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; import org.springframework.core.Ordered; import org.w3c.dom.Element; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; + /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java index 048a6fab135dec4707d4ac34908e74f644cd8981..7594bfe5c663d8cce2c9c1c74b99db695d21b2de 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java @@ -1,11 +1,7 @@ package com.ctrip.framework.apollo.spring.config; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Multimap; - -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigService; +import java.util.Collection; +import java.util.Iterator; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; @@ -17,9 +13,11 @@ import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; -import java.util.Collection; -import java.util.Iterator; -import java.util.concurrent.atomic.AtomicBoolean; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigService; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.Multimap; /** * Apollo Property Sources processor for Spring Annotation Based Application diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/BeanRegistrationUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/BeanRegistrationUtil.java index 91aaea6c2153fbd9aaffe4a8feb4feeda8c21015..1be8f80fcba02b627f59f0b5e0157d5dc3a8254e 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/BeanRegistrationUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/BeanRegistrationUtil.java @@ -1,11 +1,11 @@ package com.ctrip.framework.apollo.spring.util; +import java.util.Objects; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import java.util.Objects; - /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java index 0cc9ec9b187bd0c5cac43d8dc2a73f2911a99cab..57420487028cf017e468c2f7cb210664fc8d692b 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java @@ -1,6 +1,9 @@ package com.ctrip.framework.apollo.util; -import com.google.common.base.Strings; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.MetaDomainConsts; @@ -8,17 +11,11 @@ import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.EnvUtils; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.foundation.Foundation; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.unidal.lookup.annotation.Named; - -import java.util.concurrent.TimeUnit; +import com.google.common.base.Strings; /** * @author Jason Song(song_s@ctrip.com) */ -@Named(type = ConfigUtil.class) public class ConfigUtil { private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class); private static final String TOOLING_CLUSTER = "tooling"; diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java index fe3c700cde293565faf611fdcdba5e710a5a4a8b..282d4cee77cd8d6d12c43f3e86f1ef7457b9e7b3 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java @@ -1,10 +1,10 @@ package com.ctrip.framework.apollo.util; +import java.util.List; + import com.google.common.base.Strings; import com.google.common.collect.Lists; -import java.util.List; - /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/function/Functions.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/function/Functions.java index 391cc7a9aba396e1f81980dc4a9e248d5bee72a3..f5e1ebfdb2f6cf3bcfbe7282e915ec32d8165c9f 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/function/Functions.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/function/Functions.java @@ -1,12 +1,11 @@ package com.ctrip.framework.apollo.util.function; -import com.google.common.base.Function; +import java.util.Date; import com.ctrip.framework.apollo.exceptions.ApolloConfigException; import com.ctrip.framework.apollo.util.parser.ParserException; import com.ctrip.framework.apollo.util.parser.Parsers; - -import java.util.Date; +import com.google.common.base.Function; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java index 13818aa37a999f60948ab02128c237a62d6757bb..49d11e0f2727b6ceaf244c5d7af9f128cb50f329 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java @@ -1,31 +1,26 @@ package com.ctrip.framework.apollo.util.http; -import com.google.common.base.Charsets; -import com.google.common.base.Function; -import com.google.common.io.BaseEncoding; -import com.google.gson.Gson; - -import com.ctrip.framework.apollo.exceptions.ApolloConfigException; -import com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException; -import com.ctrip.framework.apollo.util.ConfigUtil; - -import org.unidal.helper.Files; -import org.unidal.lookup.annotation.Inject; -import org.unidal.lookup.annotation.Named; - import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; +import com.ctrip.framework.apollo.build.ApolloInjector; +import com.ctrip.framework.apollo.exceptions.ApolloConfigException; +import com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException; +import com.ctrip.framework.apollo.util.ConfigUtil; +import com.google.common.base.Function; +import com.google.common.io.BaseEncoding; +import com.google.common.io.CharStreams; +import com.google.gson.Gson; + /** * @author Jason Song(song_s@ctrip.com) */ -@Named(type = HttpUtil.class) public class HttpUtil { - @Inject private ConfigUtil m_configUtil; private Gson gson; private String basicAuth; @@ -34,6 +29,7 @@ public class HttpUtil { * Constructor. */ public HttpUtil() { + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); gson = new Gson(); try { basicAuth = "Basic " + BaseEncoding.base64().encode("user:".getBytes("UTF-8")); @@ -82,7 +78,7 @@ public class HttpUtil { private HttpResponse doGetWithSerializeFunction(HttpRequest httpRequest, Function serializeFunction) { - InputStream is = null; + InputStreamReader isr = null; int statusCode; try { HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); @@ -108,8 +104,8 @@ public class HttpUtil { statusCode = conn.getResponseCode(); if (statusCode == 200) { - is = conn.getInputStream(); - String content = Files.IO.INSTANCE.readFrom(is, Charsets.UTF_8.name()); + isr = new InputStreamReader(conn.getInputStream()); + String content = CharStreams.toString(isr); return new HttpResponse<>(statusCode, serializeFunction.apply(content)); } @@ -120,10 +116,10 @@ public class HttpUtil { } catch (Throwable ex) { throw new ApolloConfigException("Could not complete get operation", ex); } finally { - if (is != null) { + if (isr != null) { try { - is.close(); - } catch (IOException ex) { + isr.close(); + } catch (IOException e) { // ignore } } diff --git a/apollo-client/src/main/resources/META-INF/plexus/components.xml b/apollo-client/src/main/resources/META-INF/plexus/components.xml deleted file mode 100644 index d0b2d333c02225a2c0a9ee6d99288ce5a27937ce..0000000000000000000000000000000000000000 --- a/apollo-client/src/main/resources/META-INF/plexus/components.xml +++ /dev/null @@ -1,75 +0,0 @@ - - - - com.ctrip.framework.apollo.internals.ConfigManager - com.ctrip.framework.apollo.internals.DefaultConfigManager - - - com.ctrip.framework.apollo.spi.ConfigFactoryManager - - - - - com.ctrip.framework.apollo.spi.ConfigFactory - com.ctrip.framework.apollo.spi.DefaultConfigFactory - - - com.ctrip.framework.apollo.util.ConfigUtil - - - - - com.ctrip.framework.apollo.spi.ConfigRegistry - com.ctrip.framework.apollo.spi.DefaultConfigRegistry - - - com.ctrip.framework.apollo.spi.ConfigFactoryManager - com.ctrip.framework.apollo.spi.DefaultConfigFactoryManager - - - com.ctrip.framework.apollo.spi.ConfigRegistry - - - - - com.ctrip.framework.apollo.util.ConfigUtil - com.ctrip.framework.apollo.util.ConfigUtil - - - com.ctrip.framework.apollo.util.http.HttpUtil - com.ctrip.framework.apollo.util.http.HttpUtil - - - com.ctrip.framework.apollo.util.ConfigUtil - - - - - com.ctrip.framework.apollo.internals.ConfigServiceLocator - com.ctrip.framework.apollo.internals.ConfigServiceLocator - - - com.ctrip.framework.apollo.util.http.HttpUtil - - - com.ctrip.framework.apollo.util.ConfigUtil - - - - - com.ctrip.framework.apollo.internals.RemoteConfigLongPollService - com.ctrip.framework.apollo.internals.RemoteConfigLongPollService - - - com.ctrip.framework.apollo.util.ConfigUtil - - - com.ctrip.framework.apollo.util.http.HttpUtil - - - com.ctrip.framework.apollo.internals.ConfigServiceLocator - - - - - diff --git a/apollo-client/src/main/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector b/apollo-client/src/main/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector new file mode 100644 index 0000000000000000000000000000000000000000..5ed606a64e0ad380b9e46c6e71712da33b674121 --- /dev/null +++ b/apollo-client/src/main/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector @@ -0,0 +1 @@ +com.ctrip.framework.apollo.internals.DefaultInjector \ No newline at end of file diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java index 4c42f19adc57eff4ecb6a601476f02c26b19a4d9..93798492e7ed5101e8feb292190754712c9391cc 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java @@ -1,6 +1,10 @@ package com.ctrip.framework.apollo; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + import com.ctrip.framework.apollo.integration.ConfigIntegrationTest; import com.ctrip.framework.apollo.internals.DefaultConfigManagerTest; import com.ctrip.framework.apollo.internals.DefaultConfigTest; @@ -22,10 +26,6 @@ import com.ctrip.framework.apollo.util.ExceptionUtilTest; import com.ctrip.framework.apollo.util.parser.DateParserTest; import com.ctrip.framework.apollo.util.parser.DurationParserTest; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - @RunWith(Suite.class) @SuiteClasses({ ConfigServiceTest.class, DefaultConfigRegistryTest.class, DefaultConfigFactoryManagerTest.class, diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java index e4bc8cf0a1628417bafd36b4b51f83c69323b01a..351d16c1fe219cc543e8aa0dbf03c691081e653f 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java @@ -1,14 +1,14 @@ package com.ctrip.framework.apollo; -import com.google.common.base.Charsets; -import com.google.common.collect.Lists; -import com.google.common.io.Files; -import com.google.gson.Gson; +import java.io.File; +import java.io.IOException; +import java.net.ServerSocket; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; -import com.ctrip.framework.apollo.core.dto.ServiceDTO; -import com.ctrip.framework.apollo.core.enums.Env; -import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; -import com.ctrip.framework.apollo.util.ConfigUtil; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; @@ -18,22 +18,22 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; -import org.unidal.lookup.ComponentTestCase; -import java.io.File; -import java.io.IOException; -import java.net.ServerSocket; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import com.ctrip.framework.apollo.build.MockInjector; +import com.ctrip.framework.apollo.core.dto.ServiceDTO; +import com.ctrip.framework.apollo.core.enums.Env; +import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; +import com.ctrip.framework.apollo.internals.DefaultInjector; +import com.ctrip.framework.apollo.util.ConfigUtil; +import com.google.common.base.Charsets; +import com.google.common.collect.Lists; +import com.google.common.io.Files; +import com.google.gson.Gson; /** * @author Jason Song(song_s@ctrip.com) */ -public abstract class BaseIntegrationTest extends ComponentTestCase { +public abstract class BaseIntegrationTest{ private static final int PORT = findFreePort(); private static final String metaServiceUrl = "http://localhost:" + PORT; private static final String someAppName = "someAppName"; @@ -56,8 +56,6 @@ public abstract class BaseIntegrationTest extends ComponentTestCase { @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); someAppId = "1003171"; someClusterName = "someClusterName"; someDataCenter = "someDC"; @@ -65,9 +63,11 @@ public abstract class BaseIntegrationTest extends ComponentTestCase { refreshTimeUnit = TimeUnit.MINUTES; //as ConfigService is singleton, so we must manually clear its container - ConfigService.setContainer(getContainer()); + ConfigService.reset(); + MockInjector.reset(); + MockInjector.setDelegate(new DefaultInjector()); - defineComponent(ConfigUtil.class, MockConfigUtil.class); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil()); } /** @@ -93,7 +93,6 @@ public abstract class BaseIntegrationTest extends ComponentTestCase { if (server != null && server.isStarted()) { server.stop(); } - super.tearDown(); } protected ContextHandler mockMetaServerHandler() { diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java index e8ab3b392041fef157d3691f0d5d4de8e99133e9..7944d7f3f1562f9ed71967df6a625ccc72efb1b3 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java @@ -1,34 +1,35 @@ package com.ctrip.framework.apollo; +import static org.junit.Assert.assertEquals; + +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; + +import com.ctrip.framework.apollo.build.MockInjector; import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.internals.AbstractConfig; +import com.ctrip.framework.apollo.internals.DefaultInjector; import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.util.ConfigUtil; -import org.junit.Before; -import org.junit.Test; -import org.unidal.lookup.ComponentTestCase; - -import java.util.Set; - -import static org.junit.Assert.assertEquals; - /** * @author Jason Song(song_s@ctrip.com) */ -public class ConfigServiceTest extends ComponentTestCase { +public class ConfigServiceTest { private static String someAppId; - @Override @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); someAppId = "someAppId"; + //as ConfigService is singleton, so we must manually clear its container - ConfigService.setContainer(getContainer()); - defineComponent(ConfigUtil.class, MockConfigUtil.class); + ConfigService.reset(); + MockInjector.reset(); + MockInjector.setDelegate(new DefaultInjector()); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil()); } @Test @@ -58,7 +59,7 @@ public class ConfigServiceTest extends ComponentTestCase { public void testMockConfigFactory() throws Exception { String someNamespace = "mock"; String someKey = "someKey"; - defineComponent(ConfigFactory.class, someNamespace, MockConfigFactory.class); + MockInjector.setInstance(ConfigFactory.class, someNamespace, new MockConfigFactory()); Config config = ConfigService.getConfig(someNamespace); @@ -72,7 +73,7 @@ public class ConfigServiceTest extends ComponentTestCase { ConfigFileFormat someConfigFileFormat = ConfigFileFormat.Properties; String someNamespaceFileName = String.format("%s.%s", someNamespace, someConfigFileFormat.getValue()); - defineComponent(ConfigFactory.class, someNamespaceFileName, MockConfigFactory.class); + MockInjector.setInstance(ConfigFactory.class, someNamespaceFileName, new MockConfigFactory()); ConfigFile configFile = ConfigService.getConfigFile(someNamespace, someConfigFileFormat); diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/build/MockInjector.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/build/MockInjector.java new file mode 100644 index 0000000000000000000000000000000000000000..e410ebe9726ab48c1e3763a5bcf06410f7e203d5 --- /dev/null +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/build/MockInjector.java @@ -0,0 +1,64 @@ +package com.ctrip.framework.apollo.build; + +import java.util.Map; + +import com.ctrip.framework.apollo.internals.DefaultInjector; +import com.ctrip.framework.apollo.internals.Injector; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Maps; +import com.google.common.collect.Table; + +/** + * @author Jason Song(song_s@ctrip.com) + */ +public class MockInjector implements Injector { + private static Map classMap = Maps.newHashMap(); + private static Table classTable = HashBasedTable.create(); + private static Injector delegate = new DefaultInjector(); + + @Override + public T getInstance(Class clazz) { + T o = (T) classMap.get(clazz); + if (o != null) { + return o; + } + + if (delegate != null) { + return delegate.getInstance(clazz); + } + + return null; + } + + @Override + public T getInstance(Class clazz, String name) { + T o = (T) classTable.get(clazz, name); + if (o != null) { + return o; + } + + if (delegate != null) { + return delegate.getInstance(clazz, name); + } + + return null; + } + + public static void setInstance(Class clazz, Object o) { + classMap.put(clazz, o); + } + + public static void setInstance(Class clazz, String name, Object o) { + classTable.put(clazz, name, o); + } + + public static void setDelegate(Injector delegateInjector) { + delegate = delegateInjector; + } + + public static void reset() { + classMap.clear(); + classTable.clear(); + delegate = null; + } +} diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java index 45861ca75163d19bc5f89604704e4b698d5feb91..7f8263ddb043ea5f67c3a396fcffa86c460e28d8 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/integration/ConfigIntegrationTest.java @@ -1,29 +1,9 @@ package com.ctrip.framework.apollo.integration; -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.util.concurrent.SettableFuture; - -import com.ctrip.framework.apollo.BaseIntegrationTest; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigChangeListener; -import com.ctrip.framework.apollo.ConfigService; -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.core.dto.ApolloConfig; -import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; -import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; -import com.ctrip.framework.apollo.internals.RemoteConfigLongPollService; -import com.ctrip.framework.apollo.model.ConfigChangeEvent; - -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.handler.AbstractHandler; -import org.eclipse.jetty.server.handler.ContextHandler; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.springframework.test.util.ReflectionTestUtils; +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; import java.io.File; import java.io.FileOutputStream; @@ -39,10 +19,30 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -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; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.test.util.ReflectionTestUtils; + +import com.ctrip.framework.apollo.BaseIntegrationTest; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.ConfigService; +import com.ctrip.framework.apollo.build.ApolloInjector; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.core.dto.ApolloConfig; +import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; +import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; +import com.ctrip.framework.apollo.internals.RemoteConfigLongPollService; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.util.concurrent.SettableFuture; /** * @author Jason Song(song_s@ctrip.com) @@ -66,7 +66,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { configDir.delete(); } configDir.mkdirs(); - remoteConfigLongPollService = lookup(RemoteConfigLongPollService.class); + remoteConfigLongPollService = ApolloInjector.getInstance(RemoteConfigLongPollService.class); } @Override diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java index d596db32e84d1f662bf4c20de9579aacf40f771b..574a4d893936c1c7a023cce903bfd6aa1c80a892 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java @@ -1,35 +1,36 @@ package com.ctrip.framework.apollo.internals; +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 java.util.Set; + +import org.junit.Before; +import org.junit.Test; + import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.build.MockInjector; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.spi.ConfigFactory; import com.ctrip.framework.apollo.spi.ConfigFactoryManager; - -import org.junit.Before; -import org.junit.Test; -import org.unidal.lookup.ComponentTestCase; - -import java.util.Set; - -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 com.ctrip.framework.apollo.util.ConfigUtil; /** * @author Jason Song(song_s@ctrip.com) */ -public class DefaultConfigManagerTest extends ComponentTestCase { +public class DefaultConfigManagerTest { private DefaultConfigManager defaultConfigManager; private static String someConfigContent; @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); - defineComponent(ConfigFactoryManager.class, MockConfigFactoryManager.class); - defaultConfigManager = (DefaultConfigManager) lookup(ConfigManager.class); + MockInjector.reset(); + MockInjector.setInstance(ConfigFactoryManager.class, new MockConfigFactoryManager()); + MockInjector.setInstance(ConfigUtil.class, new ConfigUtil()); + defaultConfigManager = new DefaultConfigManager(); someConfigContent = "someContent"; } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java index 44c9e475a3f53a887cd8607a649378e62e59d4c4..a82a04fc892c2ec2f819d61b26703a00d5d93dce 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java @@ -1,41 +1,40 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Charsets; -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableMap; -import com.google.common.io.Files; -import com.google.common.util.concurrent.SettableFuture; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.util.Calendar; +import java.util.Date; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.build.MockInjector; import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil; import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.model.ConfigChange; import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.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.Calendar; -import java.util.Date; -import java.util.Properties; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import com.google.common.base.Charsets; +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.Files; +import com.google.common.util.concurrent.SettableFuture; /** * @author Jason Song(song_s@ctrip.com) */ -public class DefaultConfigTest extends ComponentTestCase { +public class DefaultConfigTest { private File someResourceDir; private String someNamespace; private ConfigRepository configRepository; @@ -43,9 +42,8 @@ public class DefaultConfigTest extends ComponentTestCase { @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); - defineComponent(ConfigUtil.class, MockConfigUtil.class); + MockInjector.reset(); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil()); someResourceDir = new File(ClassLoaderUtil.getClassPath() + "/META-INF/config"); someResourceDir.mkdirs(); @@ -55,7 +53,6 @@ public class DefaultConfigTest extends ComponentTestCase { @After public void tearDown() throws Exception { - super.tearDown(); recursiveDelete(someResourceDir); } @@ -197,7 +194,7 @@ public class DefaultConfigTest extends ComponentTestCase { Integer someDefaultValue = -1; - defineComponent(ConfigUtil.class, MockConfigUtilWithSmallCache.class); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithSmallCache()); //set up config repo someProperties = mock(Properties.class); @@ -230,7 +227,7 @@ public class DefaultConfigTest extends ComponentTestCase { Integer someDefaultValue = -1; - defineComponent(ConfigUtil.class, MockConfigUtilWithShortExpireTime.class); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithShortExpireTime()); //set up config repo someProperties = mock(Properties.class); diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/JsonConfigFileTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/JsonConfigFileTest.java index b60162e4e1a14c06420cb8250ca3021f73b0cb21..dffa0de656bec932dbee962b6a9bb3d009bec287 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/JsonConfigFileTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/JsonConfigFileTest.java @@ -1,7 +1,12 @@ package com.ctrip.framework.apollo.internals; -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Properties; import org.junit.Before; import org.junit.Test; @@ -9,13 +14,8 @@ 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.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java index 892d5380d8a2194570edef0a06fe3b330cf3e043..eef3ee2c2e9a8d9e127129b9b41f7b6f10fa7ead 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java @@ -1,23 +1,5 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.base.Charsets; -import com.google.common.base.Joiner; -import com.google.common.io.Files; - -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.util.ConfigUtil; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.unidal.lookup.ComponentTestCase; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Properties; - import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -27,10 +9,27 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import com.ctrip.framework.apollo.build.MockInjector; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.util.ConfigUtil; +import com.google.common.base.Charsets; +import com.google.common.base.Joiner; +import com.google.common.io.Files; + /** * Created by Jason on 4/9/16. */ -public class LocalFileConfigRepositoryTest extends ComponentTestCase { +public class LocalFileConfigRepositoryTest { private File someBaseDir; private String someNamespace; private ConfigRepository upstreamRepo; @@ -42,8 +41,6 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase { @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); someBaseDir = new File("src/test/resources/config-cache"); someBaseDir.mkdir(); @@ -55,12 +52,12 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase { upstreamRepo = mock(ConfigRepository.class); when(upstreamRepo.getConfig()).thenReturn(someProperties); - defineComponent(ConfigUtil.class, MockConfigUtil.class); + MockInjector.reset(); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil()); } @After public void tearDown() throws Exception { - super.tearDown(); recursiveDelete(someBaseDir); } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java index 5e3e5dd0bee407b5568bba9d874801cc7c77c756..995fae7ac876584378671099888b777cfdbcc6a1 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java @@ -1,6 +1,12 @@ package com.ctrip.framework.apollo.internals; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Properties; import org.junit.Before; import org.junit.Test; @@ -8,13 +14,7 @@ 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.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java index 87320a02e51c98aed49299c64df92d189c3ca35a..5a56df94112a26b74fc4dd0c0c803e69a75175c0 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java @@ -1,17 +1,23 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.util.concurrent.SettableFuture; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; -import com.ctrip.framework.apollo.core.dto.ServiceDTO; -import com.ctrip.framework.apollo.util.ConfigUtil; -import com.ctrip.framework.apollo.util.http.HttpRequest; -import com.ctrip.framework.apollo.util.http.HttpResponse; -import com.ctrip.framework.apollo.util.http.HttpUtil; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import javax.servlet.http.HttpServletResponse; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -20,36 +26,30 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; import org.springframework.test.util.ReflectionTestUtils; -import org.unidal.lookup.ComponentTestCase; - -import java.lang.reflect.Type; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import javax.servlet.http.HttpServletResponse; - -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import com.ctrip.framework.apollo.build.MockInjector; +import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; +import com.ctrip.framework.apollo.core.dto.ServiceDTO; +import com.ctrip.framework.apollo.util.ConfigUtil; +import com.ctrip.framework.apollo.util.http.HttpRequest; +import com.ctrip.framework.apollo.util.http.HttpResponse; +import com.ctrip.framework.apollo.util.http.HttpUtil; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.util.concurrent.SettableFuture; /** * @author Jason Song(song_s@ctrip.com) */ @RunWith(MockitoJUnitRunner.class) -public class RemoteConfigLongPollServiceTest extends ComponentTestCase { +public class RemoteConfigLongPollServiceTest { private RemoteConfigLongPollService remoteConfigLongPollService; @Mock private HttpResponse> pollResponse; @Mock private HttpUtil httpUtil; + @Mock + private ConfigServiceLocator configServiceLocator; private Type responseType; private static String someServerUrl; @@ -58,15 +58,19 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase { @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); + MockInjector.reset(); + + MockInjector.setInstance(HttpUtil.class, httpUtil); + + ServiceDTO serviceDTO = mock(ServiceDTO.class); + when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl); + when(configServiceLocator.getConfigServices()).thenReturn(Lists.newArrayList(serviceDTO)); + MockInjector.setInstance(ConfigServiceLocator.class, configServiceLocator); - defineComponent(ConfigUtil.class, MockConfigUtil.class); - defineComponent(ConfigServiceLocator.class, MockConfigServiceLocator.class); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil()); - remoteConfigLongPollService = lookup(RemoteConfigLongPollService.class); + remoteConfigLongPollService = new RemoteConfigLongPollService(); - ReflectionTestUtils.setField(remoteConfigLongPollService, "m_httpUtil", httpUtil); responseType = (Type) ReflectionTestUtils.getField(remoteConfigLongPollService, "m_responseType"); @@ -365,18 +369,4 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase { } } - public static class MockConfigServiceLocator extends ConfigServiceLocator { - @Override - public List getConfigServices() { - ServiceDTO serviceDTO = mock(ServiceDTO.class); - - when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl); - return Lists.newArrayList(serviceDTO); - } - - @Override - public void initialize() throws InitializationException { - //do nothing - } - } } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java index 797d213bec0d17a0aa586e64eec982d64d81f1e6..81937a93cf21d56466315a8c1a338d2a5745bf0a 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java @@ -1,20 +1,23 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.util.concurrent.SettableFuture; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import com.ctrip.framework.apollo.core.dto.ApolloConfig; -import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; -import com.ctrip.framework.apollo.core.dto.ServiceDTO; -import com.ctrip.framework.apollo.exceptions.ApolloConfigException; -import com.ctrip.framework.apollo.util.ConfigUtil; -import com.ctrip.framework.apollo.util.http.HttpRequest; -import com.ctrip.framework.apollo.util.http.HttpResponse; -import com.ctrip.framework.apollo.util.http.HttpUtil; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +import javax.servlet.http.HttpServletResponse; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,31 +26,26 @@ import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; -import org.unidal.lookup.ComponentTestCase; - -import java.lang.reflect.Type; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.TimeUnit; - -import javax.servlet.http.HttpServletResponse; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import com.ctrip.framework.apollo.build.MockInjector; +import com.ctrip.framework.apollo.core.dto.ApolloConfig; +import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; +import com.ctrip.framework.apollo.core.dto.ServiceDTO; +import com.ctrip.framework.apollo.exceptions.ApolloConfigException; +import com.ctrip.framework.apollo.util.ConfigUtil; +import com.ctrip.framework.apollo.util.http.HttpRequest; +import com.ctrip.framework.apollo.util.http.HttpResponse; +import com.ctrip.framework.apollo.util.http.HttpUtil; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.util.concurrent.SettableFuture; /** * Created by Jason on 4/9/16. */ @RunWith(MockitoJUnitRunner.class) -public class RemoteConfigRepositoryTest extends ComponentTestCase { +public class RemoteConfigRepositoryTest { @Mock private ConfigServiceLocator configServiceLocator; private String someNamespace; @@ -59,17 +57,26 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase { @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); someNamespace = "someName"; when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED); - defineComponent(ConfigUtil.class, MockConfigUtil.class); - defineComponent(ConfigServiceLocator.class, MockConfigServiceLocator.class); - defineComponent(HttpUtil.class, MockHttpUtil.class); + MockInjector.reset(); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil()); + + String someServerUrl = "http://someServer"; + + ServiceDTO serviceDTO = mock(ServiceDTO.class); - remoteConfigLongPollService = lookup(RemoteConfigLongPollService.class); + when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl); + when(configServiceLocator.getConfigServices()).thenReturn(Lists.newArrayList(serviceDTO)); + MockInjector.setInstance(ConfigServiceLocator.class, configServiceLocator); + + MockInjector.setInstance(HttpUtil.class, new MockHttpUtil()); + + remoteConfigLongPollService = new RemoteConfigLongPollService(); + + MockInjector.setInstance(RemoteConfigLongPollService.class, remoteConfigLongPollService); } @Test @@ -245,23 +252,6 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase { } } - public static class MockConfigServiceLocator extends ConfigServiceLocator { - @Override - public List getConfigServices() { - String someServerUrl = "http://someServer"; - - ServiceDTO serviceDTO = mock(ServiceDTO.class); - - when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl); - return Lists.newArrayList(serviceDTO); - } - - @Override - public void initialize() throws InitializationException { - //do nothing - } - } - public static class MockHttpUtil extends HttpUtil { @Override public HttpResponse doGet(HttpRequest httpRequest, Class responseType) { diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java index c8a8b5537bfb657c7f25c4350436efe8a1325075..7c37addc947c95fdf945ef8a20d519a3bff1d4bc 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java @@ -1,13 +1,10 @@ package com.ctrip.framework.apollo.internals; -import com.google.common.collect.ImmutableMap; -import com.google.common.util.concurrent.SettableFuture; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigChangeListener; -import com.ctrip.framework.apollo.enums.PropertyChangeType; -import com.ctrip.framework.apollo.model.ConfigChange; -import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import java.util.Properties; +import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; @@ -15,11 +12,13 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import java.util.Properties; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.enums.PropertyChangeType; +import com.ctrip.framework.apollo.model.ConfigChange; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.SettableFuture; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java index a60791fe430c770feb114dd3616c4fe9c7500d70..3d01d9fbaa49581243944876249ce0d995589804 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java @@ -1,7 +1,12 @@ package com.ctrip.framework.apollo.internals; -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Properties; import org.junit.Before; import org.junit.Test; @@ -9,13 +14,8 @@ 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.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManagerTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManagerTest.java index 6748d764a9152d2899579ca551c6e4fca001d612..dd39d668510b548495134daf7f8d732134df8cae 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManagerTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManagerTest.java @@ -1,31 +1,29 @@ package com.ctrip.framework.apollo.spi; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigFile; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; - -import org.junit.Before; -import org.junit.Test; -import org.unidal.lookup.ComponentTestCase; - 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 org.junit.Before; +import org.junit.Test; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.build.MockInjector; +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; + /** * @author Jason Song(song_s@ctrip.com) */ -public class DefaultConfigFactoryManagerTest extends ComponentTestCase { +public class DefaultConfigFactoryManagerTest { private DefaultConfigFactoryManager defaultConfigFactoryManager; @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); - defineComponent(ConfigRegistry.class, MockConfigRegistry.class); - defaultConfigFactoryManager = - (DefaultConfigFactoryManager) lookup(ConfigFactoryManager.class); + MockInjector.reset(); + MockInjector.setInstance(ConfigRegistry.class, new MockConfigRegistry()); + defaultConfigFactoryManager = new DefaultConfigFactoryManager(); } @Test @@ -39,7 +37,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase { @Test public void testGetFactoryFromNamespace() throws Exception { String someNamespace = "someName"; - defineComponent(ConfigFactory.class, someNamespace, SomeConfigFactory.class); + MockInjector.setInstance(ConfigFactory.class, someNamespace, new SomeConfigFactory()); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); @@ -50,7 +48,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase { @Test public void testGetFactoryFromNamespaceMultipleTimes() throws Exception { String someNamespace = "someName"; - defineComponent(ConfigFactory.class, someNamespace, SomeConfigFactory.class); + MockInjector.setInstance(ConfigFactory.class, someNamespace, new SomeConfigFactory()); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); ConfigFactory anotherResult = defaultConfigFactoryManager.getFactory(someNamespace); @@ -63,7 +61,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase { @Test public void testGetFactoryFromDefault() throws Exception { String someNamespace = "someName"; - defineComponent(ConfigFactory.class, AnotherConfigFactory.class); + MockInjector.setInstance(ConfigFactory.class, new AnotherConfigFactory()); ConfigFactory result = defaultConfigFactoryManager.getFactory(someNamespace); diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java index 002ef472d96266ab8b62881aa350bdbd4039cc54..1b48ec34a1cd231a3d399a428dbdb7c14113dc16 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java @@ -1,7 +1,24 @@ package com.ctrip.framework.apollo.spi; +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.assertNull; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.test.util.ReflectionTestUtils; + import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.build.MockInjector; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.internals.DefaultConfig; @@ -13,39 +30,21 @@ import com.ctrip.framework.apollo.internals.YamlConfigFile; import com.ctrip.framework.apollo.internals.YmlConfigFile; import com.ctrip.framework.apollo.util.ConfigUtil; -import org.junit.Before; -import org.junit.Test; -import org.springframework.test.util.ReflectionTestUtils; -import org.unidal.lookup.ComponentTestCase; - -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.assertNull; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - /** * @author Jason Song(song_s@ctrip.com) */ -public class DefaultConfigFactoryTest extends ComponentTestCase { +public class DefaultConfigFactoryTest { private DefaultConfigFactory defaultConfigFactory; private static String someAppId; private static Env someEnv; @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); someAppId = "someId"; someEnv = Env.DEV; - defineComponent(ConfigUtil.class, MockConfigUtil.class); - defaultConfigFactory = spy((DefaultConfigFactory) lookup(ConfigFactory.class)); + MockInjector.reset(); + MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil()); + defaultConfigFactory = spy(new DefaultConfigFactory()); } @Test diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistryTest.java index 60cd403b4419126330180252fec04de0fceec99d..ee7bd350c3064b70b3717961d2d201689c0eb7f2 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistryTest.java @@ -1,28 +1,27 @@ package com.ctrip.framework.apollo.spi; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigFile; -import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; -import org.unidal.lookup.ComponentTestCase; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigFile; +import com.ctrip.framework.apollo.build.MockInjector; +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; /** * @author Jason Song(song_s@ctrip.com) */ -public class DefaultConfigRegistryTest extends ComponentTestCase { +public class DefaultConfigRegistryTest { private DefaultConfigRegistry defaultConfigRegistry; @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); - defaultConfigRegistry = (DefaultConfigRegistry) lookup(ConfigRegistry.class); + MockInjector.reset(); + defaultConfigRegistry = new DefaultConfigRegistry(); } @Test diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java index 264125b1cbfe3b486be9a615cacfe517bbfd0a6f..f53077ef989925a8351eaefe67cb51e1ec050ec0 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java @@ -1,59 +1,52 @@ package com.ctrip.framework.apollo.spring; -import com.google.common.collect.Maps; +import java.lang.reflect.Method; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.springframework.util.ReflectionUtils; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigService; +import com.ctrip.framework.apollo.build.MockInjector; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.internals.ConfigManager; import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor; - -import org.codehaus.plexus.PlexusContainer; -import org.junit.After; -import org.junit.Before; -import org.springframework.util.ReflectionUtils; -import org.unidal.lookup.ComponentTestCase; - -import java.lang.reflect.Method; -import java.util.Map; +import com.google.common.collect.Maps; /** * @author Jason Song(song_s@ctrip.com) */ -public abstract class AbstractSpringIntegrationTest extends ComponentTestCase { +public abstract class AbstractSpringIntegrationTest { private static final Map CONFIG_REGISTRY = Maps.newHashMap(); private static Method PROPERTY_SOURCES_PROCESSOR_CLEAR; - private static Method CONFIG_SERVICE_SET_CONTAINER; + private static Method CONFIG_SERVICE_RESET; static { try { PROPERTY_SOURCES_PROCESSOR_CLEAR = PropertySourcesProcessor.class.getDeclaredMethod("reset"); ReflectionUtils.makeAccessible(PROPERTY_SOURCES_PROCESSOR_CLEAR); - CONFIG_SERVICE_SET_CONTAINER = ConfigService.class.getDeclaredMethod("setContainer", PlexusContainer.class); - ReflectionUtils.makeAccessible(CONFIG_SERVICE_SET_CONTAINER); + CONFIG_SERVICE_RESET = ConfigService.class.getDeclaredMethod("reset"); + ReflectionUtils.makeAccessible(CONFIG_SERVICE_RESET); } catch (NoSuchMethodException e) { e.printStackTrace(); } } - @Override @Before public void setUp() throws Exception { - super.tearDown();//clear the container - super.setUp(); //as PropertySourcesProcessor has some static states, so we must manually clear its state ReflectionUtils.invokeMethod(PROPERTY_SOURCES_PROCESSOR_CLEAR, null); //as ConfigService is singleton, so we must manually clear its container - ReflectionUtils.invokeMethod(CONFIG_SERVICE_SET_CONTAINER, null, getContainer()); - - defineComponent(ConfigManager.class, MockConfigManager.class); + ReflectionUtils.invokeMethod(CONFIG_SERVICE_RESET, null); + MockInjector.reset(); + MockInjector.setInstance(ConfigManager.class, new MockConfigManager()); } - @Override @After public void tearDown() throws Exception { - super.tearDown(); CONFIG_REGISTRY.clear(); } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigAnnotationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigAnnotationTest.java index 55eb10fe2f35296bbfe089925e3cf578b74bc66d..98b2b8a312c3a79f7e1e6bdce51c8504e4ecb260 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigAnnotationTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigAnnotationTest.java @@ -1,14 +1,11 @@ package com.ctrip.framework.apollo.spring; -import com.google.common.collect.Lists; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigChangeListener; -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.model.ConfigChangeEvent; -import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; -import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; -import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; +import java.util.List; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; @@ -18,12 +15,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; +import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; +import com.google.common.collect.Lists; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigPlaceholderTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigPlaceholderTest.java index ce8905cba6a410f00e76cb34fbc27104660bb8f9..e6ebe09cc5381ade329ddf5811b1198f6994c3f2 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigPlaceholderTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/JavaConfigPlaceholderTest.java @@ -1,8 +1,10 @@ package com.ctrip.framework.apollo.spring; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; @@ -11,11 +13,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XMLConfigAnnotationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XMLConfigAnnotationTest.java index fa0fc604c0970934ef2df133f0754784a352bcf0..0d5476646da037fc241e5f0783e253335d6cdf91 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XMLConfigAnnotationTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XMLConfigAnnotationTest.java @@ -1,13 +1,11 @@ package com.ctrip.framework.apollo.spring; -import com.google.common.collect.Lists; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.ConfigChangeListener; -import com.ctrip.framework.apollo.core.ConfigConsts; -import com.ctrip.framework.apollo.model.ConfigChangeEvent; -import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; -import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; +import java.util.List; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; @@ -15,12 +13,13 @@ import org.mockito.stubbing.Answer; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.support.ClassPathXmlApplicationContext; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; +import com.google.common.collect.Lists; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XmlConfigPlaceholderTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XmlConfigPlaceholderTest.java index c8af76af9447fb07af20b52dcef6ab11258f9b8b..070a07a47479b453ec6ff8a149191214509c5ac8 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XmlConfigPlaceholderTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/XmlConfigPlaceholderTest.java @@ -1,18 +1,18 @@ package com.ctrip.framework.apollo.spring; -import com.ctrip.framework.apollo.Config; -import com.ctrip.framework.apollo.core.ConfigConsts; - -import org.junit.Test; -import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException; -import org.springframework.context.support.ClassPathXmlApplicationContext; - import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.junit.Test; +import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.core.ConfigConsts; + /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java index 79e7635fc31b9bbd7d871b4e4d1814332e61255e..e7d5270f9084973c1ab68b632fbe09254cee305d 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java @@ -1,9 +1,9 @@ package com.ctrip.framework.apollo.util; -import org.junit.Test; - import static org.junit.Assert.assertEquals; +import org.junit.Test; + /** * @author Jason Song(song_s@ctrip.com) */ diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DateParserTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DateParserTest.java index ddd23c1b20ae3a2734b527dd8e179e5904812893..daca36fe7a54d85227f593e2ee3055b9974c1004 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DateParserTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DateParserTest.java @@ -1,13 +1,13 @@ package com.ctrip.framework.apollo.util.parser; -import org.junit.Test; +import static org.junit.Assert.assertEquals; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; -import static org.junit.Assert.assertEquals; +import org.junit.Test; /** * @author Jason Song(song_s@ctrip.com) diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DurationParserTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DurationParserTest.java index 8c27084b81888b8b0795230d893d5d351a615630..bf417a12236a3a65738f06ed3324be05b4408b13 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DurationParserTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/parser/DurationParserTest.java @@ -1,9 +1,9 @@ package com.ctrip.framework.apollo.util.parser; -import org.junit.Test; - import static org.junit.Assert.assertEquals; +import org.junit.Test; + public class DurationParserTest { private Parsers.DurationParser durationParser = Parsers.forDuration(); diff --git a/apollo-client/src/test/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector b/apollo-client/src/test/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector new file mode 100644 index 0000000000000000000000000000000000000000..cf034c4d4fa9b325f5a9988b7be59c3768b7c641 --- /dev/null +++ b/apollo-client/src/test/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector @@ -0,0 +1 @@ +com.ctrip.framework.apollo.build.MockInjector \ No newline at end of file diff --git a/apollo-common/pom.xml b/apollo-common/pom.xml index 7712b33a2a5dca0dbb1d47249c76ec118c87d0f5..110390a63ebd166bd1a0e84e67612ebaf0654d97 100644 --- a/apollo-common/pom.xml +++ b/apollo-common/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 diff --git a/apollo-configservice/pom.xml b/apollo-configservice/pom.xml index 35c8b71e2364695960dd357dba5c2b55d766f132..0240f68d6f64063381321e879a98dfc28ad09188 100644 --- a/apollo-configservice/pom.xml +++ b/apollo-configservice/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 diff --git a/apollo-core/pom.xml b/apollo-core/pom.xml index 2143c21723f9c042c095983f49a053993b48d002..f76eb11c18b437ea7a89fe4d7a6d009be53974b2 100644 --- a/apollo-core/pom.xml +++ b/apollo-core/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 diff --git a/apollo-demo/pom.xml b/apollo-demo/pom.xml index 5dfd69a41369a05742ed08171419ced6bf108e03..e1abec835bec214c16acf89f61dccbe78a40a584 100644 --- a/apollo-demo/pom.xml +++ b/apollo-demo/pom.xml @@ -4,7 +4,7 @@ apollo com.ctrip.framework.apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT 4.0.0 apollo-demo diff --git a/apollo-portal/pom.xml b/apollo-portal/pom.xml index 393130dfdc2946df9551d4b030ae5b2b98ab7bbc..c0b6c7692a9d0fb07150cbb9902ead4f9021fc9c 100644 --- a/apollo-portal/pom.xml +++ b/apollo-portal/pom.xml @@ -4,7 +4,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT ../pom.xml 4.0.0 diff --git a/pom.xml b/pom.xml index c092138333a62284cdb77051e3755a8116afab35..175cd4fbb9ceea6bae5c2686d861c0b96fabe1fe 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.ctrip.framework.apollo apollo - 0.6.4-SNAPSHOT + 0.7.0-SNAPSHOT Apollo pom Ctrip Configuration Center @@ -263,9 +263,9 @@ 5.1.39 - org.unidal.framework - foundation-service - 2.5.6 + com.google.inject + guice + 4.1.0 @@ -583,12 +583,6 @@ - - - internal.repo - https://raw.github.com/ctripcorp/apollo/mvn-repo/ - - @@ -599,12 +593,6 @@ true - - - internal.repo - https://raw.github.com/ctripcorp/apollo/mvn-repo/ - -