diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a9110334eaa..8955c871c00 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,10 +47,10 @@ jobs: run: mvn -B clean package -P travis jacoco:report -Dmaven.gitcommitid.skip=true - name: JDK 11 if: matrix.jdk == '11' - run: mvn clean compile -Dmaven.gitcommitid.skip=true + run: mvn -B clean compile -Dmaven.gitcommitid.skip=true - name: JDK 17 if: matrix.jdk == '17' - run: mvn clean compile -Dmaven.gitcommitid.skip=true + run: mvn -B clean compile -Dmaven.gitcommitid.skip=true - name: Upload coverage to Codecov if: matrix.jdk == '8' uses: codecov/codecov-action@v1 diff --git a/CHANGES.md b/CHANGES.md index 1b56e8e7a29..a55583abdfa 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -31,6 +31,7 @@ Apollo 2.0.0 * [Fix the NPE occurred when using EnableApolloConfig with Spring 3.1.1](https://github.com/apolloconfig/apollo/pull/4180) * [Bump guava from 29.0 to 31.0.1](https://github.com/apolloconfig/apollo/pull/4182) * [fix the json number display issue when it's longer than 16](https://github.com/apolloconfig/apollo/pull/4183) +* [Bump client springboot version](https://github.com/apolloconfig/apollo/pull/4189) ------------------ All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1) diff --git a/apollo-client/pom.xml b/apollo-client/pom.xml index c4a49d80c53..3ad5defbf2d 100644 --- a/apollo-client/pom.xml +++ b/apollo-client/pom.xml @@ -29,19 +29,6 @@ ${project.artifactId} - - - - - - org.springframework.boot - spring-boot-dependencies - 1.5.16.RELEASE - pom - import - - - diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java index 25eae814001..9d36f15a66b 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java @@ -18,22 +18,23 @@ import com.ctrip.framework.apollo.build.ApolloInjector; import com.ctrip.framework.apollo.util.factory.PropertiesFactory; -import java.util.AbstractMap; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; -import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.nodes.MappingNode; import org.yaml.snakeyaml.parser.ParserException; import com.ctrip.framework.apollo.core.utils.StringUtils; +import org.yaml.snakeyaml.representer.Representer; /** * Transplanted from org.springframework.beans.factory.config.YamlProcessor since apollo can't depend on Spring directly @@ -64,7 +65,9 @@ public void process(Properties properties, Map map) { * Create the {@link Yaml} instance to use. */ private Yaml createYaml() { - return new Yaml(new StrictMapAppenderConstructor()); + LoaderOptions loadingConfig = new LoaderOptions(); + loadingConfig.setAllowDuplicateKeys(false); + return new Yaml(new SafeConstructor(), new Representer(), new DumperOptions(), loadingConfig); } private boolean process(MatchCallback callback, Yaml yaml, String content) { @@ -163,43 +166,4 @@ private interface MatchCallback { void process(Properties properties, Map map); } - /** - * A specialized {@link SafeConstructor} that checks for duplicate keys. - */ - private static class StrictMapAppenderConstructor extends SafeConstructor { - - // Declared as public for use in subclasses - StrictMapAppenderConstructor() { - super(); - } - - @Override - protected Map constructMapping(MappingNode node) { - try { - return super.constructMapping(node); - } catch (IllegalStateException ex) { - throw new ParserException("while parsing MappingNode", node.getStartMark(), ex.getMessage(), node.getEndMark()); - } - } - - @Override - protected Map createDefaultMap() { - final Map delegate = super.createDefaultMap(); - return new AbstractMap() { - @Override - public Object put(Object key, Object value) { - if (delegate.containsKey(key)) { - throw new IllegalStateException("Duplicate key: " + key); - } - return delegate.put(key, value); - } - - @Override - public Set> entrySet() { - return delegate.entrySet(); - } - }; - } - } - } 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 6dfab334a4b..d92cbfdd1be 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 @@ -53,6 +53,7 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; @@ -119,7 +120,7 @@ public HttpResponse> answer(InvocationOnMock invo TimeUnit.MILLISECONDS.sleep(50); } catch (InterruptedException e) { } - HttpRequest request = invocation.getArgumentAt(0, HttpRequest.class); + HttpRequest request = invocation.getArgument(0, HttpRequest.class); assertTrue(request.getUrl().contains(someServerUrl + "/notifications/v2?")); assertTrue(request.getUrl().contains("appId=" + someAppId)); @@ -225,7 +226,7 @@ public HttpResponse> answer(InvocationOnMock invo } catch (InterruptedException e) { } - HttpRequest request = invocation.getArgumentAt(0, HttpRequest.class); + HttpRequest request = invocation.getArgument(0, HttpRequest.class); Map headers = request.getHeaders(); assertNotNull(headers); @@ -281,7 +282,7 @@ public HttpResponse> answer(InvocationOnMock invo //the first time if (counter.incrementAndGet() == 1) { - HttpRequest request = invocation.getArgumentAt(0, HttpRequest.class); + HttpRequest request = invocation.getArgument(0, HttpRequest.class); assertTrue(request.getUrl().contains("notifications=")); assertTrue(request.getUrl().contains(someNamespace)); @@ -291,7 +292,7 @@ public HttpResponse> answer(InvocationOnMock invo when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK); when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification)); } else if (submitAnotherNamespaceFinish.get()) { - HttpRequest request = invocation.getArgumentAt(0, HttpRequest.class); + HttpRequest request = invocation.getArgument(0, HttpRequest.class); assertTrue(request.getUrl().contains("notifications=")); assertTrue(request.getUrl().contains(someNamespace)); assertTrue(request.getUrl().contains(anotherNamespace)); @@ -314,7 +315,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { onAnotherRepositoryNotified.set(true); return null; } - }).when(anotherRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class)); + }).when(anotherRepository).onLongPollNotified(Mockito.any(ServiceDTO.class), Mockito.nullable(ApolloNotificationMessages.class)); remoteConfigLongPollService.submit(someNamespace, someRepository); @@ -326,8 +327,8 @@ public Void answer(InvocationOnMock invocation) throws Throwable { remoteConfigLongPollService.stopLongPollingRefresh(); - verify(someRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class)); - verify(anotherRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class)); + verify(someRepository, times(1)).onLongPollNotified(Mockito.any(ServiceDTO.class), Mockito.nullable(ApolloNotificationMessages.class)); + verify(anotherRepository, times(1)).onLongPollNotified(Mockito.any(ServiceDTO.class), Mockito.nullable(ApolloNotificationMessages.class)); } @Test 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 a1c41ff1578..7e1b41a7397 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 @@ -199,7 +199,7 @@ public void testLoadConfigWithAccessKeySecret() throws Exception { doAnswer(new Answer>() { @Override public HttpResponse answer(InvocationOnMock invocation) throws Throwable { - HttpRequest request = invocation.getArgumentAt(0, HttpRequest.class); + HttpRequest request = invocation.getArgument(0, HttpRequest.class); Map headers = request.getHeaders(); assertNotNull(headers); assertTrue(headers.containsKey(Signature.HTTP_HEADER_TIMESTAMP)); diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java index e2237b79c47..3a81ab41fff 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java @@ -28,6 +28,7 @@ import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -86,8 +87,8 @@ public static void beforeClass() throws Exception { when(mockedConfig.getPropertyNames()).thenReturn(Sets.newHashSet(TEST_BEAN_CONDITIONAL_ON_KEY, someProperty)); - when(mockedConfig.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), anyString())).thenReturn(Boolean.TRUE.toString()); - when(mockedConfig.getProperty(eq(someProperty), anyString())).thenReturn(someValue); + when(mockedConfig.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), Mockito.nullable(String.class))).thenReturn(Boolean.TRUE.toString()); + when(mockedConfig.getProperty(eq(someProperty), Mockito.nullable(String.class))).thenReturn(someValue); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, mockedConfig); } @@ -131,7 +132,7 @@ public static void beforeClass() throws Exception { Config anotherConfig = mock(Config.class); when(config.getPropertyNames()).thenReturn(Sets.newHashSet(TEST_BEAN_CONDITIONAL_ON_KEY)); - when(config.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), anyString())).thenReturn(Boolean.TRUE.toString()); + when(config.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), Mockito.nullable(String.class))).thenReturn(Boolean.TRUE.toString()); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, anotherConfig); mockConfig(FX_APOLLO_NAMESPACE, config); @@ -209,7 +210,7 @@ public static void beforeClass() throws Exception { Config config = mock(Config.class); when(config.getPropertyNames()).thenReturn(Sets.newHashSet(TEST_BEAN_CONDITIONAL_ON_KEY)); - when(config.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), anyString())).thenReturn(Boolean.FALSE.toString()); + when(config.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), Mockito.nullable(String.class))).thenReturn(Boolean.FALSE.toString()); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); } @@ -344,7 +345,7 @@ public static void beforeClass() throws Exception { Config config = mock(Config.class); when(config.getPropertyNames()).thenReturn(Sets.newHashSet(TEST_BEAN_CONDITIONAL_ON_KEY)); - when(config.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), anyString())).thenReturn(Boolean.FALSE.toString()); + when(config.getProperty(eq(TEST_BEAN_CONDITIONAL_ON_KEY), Mockito.nullable(String.class))).thenReturn(Boolean.FALSE.toString()); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); } @@ -420,10 +421,10 @@ public static void afterClass() { @Test public void test() { - List processorList = SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader()); + List names = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class, getClass().getClassLoader()); boolean containsApollo = false; - for (EnvironmentPostProcessor postProcessor : processorList) { - if (postProcessor instanceof ApolloApplicationContextInitializer) { + for (String name : names) { + if (name.equals("com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer")) { containsApollo = true; break; } 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 fb0e34edc10..c8ad79ca5df 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 @@ -41,6 +41,7 @@ import org.junit.After; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.springframework.beans.factory.BeanCreationException; @@ -152,7 +153,7 @@ public void testEnableApolloConfigResolveExpressionSimple() { String someValue = UUID.randomUUID().toString(); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, mock(Config.class)); Config xxxConfig = mock(Config.class); - when(xxxConfig.getProperty(eq(someKey), anyString())).thenReturn(someValue); + when(xxxConfig.getProperty(eq(someKey), Mockito.nullable(String.class))).thenReturn(someValue); mockConfig("xxx", xxxConfig); TestEnableApolloConfigResolveExpressionWithDefaultValueConfiguration configuration = @@ -160,7 +161,7 @@ public void testEnableApolloConfigResolveExpressionSimple() { // check assertEquals(someValue, configuration.getSomeKey()); - verify(xxxConfig, times(1)).getProperty(eq(someKey), anyString()); + verify(xxxConfig, times(1)).getProperty(eq(someKey), Mockito.nullable(String.class)); } @Test @@ -173,7 +174,7 @@ public void testEnableApolloConfigResolveExpressionFromSystemProperty() { System.setProperty(SystemPropertyKeyConstants.SIMPLE_NAMESPACE, resolvedNamespaceName); Config yyyConfig = mock(Config.class); - when(yyyConfig.getProperty(eq(someKey), anyString())).thenReturn(someValue); + when(yyyConfig.getProperty(eq(someKey), Mockito.nullable(String.class))).thenReturn(someValue); mockConfig(resolvedNamespaceName, yyyConfig); TestEnableApolloConfigResolveExpressionWithDefaultValueConfiguration configuration = @@ -181,7 +182,7 @@ public void testEnableApolloConfigResolveExpressionFromSystemProperty() { // check assertEquals(someValue, configuration.getSomeKey()); - verify(yyyConfig, times(1)).getProperty(eq(someKey), anyString()); + verify(yyyConfig, times(1)).getProperty(eq(someKey), Mockito.nullable(String.class)); } @Test(expected = BeanCreationException.class) @@ -210,7 +211,7 @@ public void testApolloConfigChangeListener() throws Exception { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - applicationListeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class)); + applicationListeners.add(invocation.getArgument(0, ConfigChangeListener.class)); return Void.class; } @@ -219,7 +220,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - fxApolloListeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class)); + fxApolloListeners.add(invocation.getArgument(0, ConfigChangeListener.class)); return Void.class; } @@ -283,7 +284,7 @@ public void testApolloConfigChangeListenerWithInheritance() throws Exception { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - applicationListeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class)); + applicationListeners.add(invocation.getArgument(0, ConfigChangeListener.class)); return Void.class; } @@ -292,7 +293,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - fxApolloListeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class)); + fxApolloListeners.add(invocation.getArgument(0, ConfigChangeListener.class)); return Void.class; } @@ -341,10 +342,10 @@ public void testApolloConfigChangeListenerWithInterestedKeys() throws Exception final ArgumentCaptor fxApolloConfigInterestedKeys = ArgumentCaptor.forClass(Set.class); verify(applicationConfig, times(2)) - .addChangeListener(any(ConfigChangeListener.class), applicationConfigInterestedKeys.capture(), anySetOf(String.class)); + .addChangeListener(any(ConfigChangeListener.class), applicationConfigInterestedKeys.capture(), Mockito.nullable(Set.class)); verify(fxApolloConfig, times(1)) - .addChangeListener(any(ConfigChangeListener.class), fxApolloConfigInterestedKeys.capture(), anySetOf(String.class)); + .addChangeListener(any(ConfigChangeListener.class), fxApolloConfigInterestedKeys.capture(), Mockito.nullable(Set.class)); assertEquals(2, applicationConfigInterestedKeys.getAllValues().size()); @@ -372,7 +373,7 @@ public void testApolloConfigChangeListenerWithInterestedKeyPrefixes() { .forClass(Set.class); verify(applicationConfig, times(1)) - .addChangeListener(any(ConfigChangeListener.class), anySetOf(String.class), + .addChangeListener(any(ConfigChangeListener.class), Mockito.nullable(Set.class), interestedKeyPrefixesArgumentCaptor.capture()); assertEquals(1, interestedKeyPrefixesArgumentCaptor.getAllValues().size()); @@ -403,7 +404,7 @@ public void testApolloConfigChangeListenerWithInterestedKeyPrefixes_fire() TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.class, AppConfig11.class); verify(simpleConfig, atLeastOnce()) - .addChangeListener(any(ConfigChangeListener.class), anySetOf(String.class), + .addChangeListener(any(ConfigChangeListener.class), Mockito.nullable(Set.class), anySetOf(String.class)); Properties properties = new Properties(); 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 a79728dc349..a06fb2111c5 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 @@ -30,6 +30,7 @@ import java.util.List; import java.util.Properties; import org.junit.Test; +import org.mockito.Mockito; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -59,8 +60,8 @@ public void testPropertySourceWithNoNamespace() throws Exception { int someBatch = 2000; Config config = mock(Config.class); - when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); - when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(config.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); + when(config.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -80,8 +81,8 @@ public void testApplicationPropertySource() throws Exception { int someBatch = 2000; Config config = mock(Config.class); - when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); - when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(config.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); + when(config.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -126,11 +127,11 @@ public void testMultiplePropertySources() throws Exception { int someBatch = 2000; Config application = mock(Config.class); - when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); + when(application.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application); Config fxApollo = mock(Config.class); - when(application.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(application.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(FX_APOLLO_NAMESPACE, fxApollo); check(someTimeout, someBatch, AppConfig3.class); @@ -152,7 +153,7 @@ public void testMultiplePropertiesCompatiblePropertySourcesWithSameProperties() mockConfigFile("application.yml", configFile); Config fxApollo = mock(Config.class); - when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout)); + when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(anotherTimeout)); mockConfig(FX_APOLLO_NAMESPACE, fxApollo); check(someTimeout, someBatch, AppConfig11.class); @@ -166,12 +167,12 @@ public void testMultiplePropertySourcesCoverWithSameProperties() throws Exceptio int someBatch = 2000; Config fxApollo = mock(Config.class); - when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); - when(fxApollo.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); + when(fxApollo.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(FX_APOLLO_NAMESPACE, fxApollo); Config application = mock(Config.class); - when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout)); + when(application.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(anotherTimeout)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application); check(someTimeout, someBatch, AppConfig6.class); @@ -185,12 +186,12 @@ public void testMultiplePropertySourcesCoverWithSamePropertiesWithPropertiesComp int someBatch = 2000; Config fxApollo = mock(Config.class); - when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); - when(fxApollo.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); + when(fxApollo.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(FX_APOLLO_NAMESPACE, fxApollo); Config application = mock(Config.class); - when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout)); + when(application.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(anotherTimeout)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application); check(someTimeout, someBatch, AppConfig6.class); @@ -203,12 +204,12 @@ public void testMultiplePropertySourcesWithSamePropertiesWithWeight() throws Exc int someBatch = 2000; Config application = mock(Config.class); - when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); - when(application.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(application.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); + when(application.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application); Config fxApollo = mock(Config.class); - when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(anotherTimeout)); + when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(anotherTimeout)); mockConfig(FX_APOLLO_NAMESPACE, fxApollo); check(anotherTimeout, someBatch, AppConfig2.class, AppConfig4.class); @@ -220,8 +221,8 @@ public void testApplicationPropertySourceWithValueInjectedAsParameter() throws E int someBatch = 2000; Config config = mock(Config.class); - when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); - when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(config.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); + when(config.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -239,8 +240,8 @@ public void testApplicationPropertySourceWithValueInjectedAsConstructorArgs() th int someBatch = 2000; Config config = mock(Config.class); - when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())).thenReturn(String.valueOf(someTimeout)); - when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(config.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someTimeout)); + when(config.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -259,9 +260,9 @@ public void testNestedProperty() throws Exception { int someValue = 1234; Config config = mock(Config.class); - when(config.getProperty(eq(a), anyString())).thenReturn(a); - when(config.getProperty(eq(b), anyString())).thenReturn(b); - when(config.getProperty(eq(String.format("%s.%s", a, b)), anyString())) + when(config.getProperty(eq(a), Mockito.nullable(String.class))).thenReturn(a); + when(config.getProperty(eq(b), Mockito.nullable(String.class))).thenReturn(b); + when(config.getProperty(eq(String.format("%s.%s", a, b)), Mockito.nullable(String.class))) .thenReturn(String.valueOf(someValue)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -281,9 +282,9 @@ public void testNestedPropertyWithDefaultValue() throws Exception { int someValue = 1234; Config config = mock(Config.class); - when(config.getProperty(eq(a), anyString())).thenReturn(a); - when(config.getProperty(eq(b), anyString())).thenReturn(b); - when(config.getProperty(eq(c), anyString())).thenReturn(String.valueOf(someValue)); + when(config.getProperty(eq(a), Mockito.nullable(String.class))).thenReturn(a); + when(config.getProperty(eq(b), Mockito.nullable(String.class))).thenReturn(b); + when(config.getProperty(eq(c), Mockito.nullable(String.class))).thenReturn(String.valueOf(someValue)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -300,8 +301,8 @@ public void testNestedPropertyWithNestedDefaultValue() throws Exception { String b = "b"; Config config = mock(Config.class); - when(config.getProperty(eq(a), anyString())).thenReturn(a); - when(config.getProperty(eq(b), anyString())).thenReturn(b); + when(config.getProperty(eq(a), Mockito.nullable(String.class))).thenReturn(a); + when(config.getProperty(eq(b), Mockito.nullable(String.class))).thenReturn(b); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -321,10 +322,10 @@ public void testMultipleNestedProperty() throws Exception { int someValue = 1234; Config config = mock(Config.class); - when(config.getProperty(eq(a), anyString())).thenReturn(a); - when(config.getProperty(eq(b), anyString())).thenReturn(b); - when(config.getProperty(eq(String.format("%s.%s", a, b)), anyString())).thenReturn(nestedProperty); - when(config.getProperty(eq(nestedKey), anyString())).thenReturn(String.valueOf(someValue)); + when(config.getProperty(eq(a), Mockito.nullable(String.class))).thenReturn(a); + when(config.getProperty(eq(b), Mockito.nullable(String.class))).thenReturn(b); + when(config.getProperty(eq(String.format("%s.%s", a, b)), Mockito.nullable(String.class))).thenReturn(nestedProperty); + when(config.getProperty(eq(nestedKey), Mockito.nullable(String.class))).thenReturn(String.valueOf(someValue)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -344,9 +345,9 @@ public void testMultipleNestedPropertyWithDefaultValue() throws Exception { String nestedProperty = String.format("${%s:%d}", nestedKey, someValue); Config config = mock(Config.class); - when(config.getProperty(eq(a), anyString())).thenReturn(a); - when(config.getProperty(eq(b), anyString())).thenReturn(b); - when(config.getProperty(eq(String.format("%s.%s", a, b)), anyString())).thenReturn(nestedProperty); + when(config.getProperty(eq(a), Mockito.nullable(String.class))).thenReturn(a); + when(config.getProperty(eq(b), Mockito.nullable(String.class))).thenReturn(b); + when(config.getProperty(eq(String.format("%s.%s", a, b)), Mockito.nullable(String.class))).thenReturn(nestedProperty); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -363,9 +364,9 @@ public void testApolloJsonValue() { String otherJson = "[{\"a\":\"otherString\", \"b\":10},{\"a\":\"astring2\", \"b\":20}]"; Config config = mock(Config.class); - when(config.getProperty(eq(JSON_PROPERTY), anyString())).thenReturn(someJson); - when(config.getProperty(eq(OTHER_JSON_PROPERTY), anyString())).thenReturn(otherJson); - when(config.getProperty(eq("a"), anyString())).thenReturn(JSON_PROPERTY); + when(config.getProperty(eq(JSON_PROPERTY), Mockito.nullable(String.class))).thenReturn(someJson); + when(config.getProperty(eq(OTHER_JSON_PROPERTY), Mockito.nullable(String.class))).thenReturn(otherJson); + when(config.getProperty(eq("a"), Mockito.nullable(String.class))).thenReturn(JSON_PROPERTY); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( @@ -389,9 +390,9 @@ public void testApolloJsonValueWithInvalidJson() throws Exception { String someInvalidJson = "someInvalidJson"; Config config = mock(Config.class); - when(config.getProperty(eq(JSON_PROPERTY), anyString())).thenReturn(someInvalidJson); - when(config.getProperty(eq(OTHER_JSON_PROPERTY), anyString())).thenReturn(someInvalidJson); - when(config.getProperty(eq("a"), anyString())).thenReturn(JSON_PROPERTY); + when(config.getProperty(eq(JSON_PROPERTY), Mockito.nullable(String.class))).thenReturn(someInvalidJson); + when(config.getProperty(eq(OTHER_JSON_PROPERTY), Mockito.nullable(String.class))).thenReturn(someInvalidJson); + when(config.getProperty(eq("a"), Mockito.nullable(String.class))).thenReturn(JSON_PROPERTY); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); new AnnotationConfigApplicationContext(AppConfig8.class).getBean(TestJsonPropertyBean.class); 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 1afc1b80a72..25ae27321e4 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 @@ -32,6 +32,7 @@ import java.util.Set; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.springframework.beans.factory.BeanCreationException; @@ -89,7 +90,7 @@ public void testApolloConfigChangeListener() throws Exception { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - applicationListeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class)); + applicationListeners.add(invocation.getArgument(0, ConfigChangeListener.class)); return Void.class; } @@ -98,7 +99,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - fxApolloListeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class)); + fxApolloListeners.add(invocation.getArgument(0, ConfigChangeListener.class)); return Void.class; } @@ -164,10 +165,10 @@ public void testApolloConfigChangeListenerWithInterestedKeys() throws Exception final ArgumentCaptor fxApolloConfigInterestedKeys = ArgumentCaptor.forClass(Set.class); verify(applicationConfig, times(2)) - .addChangeListener(any(ConfigChangeListener.class), applicationConfigInterestedKeys.capture(), anySetOf(String.class)); + .addChangeListener(any(ConfigChangeListener.class), applicationConfigInterestedKeys.capture(), Mockito.nullable(Set.class)); verify(fxApolloConfig, times(1)) - .addChangeListener(any(ConfigChangeListener.class), fxApolloConfigInterestedKeys.capture(), anySetOf(String.class)); + .addChangeListener(any(ConfigChangeListener.class), fxApolloConfigInterestedKeys.capture(), Mockito.nullable(Set.class)); assertEquals(2, applicationConfigInterestedKeys.getAllValues().size()); 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 fd80d7046d0..cd9b952d25b 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 @@ -26,6 +26,7 @@ import com.ctrip.framework.apollo.core.ConfigConsts; import org.junit.After; import org.junit.Test; +import org.mockito.Mockito; import org.springframework.beans.FatalBeanException; import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -57,9 +58,9 @@ public void testPropertySourceWithNoNamespace() throws Exception { int someBatch = 2000; Config config = mock(Config.class); - when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())) + when(config.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))) .thenReturn(String.valueOf(someTimeout)); - when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(config.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -79,9 +80,9 @@ public void testApplicationPropertySource() throws Exception { int someBatch = 2000; Config config = mock(Config.class); - when(config.getProperty(eq(TIMEOUT_PROPERTY), anyString())) + when(config.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))) .thenReturn(String.valueOf(someTimeout)); - when(config.getProperty(eq(BATCH_PROPERTY), anyString())).thenReturn(String.valueOf(someBatch)); + when(config.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))).thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, config); @@ -94,12 +95,12 @@ public void testMultiplePropertySources() throws Exception { int someBatch = 2000; Config application = mock(Config.class); - when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())) + when(application.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))) .thenReturn(String.valueOf(someTimeout)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application); Config fxApollo = mock(Config.class); - when(application.getProperty(eq(BATCH_PROPERTY), anyString())) + when(application.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))) .thenReturn(String.valueOf(someBatch)); mockConfig(FX_APOLLO_NAMESPACE, fxApollo); @@ -108,14 +109,14 @@ public void testMultiplePropertySources() throws Exception { private void prepare(int someTimeout, int anotherTimeout, int someBatch) { Config application = mock(Config.class); - when(application.getProperty(eq(TIMEOUT_PROPERTY), anyString())) + when(application.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))) .thenReturn(String.valueOf(someTimeout)); - when(application.getProperty(eq(BATCH_PROPERTY), anyString())) + when(application.getProperty(eq(BATCH_PROPERTY), Mockito.nullable(String.class))) .thenReturn(String.valueOf(someBatch)); mockConfig(ConfigConsts.NAMESPACE_APPLICATION, application); Config fxApollo = mock(Config.class); - when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), anyString())) + when(fxApollo.getProperty(eq(TIMEOUT_PROPERTY), Mockito.nullable(String.class))) .thenReturn(String.valueOf(anotherTimeout)); mockConfig(FX_APOLLO_NAMESPACE, fxApollo); } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/CachedCompositePropertySourceTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/CachedCompositePropertySourceTest.java index b4e763045bd..5b064a62010 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/CachedCompositePropertySourceTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/CachedCompositePropertySourceTest.java @@ -63,7 +63,7 @@ public void setUp() throws Exception { Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - ConfigChangeListener listener = invocation.getArgumentAt(0, ConfigChangeListener.class); + ConfigChangeListener listener = invocation.getArgument(0, ConfigChangeListener.class); listeners.add(listener); return Void.class; } @@ -104,7 +104,7 @@ public void testAddFirstPropertySource() { Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - ConfigChangeListener listener = invocation.getArgumentAt(0, ConfigChangeListener.class); + ConfigChangeListener listener = invocation.getArgument(0, ConfigChangeListener.class); anotherListenerList.add(listener); return Void.class; } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourceTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourceTest.java index fc634a3951d..1817cce71e3 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourceTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/config/ConfigPropertySourceTest.java @@ -99,7 +99,7 @@ public void testAddChangeListener() throws Exception { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - listeners.add(invocation.getArgumentAt(0, ConfigChangeListener.class)); + listeners.add(invocation.getArgument(0, ConfigChangeListener.class)); return Void.class; } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java index ceba33117ec..9a708472701 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java @@ -37,6 +37,7 @@ import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.io.ByteArrayResource; import org.yaml.snakeyaml.constructor.ConstructorException; +import org.yaml.snakeyaml.constructor.DuplicateKeyException; import org.yaml.snakeyaml.parser.ParserException; public class YamlParserTest { @@ -63,7 +64,7 @@ public void testValidCases() throws Exception { test("case7.yaml"); } - @Test(expected = ParserException.class) + @Test(expected = DuplicateKeyException.class) public void testcase2() throws Exception { testInvalid("case2.yaml"); } @@ -117,13 +118,13 @@ private void test(String caseName) throws Exception { private String loadYaml(String caseName) throws IOException { File file = new File("src/test/resources/yaml/" + caseName); - return Files.toString(file, Charsets.UTF_8); + return Files.asCharSource(file, Charsets.UTF_8).read(); } private void testInvalid(String caseName) throws Exception { File file = new File("src/test/resources/yaml/" + caseName); - String yamlContent = Files.toString(file, Charsets.UTF_8); + String yamlContent = Files.asCharSource(file, Charsets.UTF_8).read(); parser.yamlToProperties(yamlContent); }