Create AOP examples
- Use different types of advice - Use various types of pointcut expressions
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package org.baeldung.aop;
|
||||
|
||||
import org.baeldung.config.TestConfig;
|
||||
import org.baeldung.dao.FooDao;
|
||||
import org.junit.Before;
|
||||
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 org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
|
||||
public class AopLoggingTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
logEventHandler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
messages.add(record.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
};
|
||||
|
||||
messages = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private FooDao dao;
|
||||
|
||||
private Handler logEventHandler;
|
||||
|
||||
private List<String> messages;
|
||||
|
||||
@Test
|
||||
public void givenLoggingAspect_whenCallDaoMethod_thenBeforeAdviceIsCalled() {
|
||||
Logger logger = Logger.getLogger(LoggingAspect.class.getName());
|
||||
logger.addHandler(logEventHandler);
|
||||
|
||||
dao.findById(1L);
|
||||
assertThat(messages, hasSize(1));
|
||||
|
||||
String logMessage = messages.get(0);
|
||||
Pattern pattern = Pattern.compile("^\\[\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}:\\d{2}:\\d{3}\\]findById$");
|
||||
assertTrue(pattern.matcher(logMessage).matches());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.baeldung.aop;
|
||||
|
||||
import org.baeldung.config.TestConfig;
|
||||
import org.baeldung.dao.FooDao;
|
||||
import org.baeldung.events.FooCreationEventListener;
|
||||
import org.baeldung.model.Foo;
|
||||
import org.junit.Before;
|
||||
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 org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
|
||||
public class AopPublishingTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
logEventHandler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
messages.add(record.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
};
|
||||
|
||||
messages = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private FooDao dao;
|
||||
|
||||
private Handler logEventHandler;
|
||||
|
||||
private List<String> messages;
|
||||
|
||||
@Test
|
||||
public void givenPublishingAspect_whenCallCreate_thenCreationEventIsPublished() {
|
||||
Logger logger = Logger.getLogger(FooCreationEventListener.class.getName());
|
||||
logger.addHandler(logEventHandler);
|
||||
|
||||
dao.create(1L, "Bar");
|
||||
|
||||
String logMessage = messages.get(0);
|
||||
Pattern pattern = Pattern.compile("Created foo instance: " +
|
||||
Pattern.quote(new Foo(1L, "Bar").toString()));
|
||||
assertTrue(pattern.matcher(logMessage).matches());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.baeldung.aop;
|
||||
|
||||
import org.baeldung.dao.FooDao;
|
||||
import org.junit.Before;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("/org/baeldung/aop/beans.xml")
|
||||
public class AopXmlConfigPerformanceTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
logEventHandler = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
messages.add(record.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SecurityException {
|
||||
}
|
||||
};
|
||||
|
||||
messages = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private FooDao dao;
|
||||
|
||||
private Handler logEventHandler;
|
||||
|
||||
private List<String> messages;
|
||||
|
||||
@Test
|
||||
public void givenPerformanceAspect_whenCallDaoMethod_thenPerformanceMeasurementAdviceIsCalled() {
|
||||
Logger logger = Logger.getLogger(PerformanceAspect.class.getName());
|
||||
logger.addHandler(logEventHandler);
|
||||
|
||||
final String entity = dao.findById(1L);
|
||||
assertThat(entity, notNullValue());
|
||||
assertThat(messages, hasSize(1));
|
||||
|
||||
String logMessage = messages.get(0);
|
||||
Pattern pattern = Pattern.compile("Execution of findById took \\d+ ms");
|
||||
assertTrue(pattern.matcher(logMessage).matches());
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"org.baeldung.dao", "org.baeldung.aop"})
|
||||
@ComponentScan(basePackages = {"org.baeldung.dao", "org.baeldung.aop", "org.baeldung.events"})
|
||||
@EnableAspectJAutoProxy
|
||||
public class TestConfig {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user