diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java index 86d1838ea9b..182c5c71e16 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.social; import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -29,6 +30,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.social.config.annotation.EnableSocial; +import org.springframework.social.config.annotation.SocialConfigurerAdapter; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionFactory; import org.springframework.social.connect.ConnectionRepository; @@ -46,7 +48,9 @@ import org.springframework.web.servlet.View; * @since 1.1.0 */ @Configuration -@ConditionalOnClass({ FacebookConnectionFactory.class }) +@ConditionalOnClass({ SocialConfigurerAdapter.class, FacebookConnectionFactory.class }) +@ConditionalOnProperty(prefix = "spring.social.facebook.", value = "app-id") +@AutoConfigureBefore(SocialWebAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class FacebookAutoConfiguration { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java index 7d6cd5299a0..f6efbfb915d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.social; import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -29,6 +30,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.social.config.annotation.EnableSocial; +import org.springframework.social.config.annotation.SocialConfigurerAdapter; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionFactory; import org.springframework.social.connect.ConnectionRepository; @@ -45,7 +47,9 @@ import org.springframework.web.servlet.View; * @since 1.1.0 */ @Configuration -@ConditionalOnClass({ LinkedInConnectionFactory.class }) +@ConditionalOnClass({ SocialConfigurerAdapter.class, LinkedInConnectionFactory.class }) +@ConditionalOnProperty(prefix = "spring.social.linkedin.", value = "app-id") +@AutoConfigureBefore(SocialWebAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class LinkedInAutoConfiguration { @@ -69,7 +73,7 @@ public class LinkedInAutoConfiguration { } @Bean - @ConditionalOnMissingBean(LinkedInConnectionFactory.class) + @ConditionalOnMissingBean(LinkedIn.class) @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) public LinkedIn linkedin(ConnectionRepository repository) { Connection connection = repository diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java index a4354c34dc4..da35c98aaa5 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java @@ -16,7 +16,6 @@ package org.springframework.boot.autoconfigure.social; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; @@ -31,7 +30,6 @@ import org.springframework.social.connect.ConnectionFactory; * @author Phillip Webb * @since 1.1.0 */ -@ConditionalOnClass(SocialConfigurerAdapter.class) abstract class SocialAutoConfigurerAdapter extends SocialConfigurerAdapter implements EnvironmentAware { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java index d599bc01f0e..9714c8ff182 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java @@ -20,6 +20,7 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -27,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -45,10 +47,12 @@ import org.springframework.social.connect.web.DisconnectInterceptor; import org.springframework.social.connect.web.ProviderSignInController; import org.springframework.social.connect.web.ProviderSignInInterceptor; import org.springframework.social.connect.web.SignInAdapter; +import org.springframework.social.connect.web.thymeleaf.SpringSocialDialect; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.view.BeanNameViewResolver; +import org.thymeleaf.spring4.SpringTemplateEngine; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Social's web connection @@ -59,6 +63,8 @@ import org.springframework.web.servlet.view.BeanNameViewResolver; */ @Configuration @ConditionalOnClass({ ConnectController.class, SocialConfigurerAdapter.class }) +@ConditionalOnBean({ ConnectionFactoryLocator.class, UsersConnectionRepository.class }) +@AutoConfigureBefore(ThymeleafAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class SocialWebAutoConfiguration { @@ -119,7 +125,7 @@ public class SocialWebAutoConfiguration { @Configuration @EnableSocial @ConditionalOnWebApplication - @ConditionalOnMissingClass(name="org.springframework.security.core.context.SecurityContextHolder") + @ConditionalOnMissingClass(name = "org.springframework.security.core.context.SecurityContextHolder") protected static class AnonymousUserIdSourceConfig extends SocialConfigurerAdapter { @Override @@ -131,7 +137,6 @@ public class SocialWebAutoConfiguration { } }; } - } @Configuration @@ -148,6 +153,18 @@ public class SocialWebAutoConfiguration { } + @Configuration + @ConditionalOnClass(SpringTemplateEngine.class) + protected static class SpringSocialThymeleafConfig { + + @Bean + @ConditionalOnMissingBean + public SpringSocialDialect springSocialDialect() { + return new SpringSocialDialect(); + } + + } + private static class SecurityContextUserIdSource implements UserIdSource { @Override diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java index fd2dcae0d08..b9bbcb91b12 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.social; import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -29,6 +30,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.social.config.annotation.EnableSocial; +import org.springframework.social.config.annotation.SocialConfigurerAdapter; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionFactory; import org.springframework.social.connect.ConnectionRepository; @@ -46,7 +48,9 @@ import org.springframework.web.servlet.View; * @since 1.1.0 */ @Configuration -@ConditionalOnClass({ TwitterConnectionFactory.class }) +@ConditionalOnClass({ SocialConfigurerAdapter.class, TwitterConnectionFactory.class }) +@ConditionalOnProperty(prefix = "spring.social.twitter.", value = "app-id") +@AutoConfigureBefore(SocialWebAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class TwitterAutoConfiguration { @@ -69,7 +73,7 @@ public class TwitterAutoConfiguration { } @Bean - @ConditionalOnMissingBean(TwitterConnectionFactory.class) + @ConditionalOnMissingBean @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) public Twitter twitter(ConnectionRepository repository) { Connection connection = repository diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/AbstractSocialAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/AbstractSocialAutoConfigurationTests.java new file mode 100644 index 00000000000..35833be23cf --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/AbstractSocialAutoConfigurationTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.social; + +import org.junit.After; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.social.UserIdSource; +import org.springframework.social.connect.ConnectionFactoryLocator; +import org.springframework.social.connect.ConnectionRepository; +import org.springframework.social.connect.UsersConnectionRepository; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +/** + * Abstract base class for testing Spring Social auto-configuration. + * + * @author Craig Walls + */ +public class AbstractSocialAutoConfigurationTests { + + protected AnnotationConfigWebApplicationContext context; + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + + public AbstractSocialAutoConfigurationTests() { + super(); + } + + protected void assertConnectionFrameworkBeans() { + assertNotNull(this.context.getBean(UsersConnectionRepository.class)); + assertNotNull(this.context.getBean(ConnectionRepository.class)); + assertNotNull(this.context.getBean(ConnectionFactoryLocator.class)); + assertNotNull(this.context.getBean(UserIdSource.class)); + } + + protected void assertNoConnectionFrameworkBeans() { + assertMissingBean(UsersConnectionRepository.class); + assertMissingBean(ConnectionRepository.class); + assertMissingBean(ConnectionFactoryLocator.class); + assertMissingBean(UserIdSource.class); + } + + protected void assertMissingBean(Class beanClass) { + try { + assertNotNull(this.context.getBean(beanClass)); + fail("Unexpected bean in context of type " + beanClass.getName()); + } + catch (NoSuchBeanDefinitionException ex) { + } + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfigurationTests.java index 6a20b61d46e..a3b9c01d67f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfigurationTests.java @@ -16,13 +16,8 @@ package org.springframework.boot.autoconfigure.social; -import org.junit.After; import org.junit.Test; import org.springframework.boot.test.EnvironmentTestUtils; -import org.springframework.social.UserIdSource; -import org.springframework.social.connect.ConnectionFactoryLocator; -import org.springframework.social.connect.ConnectionRepository; -import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.facebook.api.Facebook; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -30,19 +25,10 @@ import static org.junit.Assert.assertNotNull; /** * Tests for {@link FacebookAutoConfiguration}. - * + * * @author Craig Walls */ -public class FacebookAutoConfigurationTests { - - private AnnotationConfigWebApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } +public class FacebookAutoConfigurationTests extends AbstractSocialAutoConfigurationTests { @Test public void expectedSocialBeansCreated() throws Exception { @@ -51,14 +37,20 @@ public class FacebookAutoConfigurationTests { "spring.social.facebook.appId:12345"); EnvironmentTestUtils.addEnvironment(this.context, "spring.social.facebook.appSecret:secret"); - this.context.register(SocialWebAutoConfiguration.class); this.context.register(FacebookAutoConfiguration.class); + this.context.register(SocialWebAutoConfiguration.class); this.context.refresh(); - assertNotNull(this.context.getBean(UsersConnectionRepository.class)); - assertNotNull(this.context.getBean(ConnectionRepository.class)); - assertNotNull(this.context.getBean(ConnectionFactoryLocator.class)); - assertNotNull(this.context.getBean(UserIdSource.class)); + assertConnectionFrameworkBeans(); assertNotNull(this.context.getBean(Facebook.class)); } + @Test + public void noFacebookBeanCreatedWhenPropertiesArentSet() throws Exception { + this.context = new AnnotationConfigWebApplicationContext(); + this.context.register(FacebookAutoConfiguration.class); + this.context.register(SocialWebAutoConfiguration.class); + this.context.refresh(); + assertNoConnectionFrameworkBeans(); + assertMissingBean(Facebook.class); + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfigurationTests.java index d1f57909a08..478ce256895 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfigurationTests.java @@ -16,13 +16,8 @@ package org.springframework.boot.autoconfigure.social; -import org.junit.After; import org.junit.Test; import org.springframework.boot.test.EnvironmentTestUtils; -import org.springframework.social.UserIdSource; -import org.springframework.social.connect.ConnectionFactoryLocator; -import org.springframework.social.connect.ConnectionRepository; -import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.linkedin.api.LinkedIn; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -30,19 +25,10 @@ import static org.junit.Assert.assertNotNull; /** * Tests for {@link LinkedInAutoConfiguration}. - * + * * @author Craig Walls */ -public class LinkedInAutoConfigurationTests { - - private AnnotationConfigWebApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } +public class LinkedInAutoConfigurationTests extends AbstractSocialAutoConfigurationTests { @Test public void expectedSocialBeansCreated() throws Exception { @@ -51,14 +37,21 @@ public class LinkedInAutoConfigurationTests { "spring.social.linkedin.appId:12345"); EnvironmentTestUtils.addEnvironment(this.context, "spring.social.linkedin.appSecret:secret"); - this.context.register(SocialWebAutoConfiguration.class); this.context.register(LinkedInAutoConfiguration.class); + this.context.register(SocialWebAutoConfiguration.class); this.context.refresh(); - assertNotNull(this.context.getBean(UsersConnectionRepository.class)); - assertNotNull(this.context.getBean(ConnectionRepository.class)); - assertNotNull(this.context.getBean(ConnectionFactoryLocator.class)); - assertNotNull(this.context.getBean(UserIdSource.class)); + assertConnectionFrameworkBeans(); assertNotNull(this.context.getBean(LinkedIn.class)); } + @Test + public void noLinkedInBeanCreatedIfPropertiesArentSet() throws Exception { + this.context = new AnnotationConfigWebApplicationContext(); + this.context.register(LinkedInAutoConfiguration.class); + this.context.register(SocialWebAutoConfiguration.class); + this.context.refresh(); + assertNoConnectionFrameworkBeans(); + assertMissingBean(LinkedIn.class); + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/MultiApiAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/MultiApiAutoConfigurationTests.java new file mode 100644 index 00000000000..dab458c18f1 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/MultiApiAutoConfigurationTests.java @@ -0,0 +1,120 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.social; + +import org.junit.Test; +import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.social.facebook.api.Facebook; +import org.springframework.social.linkedin.api.LinkedIn; +import org.springframework.social.twitter.api.Twitter; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import static org.junit.Assert.assertNotNull; + +/** + * Tests for Spring Social configuration with multiple API providers. + * + * @author Craig Walls + */ +public class MultiApiAutoConfigurationTests extends AbstractSocialAutoConfigurationTests { + + @Test + public void expectTwitterConfigurationOnly() throws Exception { + setupContext("spring.social.twitter.appId:12345", + "spring.social.twitter.appSecret:secret"); + assertConnectionFrameworkBeans(); + assertNotNull(this.context.getBean(Twitter.class)); + assertMissingBean(Facebook.class); + assertMissingBean(LinkedIn.class); + } + + @Test + public void expectFacebookConfigurationOnly() throws Exception { + setupContext("spring.social.facebook.appId:12345", + "spring.social.facebook.appSecret:secret"); + assertConnectionFrameworkBeans(); + assertNotNull(this.context.getBean(Facebook.class)); + assertMissingBean(Twitter.class); + assertMissingBean(LinkedIn.class); + } + + @Test + public void expectLinkedInConfigurationOnly() throws Exception { + setupContext("spring.social.linkedin.appId:12345", + "spring.social.linkedin.appSecret:secret"); + assertConnectionFrameworkBeans(); + assertNotNull(this.context.getBean(LinkedIn.class)); + assertMissingBean(Twitter.class); + assertMissingBean(Facebook.class); + } + + @Test + public void expectFacebookAndLinkedInConfigurationOnly() throws Exception { + setupContext("spring.social.facebook.appId:54321", + "spring.social.facebook.appSecret:shhhhh", + "spring.social.linkedin.appId:12345", + "spring.social.linkedin.appSecret:secret"); + assertConnectionFrameworkBeans(); + assertNotNull(this.context.getBean(Facebook.class)); + assertNotNull(this.context.getBean(LinkedIn.class)); + assertMissingBean(Twitter.class); + } + + @Test + public void expectFacebookAndTwitterConfigurationOnly() throws Exception { + setupContext("spring.social.facebook.appId:54321", + "spring.social.facebook.appSecret:shhhhh", + "spring.social.twitter.appId:12345", + "spring.social.twitter.appSecret:secret"); + assertConnectionFrameworkBeans(); + assertNotNull(this.context.getBean(Facebook.class)); + assertNotNull(this.context.getBean(Twitter.class)); + assertMissingBean(LinkedIn.class); + } + + @Test + public void expectLinkedInAndTwitterConfigurationOnly() throws Exception { + setupContext("spring.social.linkedin.appId:54321", + "spring.social.linkedin.appSecret:shhhhh", + "spring.social.twitter.appId:12345", + "spring.social.twitter.appSecret:secret"); + assertConnectionFrameworkBeans(); + assertNotNull(this.context.getBean(LinkedIn.class)); + assertNotNull(this.context.getBean(Twitter.class)); + assertMissingBean(Facebook.class); + } + + @Test + public void noSocialBeansCreatedWhenPropertiesArentSet() throws Exception { + setupContext(); + assertNoConnectionFrameworkBeans(); + assertMissingBean(Twitter.class); + assertMissingBean(Facebook.class); + assertMissingBean(LinkedIn.class); + } + + private void setupContext(String... environment) { + this.context = new AnnotationConfigWebApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, environment); + this.context.register(TwitterAutoConfiguration.class); + this.context.register(FacebookAutoConfiguration.class); + this.context.register(LinkedInAutoConfiguration.class); + this.context.register(SocialWebAutoConfiguration.class); + this.context.refresh(); + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfigurationTests.java index 09988cea4c2..aed9df591ab 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfigurationTests.java @@ -16,13 +16,8 @@ package org.springframework.boot.autoconfigure.social; -import org.junit.After; import org.junit.Test; import org.springframework.boot.test.EnvironmentTestUtils; -import org.springframework.social.UserIdSource; -import org.springframework.social.connect.ConnectionFactoryLocator; -import org.springframework.social.connect.ConnectionRepository; -import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.twitter.api.Twitter; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -30,19 +25,10 @@ import static org.junit.Assert.assertNotNull; /** * Tests for {@link TwitterAutoConfiguration}. - * + * * @author Craig Walls */ -public class TwitterAutoConfigurationTests { - - private AnnotationConfigWebApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } +public class TwitterAutoConfigurationTests extends AbstractSocialAutoConfigurationTests { @Test public void expectedSocialBeansCreated() throws Exception { @@ -51,14 +37,21 @@ public class TwitterAutoConfigurationTests { "spring.social.twitter.appId:12345"); EnvironmentTestUtils.addEnvironment(this.context, "spring.social.twitter.appSecret:secret"); - this.context.register(SocialWebAutoConfiguration.class); this.context.register(TwitterAutoConfiguration.class); + this.context.register(SocialWebAutoConfiguration.class); this.context.refresh(); - assertNotNull(this.context.getBean(UsersConnectionRepository.class)); - assertNotNull(this.context.getBean(ConnectionRepository.class)); - assertNotNull(this.context.getBean(ConnectionFactoryLocator.class)); - assertNotNull(this.context.getBean(UserIdSource.class)); + assertConnectionFrameworkBeans(); assertNotNull(this.context.getBean(Twitter.class)); } + @Test + public void noTwitterBeanCreatedWhenPropertiesArentSet() throws Exception { + this.context = new AnnotationConfigWebApplicationContext(); + this.context.register(TwitterAutoConfiguration.class); + this.context.register(SocialWebAutoConfiguration.class); + this.context.refresh(); + assertNoConnectionFrameworkBeans(); + assertMissingBean(Twitter.class); + } + }