Java Code Examples for org.springframework.aop.aspectj.annotation.AspectJProxyFactory

The following examples show how to use org.springframework.aop.aspectj.annotation.AspectJProxyFactory. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void testProgrammaticProxyCreation() {
	ITestBean testBean = new TestBean();

	AspectJProxyFactory factory = new AspectJProxyFactory();
	factory.setTarget(testBean);

	CounterAspect myCounterAspect = new CounterAspect();
	factory.addAspect(myCounterAspect);

	ITestBean proxyTestBean = factory.getProxy();

	assertTrue("Expected a proxy", proxyTestBean instanceof Advised);
	proxyTestBean.setAge(20);
	assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count);
}
 
Example 2
@Test
public void testProgrammaticProxyCreation() {
	ITestBean testBean = new TestBean();

	AspectJProxyFactory factory = new AspectJProxyFactory();
	factory.setTarget(testBean);

	CounterAspect myCounterAspect = new CounterAspect();
	factory.addAspect(myCounterAspect);

	ITestBean proxyTestBean = factory.getProxy();

	assertTrue("Expected a proxy", proxyTestBean instanceof Advised);
	proxyTestBean.setAge(20);
	assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count);
}
 
Example 3
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void timeMethod() {
    MeterRegistry registry = new SimpleMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new TimedService());
    pf.addAspect(new TimedAspect(registry));

    TimedService service = pf.getProxy();

    service.call();

    assertThat(registry.get("call")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$TimedService")
            .tag("method", "call")
            .tag("extra", "tag")
            .timer().count()).isEqualTo(1);
}
 
Example 4
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void timeMethodWithLongTaskTimer() {
    MeterRegistry registry = new SimpleMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new TimedService());
    pf.addAspect(new TimedAspect(registry));

    TimedService service = pf.getProxy();

    service.longCall();

    assertThat(registry.get("longCall")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$TimedService")
            .tag("method", "longCall")
            .tag("extra", "tag")
            .longTaskTimers().size()).isEqualTo(1);
}
 
Example 5
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void timeMethodFailure() {
    MeterRegistry failingRegistry = new FailingMeterRegistry();
    
    AspectJProxyFactory pf = new AspectJProxyFactory(new TimedService());
    pf.addAspect(new TimedAspect(failingRegistry));
    
    TimedService service = pf.getProxy();
    
    service.call();
    
    assertThatExceptionOfType(MeterNotFoundException.class).isThrownBy(() -> {
        failingRegistry.get("call")
                .tag("class", "io.micrometer.core.aop.TimedAspectTest$TimedService")
                .tag("method", "call")
                .tag("extra", "tag")
                .timer();
    });
}
 
Example 6
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void timeMethodFailureWithLongTaskTimer() {
    MeterRegistry failingRegistry = new FailingMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new TimedService());
    pf.addAspect(new TimedAspect(failingRegistry));

    TimedService service = pf.getProxy();

    service.longCall();

    assertThatExceptionOfType(MeterNotFoundException.class).isThrownBy(() -> {
        failingRegistry.get("longCall")
                .tag("class", "io.micrometer.core.aop.TimedAspectTest$TimedService")
                .tag("method", "longCall")
                .tag("extra", "tag")
                .longTaskTimer();
    });
}
 
Example 7
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void timeMethodFailureWhenCompletedExceptionally() {
    MeterRegistry failingRegistry = new FailingMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new AsyncTimedService());
    pf.addAspect(new TimedAspect(failingRegistry));

    AsyncTimedService service = pf.getProxy();

    GuardedResult guardedResult = new GuardedResult();
    CompletableFuture<?> completableFuture = service.call(guardedResult);
    guardedResult.complete();
    completableFuture.join();

    assertThatExceptionOfType(MeterNotFoundException.class).isThrownBy(() -> failingRegistry.get("call")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "call")
            .tag("extra", "tag")
            .tag("exception", "none")
            .timer());
}
 
Example 8
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void timeMethodFailureWithLongTaskTimerWhenCompleted() {
    MeterRegistry failingRegistry = new FailingMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new AsyncTimedService());
    pf.addAspect(new TimedAspect(failingRegistry));

    AsyncTimedService service = pf.getProxy();

    GuardedResult guardedResult = new GuardedResult();
    CompletableFuture<?> completableFuture = service.longCall(guardedResult);
    guardedResult.complete();
    completableFuture.join();

    assertThatExceptionOfType(MeterNotFoundException.class).isThrownBy(() -> {
        failingRegistry.get("longCall")
                .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
                .tag("method", "longCall")
                .tag("extra", "tag")
                .longTaskTimer();
    });
}
 
Example 9
@Test
public void testProgrammaticProxyCreation() {
	ITestBean testBean = new TestBean();

	AspectJProxyFactory factory = new AspectJProxyFactory();
	factory.setTarget(testBean);

	CounterAspect myCounterAspect = new CounterAspect();
	factory.addAspect(myCounterAspect);

	ITestBean proxyTestBean = factory.getProxy();

	assertTrue("Expected a proxy", proxyTestBean instanceof Advised);
	proxyTestBean.setAge(20);
	assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count);
}
 
Example 10
Source Project: cosmo   Source File: SecurityAdviceTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * SetUp.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Before
public void setUp()  {       
    testHelper = new TestHelper();
    securityManager = new MockSecurityManager();
    storage = new MockDaoStorage();        
    contentDao = new MockContentDao(storage);
    userDao = new MockUserDao(storage);
    this.sa = new SecurityAdvice(securityManager, contentDao, userDao);
    lockManager = new SingleVMLockManager();
    service = new StandardContentService(contentDao, lockManager, new StandardTriageStatusQueryProcessor());
    
    // create a factory that can generate a proxy for the given target object
    AspectJProxyFactory factory = new AspectJProxyFactory(service); 
    
    factory.addAspect(sa);

    // now get the proxy object...
    proxyService = factory.getProxy();
}
 
Example 11
/**
 * 测试AOP分布式锁
 */
@Test
public void testAopLock() throws InterruptedException {
    // 测试类中使用AOP需要手动代理
    SpringBootDemoZookeeperApplicationTests target = new SpringBootDemoZookeeperApplicationTests();
    AspectJProxyFactory factory = new AspectJProxyFactory(target);
    ZooLockAspect aspect = new ZooLockAspect(zkClient);
    factory.addAspect(aspect);
    SpringBootDemoZookeeperApplicationTests proxy = factory.getProxy();
    IntStream.range(0, 10000).forEach(i -> executorService.execute(() -> proxy.aopBuy(i)));
    TimeUnit.MINUTES.sleep(1);
    log.error("count值为{}", proxy.getCount());
}
 
Example 12
Source Project: Spring   Source File: ManuallyAddingAdvice.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void addedAdviceIsCalled() {
	DemoAspect demoAspect = new DemoAspect();
	DemoClass originalObject = new DemoClass();
	AspectJProxyFactory proxyFactory = new AspectJProxyFactory(
			originalObject);
	proxyFactory.addAspect(demoAspect);
	DemoClass proxy = proxyFactory.<DemoClass> getProxy();
	assertFalse(demoAspect.isCalled());
	proxy.advicedMethod();
	assertTrue(demoAspect.isCalled());
}
 
Example 13
/**
 * 测试AOP分布式锁
 */
@Test
public void testAopLock() throws InterruptedException {
    // 测试类中使用AOP需要手动代理
    SpringBootDemoZookeeperApplicationTests target = new SpringBootDemoZookeeperApplicationTests();
    AspectJProxyFactory factory = new AspectJProxyFactory(target);
    ZooLockAspect aspect = new ZooLockAspect(zkClient);
    factory.addAspect(aspect);
    SpringBootDemoZookeeperApplicationTests proxy = factory.getProxy();
    IntStream.range(0, 10000).forEach(i -> executorService.execute(() -> proxy.aopBuy(i)));
    TimeUnit.MINUTES.sleep(1);
    log.error("count值为{}", proxy.getCount());
}
 
Example 14
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void timeMethodWhenCompleted() {
    MeterRegistry registry = new SimpleMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new AsyncTimedService());
    pf.addAspect(new TimedAspect(registry));

    AsyncTimedService service = pf.getProxy();

    GuardedResult guardedResult = new GuardedResult();
    CompletableFuture<?> completableFuture = service.call(guardedResult);

    assertThat(registry.find("call")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "call")
            .tag("extra", "tag")
            .tag("exception", "none")
            .timer()).isNull();

    guardedResult.complete();
    completableFuture.join();

    assertThat(registry.get("call")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "call")
            .tag("extra", "tag")
            .tag("exception", "none")
            .timer().count()).isEqualTo(1);
}
 
Example 15
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void timeMethodWhenCompletedExceptionally() {
    MeterRegistry registry = new SimpleMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new AsyncTimedService());
    pf.addAspect(new TimedAspect(registry));

    AsyncTimedService service = pf.getProxy();

    GuardedResult guardedResult = new GuardedResult();
    CompletableFuture<?> completableFuture = service.call(guardedResult);

    assertThat(registry.find("call")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "call")
            .tag("extra", "tag")
            .tag("exception", "NullPointerException")
            .timer()).isNull();

    guardedResult.complete(new NullPointerException());
    catchThrowableOfType(completableFuture::join, CompletionException.class);

    assertThat(registry.get("call")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "call")
            .tag("extra", "tag")
            .tag("exception", "NullPointerException")
            .timer().count()).isEqualTo(1);
}
 
Example 16
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void timeMethodWithLongTaskTimerWhenCompleted() {
    MeterRegistry registry = new SimpleMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new AsyncTimedService());
    pf.addAspect(new TimedAspect(registry));

    AsyncTimedService service = pf.getProxy();

    GuardedResult guardedResult = new GuardedResult();
    CompletableFuture<?> completableFuture = service.longCall(guardedResult);

    assertThat(registry.find("longCall")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "longCall")
            .tag("extra", "tag")
            .longTaskTimer().activeTasks()).isEqualTo(1);

    guardedResult.complete();
    completableFuture.join();

    assertThat(registry.get("longCall")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "longCall")
            .tag("extra", "tag")
            .longTaskTimer().activeTasks()).isEqualTo(0);
}
 
Example 17
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void timeMethodWithLongTaskTimerWhenCompletedExceptionally() {
    MeterRegistry registry = new SimpleMeterRegistry();

    AspectJProxyFactory pf = new AspectJProxyFactory(new AsyncTimedService());
    pf.addAspect(new TimedAspect(registry));

    AsyncTimedService service = pf.getProxy();

    GuardedResult guardedResult = new GuardedResult();
    CompletableFuture<?> completableFuture = service.longCall(guardedResult);

    assertThat(registry.find("longCall")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "longCall")
            .tag("extra", "tag")
            .longTaskTimer().activeTasks()).isEqualTo(1);

    guardedResult.complete(new NullPointerException());
    catchThrowableOfType(completableFuture::join, CompletionException.class);

    assertThat(registry.get("longCall")
            .tag("class", "io.micrometer.core.aop.TimedAspectTest$AsyncTimedService")
            .tag("method", "longCall")
            .tag("extra", "tag")
            .longTaskTimer().activeTasks()).isEqualTo(0);
}
 
Example 18
Source Project: servicecomb-java-chassis   Source File: TestBeanUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
  Intf target = new Impl();
  AspectJProxyFactory factory = new AspectJProxyFactory(target);
  MyAspect aspect = new MyAspect();
  factory.addAspect(aspect);
  Intf proxy = factory.getProxy();

  Assert.assertEquals(Impl.class, BeanUtils.getImplClassFromBean(proxy));
  Assert.assertEquals(Impl.class, BeanUtils.getImplClassFromBean(new Impl()));
}
 
Example 19
/**
 * 测试AOP分布式锁
 */
@Test
public void testAopLock() throws InterruptedException {
    // 测试类中使用AOP需要手动代理
    SpringBootDemoZookeeperApplicationTests target = new SpringBootDemoZookeeperApplicationTests();
    AspectJProxyFactory factory = new AspectJProxyFactory(target);
    ZooLockAspect aspect = new ZooLockAspect(zkClient);
    factory.addAspect(aspect);
    SpringBootDemoZookeeperApplicationTests proxy = factory.getProxy();
    IntStream.range(0, 10000).forEach(i -> executorService.execute(() -> proxy.aopBuy(i)));
    TimeUnit.MINUTES.sleep(1);
    log.error("count值为{}", proxy.getCount());
}
 
Example 20
Source Project: cosmo   Source File: ContextServiceExtensionsAdviceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create service with the advice. It is not expected to create eventData for handers because they 
 * won't execute.
 */
private void createProxyServiceWithoutExpectedAdviceExecution() {
    eventOperationExtensionsAdvice = new ContextServiceExtensionsAdvice(){
    };
    // create a factory that can generate a proxy for the given target object
    AspectJProxyFactory factory = new AspectJProxyFactory(service); 

    factory.addAspect(eventOperationExtensionsAdvice);

    // now get the proxy object...
    proxyService = factory.getProxy();
}
 
Example 21
Source Project: cosmo   Source File: ContextServiceExtensionsAdviceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create service with the advice. The handlers wll execure with expected results.
 */
private void createProxyServiceWithExpectedAdviceExecution() {
    eventOperationExtensionsAdvice = new ContextServiceExtensionsAdvice();
    
    // create a factory that can generate a proxy for the given target object
    AspectJProxyFactory factory = new AspectJProxyFactory(service); 

    factory.addAspect(eventOperationExtensionsAdvice);

    // now get the proxy object...
    proxyService = factory.getProxy();
}
 
Example 22
Source Project: client_java   Source File: MethodTimerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void timeMethodInSubClassModel() throws Exception {
    Timeable cprime = new MockCglibProxyTestClass();
    AspectJProxyFactory factory = new AspectJProxyFactory(cprime);
    factory.addAspect(MethodTimer.class);
    Timeable proxy = factory.getProxy();

    proxy.timeMe();

    final Double tot = CollectorRegistry.defaultRegistry.getSampleValue("test_sub_class_sum");
    Assert.assertNotNull(tot);
    assertEquals(0.02, tot, 0.01);
}
 
Example 23
Source Project: client_java   Source File: MethodTimerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void timeMethod() throws Exception {
    Timeable cprime = new TestClass();
    AspectJProxyFactory factory = new AspectJProxyFactory(cprime);
    factory.addAspect(MethodTimer.class);
    Timeable proxy = factory.getProxy();

    proxy.timeMe();

    final Double tot = CollectorRegistry.defaultRegistry.getSampleValue("test_class_sum");
    Assert.assertNotNull(tot);
    assertEquals(0.02, tot, 0.01);
}
 
Example 24
Source Project: micrometer   Source File: CountedAspectTest.java    License: Apache License 2.0 4 votes vote down vote up
private <T> T getAdvisedService(T countedService) {
    AspectJProxyFactory proxyFactory = new AspectJProxyFactory(countedService);
    proxyFactory.addAspect(new CountedAspect(meterRegistry));
    return proxyFactory.getProxy();
}
 
Example 25
Source Project: client_java   Source File: MethodTimerTest.java    License: Apache License 2.0 4 votes vote down vote up
<T> T getProxy(T source){
    AspectJProxyFactory factory = new AspectJProxyFactory(source);
    factory.addAspect(MethodTimer.class);
    return factory.getProxy();
}