Skip to content

Commit

Permalink
Support @SofaService to annotated on method and refactor related code. (
Browse files Browse the repository at this point in the history
#288)

Support  @SofaService to annotated on method and refactor related code. (#288)
  • Loading branch information
QilongZhang authored and straybirdzls committed Nov 29, 2018
1 parent 0197fd0 commit 50a95ec
Show file tree
Hide file tree
Showing 34 changed files with 1,398 additions and 407 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jdk:
install:

- mvn clean install -DskipTests -B -V
- mvn test

script:
- sh ./check_format.sh

after_success:
- mvn clean test
- bash <(curl -s https://codecov.io/bash)
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.alipay.sofa.infra.utils;

import com.alipay.sofa.infra.constants.SofaBootInfraConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@
*/
package com.alipay.sofa.isle.loader;

import com.alipay.sofa.isle.ApplicationRuntimeModel;
import com.alipay.sofa.isle.constants.SofaModuleFrameworkConstants;
import com.alipay.sofa.isle.deployment.DeploymentDescriptor;
import com.alipay.sofa.isle.spring.config.SofaModuleProperties;
import com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext;
import com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory;
import com.alipay.sofa.runtime.spi.log.SofaLogger;
import java.util.Map;

import com.alipay.sofa.runtime.spring.listener.SofaRuntimeApplicationListener;
import org.springframework.beans.CachedIntrospectionResults;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
Expand All @@ -35,11 +31,16 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;

import java.util.Map;
import com.alipay.sofa.isle.ApplicationRuntimeModel;
import com.alipay.sofa.isle.constants.SofaModuleFrameworkConstants;
import com.alipay.sofa.isle.deployment.DeploymentDescriptor;
import com.alipay.sofa.isle.spring.config.SofaModuleProperties;
import com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext;
import com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory;
import com.alipay.sofa.runtime.spi.log.SofaLogger;

/**
*
Expand All @@ -58,14 +59,14 @@ public void loadSpringContext(DeploymentDescriptor deployment,
SofaModuleProperties sofaModuleProperties = rootApplicationContext
.getBean(SofaModuleFrameworkConstants.SOFA_MODULE_PROPERTIES_BEAN_ID,
SofaModuleProperties.class);

BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(
sofaModuleProperties.getBeanLoadCost());
beanFactory.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
beanFactory
.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());

GenericApplicationContext ctx = sofaModuleProperties.isPublishEventToParent() ? new GenericApplicationContext(
beanFactory) : new SofaModuleApplicationContext(beanFactory);
SofaRuntimeApplicationListener.initApplicationContext(ctx);
String activeProfiles = sofaModuleProperties.getActiveProfiles();
if (StringUtils.hasText(activeProfiles)) {
String[] profiles = activeProfiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,44 @@
*/
package com.alipay.sofa.isle;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.reflect.Method;
import java.util.HashSet;

import com.alipay.sofa.runtime.spring.ReferenceAnnotationBeanPostProcessor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.context.ApplicationContext;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.alipay.sofa.runtime.api.binding.BindingType;
import com.alipay.sofa.runtime.constants.SofaRuntimeFrameworkConstants;
import com.alipay.sofa.runtime.model.InterfaceMode;
import com.alipay.sofa.runtime.service.component.Service;
import com.alipay.sofa.runtime.service.component.impl.ReferenceImpl;
import com.alipay.sofa.runtime.service.helper.ReferenceRegisterHelper;
import com.alipay.sofa.runtime.service.impl.BindingConverterFactoryImpl;
import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
import com.alipay.sofa.runtime.spi.service.BindingConverter;
import com.alipay.sofa.runtime.spi.service.BindingConverterFactory;
import com.alipay.sofa.runtime.spring.ServiceAnnotationBeanPostProcessor;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Method;
import java.util.HashSet;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author xuanbei 18/5/15
*/
@SuppressWarnings("unchecked")
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ReferenceRegisterHelper.class, ServiceAnnotationBeanPostProcessor.class })
public class ServiceAnnotationBeanPostProcessorTest {
@PrepareForTest({ ReferenceRegisterHelper.class, ReferenceAnnotationBeanPostProcessor.class })
public class ReferenceAnnotationBeanPostProcessorTest {
@Test
public void testCreateReferenceProxy() throws Exception {
Class clazz = Class
.forName("com.alipay.sofa.runtime.spring.ServiceAnnotationBeanPostProcessor");
.forName("com.alipay.sofa.runtime.spring.ReferenceAnnotationBeanPostProcessor");
Method createReferenceProxy = clazz.getDeclaredMethod("createReferenceProxy",
SofaReference.class, Class.class);
createReferenceProxy.setAccessible(true);
Expand All @@ -72,7 +69,7 @@ public void testCreateReferenceProxy() throws Exception {
SofaRuntimeContext sofaRuntimeContext = mock(SofaRuntimeContext.class);
when(sofaRuntimeContext.getAppName()).thenReturn("testcase");
when(sofaRuntimeContext.getAppClassLoader()).thenReturn(
ServiceAnnotationBeanPostProcessorTest.class.getClassLoader());
ReferenceAnnotationBeanPostProcessorTest.class.getClassLoader());

ApplicationContext applicationContext = mock(ApplicationContext.class);
when(
Expand All @@ -81,12 +78,11 @@ public void testCreateReferenceProxy() throws Exception {

boolean hasException = false;
BindingConverterFactory bindingConverterFactory = new BindingConverterFactoryImpl();
ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor = new ServiceAnnotationBeanPostProcessor(
null, bindingConverterFactory);
serviceAnnotationBeanPostProcessor.setApplicationContext(applicationContext);
ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor = new ReferenceAnnotationBeanPostProcessor(
applicationContext, sofaRuntimeContext, null, bindingConverterFactory);
try {
createReferenceProxy.invoke(serviceAnnotationBeanPostProcessor, sofaReference,
ServiceAnnotationBeanPostProcessorTest.class);
createReferenceProxy.invoke(referenceAnnotationBeanPostProcessor, sofaReference,
ReferenceAnnotationBeanPostProcessorTest.class);
} catch (Exception e) {
Assert.assertEquals("Can not found binding converter for binding type bolt", e
.getCause().getMessage());
Expand All @@ -104,65 +100,15 @@ public void testCreateReferenceProxy() throws Exception {
// use power mockito mock static
PowerMockito.mockStatic(ReferenceRegisterHelper.class);
ReferenceImpl referenceImpl = new ReferenceImpl("uniqueId",
ServiceAnnotationBeanPostProcessorTest.class, InterfaceMode.annotation, true);
ReferenceAnnotationBeanPostProcessorTest.class, InterfaceMode.annotation, true);
PowerMockito
.whenNew(ReferenceImpl.class)
.withArguments("uniqueId", ServiceAnnotationBeanPostProcessorTest.class,
.withArguments("uniqueId", ReferenceAnnotationBeanPostProcessorTest.class,
InterfaceMode.annotation, true).thenReturn(referenceImpl);
createReferenceProxy.invoke(serviceAnnotationBeanPostProcessor, sofaReference,
ServiceAnnotationBeanPostProcessorTest.class);
createReferenceProxy.invoke(referenceAnnotationBeanPostProcessor, sofaReference,
ReferenceAnnotationBeanPostProcessorTest.class);

PowerMockito.verifyStatic();
ReferenceRegisterHelper.registerReference(referenceImpl, null, sofaRuntimeContext);
}

@Test
public void testHandleSofaServiceBinding() throws Exception {
Class clazz = Class
.forName("com.alipay.sofa.runtime.spring.ServiceAnnotationBeanPostProcessor");
Method createReferenceProxy = clazz.getDeclaredMethod("handleSofaServiceBinding",
Service.class, SofaService.class, SofaServiceBinding.class);
createReferenceProxy.setAccessible(true);

Service service = mock(Service.class);
SofaService sofaService = mock(SofaService.class);
SofaServiceBinding sofaServiceBinding = mock(SofaServiceBinding.class);

when(sofaServiceBinding.bindingType()).thenReturn("bolt");

SofaRuntimeContext sofaRuntimeContext = mock(SofaRuntimeContext.class);
when(sofaRuntimeContext.getAppName()).thenReturn("testcase");
when(sofaRuntimeContext.getAppClassLoader()).thenReturn(
ServiceAnnotationBeanPostProcessorTest.class.getClassLoader());

ApplicationContext applicationContext = mock(ApplicationContext.class);
when(
applicationContext.getBean(SofaRuntimeFrameworkConstants.SOFA_RUNTIME_CONTEXT_BEAN_ID,
SofaRuntimeContext.class)).thenReturn(sofaRuntimeContext);

boolean hasException = false;
BindingConverterFactory bindingConverterFactory = new BindingConverterFactoryImpl();
ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor = new ServiceAnnotationBeanPostProcessor(
null, bindingConverterFactory);
serviceAnnotationBeanPostProcessor.setApplicationContext(applicationContext);
try {
createReferenceProxy.invoke(serviceAnnotationBeanPostProcessor, service, sofaService,
sofaServiceBinding);
} catch (Exception e) {
Assert.assertEquals("Can not found binding converter for binding type bolt", e
.getCause().getMessage());
hasException = true;
}
Assert.assertTrue(hasException);

BindingConverter bindingConverter = mock(BindingConverter.class);
when(bindingConverter.supportBindingType()).thenReturn(new BindingType("bolt"));
when(bindingConverter.supportTagName()).thenReturn("binding:bolt");
HashSet<BindingConverter> bindingConverters = new HashSet<>();
bindingConverters.add(bindingConverter);
bindingConverterFactory.addBindingConverters(bindingConverters);

createReferenceProxy.invoke(serviceAnnotationBeanPostProcessor, service, sofaService,
sofaServiceBinding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
import com.alipay.sofa.healthcheck.configuration.HealthCheckConstants;
import com.alipay.sofa.isle.spring.configuration.SofaModuleAutoConfiguration;
import com.alipay.sofa.runtime.spring.configuration.SofaRuntimeAutoConfiguration;
import com.alipay.sofa.runtime.spring.listener.SofaRuntimeApplicationListener;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.mockito.Mockito.when;

/**
* @author abby.zh
* @since 2.4.10
Expand All @@ -38,6 +44,14 @@ public void closeContext() {
this.applicationContext.close();
}

@Before
public void before() {
ApplicationPreparedEvent applicationPreparedEvent = Mockito
.mock(ApplicationPreparedEvent.class);
when(applicationPreparedEvent.getApplicationContext()).thenReturn(applicationContext);
new SofaRuntimeApplicationListener().onApplicationEvent(applicationPreparedEvent);
}

@Test
public void testDefaultConfig() {
this.applicationContext.register(SofaModuleAutoConfiguration.class);
Expand Down
11 changes: 11 additions & 0 deletions runtime-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
<version>1.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -135,5 +140,11 @@
<artifactId>aspectjweaver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @author xuanbei 18/3/2
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD })
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
public @interface SofaReference {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @author xuanbei 18/3/1
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface SofaService {

/**
Expand Down
Loading

0 comments on commit 50a95ec

Please sign in to comment.