Skip to content

Instantly share code, notes, and snippets.

@ihopeudie
Created October 8, 2018 12:34
Show Gist options
  • Save ihopeudie/f5777d75285c7255a42cfaf66d99d410 to your computer and use it in GitHub Desktop.
Save ihopeudie/f5777d75285c7255a42cfaf66d99d410 to your computer and use it in GitHub Desktop.
package com.epamacademy.springcource.movietheater.aspects;
import com.epamacademy.springcource.movietheater.dao.EventDao;
import com.epamacademy.springcource.movietheater.domain.Event;
import com.epamacademy.springcource.movietheater.util.CounterType;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
/*
CounterAspect - count how many times each event was accessed by name,
how many times its prices were queried,
how many times its tickets were booked.
Store counters in map for now (later could be replaced by DB dao)
*/
@Aspect
public class CounterAspect {
private Map<Event, Map<CounterType, AtomicLong>> counterAspectMap;
@Autowired
private EventDao eventDao;
@PostConstruct
private void init() {
counterAspectMap = new HashMap<>();
}
@Pointcut("execution(* com.epamacademy.springcource.movietheater.dao.EventDao.getByName(String))")
private void increaseByNameCounter() {
}
@After("increaseByNameCounter()")
private void writeSomething() {
System.out.println("something");
}
Map<Event, Map<CounterType, AtomicLong>> getCounterAspectMap() {
return counterAspectMap;
}
}
package com.epamacademy.springcource.movietheater.aspects;
import com.epamacademy.springcource.movietheater.config.MovieTheaterConfig;
import com.epamacademy.springcource.movietheater.dao.EventDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MovieTheaterConfig.class)
public class CounterAspectTest {
@Autowired
private EventDao eventDao;
@Autowired
private CounterAspect counterAspect;
@Test
public void shouldInitContext() {
assertNotNull(eventDao);
assertNotNull(counterAspect);
}
@Test
public void shouldInitMap(){
assertNotNull(counterAspect.getCounterAspectMap());
//Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException:
// Bean named 'eventDao' is expected to be of type 'com.epamacademy.springcource.movietheater.dao.EventDao'
// but was actually of type 'com.sun.proxy.$Proxy27'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment