Java Code Examples for org.springframework.aop.aspectj.annotation.AspectJProxyFactory#addAspect()

The following examples show how to use org.springframework.aop.aspectj.annotation.AspectJProxyFactory#addAspect() . 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
Source File: SecurityAdviceTest.java    From cosmo with 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 2
Source File: BeanNamePointcutAtAspectTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 File: TimedAspectTest.java    From micrometer with 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 4
Source File: TimedAspectTest.java    From micrometer with 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 File: TimedAspectTest.java    From micrometer with 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 File: TimedAspectTest.java    From micrometer with 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 File: BeanNamePointcutAtAspectTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 8
Source File: TimedAspectTest.java    From micrometer with 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 9
Source File: SpringBootDemoZookeeperApplicationTests.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试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 10
Source File: TestBeanUtils.java    From servicecomb-java-chassis with 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 11
Source File: TimedAspectTest.java    From micrometer with 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 12
Source File: TimedAspectTest.java    From micrometer with 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 13
Source File: TimedAspectTest.java    From micrometer with 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 14
Source File: ContextServiceExtensionsAdviceTest.java    From cosmo with 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 15
Source File: ContextServiceExtensionsAdviceTest.java    From cosmo with 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 16
Source File: SpringBootDemoZookeeperApplicationTests.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试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 17
Source File: ManuallyAddingAdvice.java    From Spring with 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 18
Source File: MethodTimerTest.java    From client_java with 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 19
Source File: SpringBootDemoZookeeperApplicationTests.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试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 File: MethodTimerTest.java    From client_java with 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();
}