├── deploy
├── pubring.gpg.enc
├── secring.gpg.enc
├── publish.sh
└── settings.xml
├── samples
├── spring-automocker-property-source-sample
│ ├── src
│ │ ├── main
│ │ │ ├── config
│ │ │ │ └── appli.properties
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── github
│ │ │ │ └── fridujo
│ │ │ │ └── sample
│ │ │ │ └── propertySource
│ │ │ │ ├── TextService.java
│ │ │ │ └── PropertySourceApplication.java
│ │ └── test
│ │ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── fridujo
│ │ │ └── sample
│ │ │ └── propertySource
│ │ │ └── PropertySourceApplicationTest.java
│ └── pom.xml
├── spring-automocker-amqp-sample
│ ├── src
│ │ ├── main
│ │ │ ├── conf
│ │ │ │ └── application.yml
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── github
│ │ │ │ └── fridujo
│ │ │ │ └── sample
│ │ │ │ └── amqp
│ │ │ │ ├── Receiver.java
│ │ │ │ ├── Sender.java
│ │ │ │ └── AmqpApplication.java
│ │ └── test
│ │ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── fridujo
│ │ │ └── sample
│ │ │ └── amqp
│ │ │ └── AmqpApplicationTest.java
│ └── pom.xml
├── spring-automocker-jms-sample
│ ├── src
│ │ ├── main
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ │ └── github
│ │ │ │ │ └── fridujo
│ │ │ │ │ └── sample
│ │ │ │ │ └── jms
│ │ │ │ │ ├── BusinessHeaders.java
│ │ │ │ │ ├── JmsListenerContainerFactories.java
│ │ │ │ │ ├── Broker.java
│ │ │ │ │ ├── utils
│ │ │ │ │ └── ActiveMQConnectionFactoryFactory.java
│ │ │ │ │ ├── buyer
│ │ │ │ │ ├── Buyer.java
│ │ │ │ │ └── BuyerConfiguration.java
│ │ │ │ │ ├── JmsApplication.java
│ │ │ │ │ └── seller
│ │ │ │ │ ├── SellerConfiguration.java
│ │ │ │ │ └── Seller.java
│ │ │ └── conf
│ │ │ │ └── application.yml
│ │ └── test
│ │ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── fridujo
│ │ │ └── sample
│ │ │ └── jms
│ │ │ └── JmsApplicationTest.java
│ └── pom.xml
├── spring-automocker-jdbc-sample
│ ├── src
│ │ ├── main
│ │ │ ├── conf
│ │ │ │ └── application.yml
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── github
│ │ │ │ └── fridujo
│ │ │ │ └── sample
│ │ │ │ └── jdbc
│ │ │ │ ├── customer
│ │ │ │ ├── CustomerRepository.java
│ │ │ │ ├── Customer.java
│ │ │ │ └── CustomerConfig.java
│ │ │ │ ├── order
│ │ │ │ ├── OrderRepository.java
│ │ │ │ ├── LineItem.java
│ │ │ │ ├── Order.java
│ │ │ │ └── OrderConfig.java
│ │ │ │ ├── JdbcApplication.java
│ │ │ │ └── DataInitializer.java
│ │ └── test
│ │ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── fridujo
│ │ │ └── sample
│ │ │ └── jdbc
│ │ │ └── JdbcApplicationTest.java
│ └── pom.xml
├── spring-automocker-graphite-sample
│ ├── src
│ │ ├── main
│ │ │ ├── conf
│ │ │ │ └── application.yml
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── github
│ │ │ │ └── fridujo
│ │ │ │ └── sample
│ │ │ │ └── graphite
│ │ │ │ └── GraphiteApplication.java
│ │ └── test
│ │ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── fridujo
│ │ │ └── sample
│ │ │ └── graphite
│ │ │ └── GraphiteApplicationTest.java
│ └── pom.xml
└── spring-automocker-mvc-sample
│ ├── src
│ ├── main
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── fridujo
│ │ │ └── sample
│ │ │ └── mvc
│ │ │ ├── Customer.java
│ │ │ └── MvcApplication.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── fridujo
│ │ └── sample
│ │ └── mvc
│ │ └── MvcApplicationTest.java
│ └── pom.xml
├── .editorconfig
├── spring-automocker
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── github
│ │ │ │ └── fridujo
│ │ │ │ └── automocker
│ │ │ │ ├── api
│ │ │ │ ├── Resettable.java
│ │ │ │ ├── AfterBeanRegistrationExecutable.java
│ │ │ │ ├── metrics
│ │ │ │ │ ├── Point.java
│ │ │ │ │ ├── DerivativeMetricAsserter.java
│ │ │ │ │ ├── MetricAsserter.java
│ │ │ │ │ ├── AbstractMetricAsserter.java
│ │ │ │ │ ├── GraphiteMock.java
│ │ │ │ │ └── GraphiteSenderMock.java
│ │ │ │ ├── BeforeBeanRegistrationExecutable.java
│ │ │ │ ├── ResetMocks.java
│ │ │ │ ├── jms
│ │ │ │ │ ├── JmsMockLocator.java
│ │ │ │ │ ├── ErrorHandlerMock.java
│ │ │ │ │ ├── DestinationManagerResetter.java
│ │ │ │ │ ├── JmsDestinationAssert.java
│ │ │ │ │ ├── JmsListenerContainerFactoryConfigurer.java
│ │ │ │ │ ├── JmsMessageAssert.java
│ │ │ │ │ ├── JmsMock.java
│ │ │ │ │ └── JmsMessageBuilder.java
│ │ │ │ ├── AfterBeanRegistration.java
│ │ │ │ ├── BeforeBeanRegistration.java
│ │ │ │ ├── jdbc
│ │ │ │ │ ├── DataSourceResetter.java
│ │ │ │ │ ├── DataSources.java
│ │ │ │ │ └── Connections.java
│ │ │ │ ├── tools
│ │ │ │ │ └── BeanLocator.java
│ │ │ │ └── ExtendedBeanDefinitionRegistry.java
│ │ │ │ ├── utils
│ │ │ │ ├── PropertiesBuilder.java
│ │ │ │ ├── LamdaLuggageException.java
│ │ │ │ ├── ThrowingConsumer.java
│ │ │ │ ├── ThrowingBiConsumer.java
│ │ │ │ ├── Maps.java
│ │ │ │ ├── ThrowingFunction.java
│ │ │ │ ├── Version.java
│ │ │ │ ├── Classes.java
│ │ │ │ ├── Annotations.java
│ │ │ │ └── BeanDefinitions.java
│ │ │ │ ├── context
│ │ │ │ ├── AutomockerContextCustomizerFactory.java
│ │ │ │ ├── ResettableTestExecutionListener.java
│ │ │ │ ├── AutomockerContextCustomizer.java
│ │ │ │ ├── AutomockerPostProcessor.java
│ │ │ │ └── ExtendedBeanDefinitionRegistryImpl.java
│ │ │ │ └── base
│ │ │ │ ├── Automocker.java
│ │ │ │ ├── RegisterTools.java
│ │ │ │ ├── MockPropertySources.java
│ │ │ │ ├── MockJdbc.java
│ │ │ │ ├── MockAmqp.java
│ │ │ │ ├── MockWebMvc.java
│ │ │ │ ├── MockJms.java
│ │ │ │ └── MockMicrometerGraphite.java
│ │ └── resources
│ │ │ ├── META-INF
│ │ │ └── spring.factories
│ │ │ └── logback.xml
│ └── test
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── fridujo
│ │ └── automocker
│ │ ├── utils
│ │ ├── PropertiesBuilderTest.java
│ │ ├── MapsTest.java
│ │ ├── VersionTest.java
│ │ ├── AnnotationsTest.java
│ │ ├── ClassesTest.java
│ │ ├── ThrowingTest.java
│ │ └── BeanDefinitionsTest.java
│ │ ├── api
│ │ ├── jms
│ │ │ └── JmsMockLocatorTest.java
│ │ ├── ExtendedBeanDefinitionRegistryTest.java
│ │ └── tools
│ │ │ └── BeanLocatorTest.java
│ │ └── context
│ │ ├── ResettableTestExecutionListenerTest.java
│ │ └── ExtendedBeanDefinitionRegistryImplTest.java
└── pom.xml
├── starters
├── README.md
├── spring-automocker-starter-jdbc
│ └── pom.xml
├── spring-automocker-starter-web
│ └── pom.xml
├── spring-automocker-starter-jms
│ └── pom.xml
├── spring-automocker-starter-amqp
│ └── pom.xml
└── spring-automocker-starter
│ └── pom.xml
├── .gitignore
├── .travis.yml
└── README.md
/deploy/pubring.gpg.enc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fridujo/spring-automocker/HEAD/deploy/pubring.gpg.enc
--------------------------------------------------------------------------------
/deploy/secring.gpg.enc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fridujo/spring-automocker/HEAD/deploy/secring.gpg.enc
--------------------------------------------------------------------------------
/samples/spring-automocker-property-source-sample/src/main/config/appli.properties:
--------------------------------------------------------------------------------
1 | text.literal=literal text content
2 |
--------------------------------------------------------------------------------
/samples/spring-automocker-amqp-sample/src/main/conf/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | rabbitmq:
3 | host: 127.0.0.1
4 | username: guest
5 | password: guest
6 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | end_of_line = lf
5 | insert_final_newline = true
6 |
7 | charset = utf-8
8 |
9 | indent_style = space
10 | indent_size = 4
11 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/Resettable.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api;
2 |
3 | public interface Resettable {
4 |
5 | void reset();
6 | }
7 |
--------------------------------------------------------------------------------
/starters/README.md:
--------------------------------------------------------------------------------
1 | # Starters
2 |
3 | Spring-Automocker Starters are a convenient way to get all needed dependencies in one shot with the right version.
4 |
5 | See [samples](samples/) for usage.
6 |
--------------------------------------------------------------------------------
/deploy/publish.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | if [[ $TRAVIS_PULL_REQUEST == "false" && $TRAVIS_BRANCH == 'master' ]]; then
4 | mvn deploy --settings $GPG_DIR/settings.xml -DperformRelease=true -DskipTests=true
5 | exit $?
6 | fi
7 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/BusinessHeaders.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms;
2 |
3 | public interface BusinessHeaders {
4 | String BUYER_ID = "BUYER_ID";
5 | String SELLER_ID = "SELLER_ID";
6 | }
7 |
--------------------------------------------------------------------------------
/deploy/settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ossrh
5 | ${env.SONATYPE_USERNAME}
6 | ${env.SONATYPE_PASSWORD}
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/conf/application.yml:
--------------------------------------------------------------------------------
1 | order:
2 | datasource:
3 | driverClassName: org.hsqldb.jdbcDriver
4 | url: jdbc:hsqldb:mem:order
5 |
6 | customer:
7 | datasource:
8 | driverClassName: org.hsqldb.jdbcDriver
9 | url: jdbc:hsqldb:mem:customer
10 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/conf/application.yml:
--------------------------------------------------------------------------------
1 | buyer:
2 | activemq:
3 | broker-url: vm://localhost?broker.persistent=false
4 | user: guest
5 | password: guest
6 | seller:
7 | activemq:
8 | broker-url: vm://localhost?broker.persistent=false
9 | user: guest
10 | password: guest
11 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/JmsListenerContainerFactories.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms;
2 |
3 | public interface JmsListenerContainerFactories {
4 | String SELLER = "sellerJmsListenerContainerFactory";
5 | String BUYER = "buyerJmsListenerContainerFactory";
6 | }
7 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | org.springframework.test.context.ContextCustomizerFactory=\
2 | com.github.fridujo.automocker.context.AutomockerContextCustomizerFactory
3 |
4 | org.springframework.test.context.TestExecutionListener=\
5 | com.github.fridujo.automocker.context.ResettableTestExecutionListener
6 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/AfterBeanRegistrationExecutable.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api;
2 |
3 | import java.lang.annotation.Annotation;
4 |
5 | public interface AfterBeanRegistrationExecutable {
6 |
7 | void execute(A annotation, ExtendedBeanDefinitionRegistry extendedBeanDefinitionRegistry);
8 | }
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Maven
2 | target/
3 | pom.xml.tag
4 | pom.xml.releaseBackup
5 | pom.xml.versionsBackup
6 | pom.xml.next
7 | release.properties
8 | dependency-reduced-pom.xml
9 | buildNumber.properties
10 | .mvn/timing.properties
11 |
12 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
13 | !/.mvn/wrapper/maven-wrapper.jar
14 |
15 | # IntelliJ
16 | .idea/
17 | *.iml
18 |
--------------------------------------------------------------------------------
/samples/spring-automocker-graphite-sample/src/main/conf/application.yml:
--------------------------------------------------------------------------------
1 | management.metrics.export.graphite:
2 | # The location of your Graphite server
3 | host: mygraphitehost
4 |
5 | # You will probably want to conditionally disable Graphite publishing in local development.
6 | enabled: true
7 |
8 | # The interval at which metrics are sent to Graphite. The default is 1 minute.
9 | step: 1m
10 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/java/com/github/fridujo/sample/jdbc/customer/CustomerRepository.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jdbc.customer;
2 |
3 | import org.springframework.data.repository.CrudRepository;
4 |
5 | import java.util.Optional;
6 |
7 | public interface CustomerRepository extends CrudRepository {
8 |
9 | Optional findByLastname(String lastname);
10 | }
11 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/metrics/Point.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.metrics;
2 |
3 | public class Point {
4 | static final Point NEUTRAl_ELEMENT = new Point(0, 0);
5 | final long timestamp;
6 | final double value;
7 |
8 | Point(long timestamp, double value) {
9 | this.timestamp = timestamp;
10 | this.value = value;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/BeforeBeanRegistrationExecutable.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api;
2 |
3 | import org.springframework.context.ConfigurableApplicationContext;
4 |
5 | import java.lang.annotation.Annotation;
6 |
7 | public interface BeforeBeanRegistrationExecutable {
8 |
9 | void execute(A annotation, ConfigurableApplicationContext context);
10 | }
11 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/java/com/github/fridujo/sample/jdbc/order/OrderRepository.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jdbc.order;
2 |
3 | import com.github.fridujo.sample.jdbc.customer.Customer;
4 | import org.springframework.data.repository.CrudRepository;
5 |
6 | import java.util.List;
7 |
8 | public interface OrderRepository extends CrudRepository {
9 |
10 | List findByCustomer(Customer.CustomerId id);
11 | }
12 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/PropertiesBuilder.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import java.util.Properties;
4 |
5 | public final class PropertiesBuilder {
6 | private PropertiesBuilder() {
7 | }
8 |
9 | public static Properties of(String key, String value) {
10 | Properties properties = new Properties();
11 | properties.setProperty(key, value);
12 | return properties;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/LamdaLuggageException.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | @SuppressWarnings("serial")
4 | public class LamdaLuggageException extends RuntimeException {
5 |
6 | LamdaLuggageException(Exception cause) {
7 | super(cause);
8 | }
9 |
10 | public static RuntimeException wrap(Exception e) {
11 | if (e instanceof RuntimeException) {
12 | return (RuntimeException) e;
13 | } else {
14 | return new LamdaLuggageException(e);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/ThrowingConsumer.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import java.util.function.Consumer;
4 |
5 | @FunctionalInterface
6 | public interface ThrowingConsumer {
7 |
8 | static Consumer silent(ThrowingConsumer throwing) {
9 | return t -> {
10 | try {
11 | throwing.accept(t);
12 | } catch (Exception e) {
13 | throw LamdaLuggageException.wrap(e);
14 | }
15 | };
16 | }
17 |
18 | void accept(T t) throws Exception;
19 | }
20 |
--------------------------------------------------------------------------------
/spring-automocker/src/test/java/com/github/fridujo/automocker/utils/PropertiesBuilderTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.util.Properties;
6 |
7 | import static org.assertj.core.api.Assertions.assertThat;
8 | import static org.assertj.core.data.MapEntry.entry;
9 |
10 | class PropertiesBuilderTest {
11 |
12 | @Test
13 | void trivial_singleton_properties() {
14 | Properties properties = PropertiesBuilder.of("testKey", "testValue");
15 |
16 | assertThat(properties).containsOnly(entry("testKey", "testValue"));
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/samples/spring-automocker-amqp-sample/src/main/java/com/github/fridujo/sample/amqp/Receiver.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.amqp;
2 |
3 | import org.springframework.stereotype.Component;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | @Component
9 | public class Receiver {
10 |
11 | private final List messages = new ArrayList<>();
12 |
13 | public void receiveMessage(String message) {
14 | System.out.println("Received <" + message + ">");
15 | this.messages.add(message);
16 | }
17 |
18 | public List getMessages() {
19 | return messages;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/ThrowingBiConsumer.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import java.util.function.BiConsumer;
4 |
5 | @FunctionalInterface
6 | public interface ThrowingBiConsumer {
7 |
8 | static BiConsumer silent(ThrowingBiConsumer throwing) {
9 | return (t, u) -> {
10 | try {
11 | throwing.accept(t, u);
12 | } catch (Exception e) {
13 | throw LamdaLuggageException.wrap(e);
14 | }
15 | };
16 | }
17 |
18 | void accept(T t, U u) throws Exception;
19 | }
20 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/ResetMocks.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 | import java.lang.annotation.Target;
8 |
9 | /**
10 | * Activate a {@link org.springframework.test.context.TestExecutionListener} to
11 | * reset any bean implementing {@link Resettable}.
12 | */
13 | @Target(ElementType.TYPE)
14 | @Retention(RetentionPolicy.RUNTIME)
15 | @Documented
16 | public @interface ResetMocks {
17 | boolean disable() default false;
18 | }
19 |
--------------------------------------------------------------------------------
/samples/spring-automocker-amqp-sample/src/main/java/com/github/fridujo/sample/amqp/Sender.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.amqp;
2 |
3 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
4 | import org.springframework.stereotype.Component;
5 |
6 | @Component
7 | public class Sender {
8 |
9 | private final RabbitTemplate rabbitTemplate;
10 |
11 | public Sender(RabbitTemplate rabbitTemplate) {
12 | this.rabbitTemplate = rabbitTemplate;
13 | }
14 |
15 | public void send() {
16 | System.out.println("Sending message...");
17 | rabbitTemplate.convertAndSend("", AmqpApplication.QUEUE_NAME, "Hello from RabbitMQ!");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/Broker.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms;
2 |
3 | import org.springframework.beans.factory.annotation.Qualifier;
4 |
5 | import java.lang.annotation.ElementType;
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 | import java.lang.annotation.Target;
9 |
10 | @Target({ElementType.FIELD,
11 | ElementType.METHOD,
12 | ElementType.TYPE,
13 | ElementType.PARAMETER})
14 | @Retention(RetentionPolicy.RUNTIME)
15 | @Qualifier
16 | public @interface Broker {
17 | Type value();
18 |
19 | enum Type {
20 | SELLER,
21 | BUYER
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/context/AutomockerContextCustomizerFactory.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.context;
2 |
3 | import org.springframework.test.context.ContextConfigurationAttributes;
4 | import org.springframework.test.context.ContextCustomizer;
5 | import org.springframework.test.context.ContextCustomizerFactory;
6 |
7 | import java.util.List;
8 |
9 | class AutomockerContextCustomizerFactory implements ContextCustomizerFactory {
10 | @Override
11 | public ContextCustomizer createContextCustomizer(Class> testClass, List configAttributes) {
12 | return new AutomockerContextCustomizer(testClass);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/base/Automocker.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.base;
2 |
3 | import com.github.fridujo.automocker.api.ResetMocks;
4 |
5 | import java.lang.annotation.Documented;
6 | import java.lang.annotation.ElementType;
7 | import java.lang.annotation.Retention;
8 | import java.lang.annotation.RetentionPolicy;
9 | import java.lang.annotation.Target;
10 |
11 | /**
12 | * Collection of mocking strategies supplied by default.
13 | */
14 | @Target(ElementType.TYPE)
15 | @Retention(RetentionPolicy.RUNTIME)
16 | @Documented
17 |
18 | @RegisterTools
19 | @ResetMocks
20 | @MockPropertySources
21 | @MockWebMvc
22 | @MockJdbc
23 | @MockJms
24 | @MockMicrometerGraphite
25 | @MockAmqp
26 | public @interface Automocker {
27 | }
28 |
29 |
30 |
--------------------------------------------------------------------------------
/samples/spring-automocker-mvc-sample/src/main/java/com/github/fridujo/sample/mvc/Customer.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.mvc;
2 |
3 | public class Customer {
4 |
5 | private final int id;
6 | private final String firstName;
7 | private final String lastName;
8 |
9 | public Customer(int id, String firstName, String lastName) {
10 | this.id = id;
11 | this.firstName = firstName;
12 | this.lastName = lastName;
13 | }
14 |
15 | public String getFirstName() {
16 | return firstName;
17 | }
18 |
19 | public String getLastName() {
20 | return lastName;
21 | }
22 |
23 | @Override
24 | public String toString() {
25 | return String.format("Customer[firstName='%s', lastName='%s']", firstName, lastName);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/metrics/DerivativeMetricAsserter.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.metrics;
2 |
3 | import java.util.List;
4 |
5 | import static org.assertj.core.api.Assertions.assertThat;
6 |
7 | public class DerivativeMetricAsserter extends AbstractMetricAsserter {
8 |
9 | public DerivativeMetricAsserter(String metricName, List points) {
10 | super(metricName, points);
11 | }
12 |
13 | public void hasLastValue(int expectedValue) {
14 | Point lastPoint = getLastPoint();
15 | final Point penultimatePoint = getPenultimatePoint();
16 |
17 | assertThat(lastPoint.value - penultimatePoint.value)
18 | .as("Last recorded derivative of metric [" + metricName + "]")
19 | .isEqualTo(expectedValue);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jms/JmsMockLocator.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jms;
2 |
3 | import javax.jms.ConnectionFactory;
4 | import java.util.Optional;
5 | import java.util.Set;
6 |
7 | /**
8 | * Used to find {@link JmsMock} by {@link ConnectionFactory}.
9 | *
10 | * @see JmsListenerContainerFactoryConfigurer
11 | */
12 | public class JmsMockLocator {
13 |
14 | private final Set jmsMocks;
15 |
16 | JmsMockLocator(Set jmsMocks) {
17 | this.jmsMocks = jmsMocks;
18 | }
19 |
20 | Optional getJmsMockByConnectionFactory(ConnectionFactory connectionFactory) {
21 | return jmsMocks
22 | .stream()
23 | .filter(jmsMock -> jmsMock.connectionFactory == connectionFactory)
24 | .findFirst();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/metrics/MetricAsserter.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.metrics;
2 |
3 | import java.util.List;
4 |
5 | import static org.assertj.core.api.Assertions.assertThat;
6 |
7 | public class MetricAsserter extends AbstractMetricAsserter {
8 |
9 | public MetricAsserter(String metricName, List points) {
10 | super(metricName, points);
11 | }
12 |
13 | public void hasLastValue(double expectedValue) {
14 | Point lastPoint = getLastPoint();
15 |
16 | assertThat(lastPoint.value)
17 | .as("Last recorded value of metric [" + metricName + "]")
18 | .isEqualTo(expectedValue);
19 | }
20 |
21 | public DerivativeMetricAsserter derivative() {
22 | return new DerivativeMetricAsserter(metricName, points);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/Maps.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | public class Maps {
7 |
8 | @SuppressWarnings("unchecked")
9 | public static Map build(Class keyClas, Object... keysAndValues) {
10 | if (keysAndValues.length % 2 != 0) {
11 | throw new IllegalArgumentException("keysAndValues array must be even");
12 | }
13 | Map map = new HashMap<>();
14 | K key = null;
15 | for (int i = 0; i < keysAndValues.length; i++) {
16 | if (i % 2 == 0) {
17 | key = keyClas.cast(keysAndValues[i]);
18 | } else {
19 | map.put(key, (V) keysAndValues[i]);
20 | }
21 | }
22 | return map;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/ThrowingFunction.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import java.util.function.Function;
4 |
5 | @FunctionalInterface
6 | public interface ThrowingFunction {
7 |
8 | static Function silent(ThrowingFunction throwing) {
9 | return silent(throwing, e -> {
10 | throw LamdaLuggageException.wrap(e);
11 | });
12 | }
13 |
14 | static Function silent(ThrowingFunction throwing,
15 | Function errorHandler) {
16 | return t -> {
17 | try {
18 | return throwing.apply(t);
19 | } catch (Exception e) {
20 | return errorHandler.apply(e);
21 | }
22 | };
23 | }
24 |
25 | R apply(T t) throws Exception;
26 | }
27 |
--------------------------------------------------------------------------------
/samples/spring-automocker-property-source-sample/src/main/java/com/github/fridujo/sample/propertySource/TextService.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.propertySource;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.stereotype.Service;
5 |
6 | import java.util.Optional;
7 |
8 | @Service
9 | class TextService {
10 |
11 | private final String literalText;
12 | private final Optional optionalText;
13 |
14 | TextService(@Value("${text.literal}")
15 | String literalText, @Value("${text.optional:}")
16 | Optional optionalText) {
17 | this.literalText = literalText;
18 | this.optionalText = optionalText;
19 | }
20 |
21 |
22 | public String getLiteralText() {
23 | return literalText;
24 | }
25 |
26 | public String getOptionalText() {
27 | return optionalText.get();
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/spring-automocker/src/test/java/com/github/fridujo/automocker/utils/MapsTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.util.Map;
6 |
7 | import static org.assertj.core.api.Assertions.assertThat;
8 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
9 | import static org.assertj.core.data.MapEntry.entry;
10 |
11 | class MapsTest {
12 |
13 | @Test
14 | void simple_case() {
15 | Map labelsByIndex = Maps.build(Integer.class, 1, "test1", 2, "test2");
16 | assertThat(labelsByIndex).containsOnly(entry(1, "test1"), entry(2, "test2"));
17 | }
18 |
19 | @Test
20 | void odd_number_of_key_and_values() {
21 | assertThatExceptionOfType(IllegalArgumentException.class)
22 | .isThrownBy(() -> Maps.build(String.class, "key1"))
23 | .withMessage("keysAndValues array must be even");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jms/ErrorHandlerMock.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jms;
2 |
3 | import com.github.fridujo.automocker.api.Resettable;
4 | import org.springframework.util.ErrorHandler;
5 |
6 | import java.util.Optional;
7 |
8 | public class ErrorHandlerMock implements ErrorHandler, Resettable {
9 |
10 | private final Optional delegate;
11 | private Throwable lastCatched = null;
12 |
13 | ErrorHandlerMock(Optional delegate) {
14 | this.delegate = delegate;
15 | }
16 |
17 | @Override
18 | public void handleError(Throwable t) {
19 | lastCatched = t;
20 | delegate.ifPresent(e -> e.handleError(t));
21 | }
22 |
23 | public Optional getLastCatched() {
24 | return Optional.ofNullable(lastCatched);
25 | }
26 |
27 | @Override
28 | public void reset() {
29 | this.lastCatched = null;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/AfterBeanRegistration.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Inherited;
6 | import java.lang.annotation.Repeatable;
7 | import java.lang.annotation.Retention;
8 | import java.lang.annotation.RetentionPolicy;
9 | import java.lang.annotation.Target;
10 |
11 | @Target(ElementType.ANNOTATION_TYPE)
12 | @Retention(RetentionPolicy.RUNTIME)
13 | @Documented
14 | @Inherited
15 | @Repeatable(AfterBeanRegistration.AfterBeanRegistrations.class)
16 | public @interface AfterBeanRegistration {
17 |
18 | Class extends AfterBeanRegistrationExecutable> value();
19 |
20 | @Target(ElementType.ANNOTATION_TYPE)
21 | @Retention(RetentionPolicy.RUNTIME)
22 | @Documented
23 | @Inherited
24 | @interface AfterBeanRegistrations {
25 | AfterBeanRegistration[] value();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/BeforeBeanRegistration.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Inherited;
6 | import java.lang.annotation.Repeatable;
7 | import java.lang.annotation.Retention;
8 | import java.lang.annotation.RetentionPolicy;
9 | import java.lang.annotation.Target;
10 |
11 | @Target(ElementType.ANNOTATION_TYPE)
12 | @Retention(RetentionPolicy.RUNTIME)
13 | @Documented
14 | @Inherited
15 | @Repeatable(BeforeBeanRegistration.BeforeBeanRegistrations.class)
16 | public @interface BeforeBeanRegistration {
17 |
18 | Class extends BeforeBeanRegistrationExecutable> value();
19 |
20 | @Target(ElementType.ANNOTATION_TYPE)
21 | @Retention(RetentionPolicy.RUNTIME)
22 | @Documented
23 | @Inherited
24 | @interface BeforeBeanRegistrations {
25 | BeforeBeanRegistration[] value();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36}.%L - %msg%n
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/java/com/github/fridujo/sample/jdbc/order/LineItem.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jdbc.order;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.GeneratedValue;
5 | import javax.persistence.Id;
6 | import java.util.Objects;
7 |
8 | @Entity
9 | public class LineItem {
10 |
11 | private final String description;
12 | @Id
13 | @GeneratedValue
14 | private Long id;
15 |
16 | public LineItem() {
17 | description = null;
18 | }
19 |
20 | public LineItem(String description) {
21 | this.description = description;
22 | }
23 |
24 | @Override
25 | public boolean equals(Object o) {
26 | if (this == o) return true;
27 | if (o == null || getClass() != o.getClass()) return false;
28 | LineItem lineItem = (LineItem) o;
29 | return Objects.equals(id, lineItem.id);
30 | }
31 |
32 | @Override
33 | public int hashCode() {
34 |
35 | return Objects.hash(id);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/utils/ActiveMQConnectionFactoryFactory.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms.utils;
2 |
3 | import org.apache.activemq.ActiveMQConnectionFactory;
4 | import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
5 |
6 | import javax.jms.ConnectionFactory;
7 |
8 | public class ActiveMQConnectionFactoryFactory {
9 |
10 | private final ActiveMQProperties activeMQProperties;
11 |
12 | public ActiveMQConnectionFactoryFactory(ActiveMQProperties activeMQProperties) {
13 | this.activeMQProperties = activeMQProperties;
14 | }
15 |
16 | public ConnectionFactory create() {
17 | String username = activeMQProperties.getUser();
18 | String password = activeMQProperties.getPassword();
19 | String brokerUrl = activeMQProperties.getBrokerUrl();
20 |
21 | ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(username, password, brokerUrl);
22 | return connectionFactory;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/metrics/AbstractMetricAsserter.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.metrics;
2 |
3 | import java.util.List;
4 |
5 | public class AbstractMetricAsserter {
6 |
7 | protected final String metricName;
8 | protected final List points;
9 |
10 | protected AbstractMetricAsserter(String metricName, List points) {
11 | this.metricName = metricName;
12 | this.points = points;
13 | }
14 |
15 | protected Point getLastPoint() {
16 | if (points.isEmpty()) {
17 | throw new AssertionError("No values recorded for metric " + metricName);
18 | }
19 | return points.get(points.size() - 1);
20 | }
21 |
22 | protected Point getPenultimatePoint() {
23 | final Point penultimatePoint;
24 | if (points.size() > 1) {
25 | penultimatePoint = points.get(points.size() - 2);
26 | } else {
27 | penultimatePoint = Point.NEUTRAl_ELEMENT;
28 | }
29 | return penultimatePoint;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/metrics/GraphiteMock.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.metrics;
2 |
3 | import com.codahale.metrics.graphite.GraphiteReporter;
4 |
5 | public class GraphiteMock {
6 |
7 | private final GraphiteSenderMock graphiteSenderMock;
8 | private final GraphiteReporter graphiteReporter;
9 |
10 | public GraphiteMock(GraphiteSenderMock graphiteSenderMock, GraphiteReporter graphiteReporter) {
11 | this.graphiteSenderMock = graphiteSenderMock;
12 | this.graphiteReporter = graphiteReporter;
13 | }
14 |
15 | public MetricAsserter assertThatMetric(String metricName) {
16 | if (!graphiteSenderMock.getMetrics().containsKey(metricName)) {
17 | throw new IllegalArgumentException("No metric named [" + metricName + "] have been sent to Graphite");
18 | }
19 | return new MetricAsserter(metricName, graphiteSenderMock.getMetrics().get(metricName));
20 | }
21 |
22 | public GraphiteMock afterReporting() {
23 | graphiteReporter.report();
24 | return this;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/starters/spring-automocker-starter-jdbc/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | spring-automocker-reactor
8 | com.github.fridujo
9 | 1.2.2-SNAPSHOT
10 | ../../pom.xml
11 |
12 |
13 | spring-automocker-starter-jdbc
14 |
15 |
16 |
17 | com.github.fridujo
18 | spring-automocker-starter
19 | ${automocker.version}
20 | compile
21 |
22 |
23 | com.h2database
24 | h2
25 | ${h2.version}
26 | compile
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/starters/spring-automocker-starter-web/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | spring-automocker-reactor
8 | com.github.fridujo
9 | 1.2.2-SNAPSHOT
10 | ../../pom.xml
11 |
12 |
13 | spring-automocker-starter-web
14 |
15 |
16 |
17 | com.github.fridujo
18 | spring-automocker-starter
19 | ${automocker.version}
20 | compile
21 |
22 |
23 | org.hamcrest
24 | hamcrest-core
25 | ${hamcrest.version}
26 | compile
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/samples/spring-automocker-property-source-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 |
7 | spring-automocker-reactor
8 | com.github.fridujo
9 | 1.2.2-SNAPSHOT
10 | ../../pom.xml
11 |
12 |
13 | spring-automocker-property-source-sample
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-autoconfigure
19 | ${spring-boot.version}
20 |
21 |
22 |
23 |
24 | com.github.fridujo
25 | spring-automocker-starter
26 | ${automocker.version}
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/starters/spring-automocker-starter-jms/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | spring-automocker-reactor
8 | com.github.fridujo
9 | 1.2.2-SNAPSHOT
10 | ../../pom.xml
11 |
12 |
13 | spring-automocker-starter-jms
14 |
15 |
16 |
17 | com.github.fridujo
18 | spring-automocker-starter
19 | ${automocker.version}
20 | compile
21 |
22 |
23 | com.mockrunner
24 | mockrunner-jms
25 | ${mockrunner.version}
26 | compile
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/starters/spring-automocker-starter-amqp/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | spring-automocker-reactor
8 | com.github.fridujo
9 | 1.2.2-SNAPSHOT
10 | ../../pom.xml
11 |
12 |
13 | spring-automocker-starter-amqp
14 |
15 |
16 |
17 | com.github.fridujo
18 | spring-automocker-starter
19 | ${automocker.version}
20 | compile
21 |
22 |
23 | com.github.fridujo
24 | rabbitmq-mock
25 | ${rabbitmq-mock.version}
26 | compile
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/metrics/GraphiteSenderMock.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.metrics;
2 |
3 | import com.codahale.metrics.graphite.GraphiteSender;
4 |
5 | import java.util.ArrayList;
6 | import java.util.LinkedHashMap;
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | public class GraphiteSenderMock implements GraphiteSender {
11 |
12 | private final Map> metrics = new LinkedHashMap<>();
13 |
14 | @Override
15 | public void connect() {
16 | }
17 |
18 | @Override
19 | public void send(String name, String value, long timestamp) {
20 | if (!metrics.containsKey(name)) {
21 | metrics.put(name, new ArrayList<>());
22 | }
23 | metrics.get(name).add(new Point(timestamp, Double.valueOf(value)));
24 | }
25 |
26 | @Override
27 | public void flush() {
28 | }
29 |
30 | @Override
31 | public boolean isConnected() {
32 | return true;
33 | }
34 |
35 | @Override
36 | public int getFailures() {
37 | return 0;
38 | }
39 |
40 | @Override
41 | public void close() {
42 | }
43 |
44 | public Map> getMetrics() {
45 | return metrics;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/base/RegisterTools.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.base;
2 |
3 | import com.github.fridujo.automocker.api.AfterBeanRegistration;
4 | import com.github.fridujo.automocker.api.AfterBeanRegistrationExecutable;
5 | import com.github.fridujo.automocker.api.ExtendedBeanDefinitionRegistry;
6 | import com.github.fridujo.automocker.api.tools.BeanLocator;
7 |
8 | import java.lang.annotation.Annotation;
9 | import java.lang.annotation.Documented;
10 | import java.lang.annotation.ElementType;
11 | import java.lang.annotation.Retention;
12 | import java.lang.annotation.RetentionPolicy;
13 | import java.lang.annotation.Target;
14 |
15 | /**
16 | * Register utilities.
17 | */
18 | @Target(ElementType.TYPE)
19 | @Retention(RetentionPolicy.RUNTIME)
20 | @Documented
21 |
22 | @AfterBeanRegistration(RegisterTools.RegisterToolsExecutable.class)
23 | public @interface RegisterTools {
24 |
25 | class RegisterToolsExecutable implements AfterBeanRegistrationExecutable {
26 |
27 | @Override
28 | public void execute(Annotation annotation, ExtendedBeanDefinitionRegistry extendedBeanDefinitionRegistry) {
29 | extendedBeanDefinitionRegistry.registerBeanDefinition(BeanLocator.class);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/test/java/com/github/fridujo/sample/jms/JmsApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms;
2 |
3 | import com.github.fridujo.automocker.api.jms.JmsMock;
4 | import com.github.fridujo.automocker.base.Automocker;
5 | import org.assertj.core.api.AbstractAssert;
6 | import org.junit.jupiter.api.Test;
7 | import org.junit.jupiter.api.extension.ExtendWith;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.test.context.ContextConfiguration;
10 | import org.springframework.test.context.junit.jupiter.SpringExtension;
11 |
12 | @Automocker
13 | @ExtendWith(SpringExtension.class)
14 | @ContextConfiguration(classes = JmsApplication.class)
15 | class JmsApplicationTest {
16 |
17 | @Autowired
18 | @Broker(Broker.Type.BUYER)
19 | private JmsMock buyerJmsMock;
20 |
21 | @Test
22 | void buyer_receives_item_when_available() {
23 | String item = "available AC/DC concert tickets";
24 | buyerJmsMock.sendText("REQUEST_ITEM", item);
25 |
26 | buyerJmsMock.assertThatDestination("BOUGHT")
27 | .consumingFirstMessage()
28 | .textSatisfies(actual -> actual.isEqualTo(item))
29 | .headerSatisfies(BusinessHeaders.BUYER_ID, AbstractAssert::isNotNull)
30 | .headerSatisfies(BusinessHeaders.SELLER_ID, AbstractAssert::isNotNull);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/spring-automocker/src/test/java/com/github/fridujo/automocker/utils/VersionTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.junit.jupiter.params.ParameterizedTest;
5 | import org.junit.jupiter.params.provider.Arguments;
6 | import org.junit.jupiter.params.provider.MethodSource;
7 |
8 | import java.util.stream.Stream;
9 |
10 | import static com.github.fridujo.automocker.utils.Version.major;
11 | import static com.github.fridujo.automocker.utils.Version.spring;
12 | import static org.assertj.core.api.Assertions.assertThat;
13 |
14 | class VersionTest {
15 |
16 | static Stream before_behaves_as_expected() {
17 | return Stream.of(
18 | Arguments.of(major(0).minor(0), major(0).minor(1), true),
19 | Arguments.of(major(0).minor(0), major(1).minor(0), true),
20 | Arguments.of(major(1).minor(0), major(0).minor(1), false),
21 | Arguments.of(major(1).minor(4), major(1).minor(4), false)
22 | );
23 | }
24 |
25 | @ParameterizedTest(name = "{0} is before {1} = {2}")
26 | @MethodSource
27 | void before_behaves_as_expected(Version first, Version second, boolean before) {
28 | assertThat(first.isBefore(second)).isEqualTo(before);
29 | }
30 |
31 | @Test
32 | void spring_version_is_after_4_3() {
33 | assertThat(major(4).minor(3).isBefore(spring()));
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jdbc/DataSourceResetter.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jdbc;
2 |
3 |
4 | import com.github.fridujo.automocker.api.Resettable;
5 |
6 | import javax.sql.DataSource;
7 | import java.util.Map;
8 |
9 | public class DataSourceResetter implements Resettable {
10 |
11 | private final Map datasourcesByName;
12 |
13 | DataSourceResetter(Map datasourcesByName) {
14 | this.datasourcesByName = datasourcesByName;
15 | }
16 |
17 | @Override
18 | public void reset() {
19 | datasourcesByName.forEach((dbName, dataSource) -> {
20 | reset(dbName, dataSource);
21 | });
22 | }
23 |
24 | private void reset(String dbName, DataSource dataSource) {
25 | try {
26 | DataSources.doInConnection(dataSource, c -> {
27 | Connections.execute(c, "SET REFERENTIAL_INTEGRITY FALSE");
28 | try {
29 | Connections.tables(c)
30 | .forEach(tableName -> Connections.truncate(c, tableName));
31 | } finally {
32 | Connections.execute(c, "SET REFERENTIAL_INTEGRITY TRUE");
33 | }
34 | });
35 | } catch (RuntimeException e) {
36 | throw new IllegalStateException("Could not reset DB[" + dbName + "]", e);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/java/com/github/fridujo/sample/jdbc/order/Order.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jdbc.order;
2 |
3 | import com.github.fridujo.sample.jdbc.customer.Customer;
4 | import org.springframework.util.Assert;
5 |
6 | import javax.persistence.CascadeType;
7 | import javax.persistence.Entity;
8 | import javax.persistence.FetchType;
9 | import javax.persistence.GeneratedValue;
10 | import javax.persistence.Id;
11 | import javax.persistence.OneToMany;
12 | import javax.persistence.Table;
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | @Entity
17 | // Needs to be explicitly named as Order is a reserved keyword
18 | @Table(name = "SampleOrder")
19 | public class Order {
20 |
21 | private final Customer.CustomerId customer;
22 | @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
23 | private final List lineItems = new ArrayList<>();
24 | @Id
25 | @GeneratedValue
26 | private Long id;
27 |
28 | public Order() {
29 | customer = null;
30 | }
31 |
32 | public Order(Customer.CustomerId customer) {
33 | this.customer = customer;
34 | }
35 |
36 | public void add(LineItem lineItem) {
37 | Assert.notNull(lineItem, "Line item must not be null!");
38 |
39 | this.lineItems.add(lineItem);
40 | }
41 |
42 | public List getLineItems() {
43 | return lineItems;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jms/DestinationManagerResetter.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jms;
2 |
3 | import com.github.fridujo.automocker.api.Resettable;
4 | import com.mockrunner.jms.DestinationManager;
5 | import com.mockrunner.mock.jms.MockDestination;
6 | import org.springframework.test.util.ReflectionTestUtils;
7 |
8 | import java.util.Map;
9 | import java.util.Set;
10 |
11 | public class DestinationManagerResetter implements Resettable {
12 |
13 | private final Set destinationManagers;
14 |
15 | public DestinationManagerResetter(Set destinationManagers) {
16 | this.destinationManagers = destinationManagers;
17 | }
18 |
19 | @SuppressWarnings("unchecked")
20 | @Override
21 | public void reset() {
22 | for (DestinationManager destinationManager : destinationManagers) {
23 | // TODO make a PR to mockrunner-jms to avoid these reflective accesses
24 | Map queues =
25 | (Map) ReflectionTestUtils.getField(destinationManager, "queues");
26 | Map topics =
27 | (Map) ReflectionTestUtils.getField(destinationManager, "topics");
28 | queues.values().forEach(d -> d.reset());
29 | topics.values().forEach(d -> d.reset());
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 | spring-automocker-reactor
6 | com.github.fridujo
7 | 1.2.2-SNAPSHOT
8 | ../../pom.xml
9 |
10 | 4.0.0
11 |
12 | spring-automocker-jdbc-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-data-jpa
18 | ${spring-boot.version}
19 |
20 |
21 | org.hsqldb
22 | hsqldb
23 | ${hsqldb.version}
24 |
25 |
26 |
27 |
28 | com.github.fridujo
29 | spring-automocker-starter-jdbc
30 | ${automocker.version}
31 | test
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/samples/spring-automocker-mvc-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 | spring-automocker-reactor
6 | com.github.fridujo
7 | 1.2.2-SNAPSHOT
8 | ../../pom.xml
9 |
10 | 4.0.0
11 |
12 | spring-automocker-mvc-sample
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-web
18 | ${spring-boot.version}
19 |
20 |
21 | org.slf4j
22 | slf4j-api
23 | ${slf4j.version}
24 |
25 |
26 |
27 |
28 | com.github.fridujo
29 | spring-automocker-starter-web
30 | ${automocker.version}
31 | test
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/java/com/github/fridujo/sample/jdbc/customer/Customer.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jdbc.customer;
2 |
3 | import javax.persistence.Embeddable;
4 | import javax.persistence.Entity;
5 | import javax.persistence.GeneratedValue;
6 | import javax.persistence.Id;
7 | import java.io.Serializable;
8 |
9 | @Entity
10 | public class Customer {
11 | private final String firstname;
12 | private final String lastname;
13 | @Id
14 | @GeneratedValue
15 | private Long id;
16 |
17 | public Customer() {
18 | firstname = null;
19 | lastname = null;
20 | }
21 |
22 | public Customer(String firstname, String lastname) {
23 | this.firstname = firstname;
24 | this.lastname = lastname;
25 | }
26 |
27 | public CustomerId getId() {
28 | return new CustomerId(id);
29 | }
30 |
31 | public String getFirstname() {
32 | return firstname;
33 | }
34 |
35 | public String getLastname() {
36 | return lastname;
37 | }
38 |
39 | @Embeddable
40 | public static class CustomerId implements Serializable {
41 | private final Long customerId;
42 |
43 | CustomerId() {
44 | this.customerId = null;
45 | }
46 |
47 | CustomerId(Long id) {
48 | this.customerId = id;
49 | }
50 |
51 | public Long getCustomerId() {
52 | return customerId;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/samples/spring-automocker-graphite-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | spring-automocker-reactor
8 | com.github.fridujo
9 | 1.2.2-SNAPSHOT
10 | ../../pom.xml
11 |
12 |
13 | spring-automocker-graphite-sample
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-actuator
19 | ${spring-boot.version}
20 |
21 |
22 | io.micrometer
23 | micrometer-registry-graphite
24 | ${micrometer.version}
25 |
26 |
27 |
28 |
29 | com.github.fridujo
30 | spring-automocker-starter
31 | ${automocker.version}
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/samples/spring-automocker-property-source-sample/src/test/java/com/github/fridujo/sample/propertySource/PropertySourceApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.propertySource;
2 |
3 |
4 | import com.github.fridujo.automocker.base.Automocker;
5 | import org.junit.jupiter.api.Test;
6 | import org.junit.jupiter.api.extension.ExtendWith;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.beans.factory.annotation.Value;
9 | import org.springframework.test.context.ContextConfiguration;
10 | import org.springframework.test.context.TestPropertySource;
11 | import org.springframework.test.context.junit.jupiter.SpringExtension;
12 |
13 | import java.util.Optional;
14 |
15 | import static org.assertj.core.api.Assertions.assertThat;
16 |
17 | @Automocker
18 | @ExtendWith(SpringExtension.class)
19 | @ContextConfiguration(classes = PropertySourceApplication.class)
20 | @TestPropertySource(properties = {
21 | "text.literal = literal Test Text",
22 | "text.optional=optionalText"})
23 | class PropertySourceApplicationTest {
24 |
25 | @Autowired
26 | private TextService service;
27 |
28 | @Value("${missing.key:}")
29 | private Optional emptyOptional;
30 |
31 | @Test
32 | void property_source_is_mocked() {
33 | assertThat(service.getLiteralText()).isEqualTo("literal Test Text");
34 | assertThat(service.getOptionalText()).isEqualTo("optionalText");
35 | assertThat(emptyOptional).isEmpty();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jms/JmsDestinationAssert.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jms;
2 |
3 | import com.mockrunner.mock.jms.MockDestination;
4 | import org.assertj.core.api.Assertions;
5 |
6 | import javax.jms.Message;
7 | import java.util.List;
8 |
9 | public class JmsDestinationAssert {
10 |
11 | private final String destinationName;
12 | private final MockDestination destination;
13 |
14 | public JmsDestinationAssert(MockDestination mockDestination, String destinationName) {
15 | this.destination = mockDestination;
16 | this.destinationName = destinationName;
17 | }
18 |
19 | public JmsDestinationAssert hasSize(int expected) {
20 | Assertions.assertThat(destination.getReceivedMessageList()
21 | .size())
22 | .isEqualTo(expected);
23 | return this;
24 | }
25 |
26 | public JmsMessageAssert consumingFirstMessage() {
27 | if (destination.isEmpty()) {
28 | throw new IllegalArgumentException(
29 | "No message available on destination [" + destinationName + "]");
30 | }
31 | return new JmsMessageAssert(destination.getMessage());
32 | }
33 |
34 | @SuppressWarnings("unchecked")
35 | public List getMessages() {
36 | return destination.getCurrentMessageList();
37 | }
38 |
39 | public String toString() {
40 | return "Asserter on JMS destination [" + destinationName + "]";
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/context/ResettableTestExecutionListener.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.context;
2 |
3 | import com.github.fridujo.automocker.api.ResetMocks;
4 | import com.github.fridujo.automocker.api.Resettable;
5 | import org.springframework.context.ApplicationContext;
6 | import org.springframework.core.annotation.AnnotationUtils;
7 | import org.springframework.test.context.TestContext;
8 | import org.springframework.test.context.support.AbstractTestExecutionListener;
9 |
10 | public class ResettableTestExecutionListener extends AbstractTestExecutionListener {
11 |
12 | @Override
13 | public void afterTestMethod(TestContext testContext) {
14 | resetAll(testContext);
15 | }
16 |
17 | @Override
18 | public void afterTestClass(TestContext testContext) {
19 | resetAll(testContext);
20 | }
21 |
22 | @Override
23 | public void afterTestExecution(TestContext testContext) {
24 | resetAll(testContext);
25 | }
26 |
27 | private void resetAll(TestContext testContext) {
28 | ResetMocks resetMocks = AnnotationUtils.getAnnotation(testContext.getTestClass(), ResetMocks.class);
29 | if (resetMocks != null && !resetMocks.disable()) {
30 | ApplicationContext applicationContext = testContext.getApplicationContext();
31 | for (Resettable resettable : applicationContext.getBeansOfType(Resettable.class).values()) {
32 | resettable.reset();
33 | }
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/java/com/github/fridujo/sample/jdbc/JdbcApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jdbc;
2 |
3 | import com.github.fridujo.sample.jdbc.customer.Customer;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
9 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
10 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
11 | import org.springframework.transaction.annotation.EnableTransactionManagement;
12 |
13 | import javax.annotation.PostConstruct;
14 |
15 | @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
16 | DataSourceTransactionManagerAutoConfiguration.class})
17 | @EnableTransactionManagement
18 | @EnableConfigurationProperties
19 | public class JdbcApplication {
20 |
21 | @Autowired
22 | DataInitializer initializer;
23 |
24 | public static void main(String[] args) {
25 | SpringApplication.run(JdbcApplication.class, args);
26 | }
27 |
28 | @PostConstruct
29 | public void init() {
30 | Customer.CustomerId customerId = initializer.initializeCustomer();
31 | initializer.initializeOrder(customerId);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/buyer/Buyer.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms.buyer;
2 |
3 | import com.github.fridujo.sample.jms.Broker;
4 | import com.github.fridujo.sample.jms.BusinessHeaders;
5 | import com.github.fridujo.sample.jms.JmsListenerContainerFactories;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.jms.annotation.JmsListener;
9 | import org.springframework.jms.core.JmsTemplate;
10 | import org.springframework.messaging.handler.annotation.Payload;
11 | import org.springframework.stereotype.Component;
12 |
13 | import javax.jms.TextMessage;
14 | import java.util.UUID;
15 |
16 | @Component
17 | class Buyer {
18 | private final Logger logger = LoggerFactory.getLogger("buyer");
19 | private final JmsTemplate sellerSender;
20 | private final String id;
21 |
22 | Buyer(@Broker(Broker.Type.SELLER) JmsTemplate sellerSender) {
23 | this.sellerSender = sellerSender;
24 | this.id = UUID.randomUUID().toString();
25 | }
26 |
27 | @JmsListener(destination = "REQUEST_ITEM", containerFactory = JmsListenerContainerFactories.BUYER)
28 | void onItemRequest(@Payload String item) {
29 | sellerSender.send("REQUESTED_ITEM", session -> {
30 | TextMessage textMessage = session.createTextMessage(item);
31 | textMessage.setStringProperty(BusinessHeaders.BUYER_ID, id);
32 | return textMessage;
33 | });
34 | logger.info("Buyer " + id + " sent request for [" + item + "]");
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/spring-automocker/src/test/java/com/github/fridujo/automocker/api/jms/JmsMockLocatorTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jms;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import javax.jms.ConnectionFactory;
6 | import java.util.HashSet;
7 | import java.util.Set;
8 |
9 | import static org.assertj.core.api.Assertions.assertThat;
10 | import static org.mockito.Mockito.mock;
11 |
12 | class JmsMockLocatorTest {
13 |
14 | @Test
15 | void getJmsMockByConnectionFactory_returns_matching_object() {
16 | ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
17 | ConnectionFactory connectionFactory2 = mock(ConnectionFactory.class);
18 | ConnectionFactory connectionFactory3 = mock(ConnectionFactory.class);
19 | JmsMock jmsMock = new JmsMock(connectionFactory, null);
20 | JmsMock jmsMock2 = new JmsMock(connectionFactory2, null);
21 |
22 | assertThat(connectionFactory == connectionFactory2).isFalse();
23 | assertThat(jmsMock == jmsMock2).isFalse();
24 |
25 | Set jmsMocks = new HashSet<>();
26 | jmsMocks.add(jmsMock);
27 | jmsMocks.add(jmsMock2);
28 |
29 | JmsMockLocator jmsMockLocator = new JmsMockLocator(jmsMocks);
30 |
31 | assertThat(jmsMockLocator.getJmsMockByConnectionFactory(connectionFactory)).contains(jmsMock);
32 | assertThat(jmsMockLocator.getJmsMockByConnectionFactory(connectionFactory2)).contains(jmsMock2);
33 | assertThat(jmsMockLocator.getJmsMockByConnectionFactory(connectionFactory3)).isEmpty();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jms/JmsListenerContainerFactoryConfigurer.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jms;
2 |
3 | import org.springframework.jms.config.AbstractJmsListenerContainerFactory;
4 | import org.springframework.test.util.ReflectionTestUtils;
5 | import org.springframework.util.ErrorHandler;
6 |
7 | import javax.jms.ConnectionFactory;
8 | import java.util.Optional;
9 | import java.util.Set;
10 |
11 | public class JmsListenerContainerFactoryConfigurer {
12 |
13 | JmsListenerContainerFactoryConfigurer(Set> jmsListenerContainerFactories,
14 | JmsMockLocator jmsMockLocator) {
15 | jmsListenerContainerFactories.forEach(jmsListenerContainerFactory -> {
16 | ConnectionFactory connectionFactory = (ConnectionFactory) ReflectionTestUtils.getField(
17 | jmsListenerContainerFactory, "connectionFactory");
18 | Optional originalErrorHandler = Optional.ofNullable(
19 | (ErrorHandler) ReflectionTestUtils.getField(jmsListenerContainerFactory, "errorHandler"));
20 |
21 | ErrorHandlerMock automockerErrorHandler = new ErrorHandlerMock(originalErrorHandler);
22 | jmsListenerContainerFactory.setErrorHandler(automockerErrorHandler);
23 |
24 | Optional jmsMockOptional = jmsMockLocator.getJmsMockByConnectionFactory(connectionFactory);
25 | jmsMockOptional.ifPresent(jmsMock -> jmsMock.setErrorHandlerMock(automockerErrorHandler));
26 | });
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/samples/spring-automocker-graphite-sample/src/main/java/com/github/fridujo/sample/graphite/GraphiteApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.graphite;
2 |
3 | import io.micrometer.core.instrument.Counter;
4 | import io.micrometer.core.instrument.MeterRegistry;
5 | import org.springframework.boot.Banner;
6 | import org.springframework.boot.CommandLineRunner;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 |
10 | import java.util.Optional;
11 |
12 | @SpringBootApplication
13 | public class GraphiteApplication implements CommandLineRunner {
14 |
15 | private final Counter runCounter;
16 |
17 | public GraphiteApplication(MeterRegistry meterRegistry) {
18 | this.runCounter = meterRegistry.counter("run");
19 | }
20 |
21 | public static void main(String[] args) {
22 | SpringApplication app = new SpringApplication(GraphiteApplication.class);
23 | app.setBannerMode(Banner.Mode.OFF);
24 | app.run(args);
25 | }
26 |
27 | @Override
28 | public void run(String... args) {
29 | Optional.of(args)
30 | .filter(a -> a.length > 0)
31 | .map(a -> a[0])
32 | .flatMap(this::tryParse)
33 | .ifPresent(incrementFactor -> runCounter.increment(incrementFactor));
34 | }
35 |
36 | private Optional tryParse(String string) {
37 | try {
38 | return Optional.of(Double.parseDouble(string));
39 | } catch (NumberFormatException e) {
40 | return Optional.empty();
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/JmsApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms;
2 |
3 | import com.github.fridujo.sample.jms.seller.Seller;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
7 | import org.springframework.context.ConfigurableApplicationContext;
8 | import org.springframework.jms.annotation.EnableJms;
9 | import org.springframework.jms.core.JmsTemplate;
10 |
11 | import javax.jms.TextMessage;
12 | import java.util.concurrent.Semaphore;
13 |
14 | @SpringBootApplication(exclude = {JmsAutoConfiguration.class})
15 | @EnableJms
16 | public class JmsApplication {
17 |
18 | public static void main(String[] args) throws InterruptedException {
19 | try (ConfigurableApplicationContext applicationContext = SpringApplication.run(JmsApplication.class)) {
20 | Semaphore waitForSellerToSendItem = new Semaphore(0);
21 | Seller buyer = applicationContext.getBean(Seller.class);
22 | buyer.addItemSoldListener(item -> waitForSellerToSendItem.release());
23 | JmsTemplate buyerJmsTemplate = applicationContext.getBean("buyerJmsTemplate", JmsTemplate.class);
24 | buyerJmsTemplate.send("REQUEST_ITEM", session -> {
25 | TextMessage textMessage = session.createTextMessage("available concert tickets");
26 | return textMessage;
27 | });
28 | waitForSellerToSendItem.acquire();
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/samples/spring-automocker-graphite-sample/src/test/java/com/github/fridujo/sample/graphite/GraphiteApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.graphite;
2 |
3 | import com.github.fridujo.automocker.api.metrics.GraphiteMock;
4 | import com.github.fridujo.automocker.base.Automocker;
5 | import org.junit.jupiter.api.extension.ExtendWith;
6 | import org.junit.jupiter.params.ParameterizedTest;
7 | import org.junit.jupiter.params.provider.CsvSource;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.boot.CommandLineRunner;
10 | import org.springframework.test.context.ContextConfiguration;
11 | import org.springframework.test.context.junit.jupiter.SpringExtension;
12 |
13 | @Automocker
14 | @ExtendWith(SpringExtension.class)
15 | @ContextConfiguration(classes = GraphiteApplication.class)
16 | class GraphiteApplicationTest {
17 |
18 | private final CommandLineRunner commandLineRunner;
19 | private final GraphiteMock graphiteMock;
20 |
21 | GraphiteApplicationTest(@Autowired CommandLineRunner commandLineRunner, @Autowired GraphiteMock graphiteMock) {
22 | this.commandLineRunner = commandLineRunner;
23 | this.graphiteMock = graphiteMock;
24 | }
25 |
26 | @ParameterizedTest
27 | @CsvSource({"1, 1", "3, 3", "7, 7", "test, 0"})
28 | void metric_is_sent_to_graphite(String appParameter, int expectedIncrement) throws Exception {
29 | commandLineRunner.run(appParameter);
30 |
31 | graphiteMock.afterReporting().assertThatMetric("run.count")
32 | .derivative()
33 | .hasLastValue(expectedIncrement);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jdbc-sample/src/main/java/com/github/fridujo/sample/jdbc/DataInitializer.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jdbc;
2 |
3 | import com.github.fridujo.sample.jdbc.customer.Customer;
4 | import com.github.fridujo.sample.jdbc.customer.CustomerRepository;
5 | import com.github.fridujo.sample.jdbc.order.LineItem;
6 | import com.github.fridujo.sample.jdbc.order.Order;
7 | import com.github.fridujo.sample.jdbc.order.OrderRepository;
8 | import org.springframework.stereotype.Component;
9 | import org.springframework.transaction.annotation.Transactional;
10 | import org.springframework.util.Assert;
11 |
12 | @Component
13 | public class DataInitializer {
14 |
15 | private final OrderRepository orders;
16 | private final CustomerRepository customers;
17 |
18 | public DataInitializer(OrderRepository orders, CustomerRepository customers) {
19 | this.orders = orders;
20 | this.customers = customers;
21 | }
22 |
23 | @Transactional("customerTransactionManager")
24 | public Customer.CustomerId initializeCustomer() {
25 | return customers.save(new Customer("Dave", "Matthews"))
26 | .getId();
27 | }
28 |
29 | @Transactional("orderTransactionManager")
30 | public void initializeOrder(Customer.CustomerId customer) {
31 | Assert.notNull(customer, "Customer identifier must not be null!");
32 |
33 | Order order1 = new Order(customer);
34 | order1.add(new LineItem("Fender Jag-Stang Guitar"));
35 | orders.save(order1);
36 |
37 | Order order2 = new Order(customer);
38 | order2.add(new LineItem("Gene Simmons Axe Bass"));
39 | orders.save(order2);
40 |
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/Version.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import com.google.common.base.Splitter;
4 | import com.google.common.primitives.Ints;
5 | import org.springframework.context.ApplicationContext;
6 |
7 | import java.util.List;
8 |
9 | public class Version {
10 |
11 | private final int major;
12 | private final int minor;
13 |
14 | private Version(int major, int minor) {
15 | this.major = major;
16 | this.minor = minor;
17 | }
18 |
19 | public static Version major(int major) {
20 | return new Version(major, 0);
21 | }
22 |
23 | public static Version spring() {
24 | String springVersion = ApplicationContext.class.getPackage().getImplementationVersion();
25 | List tokenizedSpringVersion = Splitter.on('.').splitToList(springVersion);
26 | int major = Ints.tryParse(tokenizedSpringVersion.get(0));
27 | int minor = Ints.tryParse(tokenizedSpringVersion.get(1));
28 |
29 | return major(major).minor(minor);
30 | }
31 |
32 | public Version minor(int minor) {
33 | return new Version(this.major, minor);
34 | }
35 |
36 | public boolean isBefore(Version otherVersion) {
37 | final boolean before;
38 | if (major < otherVersion.major) {
39 | before = true;
40 | } else if (major == otherVersion.major) {
41 | before = minor < otherVersion.minor;
42 | } else {
43 | before = false;
44 | }
45 | return before;
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return "v" + major + "." + minor;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/utils/Classes.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | public final class Classes {
6 |
7 | private Classes() {
8 | }
9 |
10 | public static T instanciate(Class extends T> clazz) {
11 | try {
12 | return clazz.newInstance();
13 | } catch (InstantiationException | IllegalAccessException e) {
14 | throw new IllegalStateException("Cannot instanciate class " + clazz.getName() + ": " + e.getMessage(), e);
15 | }
16 | }
17 |
18 | public static boolean isPresent(String className) {
19 | try {
20 | Class.forName(className);
21 | return true;
22 | } catch (ClassNotFoundException | NoClassDefFoundError e) {
23 | return false;
24 | }
25 | }
26 |
27 | public static Class> forName(String className) {
28 | try {
29 | return Class.forName(className);
30 | } catch (ClassNotFoundException e) {
31 | throw new IllegalStateException("Cannot load class " + className);
32 | }
33 | }
34 |
35 | public static T getValueFromProtectedField(Object object, String fieldName) {
36 | Class> clazz = object.getClass();
37 | try {
38 | Field field = clazz.getDeclaredField(fieldName);
39 | if (!field.isAccessible()) {
40 | field.setAccessible(true);
41 | }
42 | return (T) field.get(object);
43 | } catch (NoSuchFieldException | IllegalAccessException e) {
44 | throw new IllegalStateException("Cannot find field " + fieldName + " in class " + clazz.getName(), e);
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/samples/spring-automocker-mvc-sample/src/test/java/com/github/fridujo/sample/mvc/MvcApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.mvc;
2 |
3 | import com.github.fridujo.automocker.base.Automocker;
4 | import org.junit.jupiter.api.Test;
5 | import org.junit.jupiter.api.extension.ExtendWith;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit.jupiter.SpringExtension;
9 | import org.springframework.test.web.servlet.MockMvc;
10 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
11 | import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
12 |
13 | @Automocker
14 | @ExtendWith(SpringExtension.class)
15 | @ContextConfiguration(classes = MvcApplication.class)
16 | class MvcApplicationTest {
17 |
18 | @Autowired
19 | private MockMvc mvc;
20 |
21 | @Test
22 | public void repository_is_available_and_consistent() throws Exception {
23 | mvc.perform(MockMvcRequestBuilders.get("/create_user?firstName=Alyson&lastName=Hannigan"))
24 | .andExpect(MockMvcResultMatchers.status()
25 | .isOk());
26 |
27 | mvc.perform(MockMvcRequestBuilders.get("/create_user?firstName=Angelina&lastName=Jolie"))
28 | .andExpect(MockMvcResultMatchers.status()
29 | .isOk());
30 |
31 | mvc.perform(MockMvcRequestBuilders.get("/list_users"))
32 | .andExpect(MockMvcResultMatchers.status()
33 | .isOk())
34 | .andExpect(MockMvcResultMatchers.content()
35 | .string("[{\"firstName\":\"Alyson\",\"lastName\":\"Hannigan\"},{\"firstName\":\"Angelina\",\"lastName\":\"Jolie\"}]"));
36 | }
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/samples/spring-automocker-mvc-sample/src/main/java/com/github/fridujo/sample/mvc/MvcApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.mvc;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestParam;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | import java.util.Collection;
12 | import java.util.LinkedHashMap;
13 | import java.util.Map;
14 | import java.util.concurrent.atomic.AtomicInteger;
15 |
16 | /**
17 | * Sample web application.
18 | * Run {@link #main(String[])} to launch.
19 | */
20 | @SpringBootApplication
21 | @RestController
22 | public class MvcApplication {
23 |
24 | private static final Logger LOGGER = LoggerFactory.getLogger(MvcApplication.class);
25 |
26 | private final Map database = new LinkedHashMap<>();
27 | private final AtomicInteger sequenceGenerator = new AtomicInteger();
28 |
29 | public MvcApplication() {
30 | LOGGER.info("Initiating web server");
31 | }
32 |
33 | public static void main(String[] args) {
34 | SpringApplication.run(MvcApplication.class);
35 | }
36 |
37 | @RequestMapping("/create_user")
38 | Customer createUser(@RequestParam("firstName") String firstName,
39 | @RequestParam("lastName") String lastName) {
40 | int id = sequenceGenerator.incrementAndGet();
41 | Customer newCustomer = new Customer(id, firstName, lastName);
42 | database.put(id, newCustomer);
43 | return newCustomer;
44 | }
45 |
46 | @RequestMapping("/list_users")
47 | Collection listUsers() {
48 | return database.values();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/samples/spring-automocker-amqp-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | spring-automocker-reactor
8 | com.github.fridujo
9 | 1.2.2-SNAPSHOT
10 | ../../pom.xml
11 |
12 |
13 | spring-automocker-amqp-sample
14 |
15 |
16 |
17 | org.springframework.boot
18 | spring-boot-starter-amqp
19 | ${spring-boot.version}
20 |
21 |
22 | com.rabbitmq
23 | http-client
24 |
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-actuator
30 | ${spring-boot.version}
31 |
32 |
33 | io.micrometer
34 | micrometer-registry-graphite
35 | ${micrometer.version}
36 |
37 |
38 |
39 |
40 | com.github.fridujo
41 | spring-automocker-starter-amqp
42 | ${automocker.version}
43 | test
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/samples/spring-automocker-property-source-sample/src/main/java/com/github/fridujo/sample/propertySource/PropertySourceApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.propertySource;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
7 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
8 | import org.springframework.context.ConfigurableApplicationContext;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.ComponentScan;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.context.annotation.PropertySource;
13 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
14 |
15 | @Configuration
16 | @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
17 | DataSourceTransactionManagerAutoConfiguration.class})
18 | @ComponentScan
19 | @PropertySource("classpath:appli.properties")
20 | public class PropertySourceApplication {
21 |
22 | @Bean
23 | public static PropertySourcesPlaceholderConfigurer configurer() {
24 | PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
25 | // Necessary to properly inject java.util.Optional values
26 | configurer.setNullValue("");
27 | return configurer;
28 | }
29 |
30 | public static void main(String[] args) {
31 | try (ConfigurableApplicationContext context = SpringApplication.run(PropertySourceApplication.class, args)) {
32 | TextService service = context.getBean(TextService.class);
33 |
34 | System.out.println(service.getLiteralText());
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/seller/SellerConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms.seller;
2 |
3 | import com.github.fridujo.sample.jms.Broker;
4 | import com.github.fridujo.sample.jms.JmsListenerContainerFactories;
5 | import com.github.fridujo.sample.jms.utils.ActiveMQConnectionFactoryFactory;
6 | import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
7 | import org.springframework.boot.context.properties.ConfigurationProperties;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
11 | import org.springframework.jms.core.JmsTemplate;
12 |
13 | import javax.jms.ConnectionFactory;
14 |
15 | @Configuration
16 | class SellerConfiguration {
17 |
18 | @Bean
19 | @ConfigurationProperties(prefix = "seller.activemq")
20 | @Broker(Broker.Type.SELLER)
21 | ActiveMQProperties sellerActiveMQProperties() {
22 | return new ActiveMQProperties();
23 | }
24 |
25 | @Bean
26 | @Broker(Broker.Type.SELLER)
27 | ConnectionFactory sellerConnectionFactory(@Broker(Broker.Type.SELLER) ActiveMQProperties activeMQProperties) {
28 | return new ActiveMQConnectionFactoryFactory(activeMQProperties).create();
29 | }
30 |
31 | @Bean(name = JmsListenerContainerFactories.SELLER)
32 | @Broker(Broker.Type.SELLER)
33 | DefaultJmsListenerContainerFactory sellerJmsListenerContainerFactory(@Broker(Broker.Type.SELLER) ConnectionFactory connectionFactory) {
34 | DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
35 | factory.setConnectionFactory(connectionFactory);
36 | return factory;
37 | }
38 |
39 | @Bean
40 | @Broker(Broker.Type.SELLER)
41 | JmsTemplate sellerJmsTemplate(@Broker(Broker.Type.SELLER) ConnectionFactory connectionFactory) {
42 | return new JmsTemplate(connectionFactory);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jdbc/DataSources.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jdbc;
2 |
3 | import com.github.fridujo.automocker.utils.ThrowingConsumer;
4 |
5 | import javax.sql.DataSource;
6 | import java.sql.Connection;
7 | import java.sql.PreparedStatement;
8 | import java.sql.SQLException;
9 | import java.util.List;
10 | import java.util.stream.Collectors;
11 | import java.util.stream.IntStream;
12 |
13 | public final class DataSources {
14 |
15 | private DataSources() {
16 | }
17 |
18 | public static void doInConnection(DataSource datasource, ThrowingConsumer task) {
19 | try (Connection c = datasource.getConnection()) {
20 | ThrowingConsumer.silent(task)
21 | .accept(c);
22 | c.commit();
23 | } catch (SQLException e) {
24 | throw new IllegalStateException(e);
25 | }
26 | }
27 |
28 | public static void populateTable(DataSource datasource, String tableName, List columns,
29 | List> values) {
30 | StringBuilder sql = new StringBuilder("INSERT INTO ");
31 | sql.append(tableName)
32 | .append(" (");
33 | sql.append(columns.stream()
34 | .map(String::toUpperCase)
35 | .collect(Collectors.joining(", ")));
36 | sql.append(") VALUES (");
37 | sql.append(IntStream.range(0, columns.size())
38 | .mapToObj(i -> "?")
39 | .collect(Collectors.joining(", ")));
40 | sql.append(")");
41 | doInConnection(datasource, c -> {
42 | try (PreparedStatement ps = c.prepareStatement(sql.toString())) {
43 | for (List line : values) {
44 | int i = 1;
45 | for (String value : line) {
46 | ps.setString(i, value);
47 | i++;
48 | }
49 | ps.addBatch();
50 | }
51 | ps.executeBatch();
52 | }
53 | });
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/samples/spring-automocker-jms-sample/src/main/java/com/github/fridujo/sample/jms/buyer/BuyerConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.sample.jms.buyer;
2 |
3 | import com.github.fridujo.sample.jms.Broker;
4 | import com.github.fridujo.sample.jms.JmsListenerContainerFactories;
5 | import com.github.fridujo.sample.jms.utils.ActiveMQConnectionFactoryFactory;
6 | import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
7 | import org.springframework.boot.context.properties.ConfigurationProperties;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
11 | import org.springframework.jms.config.JmsListenerContainerFactory;
12 | import org.springframework.jms.core.JmsTemplate;
13 | import org.springframework.jms.listener.DefaultMessageListenerContainer;
14 |
15 | import javax.jms.ConnectionFactory;
16 |
17 | @Configuration
18 | class BuyerConfiguration {
19 |
20 | @Bean
21 | @ConfigurationProperties(prefix = "buyer.activemq")
22 | @Broker(Broker.Type.BUYER)
23 | ActiveMQProperties buyerActiveMQProperties() {
24 | return new ActiveMQProperties();
25 | }
26 |
27 | @Bean
28 | @Broker(Broker.Type.BUYER)
29 | ConnectionFactory buyerConnectionFactory(@Broker(Broker.Type.BUYER) ActiveMQProperties activeMQProperties) {
30 | return new ActiveMQConnectionFactoryFactory(activeMQProperties).create();
31 | }
32 |
33 | @Bean(name = JmsListenerContainerFactories.BUYER)
34 | @Broker(Broker.Type.BUYER)
35 | JmsListenerContainerFactory buyerJmsListenerContainerFactory(@Broker(Broker.Type.BUYER) ConnectionFactory connectionFactory) {
36 | DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
37 | factory.setConnectionFactory(connectionFactory);
38 | return factory;
39 | }
40 |
41 | @Bean
42 | @Broker(Broker.Type.BUYER)
43 | JmsTemplate buyerJmsTemplate(@Broker(Broker.Type.BUYER) ConnectionFactory connectionFactory) {
44 | return new JmsTemplate(connectionFactory);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/spring-automocker/src/test/java/com/github/fridujo/automocker/utils/AnnotationsTest.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.utils;
2 |
3 | import org.assertj.core.api.Condition;
4 | import org.junit.jupiter.api.Test;
5 |
6 | import java.lang.annotation.Annotation;
7 | import java.lang.annotation.ElementType;
8 | import java.lang.annotation.Retention;
9 | import java.lang.annotation.RetentionPolicy;
10 | import java.lang.annotation.Target;
11 | import java.util.Set;
12 |
13 | import static org.assertj.core.api.Assertions.assertThat;
14 |
15 | class AnnotationsTest {
16 |
17 | @Test
18 | void search_annotated_annotations() {
19 | Set> annotationSet = Annotations.getAnnotationsAnnotatedWith(SampleAnnotatedClass.class, MarkingAnnotation.class);
20 |
21 | assertThat(annotationSet)
22 | .as("Annotated annotations")
23 | .hasSize(2)
24 | .are(new Condition<>(
25 | aa -> isAnnotationOfType(aa.parentAnnotation(), MarkingAnnotation.class),
26 | "@MarkingAnnotation is always the parent")
27 | )
28 | .>extracting(aa -> aa.annotation().annotationType())
29 | .containsOnly(AnnotatedAnnotation1.class, AnnotatedAnnotation2.class);
30 | }
31 |
32 | private boolean isAnnotationOfType(Annotation a, Class extends Annotation> type) {
33 | return type.isAssignableFrom(a.annotationType());
34 | }
35 |
36 | @Target(ElementType.ANNOTATION_TYPE)
37 | @Retention(RetentionPolicy.RUNTIME)
38 | private @interface MarkingAnnotation {
39 |
40 | }
41 |
42 | @Target(ElementType.TYPE)
43 | @Retention(RetentionPolicy.RUNTIME)
44 |
45 | @MarkingAnnotation
46 | private @interface AnnotatedAnnotation1 {
47 |
48 | }
49 |
50 | @Target(ElementType.TYPE)
51 | @Retention(RetentionPolicy.RUNTIME)
52 |
53 | @MarkingAnnotation
54 | @AnnotatedAnnotation1
55 | private @interface AnnotatedAnnotation2 {
56 |
57 | }
58 |
59 | @AnnotatedAnnotation1
60 | @AnnotatedAnnotation2
61 | private static class SampleAnnotatedClass {
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/spring-automocker/src/main/java/com/github/fridujo/automocker/api/jdbc/Connections.java:
--------------------------------------------------------------------------------
1 | package com.github.fridujo.automocker.api.jdbc;
2 |
3 | import java.sql.Connection;
4 | import java.sql.PreparedStatement;
5 | import java.sql.ResultSet;
6 | import java.sql.SQLException;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | public class Connections {
11 |
12 | public static List tables(Connection c) {
13 | List tables = new ArrayList<>();
14 | try (PreparedStatement ps = c.prepareStatement("SHOW TABLES"); ResultSet rs = ps.executeQuery()) {
15 | while (rs.next()) {
16 | tables.add(rs.getString(1));
17 | }
18 | } catch (SQLException e) {
19 | throw new IllegalStateException("Unable to list tables", e);
20 | }
21 | return tables;
22 | }
23 |
24 | public static void execute(Connection c, String statement) {
25 | try (PreparedStatement p = c.prepareStatement(statement)) {
26 | p.execute();
27 | } catch (SQLException e) {
28 | throw new IllegalStateException("Could not execute Statement [" + statement + "]", e);
29 | }
30 | }
31 |
32 | public static void truncate(Connection c, String tableName) {
33 | execute(c, "TRUNCATE TABLE " + tableName);
34 | }
35 |
36 | public static Object selectFirstLines(Connection c, String tableName, int maxLines) {
37 | try (PreparedStatement p = c.prepareStatement("SELECT TOP " + maxLines + " * FROM " + tableName);
38 | ResultSet rs = p.executeQuery()) {
39 |
40 | int columns = rs.getMetaData()
41 | .getColumnCount();
42 | List> result = new ArrayList<>();
43 | while (rs.next()) {
44 | List