Search in sources :

Example 1 with PlaceHolderBinder

use of com.alipay.sofa.boot.annotation.PlaceHolderBinder in project sofa-boot by sofastack.

the class AnnotationPlaceHolderTest method testServiceAnnotationPlaceHolder.

@Test
@SuppressWarnings("unchecked")
public void testServiceAnnotationPlaceHolder() {
    PlaceHolderBinder binder = new PlaceHolderBinder() {

        @Override
        public String bind(String origin) {
            return environment.resolvePlaceholders(origin);
        }
    };
    SofaService sofaService = AnnotationSampleService.class.getAnnotation(SofaService.class);
    PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder<SofaService> builder = PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder.wrap(sofaService).withBinder(binder);
    SofaService delegate = builder.build();
    Assert.assertEquals(sofaService.hashCode(), delegate.hashCode());
    Assert.assertEquals(sofaService.toString(), delegate.toString());
    Assert.assertEquals(SampleService.class, sofaService.interfaceType());
    Assert.assertEquals(SampleService.class, delegate.interfaceType());
    Assert.assertEquals("${annotation.sample.service.uniqueId}", sofaService.uniqueId());
    Assert.assertEquals("annotation-sample-service-uniqueId", delegate.uniqueId());
    Assert.assertEquals(1, sofaService.bindings().length);
    Assert.assertEquals(1, delegate.bindings().length);
    SofaServiceBinding binding = sofaService.bindings()[0];
    String[] filters = binding.filters();
    SofaServiceBinding delegateBinding = delegate.bindings()[0];
    String[] delegateFilters = delegateBinding.filters();
    Assert.assertEquals("${annotation.sample.service.bindingType}", binding.bindingType());
    Assert.assertEquals("bolt", delegateBinding.bindingType());
    Assert.assertEquals(300, binding.timeout());
    Assert.assertEquals(300, delegateBinding.timeout());
    Assert.assertEquals(2, filters.length);
    Assert.assertEquals(2, delegateFilters.length);
    Assert.assertEquals("${annotation.sample.service.filter-1}", filters[0]);
    Assert.assertEquals("service-filter-1", delegateFilters[0]);
    Assert.assertEquals("filter-2", filters[1]);
    Assert.assertEquals("filter-2", delegateFilters[1]);
    Assert.assertTrue(delegate instanceof WrapperAnnotation);
    Assert.assertTrue(delegateBinding instanceof WrapperAnnotation);
}
Also used : SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) PlaceHolderAnnotationInvocationHandler(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) WrapperAnnotation(com.alipay.sofa.boot.annotation.WrapperAnnotation) Test(org.junit.Test)

Example 2 with PlaceHolderBinder

use of com.alipay.sofa.boot.annotation.PlaceHolderBinder in project sofa-boot by alipay.

the class PlaceHolderAnnotationTest method testAnnotationPlaceHolder.

@Test
public void testAnnotationPlaceHolder() {
    PlaceHolderBinder placeHolderBinder = new PlaceHolderBinder() {

        @Override
        public String bind(String key) {
            if ("${key}".equals(key)) {
                return "value";
            } else if ("${subKey}".equals(key)) {
                return "subValue";
            } else {
                return "invalid";
            }
        }
    };
    SampleAnnotation origin = SampleClass.class.getAnnotation(SampleAnnotation.class);
    SampleAnnotation delegate = (SampleAnnotation) PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder.wrap(origin).withBinder(placeHolderBinder).build();
    Assert.assertEquals("value", delegate.id());
    Assert.assertEquals("subValue", delegate.bindings()[0].id());
    Assert.assertEquals("invalid", delegate.bindings()[1].id());
}
Also used : PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) Test(org.junit.Test)

Example 3 with PlaceHolderBinder

use of com.alipay.sofa.boot.annotation.PlaceHolderBinder in project sofa-boot by alipay.

the class AnnotationPlaceHolderTest method testServiceAnnotationPlaceHolder.

@Test
@SuppressWarnings("unchecked")
public void testServiceAnnotationPlaceHolder() {
    PlaceHolderBinder binder = new PlaceHolderBinder() {

        @Override
        public String bind(String origin) {
            return environment.resolvePlaceholders(origin);
        }
    };
    SofaService sofaService = AnnotationSampleService.class.getAnnotation(SofaService.class);
    PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder<SofaService> builder = PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder.wrap(sofaService).withBinder(binder);
    SofaService delegate = builder.build();
    Assert.assertEquals(sofaService.hashCode(), delegate.hashCode());
    Assert.assertEquals(sofaService.toString(), delegate.toString());
    Assert.assertEquals(SampleService.class, sofaService.interfaceType());
    Assert.assertEquals(SampleService.class, delegate.interfaceType());
    Assert.assertEquals("${annotation.sample.service.uniqueId}", sofaService.uniqueId());
    Assert.assertEquals("annotation-sample-service-uniqueId", delegate.uniqueId());
    Assert.assertEquals(1, sofaService.bindings().length);
    Assert.assertEquals(1, delegate.bindings().length);
    SofaServiceBinding binding = sofaService.bindings()[0];
    String[] filters = binding.filters();
    SofaServiceBinding delegateBinding = delegate.bindings()[0];
    String[] delegateFilters = delegateBinding.filters();
    Assert.assertEquals("${annotation.sample.service.bindingType}", binding.bindingType());
    Assert.assertEquals("bolt", delegateBinding.bindingType());
    Assert.assertEquals(300, binding.timeout());
    Assert.assertEquals(300, delegateBinding.timeout());
    Assert.assertEquals(2, filters.length);
    Assert.assertEquals(2, delegateFilters.length);
    Assert.assertEquals("${annotation.sample.service.filter-1}", filters[0]);
    Assert.assertEquals("service-filter-1", delegateFilters[0]);
    Assert.assertEquals("filter-2", filters[1]);
    Assert.assertEquals("filter-2", delegateFilters[1]);
    Assert.assertTrue(delegate instanceof WrapperAnnotation);
    Assert.assertTrue(delegateBinding instanceof WrapperAnnotation);
}
Also used : SofaServiceBinding(com.alipay.sofa.runtime.api.annotation.SofaServiceBinding) PlaceHolderAnnotationInvocationHandler(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) WrapperAnnotation(com.alipay.sofa.boot.annotation.WrapperAnnotation) Test(org.junit.Test)

Example 4 with PlaceHolderBinder

use of com.alipay.sofa.boot.annotation.PlaceHolderBinder in project sofa-boot by alipay.

the class AnnotationPlaceHolderTest method testReferenceAnnotationPlaceHolder.

@Test
@SuppressWarnings("unchecked")
public void testReferenceAnnotationPlaceHolder() throws Exception {
    PlaceHolderBinder binder = new PlaceHolderBinder() {

        @Override
        public String bind(String origin) {
            return environment.resolvePlaceholders(origin);
        }
    };
    SofaReference sofaReference = AnnotationSampleService.class.getField("sampleService").getAnnotation(SofaReference.class);
    PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder<SofaReference> builder = PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder.wrap(sofaReference).withBinder(binder);
    SofaReference delegate = builder.build();
    Assert.assertEquals("${annotation.sample.ref.uniqueId}", sofaReference.uniqueId());
    Assert.assertEquals("sample-reference-uniqueId", delegate.uniqueId());
    Assert.assertFalse(sofaReference.jvmFirst());
    Assert.assertFalse(delegate.jvmFirst());
    SofaReferenceBinding binding = sofaReference.binding();
    SofaReferenceBinding delegateBinding = delegate.binding();
    Assert.assertEquals("${annotation.sample.ref.bindingType}", binding.bindingType());
    Assert.assertEquals("bolt", delegateBinding.bindingType());
    Assert.assertEquals("${annotation.sample.ref.direct-url}", binding.directUrl());
    Assert.assertEquals("127.0.0.1", delegateBinding.directUrl());
    String[] filters = binding.filters();
    String[] delegateFilters = delegateBinding.filters();
    Assert.assertEquals(2, filters.length);
    Assert.assertEquals(2, delegateFilters.length);
    Assert.assertEquals("${annotation.sample.ref.filter-1}", filters[0]);
    Assert.assertEquals("reference-filter-1", delegateFilters[0]);
    Assert.assertEquals("filter-2", filters[1]);
    Assert.assertEquals("filter-2", delegateFilters[1]);
    Assert.assertTrue(delegate instanceof WrapperAnnotation);
    Assert.assertTrue(delegateBinding instanceof WrapperAnnotation);
}
Also used : PlaceHolderAnnotationInvocationHandler(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler) AnnotationSampleService(com.alipay.sofa.runtime.test.beans.service.AnnotationSampleService) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) WrapperAnnotation(com.alipay.sofa.boot.annotation.WrapperAnnotation) Test(org.junit.Test)

Example 5 with PlaceHolderBinder

use of com.alipay.sofa.boot.annotation.PlaceHolderBinder in project sofa-boot by sofastack.

the class AnnotationPlaceHolderTest method testReferenceAnnotationPlaceHolder.

@Test
@SuppressWarnings("unchecked")
public void testReferenceAnnotationPlaceHolder() throws Exception {
    PlaceHolderBinder binder = new PlaceHolderBinder() {

        @Override
        public String bind(String origin) {
            return environment.resolvePlaceholders(origin);
        }
    };
    SofaReference sofaReference = AnnotationSampleService.class.getField("sampleService").getAnnotation(SofaReference.class);
    PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder<SofaReference> builder = PlaceHolderAnnotationInvocationHandler.AnnotationWrapperBuilder.wrap(sofaReference).withBinder(binder);
    SofaReference delegate = builder.build();
    Assert.assertEquals("${annotation.sample.ref.uniqueId}", sofaReference.uniqueId());
    Assert.assertEquals("sample-reference-uniqueId", delegate.uniqueId());
    Assert.assertFalse(sofaReference.jvmFirst());
    Assert.assertFalse(delegate.jvmFirst());
    SofaReferenceBinding binding = sofaReference.binding();
    SofaReferenceBinding delegateBinding = delegate.binding();
    Assert.assertEquals("${annotation.sample.ref.bindingType}", binding.bindingType());
    Assert.assertEquals("bolt", delegateBinding.bindingType());
    Assert.assertEquals("${annotation.sample.ref.direct-url}", binding.directUrl());
    Assert.assertEquals("127.0.0.1", delegateBinding.directUrl());
    String[] filters = binding.filters();
    String[] delegateFilters = delegateBinding.filters();
    Assert.assertEquals(2, filters.length);
    Assert.assertEquals(2, delegateFilters.length);
    Assert.assertEquals("${annotation.sample.ref.filter-1}", filters[0]);
    Assert.assertEquals("reference-filter-1", delegateFilters[0]);
    Assert.assertEquals("filter-2", filters[1]);
    Assert.assertEquals("filter-2", delegateFilters[1]);
    Assert.assertTrue(delegate instanceof WrapperAnnotation);
    Assert.assertTrue(delegateBinding instanceof WrapperAnnotation);
}
Also used : PlaceHolderAnnotationInvocationHandler(com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler) AnnotationSampleService(com.alipay.sofa.runtime.test.beans.service.AnnotationSampleService) SofaReference(com.alipay.sofa.runtime.api.annotation.SofaReference) SofaReferenceBinding(com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding) PlaceHolderBinder(com.alipay.sofa.boot.annotation.PlaceHolderBinder) WrapperAnnotation(com.alipay.sofa.boot.annotation.WrapperAnnotation) Test(org.junit.Test)

Aggregations

PlaceHolderBinder (com.alipay.sofa.boot.annotation.PlaceHolderBinder)6 Test (org.junit.Test)6 PlaceHolderAnnotationInvocationHandler (com.alipay.sofa.boot.annotation.PlaceHolderAnnotationInvocationHandler)4 WrapperAnnotation (com.alipay.sofa.boot.annotation.WrapperAnnotation)4 SofaReference (com.alipay.sofa.runtime.api.annotation.SofaReference)2 SofaReferenceBinding (com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding)2 SofaService (com.alipay.sofa.runtime.api.annotation.SofaService)2 SofaServiceBinding (com.alipay.sofa.runtime.api.annotation.SofaServiceBinding)2 AnnotationSampleService (com.alipay.sofa.runtime.test.beans.service.AnnotationSampleService)2