Refine Spring Social auto-configuration

Refine auto-configuration for Spring Social to:

- Only auto-configure FB/Twitter/LinkedIn if the `app-id` property is
  set.
- Only configure ConnectController and ProviderSignInController if
  there is a ConnectionFactoryLocator.
- Auto-configure Spring Social's SpringSocialDialect for Thymeleaf if
  Thymeleaf is present.
- Added several tests around Spring Social auto-configuration.

Fixes gh-1118
This commit is contained in:
Phillip Webb
2014-06-18 13:34:53 -07:00
parent 179e1558f6
commit 46c46dbd0c
10 changed files with 270 additions and 72 deletions

View File

@@ -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) {
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}