├── .gitignore ├── AOP └── pom.xml ├── BeanPostProcessor ├── pom.xml └── src │ ├── main │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── postprocessor │ │ ├── AbstractBeanPostProcessor.java │ │ ├── SomeOtherService.java │ │ ├── SomeOtherServiceImpl.java │ │ ├── SomeService.java │ │ ├── SomeServiceImpl.java │ │ ├── SomeServicePostProcessor.java │ │ └── SpringMain.java │ └── resources │ ├── applicationContext.xml │ └── log4j.xml ├── Camel and Spring ├── pom.xml └── src │ ├── main │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── camel │ │ ├── CamelRouter.java │ │ ├── enricher │ │ ├── AgeEnricher.java │ │ ├── ClubEnricher.java │ │ └── Enrichable.java │ │ ├── jaxb │ │ ├── Convertable.java │ │ ├── PlayerDetailsConverter.java │ │ └── generated │ │ │ ├── ObjectFactory.java │ │ │ ├── PlayerDetails.java │ │ │ └── PositionType.java │ │ ├── manual │ │ ├── ActiveMQRouter.java │ │ └── jms │ │ │ ├── FinalListenerImpl.java │ │ │ ├── ListenerImpl.java │ │ │ ├── PlayerDetailsSenderImpl.java │ │ │ └── Sender.java │ │ └── service │ │ ├── EnrichingService.java │ │ └── EnrichingServiceImpl.java │ └── resources │ ├── camel │ ├── Player.xsd │ ├── RobertLewandowski.xml │ ├── camelContext.xml │ ├── jms.properties │ └── jmsApplicationContext.xml │ └── log4j.xml ├── Drools ├── Decision table │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── pl │ │ │ │ └── grzejszczak │ │ │ │ └── marcin │ │ │ │ └── drools │ │ │ │ └── decisiontable │ │ │ │ ├── converter │ │ │ │ └── ProductTypeConverter.java │ │ │ │ └── service │ │ │ │ ├── FinancialService.java │ │ │ │ ├── FinancialServiceImpl.java │ │ │ │ ├── ProductService.java │ │ │ │ └── ProductServiceImpl.java │ │ └── resources │ │ │ ├── META-INF │ │ │ └── services │ │ │ │ └── org │ │ │ │ └── apache │ │ │ │ └── camel │ │ │ │ └── TypeConverter │ │ │ ├── applicationContext.xml │ │ │ ├── drools-context.xml │ │ │ ├── log4j.xml │ │ │ ├── rules │ │ │ ├── product_table.xls │ │ │ └── user_table.xls │ │ │ └── xsd │ │ │ ├── product.xsd │ │ │ └── user.xsd │ │ └── test │ │ └── java │ │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── drools │ │ └── decisiontable │ │ └── service │ │ └── drools │ │ └── ProductServiceImplTest.java └── Spring integration │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── pl │ │ │ └── grzejszczak │ │ │ └── marcin │ │ │ └── drools │ │ │ └── springintegration │ │ │ ├── DroolsSpring.java │ │ │ ├── enums │ │ │ └── TypeEnum.java │ │ │ ├── factory │ │ │ ├── ProcessingFactory.java │ │ │ ├── drools │ │ │ │ └── ProductProcessingFactory.java │ │ │ └── nondrools │ │ │ │ └── NoRulesProcessingFactory.java │ │ │ ├── model │ │ │ └── Product.java │ │ │ ├── service │ │ │ ├── Processor.java │ │ │ ├── ProductService.java │ │ │ ├── drools │ │ │ │ └── ProductServiceImpl.java │ │ │ └── nondrools │ │ │ │ ├── ElectronicProductProcessingService.java │ │ │ │ ├── MedicalProductProcessingService.java │ │ │ │ ├── NoRulesProductServiceImpl.java │ │ │ │ └── ProductProcessingService.java │ │ │ └── utils │ │ │ └── SomeUtil.java │ └── resources │ │ ├── applicationContext.xml │ │ ├── drools-context.xml │ │ ├── log4j.xml │ │ └── rules │ │ └── products.drl │ └── test │ └── java │ └── pl │ └── grzejszczak │ └── marcin │ └── drools │ └── springintegration │ └── service │ ├── NoRulesProductServiceImplTest.java │ └── ProductServiceImplTest.java ├── Groovy ├── Mappings │ ├── build.gradle │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── blogspot │ │ │ └── toomuchcoding │ │ │ ├── XmlTransformer.java │ │ │ ├── factory │ │ │ ├── ScriptFactory.java │ │ │ ├── ScriptFactoryImpl.java │ │ │ ├── TransformerFactory.java │ │ │ └── TransformerFactoryImpl.java │ │ │ └── transformer │ │ │ └── Transformer.java │ │ └── resources │ │ ├── groovy │ │ ├── AbstractGroovyXmlTransformer.groovy │ │ └── GroovyXmlTransformer.groovy │ │ ├── log4j.xml │ │ └── xml │ │ └── SampleXml.xml └── ast │ ├── build.gradle │ └── src │ ├── main │ ├── groovy │ │ └── com │ │ │ └── blogspot │ │ │ └── toomuchcoding │ │ │ ├── ast │ │ │ ├── WithAroundASTTransformation.groovy │ │ │ └── WithLogging.groovy │ │ │ ├── basescript │ │ │ └── NewBaseScript.groovy │ │ │ ├── canonical │ │ │ └── CanonicalPlayer.groovy │ │ │ ├── category │ │ │ ├── AnnotatedTensCategory.groovy │ │ │ └── TensCategory.groovy │ │ │ ├── compilestatic │ │ │ ├── CompileStaticAndTypeChecked.groovy │ │ │ └── DynamicPlayer.groovy │ │ │ ├── delegate │ │ │ └── DelegatePlayer.groovy │ │ │ ├── equalshashcode │ │ │ └── EqualsAndHashCodePlayer.groovy │ │ │ ├── externalized │ │ │ └── ExternalizedPlayer.groovy │ │ │ ├── immutable │ │ │ ├── ImmutablePlayer.groovy │ │ │ └── MutablePlayer.groovy │ │ │ ├── inheritconstructors │ │ │ ├── MyRuntimeException.groovy │ │ │ └── MySuperRuntimeException.groovy │ │ │ ├── loggers │ │ │ ├── PlayerServiceWithLog.groovy │ │ │ └── PlayerServiceWithManualLog.groovy │ │ │ ├── mixin │ │ │ ├── Athlete.groovy │ │ │ ├── DribblingAbility.groovy │ │ │ ├── FootballPlayer.groovy │ │ │ ├── RunningAbility.groovy │ │ │ ├── TennisAbility.groovy │ │ │ └── TennisPlayer.groovy │ │ │ └── tostring │ │ │ ├── BasePlayer.groovy │ │ │ └── ChildPlayer.groovy │ └── resources │ │ ├── ast │ │ └── ClassWithAst.groovy │ │ ├── baseScript │ │ └── SomeScript.groovy │ │ ├── field │ │ ├── FieldScript.groovy │ │ └── WrongFieldScript.groovy │ │ └── logback.xml │ └── test │ └── groovy │ └── com │ └── blogspot │ └── toomuchcoding │ ├── AutoExternalizeSpec.groovy │ ├── BaseScriptSpec.groovy │ ├── CanonicalSpec.groovy │ ├── CategorySpec.groovy │ ├── CompileStaticAndTypeCheckSpec.groovy │ ├── CustomAstSpec.groovy │ ├── DelegateSpec.groovy │ ├── EqualsAndHashCodeSpec.groovy │ ├── FieldSpec.groovy │ ├── ImmutableSpec.groovy │ ├── InheritConstructorsSpec.groovy │ ├── LogSpec.groovy │ ├── MixinSpec.groovy │ ├── TensSpec.groovy │ ├── ToStringSpec.groovy │ └── util │ └── SerializationUtil.groovy ├── Guava └── Cache │ ├── pom.xml │ └── src │ ├── main │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── guava │ │ └── cache │ │ ├── GuavaCache.java │ │ └── utils │ │ └── RegexpUtils.java │ └── resources │ └── log4j.xml ├── Mocking - Checking args ├── pom.xml └── src │ ├── integration │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── ServiceIntegrationTest.java │ ├── main │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── junit │ │ ├── Processable.java │ │ ├── SomeTask.java │ │ └── invocation │ │ └── SomePojo.java │ └── resources │ └── log4j.xml ├── Mocking - Time consuming actions ├── pom.xml └── src │ ├── integration │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── ServiceIntegrationTest.java │ ├── main │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── junit │ │ ├── Processable.java │ │ ├── SomeTask.java │ │ └── invocation │ │ └── SomePojo.java │ └── resources │ └── log4j.xml ├── README.md ├── Spring JMS ├── pom.xml └── src │ └── main │ ├── java │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── camel │ │ ├── enricher │ │ ├── AgeEnricher.java │ │ ├── ClubEnricher.java │ │ └── Enrichable.java │ │ ├── jaxb │ │ ├── Convertable.java │ │ ├── PlayerDetailsConverter.java │ │ └── generated │ │ │ ├── ObjectFactory.java │ │ │ ├── PlayerDetails.java │ │ │ └── PositionType.java │ │ └── manual │ │ ├── ActiveMQRouter.java │ │ └── jms │ │ ├── FinalListenerImpl.java │ │ ├── ListenerImpl.java │ │ ├── PlayerDetailsSenderImpl.java │ │ └── Sender.java │ └── resources │ ├── camel │ ├── Player.xsd │ ├── RobertLewandowski.xml │ ├── jms.properties │ └── jmsApplicationContext.xml │ └── log4j.xml ├── Spring └── Primary │ ├── build.gradle │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── blogspot │ │ │ └── toomuchcoding │ │ │ ├── configuration │ │ │ └── SpringConfiguration.java │ │ │ └── person │ │ │ ├── domain │ │ │ └── Person.java │ │ │ └── service │ │ │ ├── AgePersonProcessingService.java │ │ │ ├── IntelligencePersonProcessingService.java │ │ │ ├── NamePersonProcessingService.java │ │ │ ├── PersonProcessingService.java │ │ │ └── PersonProcessingServiceContainer.java │ └── resources │ │ └── log4j.xml │ └── test │ └── groovy │ └── com │ └── blogspot │ └── toomuchcoding │ └── person │ └── service │ └── PersonProcessingServiceContainerIntegrationSpec.groovy ├── Unit Testing ├── Mockito - Deep Stubs │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── blogspot │ │ │ │ └── toomuchcoding │ │ │ │ └── service │ │ │ │ ├── PlayerService.java │ │ │ │ └── PlayerServiceImpl.java │ │ └── resources │ │ │ └── xsd │ │ │ └── Player.xsd │ │ └── test │ │ └── java │ │ └── com │ │ └── blogspot │ │ └── toomuchcoding │ │ └── service │ │ └── PlayerServiceImplTest.java ├── Mockito - Injecting Test Doubles in Spring │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── blogspot │ │ │ │ └── toomuchcoding │ │ │ │ ├── model │ │ │ │ └── Player.java │ │ │ │ ├── processor │ │ │ │ └── AbstractBeanPostProcessor.java │ │ │ │ └── service │ │ │ │ ├── PlayerService.java │ │ │ │ ├── PlayerServiceImpl.java │ │ │ │ ├── PlayerWebService.java │ │ │ │ └── PlayerWebServiceImpl.java │ │ └── resources │ │ │ ├── applicationContext.xml │ │ │ └── log4j.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── blogspot │ │ │ ├── postprocessor │ │ │ └── PlayerWebServicePostProcessor.java │ │ │ └── toomuchcoding │ │ │ └── service │ │ │ └── PlayerServiceImplTest.java │ │ └── resources │ │ └── testApplicationContext.xml ├── Mockito - With Interfaces │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── blogspot │ │ │ │ └── toomuchcoding │ │ │ │ ├── adapter │ │ │ │ ├── CommonPlayerAdapter.java │ │ │ │ ├── DefencePlayerAdapter.java │ │ │ │ └── OffensivePlayerAdapter.java │ │ │ │ ├── factory │ │ │ │ ├── PlayerFactory.java │ │ │ │ ├── PlayerFactoryImpl.java │ │ │ │ └── PlayerFactoryImplWithFieldSettingLogic.java │ │ │ │ ├── model │ │ │ │ ├── DJ.java │ │ │ │ ├── DefensivePlayer.java │ │ │ │ ├── JavaDeveloper.java │ │ │ │ ├── OffensivePlayer.java │ │ │ │ └── Player.java │ │ │ │ └── service │ │ │ │ ├── PlayerService.java │ │ │ │ ├── PlayerServiceImpl.java │ │ │ │ └── PlayerServiceImplWithoutUnnecessaryLogic.java │ │ └── resources │ │ │ └── xsd │ │ │ └── Player.xsd │ │ └── test │ │ └── java │ │ └── com │ │ └── blogspot │ │ └── toomuchcoding │ │ └── service │ │ └── PlayerServiceImplTest.java └── Spock - Deep Stubs │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── blogspot │ │ │ └── toomuchcoding │ │ │ └── service │ │ │ ├── PlayerService.java │ │ │ └── PlayerServiceImpl.java │ └── resources │ │ └── xsd │ │ └── Player.xsd │ └── test │ └── groovy │ └── com │ └── blogspot │ └── toomuchcoding │ ├── service │ └── PlayerServiceImplWrittenUsingSpockTest.groovy │ └── spock │ └── ExtendedSpockSpecification.groovy └── Unit_Testing_-_Matchers ├── pom.xml └── src ├── main ├── java │ └── pl │ │ └── grzejszczak │ │ └── marcin │ │ └── junit │ │ └── matchers │ │ ├── MatcherMain.java │ │ ├── builder │ │ └── SomeBigPojoBuilder.java │ │ ├── pojo │ │ ├── SomeBigPojo.java │ │ └── SomePojoConstants.java │ │ └── service │ │ └── SomeService.java └── resources │ └── log4j.xml └── test └── java └── pl └── grzejszczak └── marcin └── junit └── matchers └── builder ├── SomeBigPojoBuilderNoMatchersAndNoRefactoringTest.java ├── SomeBigPojoBuilderNoMatchersTest.java └── SomeBigPojoBuilderTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | */target/* 3 | */.idea/* -------------------------------------------------------------------------------- /BeanPostProcessor/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | pl.grzejszczak.marcin 5 | postprocessor 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 3.1.0.RELEASE 14 | 15 | 16 | 17 | 18 | spring-release 19 | http://maven.springframework.org/release 20 | 21 | 22 | 23 | 24 | 25 | junit 26 | junit 27 | 4.10 28 | 29 | 30 | 31 | org.springframework 32 | spring-core 33 | ${spring.version} 34 | 35 | 36 | org.springframework 37 | spring-beans 38 | ${spring.version} 39 | 40 | 41 | org.springframework 42 | spring-context 43 | ${spring.version} 44 | 45 | 46 | commons-lang 47 | commons-lang 48 | 2.6 49 | 50 | 51 | org.slf4j 52 | slf4j-log4j12 53 | 1.5.2 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/main/pl/grzejszczak/marcin/postprocessor/AbstractBeanPostProcessor.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.postprocessor; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.config.BeanPostProcessor; 5 | 6 | public abstract class AbstractBeanPostProcessor implements BeanPostProcessor { 7 | 8 | private Class clazz; 9 | 10 | public AbstractBeanPostProcessor(Class clazz) { 11 | this.clazz = clazz; 12 | } 13 | 14 | @Override 15 | public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { 16 | checkConditions(); 17 | 18 | if (clazz.isAssignableFrom(bean.getClass())) { 19 | doAfter(); 20 | } 21 | return bean; 22 | } 23 | 24 | @Override 25 | public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { 26 | checkConditions(); 27 | 28 | if (clazz.isAssignableFrom(bean.getClass())) { 29 | doBefore(); 30 | } 31 | return bean; 32 | } 33 | 34 | private void checkConditions() { 35 | if (clazz == null) { 36 | throw new NullPointerException("Provide the interface for the post processor"); 37 | } 38 | } 39 | 40 | public abstract void doBefore(); 41 | 42 | public abstract void doAfter(); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/main/pl/grzejszczak/marcin/postprocessor/SomeOtherService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.postprocessor; 2 | 3 | public interface SomeOtherService { 4 | void methodC(); 5 | 6 | void methodD(); 7 | } 8 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/main/pl/grzejszczak/marcin/postprocessor/SomeOtherServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.postprocessor; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | public class SomeOtherServiceImpl implements SomeOtherService { 7 | private static final Logger LOGGER = LoggerFactory.getLogger(SomeOtherServiceImpl.class); 8 | 9 | public SomeOtherServiceImpl() { 10 | LOGGER.debug("SomeOtherServiceImpl - I'm created!"); 11 | } 12 | 13 | private void afterInit() { 14 | LOGGER.debug("SomeOtherServiceImpl - After init!"); 15 | } 16 | 17 | private void destroyMethod() { 18 | LOGGER.debug("SomeOtherServiceImpl - Destroy Method!"); 19 | } 20 | 21 | @Override 22 | public void methodC() { 23 | LOGGER.debug("SomeOtherServiceImpl - Method C executed"); 24 | } 25 | 26 | @Override 27 | public void methodD() { 28 | LOGGER.debug("SomeOtherServiceImpl - Method D executed"); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/main/pl/grzejszczak/marcin/postprocessor/SomeService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.postprocessor; 2 | 3 | public interface SomeService { 4 | void methodA(); 5 | 6 | void methodB(); 7 | } 8 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/main/pl/grzejszczak/marcin/postprocessor/SomeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.postprocessor; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | public class SomeServiceImpl implements SomeService { 7 | private static final Logger LOGGER = LoggerFactory.getLogger(SomeServiceImpl.class); 8 | 9 | public SomeServiceImpl() { 10 | LOGGER.debug("SomeServiceImpl - I'm created!"); 11 | } 12 | 13 | private void afterInit() { 14 | LOGGER.debug("SomeServiceImpl - After init!"); 15 | } 16 | 17 | private void destroyMethod() { 18 | LOGGER.debug("SomeServiceImpl - Destroy Method!"); 19 | } 20 | 21 | @Override 22 | public void methodA() { 23 | LOGGER.debug("SomeServiceImpl - Method A executed"); 24 | } 25 | 26 | @Override 27 | public void methodB() { 28 | LOGGER.debug("SomeServiceImpl - Method B executed"); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/main/pl/grzejszczak/marcin/postprocessor/SomeServicePostProcessor.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.postprocessor; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class SomeServicePostProcessor extends AbstractBeanPostProcessor { 9 | 10 | private static final Logger LOGGER = LoggerFactory.getLogger(SomeServicePostProcessor.class); 11 | 12 | public SomeServicePostProcessor() { 13 | super(SomeService.class); 14 | } 15 | 16 | @Override 17 | public void doBefore() { 18 | LOGGER.info("BEFORE it's init method has been executed but AFTER SomeServiceImpl has been instantiated I would like to do sth..."); 19 | } 20 | 21 | @Override 22 | public void doAfter() { 23 | LOGGER.info("AFTER SomeServiceImpl has executed its init method I would like to do sth more..."); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/main/pl/grzejszczak/marcin/postprocessor/SpringMain.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.postprocessor; 2 | 3 | import org.springframework.context.ConfigurableApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class SpringMain { 7 | 8 | public static void main(String[] args) { 9 | ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 10 | SomeService someService = context.getBean(SomeService.class); 11 | someService.methodA(); 12 | someService.methodB(); 13 | SomeOtherService someOtherService = context.getBean(SomeOtherService.class); 14 | someOtherService.methodC(); 15 | someOtherService.methodD(); 16 | context.close(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /BeanPostProcessor/src/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/CamelRouter.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel; 2 | 3 | import org.apache.camel.spring.Main; 4 | 5 | public class CamelRouter { 6 | 7 | /** 8 | * @param args 9 | * @throws Exception 10 | */ 11 | public static void main(String[] args) throws Exception { 12 | Main main = new Main(); 13 | main.setApplicationContextUri("/camel/camelContext.xml"); 14 | main.run(args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/enricher/AgeEnricher.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.enricher; 2 | 3 | import java.util.Random; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.stereotype.Component; 8 | 9 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 10 | 11 | @Component("AgeEnricher") 12 | public class AgeEnricher implements Enrichable { 13 | private static final Logger LOGGER = LoggerFactory.getLogger(AgeEnricher.class); 14 | 15 | @Override 16 | public void enrich(PlayerDetails inputObject) { 17 | LOGGER.debug("Enriching player [{}] with age data", new Object[] { inputObject.getSurname() }); 18 | // Simulating accessing DB or some other service 19 | try { 20 | Thread.sleep(2000); 21 | } catch (InterruptedException e) { 22 | LOGGER.error("Exception while sleeping occured", e); 23 | } 24 | inputObject.setAge(new Random().nextInt(35)); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/enricher/ClubEnricher.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.enricher; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | 7 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 8 | 9 | @Component("ClubEnricher") 10 | public class ClubEnricher implements Enrichable { 11 | 12 | private static final Logger LOGGER = LoggerFactory.getLogger(ClubEnricher.class); 13 | 14 | @Override 15 | public void enrich(PlayerDetails inputObject) { 16 | LOGGER.debug("Enriching player [{}] with club data", new Object[] { inputObject.getSurname() }); 17 | // Simulating accessing DB or some other service 18 | try { 19 | Thread.sleep(2000); 20 | } catch (InterruptedException e) { 21 | LOGGER.error("Exception while sleeping occured", e); 22 | } 23 | inputObject.setTeamName("Borussia Dortmund"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/enricher/Enrichable.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.enricher; 2 | 3 | public interface Enrichable { 4 | void enrich(T inputObject); 5 | } 6 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/jaxb/Convertable.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.jaxb; 2 | 3 | public interface Convertable { 4 | String marshal(T object); 5 | 6 | T unmarshal(String objectAsString); 7 | } 8 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/jaxb/PlayerDetailsConverter.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.jaxb; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.OutputStream; 5 | 6 | import javax.xml.bind.JAXBContext; 7 | import javax.xml.bind.JAXBException; 8 | import javax.xml.bind.Marshaller; 9 | import javax.xml.bind.Unmarshaller; 10 | 11 | import org.apache.activemq.util.ByteArrayInputStream; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.stereotype.Component; 15 | 16 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 17 | 18 | @Component("PlayerDetailsConverter") 19 | public class PlayerDetailsConverter implements Convertable { 20 | private static final Logger LOGGER = LoggerFactory.getLogger(PlayerDetailsConverter.class); 21 | 22 | private final JAXBContext jaxbContext; 23 | private final Marshaller jaxbMarshaller; 24 | private final Unmarshaller jaxbUnmarshaller; 25 | 26 | public PlayerDetailsConverter() throws JAXBException { 27 | jaxbContext = JAXBContext.newInstance(PlayerDetails.class); 28 | jaxbMarshaller = jaxbContext.createMarshaller(); 29 | jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 30 | jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 31 | } 32 | 33 | @Override 34 | public String marshal(PlayerDetails object) { 35 | OutputStream stream = new ByteArrayOutputStream(); 36 | try { 37 | jaxbMarshaller.marshal(object, stream); 38 | } catch (JAXBException e) { 39 | LOGGER.error("Exception occured while marshalling", e); 40 | } 41 | return stream.toString(); 42 | } 43 | 44 | @Override 45 | public PlayerDetails unmarshal(String objectAsString) { 46 | try { 47 | return (PlayerDetails) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(objectAsString.getBytes())); 48 | } catch (JAXBException e) { 49 | LOGGER.error("Exception occured while marshalling", e); 50 | } 51 | return null; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/jaxb/generated/ObjectFactory.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2012.11.05 at 09:23:22 PM CET 6 | // 7 | 8 | 9 | package pl.grzejszczak.marcin.camel.jaxb.generated; 10 | 11 | import javax.xml.bind.annotation.XmlRegistry; 12 | 13 | 14 | /** 15 | * This object contains factory methods for each 16 | * Java content interface and Java element interface 17 | * generated in the pl.grzejszczak.marcin.camel.jaxb.generated package. 18 | *

An ObjectFactory allows you to programatically 19 | * construct new instances of the Java representation 20 | * for XML content. The Java representation of XML 21 | * content can consist of schema derived interfaces 22 | * and classes representing the binding of schema 23 | * type definitions, element declarations and model 24 | * groups. Factory methods for each of these are 25 | * provided in this class. 26 | * 27 | */ 28 | @XmlRegistry 29 | public class ObjectFactory { 30 | 31 | 32 | /** 33 | * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: pl.grzejszczak.marcin.camel.jaxb.generated 34 | * 35 | */ 36 | public ObjectFactory() { 37 | } 38 | 39 | /** 40 | * Create an instance of {@link PlayerDetails } 41 | * 42 | */ 43 | public PlayerDetails createPlayerDetails() { 44 | return new PlayerDetails(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/jaxb/generated/PositionType.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2012.11.05 at 09:23:22 PM CET 6 | // 7 | 8 | 9 | package pl.grzejszczak.marcin.camel.jaxb.generated; 10 | 11 | import javax.xml.bind.annotation.XmlEnum; 12 | import javax.xml.bind.annotation.XmlType; 13 | 14 | 15 | /** 16 | *

Java class for PositionType. 17 | * 18 | *

The following schema fragment specifies the expected content contained within this class. 19 | *

20 | *

21 |  * <simpleType name="PositionType">
22 |  *   <restriction base="{http://www.w3.org/2001/XMLSchema}string">
23 |  *     <enumeration value="GK"/>
24 |  *     <enumeration value="DEF"/>
25 |  *     <enumeration value="MID"/>
26 |  *     <enumeration value="ATT"/>
27 |  *   </restriction>
28 |  * </simpleType>
29 |  * 
30 | * 31 | */ 32 | @XmlType(name = "PositionType") 33 | @XmlEnum 34 | public enum PositionType { 35 | 36 | GK, 37 | DEF, 38 | MID, 39 | ATT; 40 | 41 | public String value() { 42 | return name(); 43 | } 44 | 45 | public static PositionType fromValue(String v) { 46 | return valueOf(v); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/manual/ActiveMQRouter.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual; 2 | 3 | import java.io.File; 4 | import java.util.Scanner; 5 | 6 | import javax.jms.JMSException; 7 | 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.support.ClassPathXmlApplicationContext; 10 | import org.springframework.core.io.ClassPathResource; 11 | import org.springframework.core.io.Resource; 12 | 13 | import pl.grzejszczak.marcin.camel.jaxb.PlayerDetailsConverter; 14 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 15 | import pl.grzejszczak.marcin.camel.manual.jms.FinalListenerImpl; 16 | import pl.grzejszczak.marcin.camel.manual.jms.Sender; 17 | 18 | public class ActiveMQRouter { 19 | 20 | /** 21 | * @param args 22 | * @throws JMSException 23 | */ 24 | public static void main(String[] args) throws Exception { 25 | ApplicationContext context = new ClassPathXmlApplicationContext("/camel/jmsApplicationContext.xml"); 26 | @SuppressWarnings("unchecked") 27 | Sender sender = (Sender) context.getBean("originPlayerSender"); 28 | 29 | Resource resource = new ClassPathResource("/camel/RobertLewandowski.xml"); 30 | 31 | Scanner scanner = new Scanner(new File(resource.getURI())).useDelimiter("\\Z"); 32 | String contents = scanner.next(); 33 | 34 | PlayerDetailsConverter converter = context.getBean(PlayerDetailsConverter.class); 35 | 36 | FinalListenerImpl listener = (FinalListenerImpl) context.getBean("finalListenerImpl"); 37 | 38 | sender.sendMessage(converter.unmarshal(contents)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/manual/jms/FinalListenerImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | import javax.jms.BytesMessage; 4 | import javax.jms.Message; 5 | import javax.jms.MessageListener; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.jms.support.converter.MessageConverter; 11 | import org.springframework.stereotype.Component; 12 | 13 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 14 | 15 | @Component("finalListenerImpl") 16 | public class FinalListenerImpl implements MessageListener { 17 | 18 | private static final Logger LOG = LoggerFactory.getLogger(FinalListenerImpl.class); 19 | 20 | @Autowired 21 | private MessageConverter messageConverter; 22 | 23 | @Override 24 | public void onMessage(Message message) { 25 | if (!(message instanceof BytesMessage)) { 26 | LOG.error("Wrong msg!"); 27 | return; 28 | } 29 | 30 | PlayerDetails playerDetails = null; 31 | try { 32 | playerDetails = (PlayerDetails) messageConverter.fromMessage(message); 33 | 34 | if (playerDetails.getTeamName() != null) { 35 | LOG.debug("Message already enriched! Shutting down the system"); 36 | System.exit(0); 37 | } else { 38 | LOG.debug("The message should have been enriched but wasn't"); 39 | System.exit(1); 40 | } 41 | 42 | } catch (Exception e) { 43 | LOG.error("Exception occured", e); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/manual/jms/ListenerImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | import javax.jms.BytesMessage; 4 | import javax.jms.Message; 5 | import javax.jms.MessageListener; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.beans.factory.annotation.Qualifier; 11 | import org.springframework.jms.support.converter.MessageConverter; 12 | 13 | import pl.grzejszczak.marcin.camel.enricher.Enrichable; 14 | import pl.grzejszczak.marcin.camel.jaxb.Convertable; 15 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 16 | 17 | public class ListenerImpl implements MessageListener { 18 | 19 | private static final Logger LOG = LoggerFactory.getLogger(ListenerImpl.class); 20 | 21 | public ListenerImpl() { 22 | LOG.info("Listener is created"); 23 | } 24 | 25 | @Autowired 26 | private Convertable playerDetailsConverter; 27 | 28 | @Autowired 29 | @Qualifier("enrichingService") 30 | private Enrichable enrichingService; 31 | 32 | @Autowired 33 | private MessageConverter messageConverter; 34 | 35 | @Autowired 36 | @Qualifier("destinationPlayerSender") 37 | private Sender sender; 38 | 39 | @Override 40 | public void onMessage(Message message) { 41 | if (!(message instanceof BytesMessage)) { 42 | LOG.error("Wrong msg!"); 43 | return; 44 | } 45 | 46 | PlayerDetails playerDetails = null; 47 | try { 48 | playerDetails = (PlayerDetails) messageConverter.fromMessage(message); 49 | 50 | LOG.debug("Enriching the input message"); 51 | enrichingService.enrich(playerDetails); 52 | 53 | LOG.debug("Enriched text message: [{}]", new Object[] { playerDetailsConverter.marshal(playerDetails) }); 54 | sender.sendMessage(playerDetails); 55 | } catch (Exception e) { 56 | LOG.error("Exception occured", e); 57 | } 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/manual/jms/PlayerDetailsSenderImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | import javax.jms.Destination; 4 | import javax.jms.JMSException; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.jms.core.JmsTemplate; 10 | import org.springframework.stereotype.Component; 11 | 12 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 13 | 14 | @Component 15 | public class PlayerDetailsSenderImpl implements Sender { 16 | 17 | private static final Logger LOGGER = LoggerFactory.getLogger(PlayerDetailsSenderImpl.class); 18 | 19 | private Destination destination; 20 | 21 | @Autowired 22 | private JmsTemplate jmsTemplate; 23 | 24 | @Override 25 | public void sendMessage(final PlayerDetails object) throws JMSException { 26 | LOGGER.debug("Sending [{}] to topic [{}]", new Object[] { object, destination }); 27 | jmsTemplate.convertAndSend(destination, object); 28 | } 29 | 30 | public Destination getDestination() { 31 | return destination; 32 | } 33 | 34 | public void setDestination(Destination destination) { 35 | this.destination = destination; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/manual/jms/Sender.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | public interface Sender { 4 | void sendMessage(T object) throws Exception; 5 | } 6 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/service/EnrichingService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.service; 2 | 3 | public interface EnrichingService { 4 | void enrich(); 5 | } 6 | -------------------------------------------------------------------------------- /Camel and Spring/src/main/pl/grzejszczak/marcin/camel/service/EnrichingServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.service; 2 | 3 | import java.util.List; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | 9 | import pl.grzejszczak.marcin.camel.enricher.Enrichable; 10 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 11 | 12 | public class EnrichingServiceImpl implements Enrichable { 13 | 14 | private static final Logger LOGGER = LoggerFactory.getLogger(EnrichingServiceImpl.class); 15 | 16 | @Autowired 17 | private List> listOfEnrichers; 18 | 19 | @Override 20 | public void enrich(PlayerDetails playerDetails) { 21 | LOGGER.debug("Enriching player details"); 22 | 23 | for (Enrichable enrichable : listOfEnrichers) { 24 | enrichable.enrich(playerDetails); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Camel and Spring/src/resources/camel/Player.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Camel and Spring/src/resources/camel/RobertLewandowski.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Robert 4 | Lewandowski 5 | ATT 6 | -------------------------------------------------------------------------------- /Camel and Spring/src/resources/camel/camelContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Camel and Spring/src/resources/camel/jms.properties: -------------------------------------------------------------------------------- 1 | jms.origin=Initial.Topic 2 | jms.destination=Routed.Topic -------------------------------------------------------------------------------- /Camel and Spring/src/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/java/pl/grzejszczak/marcin/drools/decisiontable/converter/ProductTypeConverter.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.decisiontable.converter; 2 | 3 | import org.apache.camel.Converter; 4 | import org.drools.command.Command; 5 | import org.drools.command.CommandFactory; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import pl.grzejszczak.marcin.drools.decisiontable.model.Product; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created with IntelliJ IDEA. 14 | * User: mgrzejszczak 15 | * Date: 30.01.13 16 | * Time: 21:42 17 | */ 18 | @Converter 19 | public class ProductTypeConverter { 20 | 21 | private static final Logger LOGGER = LoggerFactory.getLogger(ProductTypeConverter.class); 22 | 23 | @Converter 24 | public static Command toCommandFromList(List inputList) { 25 | LOGGER.debug("Executing ProductTypeConverter's toCommandFromList method"); 26 | return CommandFactory.newInsertElements(inputList); 27 | } 28 | 29 | @Converter 30 | public static Command toCommand(Product product) { 31 | LOGGER.debug("Executing ProductTypeConverter's toCommand method"); 32 | return CommandFactory.newInsert(product); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/java/pl/grzejszczak/marcin/drools/decisiontable/service/FinancialService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.decisiontable.service; 2 | 3 | import pl.grzejszczak.marcin.drools.decisiontable.model.Product; 4 | import pl.grzejszczak.marcin.drools.decisiontable.model.User; 5 | 6 | /** 7 | * Created with IntelliJ IDEA. 8 | * User: mgrzejszczak 9 | * Date: 03.02.13 10 | * Time: 18:45 11 | */ 12 | public interface FinancialService { 13 | boolean processOrder(User user, Product product); 14 | } 15 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/java/pl/grzejszczak/marcin/drools/decisiontable/service/FinancialServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.decisiontable.service; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | import pl.grzejszczak.marcin.drools.decisiontable.model.Product; 7 | import pl.grzejszczak.marcin.drools.decisiontable.model.User; 8 | 9 | import static pl.grzejszczak.marcin.drools.decisiontable.model.DecisionType.ACCEPTED; 10 | 11 | /** 12 | * Created with IntelliJ IDEA. 13 | * User: mgrzejszczak 14 | * Date: 03.02.13 15 | * Time: 18:45 16 | */ 17 | @Component("financialServiceImpl") 18 | public class FinancialServiceImpl implements FinancialService { 19 | 20 | private static final Logger LOGGER = LoggerFactory.getLogger(FinancialServiceImpl.class); 21 | 22 | @Override 23 | public boolean processOrder(User user, Product product) { 24 | if(ACCEPTED.equals(user.getDecision())){ 25 | LOGGER.debug("User has been approved - processing the order..."); 26 | return true; 27 | } 28 | 29 | LOGGER.debug("Sorry, user has been rejected..."); 30 | return false; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/java/pl/grzejszczak/marcin/drools/decisiontable/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.decisiontable.service; 2 | 3 | import pl.grzejszczak.marcin.drools.decisiontable.model.User; 4 | import pl.grzejszczak.marcin.drools.decisiontable.model.Product; 5 | 6 | /** 7 | * Created with IntelliJ IDEA. 8 | * User: mgrzejszczak 9 | * Date: 14.01.13 10 | */ 11 | public interface ProductService { 12 | void runProductLogic(User user, Product product); 13 | } 14 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/java/pl/grzejszczak/marcin/drools/decisiontable/service/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.decisiontable.service; 2 | 3 | import org.apache.camel.CamelContext; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Component; 8 | import pl.grzejszczak.marcin.drools.decisiontable.model.Product; 9 | import pl.grzejszczak.marcin.drools.decisiontable.model.User; 10 | 11 | import static com.google.common.collect.Lists.newArrayList; 12 | 13 | /** 14 | * Created with IntelliJ IDEA. 15 | * User: mgrzejszczak 16 | * Date: 14.01.13 17 | */ 18 | @Component("productServiceImpl") 19 | public class ProductServiceImpl implements ProductService { 20 | 21 | private static final Logger LOGGER = LoggerFactory.getLogger(ProductServiceImpl.class); 22 | 23 | @Autowired 24 | CamelContext camelContext; 25 | 26 | @Autowired 27 | FinancialService financialService; 28 | 29 | @Override 30 | public void runProductLogic(User user, Product product) { 31 | LOGGER.debug("Running product logic - first acceptance Route, then discount Route"); 32 | camelContext.createProducerTemplate().sendBody("direct:acceptanceRoute", newArrayList(user, product)); 33 | camelContext.createProducerTemplate().sendBody("direct:discountRoute", newArrayList(user, product)); 34 | financialService.processOrder(user, product); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/META-INF/services/org/apache/camel/TypeConverter: -------------------------------------------------------------------------------- 1 | pl.grzejszczak.marcin.drools.decisiontable.converter.ProductTypeConverter -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/drools-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/rules/product_table.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcingrzejszczak/too-much-coding/b8dc703e104b5ab57dd06ebfae20d42dfad448bc/Drools/Decision table/src/main/resources/rules/product_table.xls -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/rules/user_table.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcingrzejszczak/too-much-coding/b8dc703e104b5ab57dd06ebfae20d42dfad448bc/Drools/Decision table/src/main/resources/rules/user_table.xls -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/xsd/product.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Drools/Decision table/src/main/resources/xsd/user.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Drools/Spring integration/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | pl.grzejszczak.marcin 5 | drools-spring-integration 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 3.1.1.RELEASE 14 | 15 | 16 | 17 | spring-release 18 | http://maven.springframework.org/release 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.springframework 26 | spring-core 27 | ${spring.version} 28 | 29 | 30 | org.springframework 31 | spring-context 32 | ${spring.version} 33 | 34 | 35 | com.google.guava 36 | guava 37 | 13.0.1 38 | 39 | 40 | org.springframework 41 | spring-test 42 | ${spring.version} 43 | 44 | 45 | org.drools 46 | drools-spring 47 | 5.4.0.Final 48 | 49 | 50 | org.slf4j 51 | slf4j-log4j12 52 | 1.6.6 53 | 54 | 55 | junit 56 | junit 57 | 4.10 58 | test 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/DroolsSpring.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | import pl.grzejszczak.marcin.drools.springintegration.service.ProductService; 6 | 7 | /** 8 | * Created with IntelliJ IDEA. 9 | * User: mgrzejszczak 10 | * Date: 14.01.13 11 | */ 12 | public class DroolsSpring { 13 | 14 | public static void main(String args[]){ 15 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 16 | ((ProductService)applicationContext.getBean("ProductServiceImpl")).runProductLogic(); 17 | ((ProductService)applicationContext.getBean("NoRulesProductServiceImpl")).runProductLogic(); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/enums/TypeEnum.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.enums; 2 | 3 | /** 4 | * Created with IntelliJ IDEA. 5 | * User: mgrzejszczak 6 | * Date: 14.01.13 7 | */ 8 | public enum TypeEnum { 9 | MEDICAL("medical", "aaabbbccc"), ELECTRONIC("electronic", "cccbbbaaa"); 10 | 11 | 12 | private final String type; 13 | private final String someOutputString; 14 | 15 | private TypeEnum(String type, String someOutputString) { 16 | this.type = type; 17 | this.someOutputString = someOutputString; 18 | } 19 | 20 | public String getType() { 21 | return type; 22 | } 23 | 24 | public String getSomeOutputString() { 25 | return someOutputString; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/factory/ProcessingFactory.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.factory; 2 | 3 | /** 4 | * Created with IntelliJ IDEA. 5 | * User: mgrzejszczak 6 | * Date: 14.01.13 7 | */ 8 | public interface ProcessingFactory { 9 | T createProcessingObject(V inputObject); 10 | } 11 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/factory/drools/ProductProcessingFactory.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.factory.drools; 2 | 3 | import org.drools.runtime.StatelessKnowledgeSession; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.stereotype.Component; 7 | import pl.grzejszczak.marcin.drools.springintegration.factory.ProcessingFactory; 8 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 9 | 10 | /** 11 | * Created with IntelliJ IDEA. 12 | * User: mgrzejszczak 13 | * Date: 14.01.13 14 | */ 15 | @Component("ProductProcessingFactory") 16 | public class ProductProcessingFactory implements ProcessingFactory { 17 | 18 | @Autowired 19 | private ApplicationContext applicationContext; 20 | 21 | @Override 22 | public StatelessKnowledgeSession createProcessingObject(Product inputObject) { 23 | return (StatelessKnowledgeSession)applicationContext.getBean(inputObject.getTypeEnum().getType() + "KSession"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/factory/nondrools/NoRulesProcessingFactory.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.factory.nondrools; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import org.springframework.stereotype.Component; 5 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum; 6 | import pl.grzejszczak.marcin.drools.springintegration.factory.ProcessingFactory; 7 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 8 | import pl.grzejszczak.marcin.drools.springintegration.service.nondrools.ElectronicProductProcessingService; 9 | import pl.grzejszczak.marcin.drools.springintegration.service.nondrools.MedicalProductProcessingService; 10 | import pl.grzejszczak.marcin.drools.springintegration.service.Processor; 11 | 12 | import java.util.Map; 13 | 14 | /** 15 | * Created with IntelliJ IDEA. 16 | * User: mgrzejszczak 17 | * Date: 14.01.13 18 | */ 19 | @Component("NoRulesProcessingFactory") 20 | public class NoRulesProcessingFactory implements ProcessingFactory { 21 | 22 | private static final Map PROCESSOR_MAP = new ImmutableMap.Builder(). 23 | put(TypeEnum.MEDICAL, new MedicalProductProcessingService()). 24 | put(TypeEnum.ELECTRONIC, new ElectronicProductProcessingService()). 25 | build(); 26 | 27 | /** 28 | * By using the map we don't have any ifs 29 | * @param inputObject 30 | * @return 31 | */ 32 | @Override 33 | public Processor createProcessingObject(Product inputObject) { 34 | return PROCESSOR_MAP.get(inputObject.getTypeEnum()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/model/Product.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.model; 2 | 3 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum; 4 | 5 | /** 6 | * Created with IntelliJ IDEA. 7 | * User: mgrzejszczak 8 | * Date: 14.01.13 9 | */ 10 | public class Product { 11 | private final TypeEnum typeEnum; 12 | private String productName; 13 | 14 | public Product(TypeEnum typeEnum) { 15 | this.typeEnum = typeEnum; 16 | productName = typeEnum.getSomeOutputString(); 17 | } 18 | 19 | public TypeEnum getTypeEnum() { 20 | return typeEnum; 21 | } 22 | 23 | public String getProductName() { 24 | return productName; 25 | } 26 | 27 | public void setProductName(String productName) { 28 | this.productName = productName; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/service/Processor.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service; 2 | 3 | /** 4 | * Created with IntelliJ IDEA. 5 | * User: mgrzejszczak 6 | * Date: 14.01.13 7 | */ 8 | public interface Processor { 9 | void process(T input); 10 | } 11 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service; 2 | 3 | /** 4 | * Created with IntelliJ IDEA. 5 | * User: mgrzejszczak 6 | * Date: 14.01.13 7 | */ 8 | public interface ProductService { 9 | void runProductLogic(); 10 | } 11 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/service/drools/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service.drools; 2 | 3 | import org.drools.runtime.StatelessKnowledgeSession; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.stereotype.Component; 9 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum; 10 | import pl.grzejszczak.marcin.drools.springintegration.factory.ProcessingFactory; 11 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 12 | import pl.grzejszczak.marcin.drools.springintegration.service.ProductService; 13 | 14 | import static com.google.common.collect.Lists.newArrayList; 15 | 16 | /** 17 | * Created with IntelliJ IDEA. 18 | * User: mgrzejszczak 19 | * Date: 14.01.13 20 | */ 21 | @Component("ProductServiceImpl") 22 | public class ProductServiceImpl implements ProductService { 23 | 24 | private static final Logger LOGGER = LoggerFactory.getLogger(ProductServiceImpl.class); 25 | 26 | @Autowired 27 | @Qualifier("ProductProcessingFactory") 28 | ProcessingFactory processingFactory; 29 | 30 | @Override 31 | public void runProductLogic() { 32 | LOGGER.debug("Running product logic"); 33 | Product medicalProduct = new Product(TypeEnum.MEDICAL); 34 | Product electronicProduct = new Product(TypeEnum.ELECTRONIC); 35 | StatelessKnowledgeSession statelessKnowledgeSession = processingFactory.createProcessingObject(medicalProduct); 36 | LOGGER.debug("Running rules for products..."); 37 | statelessKnowledgeSession.execute(newArrayList(medicalProduct, electronicProduct)); 38 | LOGGER.debug("...finished running products."); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/service/nondrools/ElectronicProductProcessingService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service.nondrools; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum; 6 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 7 | import pl.grzejszczak.marcin.drools.springintegration.service.Processor; 8 | import pl.grzejszczak.marcin.drools.springintegration.utils.SomeUtil; 9 | 10 | import static com.google.common.base.Preconditions.checkArgument; 11 | import static java.lang.String.format; 12 | 13 | /** 14 | * Created with IntelliJ IDEA. 15 | * User: mgrzejszczak 16 | * Date: 14.01.13 17 | */ 18 | public class ElectronicProductProcessingService implements Processor { 19 | 20 | private static final Logger LOGGER = LoggerFactory.getLogger(ElectronicProductProcessingService.class); 21 | 22 | @Override 23 | public void process(Product input) { 24 | checkArgument(TypeEnum.ELECTRONIC.equals(input.getTypeEnum()), "This processing service works only for electronic devices"); 25 | checkArgument(!SomeUtil.replaceAWithB(input.getProductName()).equals(input.getProductName()), "The input has already been processed"); 26 | LOGGER.debug("Running processing for Electronic Product"); 27 | input.setProductName(SomeUtil.replaceAWithB(input.getProductName())); 28 | LOGGER.debug(format("ELECTRONIC rule applied without Drools, product name is now equal to [%s]", input.getProductName())); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/service/nondrools/MedicalProductProcessingService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service.nondrools; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum; 6 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 7 | import pl.grzejszczak.marcin.drools.springintegration.service.Processor; 8 | import pl.grzejszczak.marcin.drools.springintegration.utils.SomeUtil; 9 | 10 | import static com.google.common.base.Preconditions.checkArgument; 11 | import static java.lang.String.format; 12 | 13 | /** 14 | * Created with IntelliJ IDEA. 15 | * User: mgrzejszczak 16 | * Date: 14.01.13 17 | */ 18 | public class MedicalProductProcessingService implements Processor { 19 | 20 | private static final Logger LOGGER = LoggerFactory.getLogger(MedicalProductProcessingService.class); 21 | 22 | @Override 23 | public void process(Product input) { 24 | checkArgument(TypeEnum.MEDICAL.equals(input.getTypeEnum()), "This processing service works only for medical devices"); 25 | checkArgument(!SomeUtil.replaceAWithB(input.getProductName()).equals(input.getProductName()), "The input has already been processed"); 26 | LOGGER.debug("Running processing for Medical Product"); 27 | input.setProductName(SomeUtil.replaceAWithB(input.getProductName())); 28 | LOGGER.debug(format("MEDICAL rule applied without Drools, product name is now equal to [%s]", input.getProductName())); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/service/nondrools/NoRulesProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service.nondrools; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Qualifier; 7 | import org.springframework.stereotype.Component; 8 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum; 9 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 10 | import pl.grzejszczak.marcin.drools.springintegration.service.Processor; 11 | import pl.grzejszczak.marcin.drools.springintegration.service.ProductService; 12 | 13 | import java.util.List; 14 | 15 | import static com.google.common.collect.Lists.newArrayList; 16 | 17 | /** 18 | * Created with IntelliJ IDEA. 19 | * User: mgrzejszczak 20 | * Date: 14.01.13 21 | */ 22 | @Component("NoRulesProductServiceImpl") 23 | public class NoRulesProductServiceImpl implements ProductService { 24 | 25 | private static final Logger LOGGER = LoggerFactory.getLogger(NoRulesProductServiceImpl.class); 26 | 27 | @Autowired 28 | @Qualifier("ProductProcessingService") 29 | private Processor> productProcessingService; 30 | 31 | @Override 32 | public void runProductLogic() { 33 | LOGGER.debug("Running product logic without Drools"); 34 | Product medicalProduct = new Product(TypeEnum.MEDICAL); 35 | Product electronicProduct = new Product(TypeEnum.ELECTRONIC); 36 | LOGGER.debug("Running rules for products..."); 37 | productProcessingService.process(newArrayList(medicalProduct, electronicProduct)); 38 | LOGGER.debug("...finished running products."); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/service/nondrools/ProductProcessingService.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service.nondrools; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.stereotype.Component; 6 | import pl.grzejszczak.marcin.drools.springintegration.factory.ProcessingFactory; 7 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 8 | import pl.grzejszczak.marcin.drools.springintegration.service.Processor; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created with IntelliJ IDEA. 14 | * User: mgrzejszczak 15 | * Date: 14.01.13 16 | */ 17 | @Component("ProductProcessingService") 18 | public class ProductProcessingService implements Processor> { 19 | 20 | @Autowired 21 | @Qualifier("NoRulesProcessingFactory") 22 | private ProcessingFactory processingFactory; 23 | 24 | @Override 25 | public void process(List input) { 26 | for(Product product : input){ 27 | Processor processor = processingFactory.createProcessingObject(product); 28 | processor.process(product); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/java/pl/grzejszczak/marcin/drools/springintegration/utils/SomeUtil.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.utils; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | /** 7 | * Created with IntelliJ IDEA. 8 | * User: mgrzejszczak 9 | * Date: 14.01.13 10 | */ 11 | public final class SomeUtil { 12 | private static final Logger LOGGER = LoggerFactory.getLogger(SomeUtil.class); 13 | 14 | private SomeUtil(){ 15 | throw new UnsupportedOperationException("SomeUtil is a utility class that should not be instantiated"); 16 | } 17 | 18 | public static String replaceAWithB(String input){ 19 | LOGGER.debug("Executing some logic"); 20 | return input.toLowerCase().replaceAll("a", "b"); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/resources/drools-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Drools/Spring integration/src/main/resources/rules/products.drl: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin 2 | 3 | import org.slf4j.LoggerFactory 4 | import pl.grzejszczak.marcin.drools.springintegration.DroolsSpring 5 | import pl.grzejszczak.marcin.drools.springintegration.model.Product 6 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum 7 | 8 | import function pl.grzejszczak.marcin.drools.springintegration.utils.SomeUtil.replaceAWithB 9 | import function org.drools.core.util.StringUtils.isEmpty 10 | 11 | 12 | rule "MEDICAL rule" 13 | dialect "mvel" 14 | when 15 | $product : Product( typeEnum == TypeEnum.MEDICAL, productName != replaceAWithB($product.typeEnum.someOutputString) ) 16 | then 17 | modify ($product) {productName = replaceAWithB($product.typeEnum.someOutputString)} 18 | LoggerFactory.getLogger(DroolsSpring.class).debug(String.format("MEDICAL rule applied, product name is now equal to [%s]", $product.productName)) 19 | end 20 | 21 | rule "ELECTRONIC rule" 22 | dialect "mvel" 23 | when 24 | $product : Product( typeEnum == TypeEnum.ELECTRONIC, productName != replaceAWithB($product.typeEnum.someOutputString) ) 25 | then 26 | modify ($product) {productName = replaceAWithB($product.typeEnum.someOutputString)} 27 | LoggerFactory.getLogger(DroolsSpring.class).debug(String.format("ELECTRONIC rule applied, product name is now equal to [%s]", $product.productName)) 28 | end -------------------------------------------------------------------------------- /Drools/Spring integration/src/test/java/pl/grzejszczak/marcin/drools/springintegration/service/NoRulesProductServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.drools.springintegration.service; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Qualifier; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | import pl.grzejszczak.marcin.drools.springintegration.enums.TypeEnum; 10 | import pl.grzejszczak.marcin.drools.springintegration.model.Product; 11 | import pl.grzejszczak.marcin.drools.springintegration.utils.SomeUtil; 12 | 13 | import java.util.List; 14 | 15 | import static com.google.common.collect.Lists.newArrayList; 16 | import static java.lang.String.format; 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertNotSame; 19 | 20 | /** 21 | * Created with IntelliJ IDEA. 22 | * User: mgrzejszczak 23 | * Date: 14.01.13 24 | */ 25 | @RunWith(SpringJUnit4ClassRunner.class) 26 | @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) 27 | public class NoRulesProductServiceImplTest { 28 | 29 | @Autowired 30 | @Qualifier("ProductProcessingService") 31 | private Processor> productProcessingService; 32 | 33 | 34 | /** 35 | * Test is not run in perfect isolation - the purpose is to show the outcome of processing without Drools 36 | * 37 | * @throws Exception 38 | */ 39 | @Test 40 | public void testRunProductLogic() throws Exception { 41 | Product medicalProduct = new Product(TypeEnum.MEDICAL); 42 | Product electronicProduct = new Product(TypeEnum.ELECTRONIC); 43 | String initialMedicalProductName = medicalProduct.getProductName(); 44 | String initialElectronicProduct = electronicProduct.getProductName(); 45 | System.out.println(format("Initial productName for Medical [%s]", medicalProduct.getProductName())); 46 | System.out.println(format("Initial productName for Electronic [%s]", electronicProduct.getProductName())); 47 | 48 | productProcessingService.process(newArrayList(medicalProduct, electronicProduct)); 49 | 50 | String finalMedicalProduct = medicalProduct.getProductName(); 51 | String finalElectronicProduct = electronicProduct.getProductName(); 52 | assertNotSame(finalMedicalProduct, initialMedicalProductName); 53 | assertNotSame(finalElectronicProduct, initialElectronicProduct); 54 | assertEquals(SomeUtil.replaceAWithB(initialMedicalProductName), finalMedicalProduct); 55 | assertEquals(SomeUtil.replaceAWithB(initialElectronicProduct), finalElectronicProduct); 56 | 57 | System.out.println(format("Final productName for Medical [%s]", medicalProduct.getProductName())); 58 | System.out.println(format("Final productName for Electronic [%s]", electronicProduct.getProductName())); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Groovy/Mappings/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | group = 'com.blogspot.toomuchcoding' 4 | version = '1.0' 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | compile 'org.codehaus.groovy:groovy-all:2.0.5' 12 | compile 'org.slf4j:slf4j-log4j12:1.7.2' 13 | compile 'log4j:log4j:1.2.16' 14 | compile 'com.google.guava:guava:14.0' 15 | 16 | testCompile group: 'junit', name: 'junit', version: '4.+' 17 | } 18 | 19 | task(executeMain, dependsOn: 'classes', type: JavaExec) { 20 | main = 'com.blogspot.toomuchcoding.XmlTransformer' 21 | classpath = sourceSets.main.runtimeClasspath 22 | } -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/java/com/blogspot/toomuchcoding/XmlTransformer.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding; 2 | 3 | import com.blogspot.toomuchcoding.factory.ScriptFactoryImpl; 4 | import com.blogspot.toomuchcoding.factory.TransformerFactory; 5 | import com.blogspot.toomuchcoding.factory.TransformerFactoryImpl; 6 | import com.google.common.base.Charsets; 7 | import com.google.common.io.Resources; 8 | 9 | import java.io.IOException; 10 | import java.net.URL; 11 | 12 | /** 13 | * Created with IntelliJ IDEA. 14 | * User: mgrzejszczak 15 | * Date: 22.03.13 16 | * Time: 23:50 17 | */ 18 | 19 | public class XmlTransformer { 20 | 21 | public static void main(String... args) throws IOException{ 22 | URL sampleXmlUrl = Resources.getResource("xml/SampleXml.xml"); 23 | String inputXml = Resources.toString(sampleXmlUrl, Charsets.UTF_8); 24 | System.out.println("Converted from [" + inputXml + "]\n"); 25 | TransformerFactory transformerFactory = new TransformerFactoryImpl(new ScriptFactoryImpl()); 26 | System.out.println(" to\n\n[" + transformerFactory.createTransformer().transform(inputXml) + "]"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/java/com/blogspot/toomuchcoding/factory/ScriptFactory.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.factory; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * Created with IntelliJ IDEA. 7 | * User: mgrzejszczak 8 | * Date: 30.03.13 9 | * Time: 16:56 10 | */ 11 | public interface ScriptFactory { 12 | File createScript(); 13 | } 14 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/java/com/blogspot/toomuchcoding/factory/ScriptFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.factory; 2 | 3 | import com.google.common.io.Resources; 4 | 5 | import java.io.File; 6 | import java.net.URL; 7 | 8 | /** 9 | * Created with IntelliJ IDEA. 10 | * User: mgrzejszczak 11 | * Date: 30.03.13 12 | * Time: 16:57 13 | */ 14 | public class ScriptFactoryImpl implements ScriptFactory { 15 | private static final String TRANSFORMER_SCRIPT_PATH = "groovy/GroovyXmlTransformer.groovy"; 16 | 17 | @Override 18 | public File createScript() { 19 | URL transformerScriptUrl = Resources.getResource(TRANSFORMER_SCRIPT_PATH); 20 | return new File(transformerScriptUrl.getFile()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/java/com/blogspot/toomuchcoding/factory/TransformerFactory.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.factory; 2 | 3 | import com.blogspot.toomuchcoding.transformer.Transformer; 4 | 5 | /** 6 | * Created with IntelliJ IDEA. 7 | * User: mgrzejszczak 8 | * Date: 22.03.13 9 | * Time: 23:53 10 | */ 11 | public interface TransformerFactory { 12 | Transformer createTransformer(); 13 | } 14 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/java/com/blogspot/toomuchcoding/factory/TransformerFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.factory; 2 | 3 | import com.blogspot.toomuchcoding.transformer.Transformer; 4 | import com.google.common.io.Resources; 5 | import groovy.util.GroovyScriptEngine; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.io.File; 10 | import java.io.IOException; 11 | import java.net.URL; 12 | 13 | /** 14 | * Created with IntelliJ IDEA. 15 | * User: mgrzejszczak 16 | * Date: 22.03.13 17 | * Time: 23:54 18 | */ 19 | public class TransformerFactoryImpl implements TransformerFactory { 20 | private static final String GROOVY_SCRIPTS_CLASSPATH = "groovy/"; 21 | private static Logger LOGGER = LoggerFactory.getLogger(TransformerFactoryImpl.class); 22 | 23 | private ScriptFactory scriptFactory; 24 | private GroovyScriptEngine groovyScriptEngine; 25 | 26 | public TransformerFactoryImpl(ScriptFactory scriptFactory) { 27 | this.scriptFactory = scriptFactory; 28 | try { 29 | groovyScriptEngine = new GroovyScriptEngine(GROOVY_SCRIPTS_CLASSPATH); 30 | } catch (IOException e) { 31 | LOGGER.error("Exception occurred while trying to create the Groovy script engine", e); 32 | throw new RuntimeException(e); 33 | } 34 | } 35 | 36 | @Override 37 | public Transformer createTransformer() { 38 | Transformer transformerFromScript = null; 39 | try { 40 | File scriptFile = scriptFactory.createScript(); 41 | URL scriptAsAClasspathResource = Resources.getResource(GROOVY_SCRIPTS_CLASSPATH + scriptFile.getName()); 42 | Class classFromScript = groovyScriptEngine.loadScriptByName(scriptAsAClasspathResource.getFile()); 43 | transformerFromScript = (Transformer) classFromScript.newInstance(); 44 | } catch (Exception e) { 45 | LOGGER.error("Exception occurred while trying to execute Groovy script", e); 46 | } 47 | return transformerFromScript; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/java/com/blogspot/toomuchcoding/transformer/Transformer.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.transformer; 2 | 3 | /** 4 | * Created with IntelliJ IDEA. 5 | * User: mgrzejszczak 6 | * Date: 22.03.13 7 | * Time: 23:51 8 | */ 9 | public interface Transformer { 10 | T transform(V input); 11 | } 12 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/resources/groovy/AbstractGroovyXmlTransformer.groovy: -------------------------------------------------------------------------------- 1 | package groovy 2 | 3 | import com.blogspot.toomuchcoding.transformer.Transformer 4 | import groovy.util.slurpersupport.NodeChildren 5 | import groovy.xml.MarkupBuilder 6 | 7 | /** 8 | * Created with IntelliJ IDEA. 9 | * User: mgrzejszczak 10 | * Date: 23.03.13 11 | * Time: 02:16 12 | */ 13 | abstract class AbstractGroovyXmlTransformer implements Transformer { 14 | 15 | static Map MISSING_PROPERTIES = ["convertDate": new DateConverter(), "map": new Mapper()] 16 | 17 | @Override 18 | String transform(String input) { 19 | def inputXml = new XmlSlurper().parseText input 20 | def writer = new StringWriter() 21 | def outputXml = new MarkupBuilder(writer) 22 | doTransform inputXml, outputXml 23 | writer.toString() 24 | } 25 | 26 | abstract void doTransform(inputXml, outputXml) 27 | 28 | def propertyMissing(String name) { 29 | Object property = MISSING_PROPERTIES[name] 30 | assert property != null, "There is no function like [$name]. The ones that are supported are ${MISSING_PROPERTIES.keySet()}" 31 | property 32 | } 33 | 34 | 35 | protected static class Mapper { 36 | private Map inputParameters 37 | 38 | Mapper given(Map inputParameters) { 39 | this.inputParameters = inputParameters 40 | this 41 | } 42 | 43 | String from(NodeChildren nodeChildren) { 44 | assert inputParameters != null, "The mapping can't be null!" 45 | assert nodeChildren != null, "Node can't be null!" 46 | String nodeText = nodeChildren.text() 47 | String mappedValue = inputParameters[nodeText] 48 | mappedValue ?: inputParameters.default 49 | } 50 | 51 | static Mapper map(Map inputParameters) { 52 | return new Mapper(inputParameters) 53 | } 54 | } 55 | 56 | protected static class DateConverter { 57 | private String inputDate 58 | private String inputDateFormat 59 | 60 | DateConverter from(NodeChildren nodeChildren) { 61 | this.inputDate = nodeChildren.text() 62 | this 63 | } 64 | 65 | DateConverter havingDateFormat(String inputDateFormat) { 66 | this.inputDateFormat = inputDateFormat 67 | this 68 | } 69 | 70 | String toOutputDateFormat(String outputDateFormat) { 71 | assert inputDate != null, "The input date for which you are trying to do the conversion can't be null" 72 | assert inputDateFormat != null, "The input date format for which you are trying to do the conversion can't be null" 73 | assert outputDateFormat != null, "The output date format for which you are trying to do the conversion can't be null" 74 | Date.parse(inputDateFormat, inputDate).format(outputDateFormat) 75 | } 76 | 77 | static DateConverter convertDate() { 78 | new DateConverter() 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/resources/groovy/GroovyXmlTransformer.groovy: -------------------------------------------------------------------------------- 1 | package groovy 2 | 3 | /** 4 | * Created with IntelliJ IDEA. 5 | * User: mgrzejszczak 6 | * Date: 22.03.13 7 | * Time: 23:59 8 | * 9 | * additional functions: 10 | * 11 | * convertDate.from(Node).havingDateFormat("DateFormat").toOutputDateFormat("AnotherDateFormat") 12 | * map.given("Value to be mapped from" : "Value to be mapped to", "default" : "default value").from(Node) 13 | * 14 | */ 15 | class GroovyXmlTransformer extends AbstractGroovyXmlTransformer { 16 | 17 | @Override 18 | void doTransform(inputXml, outputXml) { 19 | outputXml.OutputSystemEnvelope() { 20 | OutputSystemContext { 21 | ResponseID(inputXml.InputSystemContext.RequestID.text().reverse()) 22 | OutputSource('OUTPUT_SYSTEM') 23 | OutputDate(convertDate.from(inputXml.InputSystemContext.InputDate).havingDateFormat("dd/MM/yyyy").toOutputDateFormat("yy/MM/dd")) 24 | } 25 | OutputAdditionalData { 26 | OutputReferenceNo("SOME_PREFIX_${inputXml.AdditionalData.ReferenceNo.text()}_SOME_SUFIX") 27 | OutputTypeOfAction(map.given("Some_action" : "Some_output_action", "default" : "something else").from(inputXml.AdditionalData.TypeOfAction)) 28 | OutputTransactions { 29 | inputXml.AdditionalData.Transactions.Transaction.each { 30 | OutputTransaction(Client: it.Client, ProductType: it.ProductType, 'Done') 31 | } 32 | } 33 | OutputProducts { 34 | def minProduct = inputXml.AdditionalData.Products.Product.list().min { it.Value.text() } 35 | def maxProduct = inputXml.AdditionalData.Products.Product.list().max { it.Value.text() } 36 | MinProduct(name: minProduct.Name.text(), minProduct.Value.text()) 37 | MaxProduct(name: maxProduct.Name.text(), maxProduct.Value.text()) 38 | } 39 | } 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Groovy/Mappings/src/main/resources/xml/SampleXml.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1234567890 4 | INPUT_SYSTEM 5 | 22/03/2013 6 | 7 | 8 | Ref1234567 9 | Some_action 10 | 11 | 12 | ACME 13 | IRS 14 | 15 | 16 | Oracle 17 | DB 18 | 19 | 20 | 21 | 22 | Book 23 | 1 24 | 25 | 26 | Car 27 | 10000 28 | 29 | 30 | Boat 31 | 100000000 32 | 33 | 34 | Spaceship 35 | 1000000000000000000 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Groovy/ast/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'groovy' 2 | 3 | repositories { 4 | mavenCentral() 5 | } 6 | 7 | dependencies { 8 | compile 'org.codehaus.groovy:groovy-all:2.2.0' 9 | compile 'org.slf4j:slf4j-api:1.7.5' 10 | compile 'org.slf4j:slf4j-simple:1.7.5' 11 | testCompile 'org.spockframework:spock-core:0.7-groovy-2.0' 12 | testCompile 'org.assertj:assertj-core:1.5.0' 13 | } -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/ast/WithLogging.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.ast 2 | 3 | import org.codehaus.groovy.transform.GroovyASTTransformationClass 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 | @Retention(RetentionPolicy.SOURCE) 11 | @Target([ElementType.METHOD]) 12 | @GroovyASTTransformationClass(["com.blogspot.toomuchcoding.ast.WithAroundASTTransformation"]) 13 | public @interface WithLogging { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/basescript/NewBaseScript.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.basescript 2 | 3 | class NewBaseScript extends Script { 4 | 5 | static final String OUTPUT_MSG = "Inner hello" 6 | 7 | String hello() { 8 | return OUTPUT_MSG 9 | } 10 | 11 | @Override 12 | Object run() { 13 | return "script" 14 | } 15 | } -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/canonical/CanonicalPlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.canonical 2 | 3 | import groovy.transform.Canonical 4 | 5 | @Canonical 6 | class CanonicalPlayer { 7 | String name, surname 8 | Collection positions 9 | int age 10 | Map skills 11 | } -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/category/AnnotatedTensCategory.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.category 2 | 3 | @Category(Integer) 4 | class AnnotatedTensCategory { 5 | Integer getAnnotatedTens() { 6 | return this * 10 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/category/TensCategory.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.category 2 | 3 | class TensCategory { 4 | static Integer getTens(Integer self) { 5 | return self * 10 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/compilestatic/CompileStaticAndTypeChecked.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.compilestatic 2 | 3 | import groovy.transform.CompileStatic 4 | import groovy.transform.TypeChecked 5 | 6 | class CompileStaticAndTypeChecked { 7 | DynamicPlayer player 8 | 9 | CompileStaticAndTypeChecked() { 10 | player = new DynamicPlayer() 11 | } 12 | 13 | /** 14 | * There is no method bar yet the code compiles - try to add @TypeChecked or @CompileStatic and the code won't compile 15 | * @return 16 | */ 17 | def groovy() { 18 | player.bar 19 | } 20 | 21 | @TypeChecked 22 | def typed() { 23 | player.unknownType 24 | } 25 | 26 | @CompileStatic 27 | def compiled() { 28 | player.unknownType 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/compilestatic/DynamicPlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.compilestatic 2 | 3 | class DynamicPlayer { 4 | static final String ORIGINAL_RESULT = "bar" 5 | 6 | def unknownType = ORIGINAL_RESULT 7 | } 8 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/delegate/DelegatePlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.delegate 2 | 3 | class DelegatePlayer { 4 | String name, surname 5 | @Delegate 6 | BigDecimal value 7 | } 8 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/equalshashcode/EqualsAndHashCodePlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.equalshashcode 2 | import groovy.transform.EqualsAndHashCode 3 | 4 | @EqualsAndHashCode 5 | class EqualsAndHashCodePlayer { 6 | String name, surname 7 | } 8 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/externalized/ExternalizedPlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.externalized 2 | 3 | import groovy.transform.AutoExternalize 4 | 5 | @AutoExternalize 6 | class ExternalizedPlayer { 7 | 8 | static final long serialVersionUID = 1L 9 | 10 | String name 11 | String surname 12 | } 13 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/immutable/ImmutablePlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.immutable 2 | 3 | import groovy.transform.Immutable 4 | 5 | @Immutable 6 | class ImmutablePlayer { 7 | String name 8 | MutablePlayer mutablePlayer 9 | } 10 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/immutable/MutablePlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.immutable 2 | 3 | class MutablePlayer { 4 | int age 5 | } 6 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/inheritconstructors/MyRuntimeException.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.inheritconstructors 2 | 3 | /** 4 | * You have to write the constructors yourself or make IDE generate it for you... 5 | */ 6 | class MyRuntimeException extends RuntimeException { 7 | MyRuntimeException() { 8 | } 9 | 10 | MyRuntimeException(String s) { 11 | super(s) 12 | } 13 | 14 | MyRuntimeException(String s, Throwable throwable) { 15 | super(s, throwable) 16 | } 17 | 18 | MyRuntimeException(Throwable throwable) { 19 | super(throwable) 20 | } 21 | 22 | MyRuntimeException(String s, Throwable throwable, boolean b, boolean b1) { 23 | super(s, throwable, b, b1) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/inheritconstructors/MySuperRuntimeException.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.inheritconstructors 2 | 3 | import groovy.transform.InheritConstructors 4 | 5 | @InheritConstructors 6 | class MySuperRuntimeException extends RuntimeException { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/loggers/PlayerServiceWithLog.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.loggers 2 | 3 | import groovy.util.logging.Slf4j 4 | 5 | @Slf4j 6 | class PlayerServiceWithLog { 7 | void someMethod() { 8 | log.info("THANK GOD!!!!!!!!!") 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/loggers/PlayerServiceWithManualLog.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.loggers 2 | 3 | import org.slf4j.Logger 4 | import org.slf4j.LoggerFactory 5 | 6 | class PlayerServiceWithManualLog { 7 | private static final Logger log = LoggerFactory.getLogger(PlayerServiceWithManualLog) 8 | 9 | void someMethod() { 10 | log.info("HOW MANY TIMES DID YOU HAVE TO WRITE THAT LOGGER?") 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/mixin/Athlete.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.mixin 2 | 3 | interface Athlete { 4 | String getName() 5 | } 6 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/mixin/DribblingAbility.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.mixin 2 | 3 | @Category(Athlete) 4 | class DribblingAbility { 5 | def dribble() { 6 | return "My name is [${name}]. I can dribble." 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/mixin/FootballPlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.mixin 2 | 3 | @Mixin([RunningAbility, DribblingAbility]) 4 | class FootballPlayer implements Athlete { 5 | String getName() { 6 | return "Robert Lewandowski" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/mixin/RunningAbility.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.mixin 2 | 3 | @Category(Athlete) 4 | class RunningAbility { 5 | def run() { 6 | return "My name is [${name}]. I can run." 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/mixin/TennisAbility.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.mixin 2 | 3 | @Category(Athlete) 4 | class TennisAbility { 5 | def backhand() { 6 | return "My name is [${name}]. I can use my backhand." 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/mixin/TennisPlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.mixin 2 | 3 | @Mixin([RunningAbility, TennisAbility]) 4 | class TennisPlayer implements Athlete { 5 | String getName() { 6 | return "Lukasz Kubot" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/tostring/BasePlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.tostring 2 | 3 | import groovy.transform.ToString 4 | 5 | @ToString(includePackage = false) 6 | class BasePlayer { 7 | String name, surname 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/groovy/com/blogspot/toomuchcoding/tostring/ChildPlayer.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.tostring 2 | 3 | import groovy.transform.ToString 4 | 5 | @ToString(includeSuper = true, includeNames = true) 6 | class ChildPlayer extends BasePlayer { 7 | int age 8 | } 9 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/resources/ast/ClassWithAst.groovy: -------------------------------------------------------------------------------- 1 | package ast 2 | 3 | import com.blogspot.toomuchcoding.ast.WithLogging 4 | 5 | @WithLogging() 6 | void doSth() { 7 | println "In the middle" 8 | } 9 | 10 | doSth() -------------------------------------------------------------------------------- /Groovy/ast/src/main/resources/baseScript/SomeScript.groovy: -------------------------------------------------------------------------------- 1 | package baseScript 2 | 3 | import com.blogspot.toomuchcoding.basescript.NewBaseScript 4 | import groovy.transform.BaseScript 5 | 6 | println "Hello" 7 | 8 | @BaseScript NewBaseScript newBaseScript 9 | 10 | assert newBaseScript == this 11 | return hello() -------------------------------------------------------------------------------- /Groovy/ast/src/main/resources/field/FieldScript.groovy: -------------------------------------------------------------------------------- 1 | package field 2 | 3 | import groovy.transform.Field 4 | 5 | @Field def newMap = ["key" : "value"] 6 | 7 | String someOtherMethod() { 8 | return newMap["key"] 9 | } 10 | 11 | return someOtherMethod() -------------------------------------------------------------------------------- /Groovy/ast/src/main/resources/field/WrongFieldScript.groovy: -------------------------------------------------------------------------------- 1 | package field 2 | 3 | def map = ["key": "value"] 4 | 5 | String someMethod() { 6 | return map["key"] 7 | } 8 | 9 | return someMethod() 10 | -------------------------------------------------------------------------------- /Groovy/ast/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/AutoExternalizeSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.externalized.ExternalizedPlayer 4 | import com.blogspot.toomuchcoding.util.SerializationUtil 5 | import spock.lang.Specification 6 | 7 | class AutoExternalizeSpec extends Specification { 8 | 9 | def "should serialize and deserialize object"() { 10 | given: 11 | ExternalizedPlayer objectUnderTest = new ExternalizedPlayer(name: "name", surname: "surname") 12 | 13 | when: 14 | def deserializedObject = SerializationUtil.serializeAndBack(objectUnderTest) 15 | 16 | then: 17 | deserializedObject.name == objectUnderTest.name 18 | deserializedObject.surname == objectUnderTest.surname 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/BaseScriptSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.basescript.NewBaseScript 4 | import spock.lang.Specification 5 | 6 | class BaseScriptSpec extends Specification { 7 | 8 | def "should make script return the @BaseScript annotated object"() { 9 | given: 10 | Class baseScript = new GroovyClassLoader().parseClass(this.getClass().getResource("/baseScript/SomeScript.groovy").text) 11 | Script script = (Script) baseScript.newInstance() 12 | when: 13 | def result = script.run() 14 | then: 15 | NewBaseScript.OUTPUT_MSG == result 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/CanonicalSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.canonical.CanonicalPlayer 4 | import spock.lang.Specification 5 | 6 | class CanonicalSpec extends Specification { 7 | 8 | def "should return equal objects by instantiation using map arguments and created constructor"() { 9 | given: 10 | def player = new CanonicalPlayer(name: "name", surname: "surname", positions: ["forward", "defence"], age: 10, skills: ["offence": "good", "defence": "bad"]) 11 | when: 12 | def canonicalPlayer = new CanonicalPlayer("name", "surname", ["forward", "defence"], 10, ["offence": "good", "defence": "bad"]) 13 | then: 14 | player == canonicalPlayer 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/CategorySpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.category.AnnotatedTensCategory 4 | import com.blogspot.toomuchcoding.category.TensCategory 5 | import spock.lang.Specification 6 | 7 | class CategorySpec extends Specification { 8 | 9 | def "should mix in new functionality using TensCategory"() { 10 | when: 11 | def result 12 | use(TensCategory) { 13 | result = 4.tens 14 | } 15 | then: 16 | 40 == result 17 | 18 | } 19 | 20 | def "should mix in new functionality using AnnotatedTensCategory"() { 21 | when: 22 | def result 23 | use(AnnotatedTensCategory) { 24 | result = 4.annotatedTens 25 | } 26 | then: 27 | 40 == result 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/CompileStaticAndTypeCheckSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.compilestatic.CompileStaticAndTypeChecked 4 | import com.blogspot.toomuchcoding.compilestatic.DynamicPlayer 5 | import spock.lang.Specification 6 | 7 | import static com.blogspot.toomuchcoding.compilestatic.DynamicPlayer.getORIGINAL_RESULT 8 | 9 | class CompileStaticAndTypeCheckSpec extends Specification { 10 | 11 | static final String OVERLOADED_RESULT = "" 12 | 13 | def setupSpec() { 14 | DynamicPlayer.metaClass.getUnknownType = { OVERLOADED_RESULT } 15 | } 16 | 17 | def "should return injected method's result for @TypeChecked annotated method"() { 18 | given: 19 | CompileStaticAndTypeChecked object = new CompileStaticAndTypeChecked() 20 | when: 21 | String result = object.typed() 22 | then: 23 | OVERLOADED_RESULT == result 24 | } 25 | 26 | def "should return original method's result for @CompileStatic annotated method"() { 27 | given: 28 | CompileStaticAndTypeChecked object = new CompileStaticAndTypeChecked() 29 | when: 30 | String result = object.compiled() 31 | then: 32 | ORIGINAL_RESULT == result 33 | } 34 | 35 | def cleanupSpec() { 36 | DynamicPlayer.metaClass = null 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/CustomAstSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import spock.lang.Specification 4 | 5 | class CustomAstSpec extends Specification { 6 | ByteArrayOutputStream outContent = new ByteArrayOutputStream() 7 | 8 | def setup() { 9 | System.out = new PrintStream(outContent) 10 | } 11 | 12 | def "should print content of the method in the script together with the logic from AST transform"() { 13 | given: 14 | Class baseScript = new GroovyClassLoader().parseClass(this.getClass().getResource("/ast/ClassWithAst.groovy").text) 15 | Script script = (Script) baseScript.newInstance() 16 | when: 17 | script.run() 18 | then: 19 | "Starting doSth\r\n"+ 20 | "In the middle\r\n"+ 21 | "Ending doSth\r\n" == outContent.toString() 22 | } 23 | 24 | def cleanup() { 25 | System.out = null 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/DelegateSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.delegate.DelegatePlayer 4 | import spock.lang.Specification 5 | 6 | class DelegateSpec extends Specification { 7 | 8 | public static final int ROBERT_VALUE = 30_000_000 9 | public static final int WOJCIECH_VALUE = 10_000_000 10 | 11 | def "should calculate total amount by allowing a player to call big decimal methods"() { 12 | given: 13 | DelegatePlayer robert = new DelegatePlayer(name: "robert", surname: "lewandowski", value: ROBERT_VALUE) 14 | DelegatePlayer wojciech = new DelegatePlayer(name: "wojciech", surname: "szczesny", value: WOJCIECH_VALUE) 15 | when: 16 | BigDecimal totalAmount = robert.add(wojciech.value) 17 | then: 18 | totalAmount == ROBERT_VALUE + WOJCIECH_VALUE 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/EqualsAndHashCodeSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.equalshashcode.EqualsAndHashCodePlayer 4 | import spock.lang.Specification 5 | 6 | class EqualsAndHashCodeSpec extends Specification { 7 | 8 | def "should return true for equal player objects "() { 9 | given: 10 | EqualsAndHashCodePlayer player = new EqualsAndHashCodePlayer(name: "name", surname: "surname") 11 | EqualsAndHashCodePlayer player2 = new EqualsAndHashCodePlayer(name: "name", surname: "surname") 12 | expect: 13 | player == player2 14 | } 15 | 16 | def "should insert one entry in map for the same player"() { 17 | given: 18 | EqualsAndHashCodePlayer player = new EqualsAndHashCodePlayer(name: "name", surname: "surname") 19 | def map = [(player): "one"] 20 | when: 21 | map[player] = "two" 22 | then: 23 | map.size() == 1 24 | map[player] == "two" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/FieldSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import spock.lang.Specification 4 | 5 | class FieldSpec extends Specification { 6 | def "should throw exception due to undefined variable"() { 7 | given: 8 | Class baseScript = new GroovyClassLoader().parseClass(this.getClass().getResource("/field/WrongFieldScript.groovy").text) 9 | Script script = (Script) baseScript.newInstance() 10 | when: 11 | script.run() 12 | then: 13 | thrown(MissingPropertyException) 14 | } 15 | 16 | def "should return value from @Field annotated map"() { 17 | given: 18 | Class baseScript = new GroovyClassLoader().parseClass(this.getClass().getResource("/field/FieldScript.groovy").text) 19 | Script script = (Script) baseScript.newInstance() 20 | when: 21 | def result = script.run() 22 | then: 23 | result == "value" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/ImmutableSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.immutable.ImmutablePlayer 4 | import com.blogspot.toomuchcoding.immutable.MutablePlayer 5 | import spock.lang.Specification 6 | 7 | class ImmutableSpec extends Specification { 8 | 9 | def "should not allow to construct an immutable object with a mutable property"() { 10 | when: 11 | new ImmutablePlayer("name", new MutablePlayer(age: 10)) 12 | then: 13 | RuntimeException exception = thrown() 14 | exception.message.contains("@Immutable processor doesn't know how to handle field 'mutablePlayer' of type") 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/InheritConstructorsSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.inheritconstructors.MySuperRuntimeException 4 | import spock.lang.Specification 5 | 6 | class InheritConstructorsSpec extends Specification { 7 | 8 | def "should instantiate exceptions"() { 9 | expect: 10 | new MySuperRuntimeException() 11 | new MySuperRuntimeException("msg") 12 | new MySuperRuntimeException("msg", new RuntimeException()) 13 | new MySuperRuntimeException(new RuntimeException()) 14 | new MySuperRuntimeException("msg", new RuntimeException(), true, false) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/LogSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.loggers.PlayerServiceWithLog 4 | import spock.lang.Specification 5 | 6 | class LogSpec extends Specification { 7 | def "should add logger through ast"() { 8 | expect: 9 | PlayerServiceWithLog.log != null 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/MixinSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.mixin.FootballPlayer 4 | import com.blogspot.toomuchcoding.mixin.TennisPlayer 5 | import spock.lang.Specification 6 | 7 | class MixinSpec extends Specification { 8 | def "should make football player run"() { 9 | given: 10 | def player = new FootballPlayer() 11 | expect: 12 | player.run() =~ /^.*run.$/ 13 | } 14 | 15 | def "should make football player dribble"() { 16 | given: 17 | def player = new FootballPlayer() 18 | expect: 19 | player.dribble() =~ /^.*dribble.$/ 20 | } 21 | 22 | def "should make tennis player run"() { 23 | given: 24 | def player = new TennisPlayer() 25 | expect: 26 | player.run() =~ /^.*run.$/ 27 | } 28 | 29 | def "should make tennis player dribble"() { 30 | given: 31 | def player = new TennisPlayer() 32 | expect: 33 | player.backhand() =~ /^.*backhand.$/ 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/TensSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import spock.lang.Specification 4 | 5 | class TensSpec extends Specification { 6 | 7 | def "should calculate ten times given number"() { 8 | given: 9 | int initialNumber = 4 10 | when: 11 | int calculatedNumber = getTimesTenOf(initialNumber) 12 | then: 13 | 40 == calculatedNumber 14 | } 15 | 16 | Integer getTimesTenOf(Integer number) { 17 | return 10 * number; 18 | } 19 | 20 | def "should calculate ten times given number by mixing a method to object"() { 21 | given: 22 | int initialNumber = 4 23 | and: 24 | initialNumber.metaClass.getTimesTen = { 25 | return 10 * delegate 26 | } 27 | when: 28 | int calculatedNumber = 4.timesTen 29 | then: 30 | 40 == calculatedNumber 31 | } 32 | 33 | def "should calculate ten times given number by mixing a method to class"() { 34 | given: 35 | Integer.metaClass.getTimesTen = { 36 | return 10 * delegate 37 | } 38 | when: 39 | int calculatedNumber = 4.timesTen 40 | then: 41 | 40 == calculatedNumber 42 | } 43 | 44 | def cleanup() { 45 | Integer.metaClass = null 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/ToStringSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding 2 | 3 | import com.blogspot.toomuchcoding.tostring.ChildPlayer 4 | import spock.lang.Specification 5 | 6 | class ToStringSpec extends Specification { 7 | 8 | def "should return nice looking to string"() { 9 | given: 10 | ChildPlayer childPlayer = new ChildPlayer(name: "Name", surname: "Surname", age: 10) 11 | when: 12 | String result = childPlayer.toString() 13 | then: 14 | !result.startsWith("${childPlayer.class.name}@") 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Groovy/ast/src/test/groovy/com/blogspot/toomuchcoding/util/SerializationUtil.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.util 2 | 3 | class SerializationUtil { 4 | static T serializeAndBack(T obj) throws Exception { 5 | ByteArrayOutputStream os = serializeObject(obj); 6 | return (T) deserializeObject(os, Object.class); 7 | } 8 | 9 | static T deserializeObject(ByteArrayOutputStream serialized, Class type) throws IOException, ClassNotFoundException { 10 | InputStream unserialize = new ByteArrayInputStream(serialized.toByteArray()); 11 | return deserializeObject(unserialize, type); 12 | } 13 | 14 | static T deserializeObject(InputStream unserialize, Class type) throws IOException, ClassNotFoundException { 15 | return new ObjectInputStream(unserialize).readObject(); 16 | } 17 | 18 | static ByteArrayOutputStream serializeObject(Object object) throws IOException { 19 | ByteArrayOutputStream serialized = new ByteArrayOutputStream(); 20 | new ObjectOutputStream(serialized).writeObject(object); 21 | return serialized; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Guava/Cache/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | pl.grzejszczak.marcin 5 | guava-cache 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 14 | 15 | 16 | junit 17 | junit 18 | 4.10 19 | 20 | 21 | com.google.guava 22 | guava 23 | 13.0.1 24 | 25 | 26 | org.slf4j 27 | slf4j-log4j12 28 | 1.5.2 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Guava/Cache/src/main/pl/grzejszczak/marcin/guava/cache/utils/RegexpUtils.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.guava.cache.utils; 2 | 3 | import com.google.common.cache.CacheBuilder; 4 | import com.google.common.cache.CacheLoader; 5 | import com.google.common.cache.LoadingCache; 6 | 7 | import java.util.concurrent.ExecutionException; 8 | import java.util.regex.Matcher; 9 | import java.util.regex.Pattern; 10 | 11 | import static java.lang.String.format; 12 | 13 | public final class RegexpUtils { 14 | 15 | private RegexpUtils() { 16 | throw new UnsupportedOperationException("RegexpUtils is a utility class - don't instantiate it!"); 17 | } 18 | 19 | private static final LoadingCache COMPILED_PATTERNS = 20 | CacheBuilder.newBuilder().build(new CacheLoader() { 21 | @Override 22 | public Pattern load(String regexp) throws Exception { 23 | return Pattern.compile(regexp); 24 | } 25 | }); 26 | 27 | public static Pattern getPattern(String regexp) { 28 | try { 29 | return COMPILED_PATTERNS.get(regexp); 30 | } catch (ExecutionException e) { 31 | throw new RuntimeException(format("Error when getting a pattern [%s] from cache", regexp), e); 32 | } 33 | } 34 | 35 | public static boolean matches(String stringToCheck, String regexp) { 36 | return doGetMatcher(stringToCheck, regexp).matches(); 37 | } 38 | 39 | public static Matcher getMatcher(String stringToCheck, String regexp) { 40 | return doGetMatcher(stringToCheck, regexp); 41 | } 42 | 43 | private static Matcher doGetMatcher(String stringToCheck, String regexp) { 44 | Pattern pattern = getPattern(regexp); 45 | return pattern.matcher(stringToCheck); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Guava/Cache/src/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Mocking - Checking args/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | pl.grzejszczak.marcin 5 | mockito-argumentcaptor 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 14 | 15 | 16 | junit 17 | junit 18 | 4.10 19 | 20 | 21 | org.mockito 22 | mockito-all 23 | 1.9.0 24 | 25 | 26 | org.mockito 27 | mockito-core 28 | 1.9.0 29 | 30 | 31 | org.slf4j 32 | slf4j-log4j12 33 | 1.5.2 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Mocking - Checking args/src/integration/pl/grzejszczak/marcin/ServiceIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | import org.junit.Assert; 7 | import org.mockito.ArgumentCaptor; 8 | import org.mockito.Mockito; 9 | import org.mockito.invocation.InvocationOnMock; 10 | import org.mockito.stubbing.Answer; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | import pl.grzejszczak.marcin.junit.Processable; 15 | import pl.grzejszczak.marcin.junit.SomeTask; 16 | import pl.grzejszczak.marcin.junit.invocation.SomePojo; 17 | 18 | public class ServiceIntegrationTest { 19 | 20 | private static final Logger LOGGER = LoggerFactory.getLogger(ServiceIntegrationTest.class); 21 | 22 | private ExecutorService executorService = Executors.newCachedThreadPool(); 23 | private Processable timeConsumingExternalServiceMock = Mockito.mock(Processable.class); 24 | private SomeTask someTask = new SomeTask(); 25 | 26 | public ServiceIntegrationTest() { 27 | initializeMocks(); 28 | } 29 | 30 | private void initializeMocks() { 31 | // Inject the mock to the Task - in any possible way 32 | someTask.setTimeConsumingExternalService(timeConsumingExternalServiceMock); 33 | } 34 | 35 | public void executeTest() { 36 | executorService.execute(someTask); 37 | 38 | final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(SomePojo.class); 39 | Mockito.verify(timeConsumingExternalServiceMock).processSomeObject(argumentCaptor.capture()); 40 | SomePojo somePojo = argumentCaptor.getValue(); 41 | Assert.assertEquals("name", somePojo.getName()); 42 | LOGGER.debug("Names are equal"); 43 | Assert.assertEquals("surname", somePojo.getSurname()); 44 | LOGGER.debug("Surnames are equal"); 45 | Assert.assertTrue(1 == somePojo.getIntValue()); 46 | LOGGER.debug("Ints are equal"); 47 | Assert.assertTrue(1.0 == somePojo.getDoubleValue()); 48 | LOGGER.debug("Doubles are equal"); 49 | 50 | } 51 | 52 | public static void main(String args[]) { 53 | ServiceIntegrationTest integrationTest = new ServiceIntegrationTest(); 54 | integrationTest.executeTest(); 55 | System.exit(0); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Mocking - Checking args/src/main/pl/grzejszczak/marcin/junit/Processable.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit; 2 | 3 | import pl.grzejszczak.marcin.junit.invocation.SomePojo; 4 | 5 | public interface Processable { 6 | void process(); 7 | 8 | void processSomeObject(SomePojo argument); 9 | } 10 | -------------------------------------------------------------------------------- /Mocking - Checking args/src/main/pl/grzejszczak/marcin/junit/SomeTask.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import pl.grzejszczak.marcin.junit.invocation.SomePojo; 7 | 8 | /** 9 | * Service that does some things including processing of the external service 10 | * 11 | * @author marcin 12 | * 13 | */ 14 | public class SomeTask implements Runnable { 15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(SomeTask.class); 17 | 18 | // Service is injected via a dependency injection system 19 | private Processable timeConsumingExternalService; 20 | 21 | private void methodThatConnectsToExternalServices() { 22 | LOGGER.debug("Before processing an object"); 23 | timeConsumingExternalService.processSomeObject(new SomePojo("name", "surname", 1, 1.0)); 24 | LOGGER.debug("After processing an object"); 25 | } 26 | 27 | public void run() { 28 | methodThatConnectsToExternalServices(); 29 | } 30 | 31 | public Processable getTimeConsumingExternalService() { 32 | return timeConsumingExternalService; 33 | } 34 | 35 | public void setTimeConsumingExternalService(Processable timeConsumingExternalService) { 36 | this.timeConsumingExternalService = timeConsumingExternalService; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Mocking - Checking args/src/main/pl/grzejszczak/marcin/junit/invocation/SomePojo.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit.invocation; 2 | 3 | public class SomePojo { 4 | private String name; 5 | private String surname; 6 | private int intValue; 7 | private double doubleValue; 8 | 9 | public SomePojo(String name, String surname, int intValue, double doubleValue) { 10 | super(); 11 | this.name = name; 12 | this.surname = surname; 13 | this.intValue = intValue; 14 | this.doubleValue = doubleValue; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public String getSurname() { 22 | return surname; 23 | } 24 | 25 | public int getIntValue() { 26 | return intValue; 27 | } 28 | 29 | public double getDoubleValue() { 30 | return doubleValue; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "SomePojo [name=" + name + ", surname=" + surname + ", intValue=" + intValue + ", doubleValue=" + doubleValue + "]"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Mocking - Checking args/src/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Mocking - Time consuming actions/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | pl.grzejszczak.marcin 5 | mockito-invocationonmock 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 14 | 15 | 16 | 17 | junit 18 | junit 19 | 4.10 20 | 21 | 22 | org.mockito 23 | mockito-all 24 | 1.9.0 25 | 26 | 27 | org.mockito 28 | mockito-core 29 | 1.9.0 30 | 31 | 32 | org.slf4j 33 | slf4j-log4j12 34 | 1.5.2 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Mocking - Time consuming actions/src/integration/pl/grzejszczak/marcin/ServiceIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | import org.mockito.Mockito; 7 | import org.mockito.invocation.InvocationOnMock; 8 | import org.mockito.stubbing.Answer; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import pl.grzejszczak.marcin.junit.Processable; 13 | import pl.grzejszczak.marcin.junit.SomeTask; 14 | 15 | public class ServiceIntegrationTest { 16 | 17 | private static final Logger LOGGER = LoggerFactory.getLogger(ServiceIntegrationTest.class); 18 | 19 | private ExecutorService executorService = Executors.newSingleThreadExecutor(); 20 | private Processable timeConsumingExternalServiceMock = Mockito.mock(Processable.class); 21 | private SomeTask someTask = new SomeTask(); 22 | 23 | public ServiceIntegrationTest() { 24 | initializeMocks(); 25 | } 26 | 27 | private void initializeMocks() { 28 | Mockito.doAnswer(new Answer() { 29 | public Object answer(InvocationOnMock invocation) throws Throwable { 30 | // Simulation of connection to external services 31 | LOGGER.debug("Sleeping"); 32 | Thread.sleep(5000); 33 | LOGGER.debug("Stopped Sleeping"); 34 | return null; 35 | } 36 | }).when(timeConsumingExternalServiceMock).process(); 37 | // Inject the mock to the Task - in any possible way 38 | someTask.setTimeConsumingExternalService(timeConsumingExternalServiceMock); 39 | } 40 | 41 | public void executeTest() { 42 | executorService.execute(someTask); 43 | } 44 | 45 | public static void main(String args[]) { 46 | ServiceIntegrationTest integrationTest = new ServiceIntegrationTest(); 47 | integrationTest.executeTest(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Mocking - Time consuming actions/src/main/pl/grzejszczak/marcin/junit/Processable.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit; 2 | 3 | import pl.grzejszczak.marcin.junit.invocation.SomePojo; 4 | 5 | public interface Processable { 6 | void process(); 7 | 8 | void processSomeObject(SomePojo argument); 9 | } 10 | -------------------------------------------------------------------------------- /Mocking - Time consuming actions/src/main/pl/grzejszczak/marcin/junit/SomeTask.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import pl.grzejszczak.marcin.junit.invocation.SomePojo; 7 | 8 | /** 9 | * Service that does some things including processing of the external service 10 | * 11 | * @author marcin 12 | * 13 | */ 14 | public class SomeTask implements Runnable { 15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(SomeTask.class); 17 | 18 | // Service is injected via a dependency injection system 19 | private Processable timeConsumingExternalService; 20 | 21 | private void methodThatConnectsToExternalServices() { 22 | // connects to an external service and spends a couple of seconds there 23 | LOGGER.debug("Before processing"); 24 | timeConsumingExternalService.process(); 25 | LOGGER.debug("After processing"); 26 | } 27 | 28 | public void run() { 29 | methodThatConnectsToExternalServices(); 30 | } 31 | 32 | public void setTimeConsumingExternalService(Processable timeConsumingExternalService) { 33 | this.timeConsumingExternalService = timeConsumingExternalService; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Mocking - Time consuming actions/src/main/pl/grzejszczak/marcin/junit/invocation/SomePojo.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit.invocation; 2 | 3 | public class SomePojo { 4 | private String name; 5 | private String surname; 6 | private int intValue; 7 | private double doubleValue; 8 | 9 | public SomePojo(String name, String surname, int intValue, double doubleValue) { 10 | super(); 11 | this.name = name; 12 | this.surname = surname; 13 | this.intValue = intValue; 14 | this.doubleValue = doubleValue; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public String getSurname() { 22 | return surname; 23 | } 24 | 25 | public int getIntValue() { 26 | return intValue; 27 | } 28 | 29 | public double getDoubleValue() { 30 | return doubleValue; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "SomePojo [name=" + name + ", surname=" + surname + ", intValue=" + intValue + ", doubleValue=" + doubleValue + "]"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Mocking - Time consuming actions/src/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | too-much-coding 2 | =============== 3 | 4 | Too Much Coding repository. Visit the blog at http://toomuchcoding.com. You can also visit my site at http://marcin.grzejszczak.pl 5 | -------------------------------------------------------------------------------- /Spring JMS/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | pl.grzejszczak.marcin 5 | spring-jms 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 3.1.1.RELEASE 14 | 15 | 16 | 17 | spring-release 18 | http://maven.springframework.org/release 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.springframework 26 | spring-core 27 | ${spring.version} 28 | 29 | 30 | org.springframework 31 | spring-context 32 | ${spring.version} 33 | 34 | 35 | org.springframework 36 | spring-jms 37 | ${spring.version} 38 | 39 | 40 | org.springframework 41 | spring-oxm 42 | ${spring.version} 43 | 44 | 45 | org.apache.activemq 46 | activemq-core 47 | 5.7.0 48 | 49 | 50 | com.sun.xml.bind 51 | jaxb-impl 52 | 2.2.6 53 | 54 | 55 | org.slf4j 56 | slf4j-log4j12 57 | 1.6.6 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/enricher/AgeEnricher.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.enricher; 2 | 3 | import java.util.Random; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.stereotype.Component; 8 | 9 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 10 | 11 | @Component("AgeEnricher") 12 | public class AgeEnricher implements Enrichable { 13 | private static final Logger LOGGER = LoggerFactory.getLogger(AgeEnricher.class); 14 | 15 | @Override 16 | public void enrich(PlayerDetails inputObject) { 17 | LOGGER.debug("Enriching player [{}] with age data", new Object[] { inputObject.getSurname() }); 18 | // Simulating accessing DB or some other service 19 | try { 20 | Thread.sleep(2000); 21 | } catch (InterruptedException e) { 22 | LOGGER.error("Exception while sleeping occured", e); 23 | } 24 | inputObject.setAge(new Random().nextInt(35)); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/enricher/ClubEnricher.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.enricher; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | 7 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 8 | 9 | @Component("ClubEnricher") 10 | public class ClubEnricher implements Enrichable { 11 | 12 | private static final Logger LOGGER = LoggerFactory.getLogger(ClubEnricher.class); 13 | 14 | @Override 15 | public void enrich(PlayerDetails inputObject) { 16 | LOGGER.debug("Enriching player [{}] with club data", new Object[] { inputObject.getSurname() }); 17 | // Simulating accessing DB or some other service 18 | try { 19 | Thread.sleep(2000); 20 | } catch (InterruptedException e) { 21 | LOGGER.error("Exception while sleeping occured", e); 22 | } 23 | inputObject.setTeamName("Borussia Dortmund"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/enricher/Enrichable.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.enricher; 2 | 3 | public interface Enrichable { 4 | void enrich(T inputObject); 5 | } 6 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/jaxb/Convertable.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.jaxb; 2 | 3 | public interface Convertable { 4 | String marshal(T object); 5 | 6 | T unmarshal(String objectAsString); 7 | } 8 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/jaxb/PlayerDetailsConverter.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.jaxb; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.OutputStream; 5 | 6 | import javax.xml.bind.JAXBContext; 7 | import javax.xml.bind.JAXBException; 8 | import javax.xml.bind.Marshaller; 9 | import javax.xml.bind.Unmarshaller; 10 | 11 | import org.apache.activemq.util.ByteArrayInputStream; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.stereotype.Component; 15 | 16 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 17 | 18 | @Component("PlayerDetailsConverter") 19 | public class PlayerDetailsConverter implements Convertable { 20 | private static final Logger LOGGER = LoggerFactory.getLogger(PlayerDetailsConverter.class); 21 | 22 | private final JAXBContext jaxbContext; 23 | private final Marshaller jaxbMarshaller; 24 | private final Unmarshaller jaxbUnmarshaller; 25 | 26 | public PlayerDetailsConverter() throws JAXBException { 27 | jaxbContext = JAXBContext.newInstance(PlayerDetails.class); 28 | jaxbMarshaller = jaxbContext.createMarshaller(); 29 | jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 30 | jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 31 | } 32 | 33 | @Override 34 | public String marshal(PlayerDetails object) { 35 | OutputStream stream = new ByteArrayOutputStream(); 36 | try { 37 | jaxbMarshaller.marshal(object, stream); 38 | } catch (JAXBException e) { 39 | LOGGER.error("Exception occured while marshalling", e); 40 | } 41 | return stream.toString(); 42 | } 43 | 44 | @Override 45 | public PlayerDetails unmarshal(String objectAsString) { 46 | try { 47 | return (PlayerDetails) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(objectAsString.getBytes())); 48 | } catch (JAXBException e) { 49 | LOGGER.error("Exception occured while marshalling", e); 50 | } 51 | return null; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/jaxb/generated/ObjectFactory.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2012.11.05 at 09:23:22 PM CET 6 | // 7 | 8 | 9 | package pl.grzejszczak.marcin.camel.jaxb.generated; 10 | 11 | import javax.xml.bind.annotation.XmlRegistry; 12 | 13 | 14 | /** 15 | * This object contains factory methods for each 16 | * Java content interface and Java element interface 17 | * generated in the pl.grzejszczak.marcin.camel.jaxb.generated package. 18 | *

An ObjectFactory allows you to programatically 19 | * construct new instances of the Java representation 20 | * for XML content. The Java representation of XML 21 | * content can consist of schema derived interfaces 22 | * and classes representing the binding of schema 23 | * type definitions, element declarations and model 24 | * groups. Factory methods for each of these are 25 | * provided in this class. 26 | * 27 | */ 28 | @XmlRegistry 29 | public class ObjectFactory { 30 | 31 | 32 | /** 33 | * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: pl.grzejszczak.marcin.camel.jaxb.generated 34 | * 35 | */ 36 | public ObjectFactory() { 37 | } 38 | 39 | /** 40 | * Create an instance of {@link PlayerDetails } 41 | * 42 | */ 43 | public PlayerDetails createPlayerDetails() { 44 | return new PlayerDetails(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/jaxb/generated/PositionType.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2012.11.05 at 09:23:22 PM CET 6 | // 7 | 8 | 9 | package pl.grzejszczak.marcin.camel.jaxb.generated; 10 | 11 | import javax.xml.bind.annotation.XmlEnum; 12 | import javax.xml.bind.annotation.XmlType; 13 | 14 | 15 | /** 16 | *

Java class for PositionType. 17 | * 18 | *

The following schema fragment specifies the expected content contained within this class. 19 | *

20 | *

21 |  * <simpleType name="PositionType">
22 |  *   <restriction base="{http://www.w3.org/2001/XMLSchema}string">
23 |  *     <enumeration value="GK"/>
24 |  *     <enumeration value="DEF"/>
25 |  *     <enumeration value="MID"/>
26 |  *     <enumeration value="ATT"/>
27 |  *   </restriction>
28 |  * </simpleType>
29 |  * 
30 | * 31 | */ 32 | @XmlType(name = "PositionType") 33 | @XmlEnum 34 | public enum PositionType { 35 | 36 | GK, 37 | DEF, 38 | MID, 39 | ATT; 40 | 41 | public String value() { 42 | return name(); 43 | } 44 | 45 | public static PositionType fromValue(String v) { 46 | return valueOf(v); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/manual/ActiveMQRouter.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual; 2 | 3 | import java.io.File; 4 | import java.util.Scanner; 5 | 6 | import javax.jms.JMSException; 7 | 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.support.ClassPathXmlApplicationContext; 10 | import org.springframework.core.io.ClassPathResource; 11 | import org.springframework.core.io.Resource; 12 | 13 | import pl.grzejszczak.marcin.camel.jaxb.PlayerDetailsConverter; 14 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 15 | import pl.grzejszczak.marcin.camel.manual.jms.Sender; 16 | 17 | public class ActiveMQRouter { 18 | 19 | /** 20 | * @param args 21 | * @throws JMSException 22 | */ 23 | public static void main(String[] args) throws Exception { 24 | ApplicationContext context = new ClassPathXmlApplicationContext("/camel/jmsApplicationContext.xml"); 25 | @SuppressWarnings("unchecked") 26 | Sender sender = (Sender) context.getBean("originPlayerSender"); 27 | 28 | Resource resource = new ClassPathResource("/camel/RobertLewandowski.xml"); 29 | 30 | Scanner scanner = new Scanner(new File(resource.getURI())).useDelimiter("\\Z"); 31 | String contents = scanner.next(); 32 | 33 | PlayerDetailsConverter converter = context.getBean(PlayerDetailsConverter.class); 34 | 35 | sender.sendMessage(converter.unmarshal(contents)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/manual/jms/FinalListenerImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | import javax.jms.BytesMessage; 4 | import javax.jms.Message; 5 | import javax.jms.MessageListener; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.jms.support.converter.MessageConverter; 11 | import org.springframework.stereotype.Component; 12 | 13 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 14 | 15 | @Component 16 | public class FinalListenerImpl implements MessageListener { 17 | 18 | private static final Logger LOG = LoggerFactory.getLogger(FinalListenerImpl.class); 19 | 20 | @Autowired 21 | private MessageConverter messageConverter; 22 | 23 | @Override 24 | public void onMessage(Message message) { 25 | if (!(message instanceof BytesMessage)) { 26 | LOG.error("Wrong msg!"); 27 | return; 28 | } 29 | 30 | PlayerDetails playerDetails = null; 31 | try { 32 | playerDetails = (PlayerDetails) messageConverter.fromMessage(message); 33 | 34 | if (playerDetails.getTeamName() != null) { 35 | LOG.debug("Message already enriched! Shutting down the system"); 36 | System.exit(0); 37 | } else { 38 | LOG.debug("The message should have been enriched but wasn't"); 39 | System.exit(1); 40 | } 41 | 42 | } catch (Exception e) { 43 | LOG.error("Exception occured", e); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/manual/jms/ListenerImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | import java.util.List; 4 | 5 | import javax.jms.BytesMessage; 6 | import javax.jms.Message; 7 | import javax.jms.MessageListener; 8 | 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.beans.factory.annotation.Qualifier; 13 | import org.springframework.jms.support.converter.MessageConverter; 14 | import org.springframework.stereotype.Component; 15 | 16 | import pl.grzejszczak.marcin.camel.enricher.Enrichable; 17 | import pl.grzejszczak.marcin.camel.jaxb.Convertable; 18 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 19 | 20 | @Component 21 | public class ListenerImpl implements MessageListener { 22 | 23 | private static final Logger LOG = LoggerFactory.getLogger(ListenerImpl.class); 24 | 25 | @Autowired 26 | private Convertable playerDetailsConverter; 27 | 28 | @Autowired 29 | private List> listOfEnrichers; 30 | 31 | @Autowired 32 | private MessageConverter messageConverter; 33 | 34 | @Autowired 35 | @Qualifier("destinationPlayerSender") 36 | private Sender sender; 37 | 38 | @Override 39 | public void onMessage(Message message) { 40 | if (!(message instanceof BytesMessage)) { 41 | LOG.error("Wrong msg!"); 42 | return; 43 | } 44 | 45 | PlayerDetails playerDetails = null; 46 | try { 47 | playerDetails = (PlayerDetails) messageConverter.fromMessage(message); 48 | 49 | LOG.debug("Enriching the input message"); 50 | for (Enrichable enrichable : listOfEnrichers) { 51 | enrichable.enrich(playerDetails); 52 | } 53 | LOG.debug("Enriched text message: [{}]", new Object[] { playerDetailsConverter.marshal(playerDetails) }); 54 | sender.sendMessage(playerDetails); 55 | } catch (Exception e) { 56 | LOG.error("Exception occured", e); 57 | } 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/manual/jms/PlayerDetailsSenderImpl.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | import javax.jms.Destination; 4 | import javax.jms.JMSException; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.jms.core.JmsTemplate; 10 | import org.springframework.stereotype.Component; 11 | 12 | import pl.grzejszczak.marcin.camel.jaxb.generated.PlayerDetails; 13 | 14 | @Component 15 | public class PlayerDetailsSenderImpl implements Sender { 16 | 17 | private static final Logger LOGGER = LoggerFactory.getLogger(PlayerDetailsSenderImpl.class); 18 | 19 | private Destination destination; 20 | 21 | @Autowired 22 | private JmsTemplate jmsTemplate; 23 | 24 | @Override 25 | public void sendMessage(final PlayerDetails object) throws JMSException { 26 | LOGGER.debug("Sending [{}] to topic [{}]", new Object[] { object, destination }); 27 | jmsTemplate.convertAndSend(destination, object); 28 | } 29 | 30 | public Destination getDestination() { 31 | return destination; 32 | } 33 | 34 | public void setDestination(Destination destination) { 35 | this.destination = destination; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Spring JMS/src/main/java/pl/grzejszczak/marcin/camel/manual/jms/Sender.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.camel.manual.jms; 2 | 3 | public interface Sender { 4 | void sendMessage(T object) throws Exception; 5 | } 6 | -------------------------------------------------------------------------------- /Spring JMS/src/main/resources/camel/Player.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Spring JMS/src/main/resources/camel/RobertLewandowski.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Robert 4 | Lewandowski 5 | ATT 6 | -------------------------------------------------------------------------------- /Spring JMS/src/main/resources/camel/jms.properties: -------------------------------------------------------------------------------- 1 | jms.origin=Initial.Queue 2 | jms.destination=Routed.Queue -------------------------------------------------------------------------------- /Spring JMS/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Spring/Primary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: "java" 2 | apply plugin: "groovy" 3 | 4 | group = "com.blogspot.toomuchcoding" 5 | version = "0.0.1-SNAPSHOT" 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | ext { 12 | GROOVY_VERSION="2.1.9" 13 | SPRING_VERSION="3.2.4.RELEASE" 14 | SPOCK_VERSION="0.7-groovy-2.0" 15 | CGLIB_VERSION="2.2.2" 16 | OBJENESIS_VERSION="1.3" 17 | } 18 | 19 | dependencies { 20 | compile "org.codehaus.groovy:groovy-all:$GROOVY_VERSION" 21 | compile "org.springframework:spring-beans:$SPRING_VERSION" 22 | compile "org.springframework:spring-core:$SPRING_VERSION" 23 | compile "org.springframework:spring-context:$SPRING_VERSION" 24 | compile "commons-lang:commons-lang:2.6" 25 | compile "org.slf4j:slf4j-log4j12:1.7.5" 26 | 27 | testCompile group: "junit", name: "junit", version: "4.+" 28 | testCompile "org.springframework:spring-test:$SPRING_VERSION" 29 | testCompile "org.spockframework:spock-core:$SPOCK_VERSION" 30 | testCompile "org.spockframework:spock-spring:$SPOCK_VERSION" 31 | testCompile "cglib:cglib-nodep:$CGLIB_VERSION" 32 | testCompile "org.objenesis:objenesis:$OBJENESIS_VERSION" 33 | } -------------------------------------------------------------------------------- /Spring/Primary/src/main/java/com/blogspot/toomuchcoding/configuration/SpringConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.configuration; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @Configuration 7 | @ComponentScan("com.blogspot.toomuchcoding") 8 | public class SpringConfiguration { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Spring/Primary/src/main/java/com/blogspot/toomuchcoding/person/domain/Person.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.person.domain; 2 | 3 | public final class Person { 4 | private final String name; 5 | private final int age; 6 | private final boolean stupid; 7 | 8 | public Person(String name, int age, boolean stupid) { 9 | this.name = name; 10 | this.age = age; 11 | this.stupid = stupid; 12 | } 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public int getAge() { 19 | return age; 20 | } 21 | 22 | public boolean isStupid() { 23 | return stupid; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Spring/Primary/src/main/java/com/blogspot/toomuchcoding/person/service/AgePersonProcessingService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.person.service; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.blogspot.toomuchcoding.person.domain.Person; 6 | 7 | @Component 8 | class AgePersonProcessingService implements PersonProcessingService { 9 | 10 | @Override 11 | public boolean isApplicableFor(Person person) { 12 | return person.getAge() >= 18; 13 | } 14 | 15 | @Override 16 | public String process(Person person) { 17 | return String.format("Person's AGE is [%s] so we could process him", person.getAge()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Spring/Primary/src/main/java/com/blogspot/toomuchcoding/person/service/IntelligencePersonProcessingService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.person.service; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.blogspot.toomuchcoding.person.domain.Person; 6 | 7 | @Component 8 | class IntelligencePersonProcessingService implements PersonProcessingService { 9 | 10 | @Override 11 | public boolean isApplicableFor(Person person) { 12 | return person.isStupid(); 13 | } 14 | 15 | @Override 16 | public String process(Person person) { 17 | return "Person's STUPID so can process him"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Spring/Primary/src/main/java/com/blogspot/toomuchcoding/person/service/NamePersonProcessingService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.person.service; 2 | 3 | import org.apache.commons.lang.StringUtils; 4 | import org.springframework.stereotype.Component; 5 | 6 | import com.blogspot.toomuchcoding.person.domain.Person; 7 | 8 | @Component 9 | class NamePersonProcessingService implements PersonProcessingService { 10 | 11 | @Override 12 | public boolean isApplicableFor(Person person) { 13 | return StringUtils.isNotBlank(person.getName()); 14 | } 15 | 16 | @Override 17 | public String process(Person person) { 18 | return String.format("Person with NAME [%s] has been processed", person.getName()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Spring/Primary/src/main/java/com/blogspot/toomuchcoding/person/service/PersonProcessingService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.person.service; 2 | 3 | import com.blogspot.toomuchcoding.person.domain.Person; 4 | 5 | public interface PersonProcessingService { 6 | boolean isApplicableFor(Person person); 7 | String process(Person person); 8 | } 9 | -------------------------------------------------------------------------------- /Spring/Primary/src/main/java/com/blogspot/toomuchcoding/person/service/PersonProcessingServiceContainer.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.person.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.apache.commons.lang.StringUtils; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.context.annotation.Primary; 11 | import org.springframework.stereotype.Component; 12 | 13 | import com.blogspot.toomuchcoding.person.domain.Person; 14 | 15 | @Component 16 | @Primary 17 | class PersonProcessingServiceContainer implements PersonProcessingService { 18 | 19 | private static final Logger LOGGER = LoggerFactory.getLogger(PersonProcessingServiceContainer.class); 20 | 21 | @Autowired 22 | private List personProcessingServices = new ArrayList(); 23 | 24 | @Override 25 | public boolean isApplicableFor(Person person) { 26 | return person != null; 27 | } 28 | 29 | @Override 30 | public String process(Person person) { 31 | List output = new ArrayList(); 32 | for(PersonProcessingService personProcessingService : personProcessingServices){ 33 | if(personProcessingService.isApplicableFor(person)){ 34 | output.add(personProcessingService.process(person)); 35 | } 36 | } 37 | String result = StringUtils.join(output, ","); 38 | LOGGER.info(result); 39 | return result; 40 | } 41 | 42 | public List getPersonProcessingServices() { 43 | return personProcessingServices; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Spring/Primary/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Spring/Primary/src/test/groovy/com/blogspot/toomuchcoding/person/service/PersonProcessingServiceContainerIntegrationSpec.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.person.service 2 | import com.blogspot.toomuchcoding.configuration.SpringConfiguration 3 | import com.blogspot.toomuchcoding.person.domain.Person 4 | import org.springframework.beans.factory.annotation.Autowired 5 | import org.springframework.test.context.ContextConfiguration 6 | import spock.lang.Specification 7 | import spock.lang.Unroll 8 | 9 | import static org.hamcrest.CoreMatchers.notNullValue 10 | 11 | @ContextConfiguration(classes = [SpringConfiguration]) 12 | class PersonProcessingServiceContainerIntegrationSpec extends Specification { 13 | 14 | @Autowired 15 | PersonProcessingService personProcessingService 16 | 17 | def "should autowire container even though there are many implementations of service"(){ 18 | expect: 19 | personProcessingService instanceof PersonProcessingServiceContainer 20 | } 21 | 22 | def "the autowired container should not have itself in the list of autowired services"(){ 23 | expect: 24 | personProcessingService instanceof PersonProcessingServiceContainer 25 | and: 26 | !(personProcessingService as PersonProcessingServiceContainer).personProcessingServices.findResult { 27 | it instanceof PersonProcessingServiceContainer 28 | } 29 | } 30 | 31 | def "should not be applicable for processing if a person doesn't exist"(){ 32 | given: 33 | Person person = null 34 | expect: 35 | !personProcessingService.isApplicableFor(person) 36 | } 37 | 38 | def "should return an empty result for a person not applicable for anything"(){ 39 | given: 40 | Person person = new Person("", 17, false) 41 | when: 42 | def result = personProcessingService.process(person) 43 | then: 44 | result notNullValue() 45 | result.isEmpty() 46 | } 47 | 48 | @Unroll("For name [#name], age [#age] and being stupid [#stupid] the result should contain keywords #keywords") 49 | def "should perform different processing depending on input"(){ 50 | given: 51 | Person person = new Person(name, age, stupid) 52 | when: 53 | def result = personProcessingService.process(person) 54 | then: 55 | keywords.every { 56 | result.contains(it) 57 | } 58 | where: 59 | name | age | stupid || keywords 60 | "jan" | 20 | true || ['NAME', 'AGE', 'STUPID'] 61 | "" | 20 | true || ['AGE', 'STUPID'] 62 | "" | 20 | false || ['AGE'] 63 | null | 17 | true || ['STUPID'] 64 | "jan" | 17 | true || ['NAME'] 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Deep Stubs/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.blogspot.toomuchcoding 5 | mockito-deep_stubs 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 14 | 15 | spring-release 16 | http://maven.springframework.org/release 17 | 18 | 19 | maven-us-nuxeo 20 | https://maven-us.nuxeo.org/nexus/content/groups/public 21 | 22 | 23 | 24 | 25 | 26 | junit 27 | junit 28 | 4.10 29 | 30 | 31 | org.mockito 32 | mockito-all 33 | 1.9.5 34 | test 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.apache.maven.plugins 44 | maven-compiler-plugin 45 | 2.5.1 46 | 47 | 48 | 49 | 50 | 51 | org.codehaus.mojo 52 | jaxb2-maven-plugin 53 | 1.5 54 | 55 | 56 | xjc 57 | 58 | xjc 59 | 60 | 61 | 62 | 63 | com.blogspot.toomuchcoding.model 64 | ${project.basedir}/src/main/resources/xsd 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Deep Stubs/src/main/java/com/blogspot/toomuchcoding/service/PlayerService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.PlayerDetails; 4 | 5 | /** 6 | * User: mgrzejszczak 7 | * Date: 08.06.13 8 | * Time: 18:59 9 | */ 10 | public interface PlayerService { 11 | boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country); 12 | } 13 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Deep Stubs/src/main/java/com/blogspot/toomuchcoding/service/PlayerServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.PlayerDetails; 4 | 5 | /** 6 | * User: mgrzejszczak 7 | * Date: 08.06.13 8 | * Time: 19:02 9 | */ 10 | public class PlayerServiceImpl implements PlayerService { 11 | @Override 12 | public boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country) { 13 | String countryValue = playerDetails.getClubDetails().getCountry().getCountryCode().getCountryCode().value(); 14 | return countryValue.equalsIgnoreCase(country); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Deep Stubs/src/main/resources/xsd/Player.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Deep Stubs/src/test/java/com/blogspot/toomuchcoding/service/PlayerServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.*; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.junit.Assert.assertThat; 8 | import static org.mockito.Mockito.RETURNS_DEEP_STUBS; 9 | import static org.mockito.Mockito.mock; 10 | import static org.mockito.Mockito.when; 11 | 12 | /** 13 | * User: mgrzejszczak 14 | * Date: 08.06.13 15 | * Time: 19:26 16 | */ 17 | public class PlayerServiceImplTest { 18 | 19 | public static final String COUNTRY_CODE_ENG = "ENG"; 20 | PlayerServiceImpl objectUnderTest = new PlayerServiceImpl(); 21 | 22 | @Test 23 | public void shouldReturnTrueIfCountryCodeIsTheSame() throws Exception { 24 | //given 25 | PlayerDetails playerDetails = new PlayerDetails(); 26 | ClubDetails clubDetails = new ClubDetails(); 27 | CountryDetails countryDetails = new CountryDetails(); 28 | CountryCodeDetails countryCodeDetails = new CountryCodeDetails(); 29 | playerDetails.setClubDetails(clubDetails); 30 | clubDetails.setCountry(countryDetails); 31 | countryDetails.setCountryCode(countryCodeDetails); 32 | countryCodeDetails.setCountryCode(CountryCodeType.ENG); 33 | 34 | //when 35 | boolean playerOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG); 36 | 37 | //then 38 | assertThat(playerOfGivenCountry, is(true)); 39 | } 40 | 41 | @Test 42 | public void shouldReturnTrueIfCountryCodeIsTheSameUsingMockitoReturnDeepStubs() throws Exception { 43 | //given 44 | PlayerDetails playerDetailsMock = mock(PlayerDetails.class, RETURNS_DEEP_STUBS); 45 | CountryCodeType countryCodeType = CountryCodeType.ENG; 46 | when(playerDetailsMock.getClubDetails().getCountry().getCountryCode().getCountryCode()).thenReturn(countryCodeType); 47 | 48 | //when 49 | boolean playerOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetailsMock, COUNTRY_CODE_ENG); 50 | 51 | //then 52 | assertThat(playerOfGivenCountry, is(true)); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.blogspot.toomuchcoding 5 | mockito-injecting_test_doubles_in_spring 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 3.1.1.RELEASE 13 | 14 | 15 | 16 | spring-release 17 | http://maven.springframework.org/release 18 | 19 | 20 | maven-us-nuxeo 21 | https://maven-us.nuxeo.org/nexus/content/groups/public 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.springframework 29 | spring-core 30 | ${spring.version} 31 | 32 | 33 | org.springframework 34 | spring-context 35 | ${spring.version} 36 | 37 | 38 | org.springframework 39 | spring-test 40 | ${spring.version} 41 | 42 | 43 | org.slf4j 44 | slf4j-api 45 | 1.6.6 46 | 47 | 48 | org.slf4j 49 | slf4j-log4j12 50 | 1.6.6 51 | 52 | 53 | junit 54 | junit 55 | 4.10 56 | 57 | 58 | org.mockito 59 | mockito-all 60 | 1.9.5 61 | test 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-compiler-plugin 72 | 2.5.1 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/java/com/blogspot/toomuchcoding/model/Player.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.model; 2 | 3 | import java.math.BigDecimal; 4 | 5 | /** 6 | * User: mgrzejszczak 7 | * Date: 08.08.13 8 | * Time: 14:38 9 | */ 10 | public final class Player { 11 | private final String playerName; 12 | private final BigDecimal playerValue; 13 | 14 | public Player(final String playerName, final BigDecimal playerValue) { 15 | this.playerName = playerName; 16 | this.playerValue = playerValue; 17 | } 18 | 19 | public String getPlayerName() { 20 | return playerName; 21 | } 22 | 23 | public BigDecimal getPlayerValue() { 24 | return playerValue; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/java/com/blogspot/toomuchcoding/processor/AbstractBeanPostProcessor.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.processor; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.config.BeanPostProcessor; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.context.ApplicationContextAware; 7 | 8 | /** 9 | * Created with IntelliJ IDEA. 10 | * User: mgrzejszczak 11 | * Date: 26.03.13 12 | * Time: 13:14 13 | */ 14 | public abstract class AbstractBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { 15 | 16 | private Class clazz; 17 | protected ApplicationContext applicationContext; 18 | 19 | public AbstractBeanPostProcessor(Class clazz) { 20 | this.clazz = clazz; 21 | } 22 | 23 | @Override 24 | public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { 25 | checkConditions(); 26 | if (clazz.isAssignableFrom(bean.getClass())) { 27 | return doAfter((T) bean); 28 | } 29 | return bean; 30 | } 31 | 32 | @Override 33 | public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { 34 | checkConditions(); 35 | 36 | if (clazz.isAssignableFrom(bean.getClass())) { 37 | return doBefore((T) bean); 38 | } 39 | return bean; 40 | } 41 | 42 | private void checkConditions() { 43 | if (clazz == null) { 44 | throw new RuntimeException("Provide the interface for the post processor"); 45 | } 46 | } 47 | 48 | public abstract T doBefore(T bean); 49 | 50 | public abstract T doAfter(T bean); 51 | 52 | public void setApplicationContext(ApplicationContext applicationContext) { 53 | this.applicationContext = applicationContext; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/java/com/blogspot/toomuchcoding/service/PlayerService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | 5 | /** 6 | * User: mgrzejszczak 7 | * Date: 08.06.13 8 | * Time: 18:59 9 | */ 10 | public interface PlayerService { 11 | Player getPlayerByName(String playerName); 12 | } 13 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/java/com/blogspot/toomuchcoding/service/PlayerServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | /** 10 | * User: mgrzejszczak 11 | * Date: 08.06.13 12 | * Time: 19:02 13 | */ 14 | @Service 15 | public class PlayerServiceImpl implements PlayerService { 16 | private static final Logger LOGGER = LoggerFactory.getLogger(PlayerServiceImpl.class); 17 | 18 | @Autowired 19 | private PlayerWebService playerWebService; 20 | 21 | @Override 22 | public Player getPlayerByName(String playerName) { 23 | LOGGER.debug(String.format("Logging the player web service name [%s]", playerWebService.getWebServiceName())); 24 | return playerWebService.getPlayerByName(playerName); 25 | } 26 | 27 | public PlayerWebService getPlayerWebService() { 28 | return playerWebService; 29 | } 30 | 31 | public void setPlayerWebService(PlayerWebService playerWebService) { 32 | this.playerWebService = playerWebService; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/java/com/blogspot/toomuchcoding/service/PlayerWebService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | 5 | /** 6 | * User: mgrzejszczak 7 | * Date: 08.08.13 8 | * Time: 14:47 9 | */ 10 | public interface PlayerWebService { 11 | String getWebServiceName(); 12 | Player getPlayerByName(String name); 13 | } 14 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/java/com/blogspot/toomuchcoding/service/PlayerWebServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.math.BigDecimal; 9 | 10 | /** 11 | * User: mgrzejszczak 12 | * Date: 08.08.13 13 | * Time: 14:48 14 | */ 15 | @Service 16 | public class PlayerWebServiceImpl implements PlayerWebService { 17 | private static final Logger LOGGER = LoggerFactory.getLogger(PlayerWebServiceImpl.class); 18 | public static final String WEB_SERVICE_NAME = "SuperPlayerWebService"; 19 | public static final String SAMPLE_PLAYER_VALUE = "1000"; 20 | 21 | @Override 22 | public String getWebServiceName() { 23 | return WEB_SERVICE_NAME; 24 | } 25 | 26 | @Override 27 | public Player getPlayerByName(String name) { 28 | try { 29 | LOGGER.debug("Simulating awaiting time for a response from a web service"); 30 | Thread.sleep(5000); 31 | } catch (InterruptedException e) { 32 | LOGGER.error(String.format("[%s] occurred while trying to make the thread sleep", e)); 33 | } 34 | return new Player(name, new BigDecimal(SAMPLE_PLAYER_VALUE)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/test/java/com/blogspot/postprocessor/PlayerWebServicePostProcessor.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.postprocessor; 2 | 3 | 4 | import com.blogspot.toomuchcoding.processor.AbstractBeanPostProcessor; 5 | import com.blogspot.toomuchcoding.service.PlayerWebService; 6 | import org.springframework.stereotype.Component; 7 | 8 | import static org.mockito.Mockito.spy; 9 | 10 | /** 11 | * User: mgrzejszczak 12 | * Date: 07.05.13 13 | * Time: 11:30 14 | */ 15 | public class PlayerWebServicePostProcessor extends AbstractBeanPostProcessor { 16 | public PlayerWebServicePostProcessor() { 17 | super(PlayerWebService.class); 18 | } 19 | 20 | @Override 21 | public PlayerWebService doBefore(PlayerWebService bean) { 22 | return spy(bean); 23 | } 24 | 25 | @Override 26 | public PlayerWebService doAfter(PlayerWebService bean) { 27 | return bean; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/test/java/com/blogspot/toomuchcoding/service/PlayerServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | import java.math.BigDecimal; 11 | 12 | import static org.hamcrest.CoreMatchers.is; 13 | import static org.junit.Assert.assertThat; 14 | import static org.mockito.BDDMockito.doReturn; 15 | import static org.mockito.Mockito.verify; 16 | 17 | /** 18 | * User: mgrzejszczak 19 | * Date: 08.06.13 20 | * Time: 19:26 21 | */ 22 | @RunWith(SpringJUnit4ClassRunner.class) 23 | @ContextConfiguration("classpath:testApplicationContext.xml") 24 | public class PlayerServiceImplTest { 25 | 26 | public static final String PLAYER_NAME = "Lewandowski"; 27 | public static final BigDecimal PLAYER_VALUE = new BigDecimal("35000000"); 28 | 29 | @Autowired 30 | PlayerWebService playerWebServiceSpy; 31 | 32 | @Autowired 33 | PlayerService objectUnderTest; 34 | 35 | @Test 36 | public void shouldReturnAPlayerFromPlayerWebService(){ 37 | //given 38 | Player referencePlayer = new Player(PLAYER_NAME, PLAYER_VALUE); 39 | doReturn(referencePlayer).when(playerWebServiceSpy).getPlayerByName(PLAYER_NAME); 40 | 41 | //when 42 | Player player = objectUnderTest.getPlayerByName(PLAYER_NAME); 43 | 44 | //then 45 | assertThat(player, is(referencePlayer)); 46 | verify(playerWebServiceSpy).getWebServiceName(); 47 | assertThat(playerWebServiceSpy.getWebServiceName(), is(PlayerWebServiceImpl.WEB_SERVICE_NAME)); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - Injecting Test Doubles in Spring/src/test/resources/testApplicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.blogspot.toomuchcoding 5 | mockito-interfaces 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 13 | 14 | 15 | spring-release 16 | http://maven.springframework.org/release 17 | 18 | 19 | maven-us-nuxeo 20 | https://maven-us.nuxeo.org/nexus/content/groups/public 21 | 22 | 23 | 24 | 25 | 26 | junit 27 | junit 28 | 4.10 29 | 30 | 31 | org.mockito 32 | mockito-all 33 | 1.9.5 34 | test 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-compiler-plugin 44 | 2.5.1 45 | 46 | 47 | 48 | 49 | 50 | org.codehaus.mojo 51 | jaxb2-maven-plugin 52 | 1.5 53 | 54 | 55 | xjc 56 | 57 | xjc 58 | 59 | 60 | 61 | 62 | com.blogspot.toomuchcoding.model 63 | ${project.basedir}/src/main/resources/xsd 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/adapter/CommonPlayerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.adapter; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | import com.blogspot.toomuchcoding.model.PlayerDetails; 5 | 6 | /** 7 | * User: mgrzejszczak 8 | * Date: 09.06.13 9 | * Time: 15:42 10 | */ 11 | public class CommonPlayerAdapter implements Player { 12 | private final PlayerDetails playerDetails; 13 | 14 | public CommonPlayerAdapter(PlayerDetails playerDetails){ 15 | this.playerDetails = playerDetails; 16 | } 17 | 18 | @Override 19 | public void run() { 20 | System.out.printf("Run %s. Run!%n", playerDetails.getName()); 21 | } 22 | 23 | public PlayerDetails getPlayerDetails() { 24 | return playerDetails; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/adapter/DefencePlayerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.adapter; 2 | 3 | import com.blogspot.toomuchcoding.model.DJ; 4 | import com.blogspot.toomuchcoding.model.DefensivePlayer; 5 | import com.blogspot.toomuchcoding.model.JavaDeveloper; 6 | import com.blogspot.toomuchcoding.model.PlayerDetails; 7 | 8 | /** 9 | * User: mgrzejszczak 10 | * Date: 09.06.13 11 | * Time: 15:42 12 | */ 13 | public class DefencePlayerAdapter extends CommonPlayerAdapter implements DefensivePlayer, DJ, JavaDeveloper { 14 | 15 | public DefencePlayerAdapter(PlayerDetails playerDetails){ 16 | super(playerDetails); 17 | } 18 | 19 | @Override 20 | public void defend(){ 21 | System.out.printf("Defence! %s. Defence!%n", getPlayerDetails().getName()); 22 | } 23 | 24 | @Override 25 | public void playSomeMusic() { 26 | System.out.println("Oops I did it again...!"); 27 | } 28 | 29 | @Override 30 | public void doSomeSeriousCoding() { 31 | System.out.println("System.out.println(\"Hello world\");"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/adapter/OffensivePlayerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.adapter; 2 | 3 | import com.blogspot.toomuchcoding.model.OffensivePlayer; 4 | import com.blogspot.toomuchcoding.model.PlayerDetails; 5 | 6 | /** 7 | * User: mgrzejszczak 8 | * Date: 09.06.13 9 | * Time: 15:42 10 | */ 11 | public class OffensivePlayerAdapter extends CommonPlayerAdapter implements OffensivePlayer { 12 | 13 | public OffensivePlayerAdapter(PlayerDetails playerDetails){ 14 | super(playerDetails); 15 | } 16 | 17 | @Override 18 | public void shoot(){ 19 | System.out.printf("%s Shooooot!.%n", getPlayerDetails().getName()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/factory/PlayerFactory.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.factory; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | import com.blogspot.toomuchcoding.model.PositionType; 5 | 6 | /** 7 | * User: mgrzejszczak 8 | * Date: 28.05.13 9 | * Time: 23:16 10 | */ 11 | public interface PlayerFactory { 12 | Player createPlayer(PositionType positionType); 13 | } 14 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/factory/PlayerFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.factory; 2 | 3 | import com.blogspot.toomuchcoding.adapter.CommonPlayerAdapter; 4 | import com.blogspot.toomuchcoding.adapter.DefencePlayerAdapter; 5 | import com.blogspot.toomuchcoding.adapter.OffensivePlayerAdapter; 6 | import com.blogspot.toomuchcoding.model.Player; 7 | import com.blogspot.toomuchcoding.model.PlayerDetails; 8 | import com.blogspot.toomuchcoding.model.PositionType; 9 | 10 | /** 11 | * User: mgrzejszczak 12 | * Date: 09.06.13 13 | * Time: 15:53 14 | */ 15 | 16 | public class PlayerFactoryImpl implements PlayerFactory { 17 | 18 | @Override 19 | public Player createPlayer(PositionType positionType) { 20 | PlayerDetails player = createCommonPlayer(positionType); 21 | switch (positionType){ 22 | case ATT: 23 | return new OffensivePlayerAdapter(player); 24 | case MID: 25 | return new OffensivePlayerAdapter(player); 26 | case DEF: 27 | return new DefencePlayerAdapter(player); 28 | case GK: 29 | return new DefencePlayerAdapter(player); 30 | default: 31 | return new CommonPlayerAdapter(player); 32 | } 33 | } 34 | 35 | private PlayerDetails createCommonPlayer(PositionType positionType){ 36 | PlayerDetails playerDetails = new PlayerDetails(); 37 | playerDetails.setPosition(positionType); 38 | return playerDetails; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/factory/PlayerFactoryImplWithFieldSettingLogic.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.factory; 2 | 3 | import com.blogspot.toomuchcoding.adapter.CommonPlayerAdapter; 4 | import com.blogspot.toomuchcoding.adapter.DefencePlayerAdapter; 5 | import com.blogspot.toomuchcoding.adapter.OffensivePlayerAdapter; 6 | import com.blogspot.toomuchcoding.model.*; 7 | 8 | /** 9 | * User: mgrzejszczak 10 | * Date: 09.06.13 11 | * Time: 15:53 12 | */ 13 | 14 | public class PlayerFactoryImplWithFieldSettingLogic implements PlayerFactory { 15 | 16 | @Override 17 | public Player createPlayer(PositionType positionType) { 18 | PlayerDetails player = createCommonPlayer(positionType); 19 | switch (positionType){ 20 | case ATT: 21 | return createOffensivePlayer(player); 22 | case MID: 23 | return createOffensivePlayer(player); 24 | case DEF: 25 | return createDefensivePlayer(player); 26 | case GK: 27 | return createDefensivePlayer(player); 28 | default: 29 | return new CommonPlayerAdapter(player); 30 | } 31 | } 32 | 33 | private Player createDefensivePlayer(PlayerDetails player) { 34 | DefencePlayerAdapter defencePlayerAdapter = new DefencePlayerAdapter(player); 35 | defencePlayerAdapter.defend(); 36 | defencePlayerAdapter.playSomeMusic(); 37 | defencePlayerAdapter.doSomeSeriousCoding(); 38 | return defencePlayerAdapter; 39 | } 40 | 41 | private OffensivePlayer createOffensivePlayer(PlayerDetails player) { 42 | OffensivePlayer offensivePlayer = new OffensivePlayerAdapter(player); 43 | offensivePlayer.shoot(); 44 | return offensivePlayer; 45 | } 46 | 47 | private PlayerDetails createCommonPlayer(PositionType positionType){ 48 | PlayerDetails playerDetails = new PlayerDetails(); 49 | playerDetails.setPosition(positionType); 50 | return playerDetails; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/DJ.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.model; 2 | 3 | /** 4 | * User: mgrzejszczak 5 | * Date: 09.06.13 6 | * Time: 22:19 7 | */ 8 | public interface DJ { 9 | void playSomeMusic(); 10 | } 11 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/DefensivePlayer.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.model; 2 | 3 | /** 4 | * User: mgrzejszczak 5 | * Date: 09.06.13 6 | * Time: 15:44 7 | */ 8 | public interface DefensivePlayer extends Player { 9 | void defend(); 10 | } 11 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/JavaDeveloper.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.model; 2 | 3 | /** 4 | * User: mgrzejszczak 5 | * Date: 09.06.13 6 | * Time: 22:20 7 | */ 8 | public interface JavaDeveloper { 9 | void doSomeSeriousCoding(); 10 | } 11 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/OffensivePlayer.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.model; 2 | 3 | /** 4 | * User: mgrzejszczak 5 | * Date: 09.06.13 6 | * Time: 15:44 7 | */ 8 | public interface OffensivePlayer extends Player { 9 | void shoot(); 10 | } 11 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/Player.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.model; 2 | 3 | /** 4 | * User: mgrzejszczak 5 | * Date: 09.06.13 6 | * Time: 15:44 7 | */ 8 | public interface Player { 9 | void run(); 10 | } 11 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/service/PlayerService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.Player; 4 | import com.blogspot.toomuchcoding.model.PositionType; 5 | 6 | /** 7 | * User: mgrzejszczak 8 | * Date: 08.06.13 9 | * Time: 18:59 10 | */ 11 | public interface PlayerService { 12 | Player playAGameWithAPlayerOfPosition(PositionType positionType); 13 | } 14 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/service/PlayerServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.factory.PlayerFactory; 4 | import com.blogspot.toomuchcoding.model.*; 5 | 6 | /** 7 | * User: mgrzejszczak 8 | * Date: 08.06.13 9 | * Time: 19:02 10 | */ 11 | public class PlayerServiceImpl implements PlayerService { 12 | 13 | private PlayerFactory playerFactory; 14 | 15 | @Override 16 | public Player playAGameWithAPlayerOfPosition(PositionType positionType) { 17 | Player player = playerFactory.createPlayer(positionType); 18 | player.run(); 19 | performAdditionalActions(player); 20 | return player; 21 | } 22 | 23 | private void performAdditionalActions(Player player) { 24 | if(player instanceof OffensivePlayer){ 25 | OffensivePlayer offensivePlayer = (OffensivePlayer) player; 26 | performAdditionalActionsForTheOffensivePlayer(offensivePlayer); 27 | }else if(player instanceof DefensivePlayer){ 28 | DefensivePlayer defensivePlayer = (DefensivePlayer) player; 29 | performAdditionalActionsForTheDefensivePlayer(defensivePlayer); 30 | } 31 | } 32 | 33 | private void performAdditionalActionsForTheOffensivePlayer(OffensivePlayer offensivePlayer){ 34 | offensivePlayer.shoot(); 35 | } 36 | 37 | private void performAdditionalActionsForTheDefensivePlayer(DefensivePlayer defensivePlayer){ 38 | defensivePlayer.defend(); 39 | try{ 40 | DJ dj = (DJ)defensivePlayer; 41 | dj.playSomeMusic(); 42 | JavaDeveloper javaDeveloper = (JavaDeveloper)defensivePlayer; 43 | javaDeveloper.doSomeSeriousCoding(); 44 | }catch(ClassCastException exception){ 45 | System.err.println("Sorry, I can't do more than just play football..."); 46 | } 47 | } 48 | 49 | public PlayerFactory getPlayerFactory() { 50 | return playerFactory; 51 | } 52 | 53 | public void setPlayerFactory(PlayerFactory playerFactory) { 54 | this.playerFactory = playerFactory; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/service/PlayerServiceImplWithoutUnnecessaryLogic.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.factory.PlayerFactory; 4 | import com.blogspot.toomuchcoding.model.*; 5 | 6 | /** 7 | * User: mgrzejszczak 8 | * Date: 08.06.13 9 | * Time: 19:02 10 | */ 11 | public class PlayerServiceImplWithoutUnnecessaryLogic implements PlayerService { 12 | 13 | private PlayerFactory playerFactory; 14 | 15 | /** 16 | * What's the point in having this method then? 17 | * @param positionType 18 | * @return 19 | */ 20 | @Override 21 | public Player playAGameWithAPlayerOfPosition(PositionType positionType) { 22 | return playerFactory.createPlayer(positionType); 23 | } 24 | 25 | public PlayerFactory getPlayerFactory() { 26 | return playerFactory; 27 | } 28 | 29 | public void setPlayerFactory(PlayerFactory playerFactory) { 30 | this.playerFactory = playerFactory; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Unit Testing/Mockito - With Interfaces/src/main/resources/xsd/Player.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Unit Testing/Spock - Deep Stubs/src/main/java/com/blogspot/toomuchcoding/service/PlayerService.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.PlayerDetails; 4 | 5 | /** 6 | * User: mgrzejszczak 7 | * Date: 08.06.13 8 | * Time: 18:59 9 | */ 10 | public interface PlayerService { 11 | boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country); 12 | } 13 | -------------------------------------------------------------------------------- /Unit Testing/Spock - Deep Stubs/src/main/java/com/blogspot/toomuchcoding/service/PlayerServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.service; 2 | 3 | import com.blogspot.toomuchcoding.model.PlayerDetails; 4 | 5 | /** 6 | * User: mgrzejszczak 7 | * Date: 08.06.13 8 | * Time: 19:02 9 | */ 10 | public class PlayerServiceImpl implements PlayerService { 11 | @Override 12 | public boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country) { 13 | String countryValue = playerDetails.getClubDetails().getCountry().getCountryCode().getCountryCode().value(); 14 | return countryValue.equalsIgnoreCase(country); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Unit Testing/Spock - Deep Stubs/src/main/resources/xsd/Player.xsd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Unit Testing/Spock - Deep Stubs/src/test/groovy/com/blogspot/toomuchcoding/spock/ExtendedSpockSpecification.groovy: -------------------------------------------------------------------------------- 1 | package com.blogspot.toomuchcoding.spock; 2 | 3 | import spock.lang.Specification 4 | 5 | /** 6 | * Created with IntelliJ IDEA. 7 | * User: MGrzejszczak 8 | * Date: 14.06.13 9 | * Time: 15:26 10 | */ 11 | abstract class ExtendedSpockSpecification extends Specification { 12 | /** 13 | * The method creates nested structure of spies for all the elements present in the property parameter. Those spies are set on the input object. 14 | * 15 | * @param object - object on which you want to create nested spies 16 | * @param property - field accessors delimited by a dot - JavaBean convention 17 | * @return Spy of the last object from the property path 18 | */ 19 | protected def createNestedSpies(object, String property) { 20 | def lastObject = object 21 | property.tokenize('.').inject object, { obj, prop -> 22 | if (obj[prop] == null) { 23 | def foundProp = obj.metaClass.properties.find { it.name == prop } 24 | obj[prop] = Spy(foundProp.type) 25 | } 26 | lastObject = obj[prop] 27 | } 28 | lastObject 29 | } 30 | 31 | /** 32 | * The method creates nested structure of mocks for all the elements present in the property parameter. Those mocks are set on the input object. 33 | * 34 | * @param object - object on which you want to create nested mocks 35 | * @param property - field accessors delimited by a dot - JavaBean convention 36 | * @return Mock of the last object from the property path 37 | */ 38 | protected def createNestedMocks(object, String property) { 39 | def lastObject = object 40 | property.tokenize('.').inject object, { obj, prop -> 41 | def foundProp = obj.metaClass.properties.find { it.name == prop } 42 | def mockedProp = Mock(foundProp.type) 43 | lastObject."${prop}" >> mockedProp 44 | lastObject = mockedProp 45 | } 46 | lastObject 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Unit_Testing_-_Matchers/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | pl.grzejszczak.marcin 5 | unittesting-matchers 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | UTF-8 10 | 1.6 11 | 1.6 12 | 1.2.0 13 | 14 | 15 | 16 | 17 | junit 18 | junit 19 | 4.11 20 | test 21 | 22 | 23 | org.mockito 24 | mockito-all 25 | 1.9.5 26 | test 27 | 28 | 29 | org.slf4j 30 | slf4j-log4j12 31 | 1.5.2 32 | 33 | 34 | com.google.guava 35 | guava 36 | 17.0 37 | 38 | 39 | commons-lang 40 | commons-lang 41 | 2.6 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Unit_Testing_-_Matchers/src/main/java/pl/grzejszczak/marcin/junit/matchers/MatcherMain.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit.matchers; 2 | 3 | import pl.grzejszczak.marcin.junit.matchers.builder.SomeBigPojoBuilder; 4 | import pl.grzejszczak.marcin.junit.matchers.service.SomeService; 5 | 6 | public class MatcherMain { 7 | public static void main(String[] args){ 8 | SomeService someService = new SomeService(); 9 | 10 | someService.setSomeBigPojoBuilder(new SomeBigPojoBuilder()); 11 | someService.someLogicForAPojoWithoutBuilder(); 12 | 13 | someService.setSomeBigPojoBuilder(new SomeBigPojoBuilder()); 14 | someService.someLogicForAPojoWithBuilder(); 15 | 16 | someService.setSomeBigPojoBuilder(new SomeBigPojoBuilder()); 17 | someService.someLogicForAPojoWithBuilderBadArgument(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Unit_Testing_-_Matchers/src/main/java/pl/grzejszczak/marcin/junit/matchers/pojo/SomePojoConstants.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit.matchers.pojo; 2 | 3 | public interface SomePojoConstants { 4 | int STRING_FIELD_0_LENGTH = 1; 5 | int STRING_FIELD_1_LENGTH = 2; 6 | int STRING_FIELD_2_LENGTH = 3; 7 | int STRING_FIELD_3_LENGTH = 4; 8 | int STRING_FIELD_4_LENGTH = 5; 9 | int STRING_FIELD_5_LENGTH = 6; 10 | int STRING_FIELD_6_LENGTH = 7; 11 | int STRING_FIELD_7_LENGTH = 8; 12 | int STRING_FIELD_8_LENGTH = 9; 13 | int STRING_FIELD_9_LENGTH = 10; 14 | } 15 | -------------------------------------------------------------------------------- /Unit_Testing_-_Matchers/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Unit_Testing_-_Matchers/src/test/java/pl/grzejszczak/marcin/junit/matchers/builder/SomeBigPojoBuilderNoMatchersAndNoRefactoringTest.java: -------------------------------------------------------------------------------- 1 | package pl.grzejszczak.marcin.junit.matchers.builder; 2 | 3 | import static junit.framework.Assert.*; 4 | import static org.apache.commons.lang.StringUtils.*; 5 | import static pl.grzejszczak.marcin.junit.matchers.pojo.SomePojoConstants.*; 6 | 7 | import org.junit.Test; 8 | 9 | import junit.framework.AssertionFailedError; 10 | import pl.grzejszczak.marcin.junit.matchers.pojo.SomeBigPojo; 11 | 12 | public class SomeBigPojoBuilderNoMatchersAndNoRefactoringTest { 13 | 14 | private SomeBigPojoBuilder systemUnderTest = new SomeBigPojoBuilder(); 15 | 16 | @Test 17 | public void testCreateSomeBigPojoWithBuilder() throws Exception { 18 | SomeBigPojo someBigPojo = systemUnderTest 19 | .setBooleanField1(true) 20 | .setStringField0("1") 21 | .setStringField1("12") 22 | .setStringField2("123") 23 | .setStringField3("1234") 24 | .setStringField4("12345") 25 | .setStringField5("123456") 26 | .setStringField6("1234567") 27 | .setStringField7("12345678") 28 | .setStringField8("123456789") 29 | .setStringField9("1234567890") 30 | .createSomeBigPojoWithBuilder(); 31 | 32 | isPojoProperlyBuilt(someBigPojo); 33 | } 34 | 35 | @Test 36 | public void should_fail_to_build_pojo() { 37 | // given 38 | SomeBigPojo someBigPojo = systemUnderTest 39 | .setStringField0("0") 40 | .setStringField1("too long") 41 | .createSomeBigPojoWithBuilder(); 42 | 43 | // expect 44 | try { 45 | isPojoProperlyBuilt(someBigPojo); 46 | fail(); 47 | } catch (AssertionFailedError error) {} 48 | } 49 | 50 | /** 51 | * Of course don't use assertTrue - use assertThat 52 | */ 53 | private void isPojoProperlyBuilt(SomeBigPojo someBigPojo) { 54 | assertTrue(someBigPojo.getStringField0().length() == STRING_FIELD_0_LENGTH); 55 | assertTrue(isNumeric(someBigPojo.getStringField0())); 56 | 57 | assertTrue(someBigPojo.getStringField1().length() == STRING_FIELD_1_LENGTH); 58 | assertTrue(isNumeric(someBigPojo.getStringField0())); 59 | 60 | assertTrue(someBigPojo.getStringField2().length() == STRING_FIELD_2_LENGTH); 61 | assertTrue(isNumeric(someBigPojo.getStringField0())); 62 | 63 | assertTrue(someBigPojo.getStringField3().length() == STRING_FIELD_3_LENGTH); 64 | assertTrue(isNumeric(someBigPojo.getStringField0())); 65 | 66 | assertTrue(someBigPojo.getStringField4().length() == STRING_FIELD_4_LENGTH); 67 | assertTrue(isNumeric(someBigPojo.getStringField0())); 68 | 69 | assertTrue(someBigPojo.getStringField5().length() == STRING_FIELD_5_LENGTH); 70 | 71 | assertTrue(someBigPojo.getStringField6().length() == STRING_FIELD_6_LENGTH); 72 | 73 | assertTrue(someBigPojo.getStringField7().length() == STRING_FIELD_7_LENGTH); 74 | 75 | assertTrue(someBigPojo.getStringField8().length() == STRING_FIELD_8_LENGTH); 76 | 77 | assertTrue(someBigPojo.getStringField9().length() == STRING_FIELD_9_LENGTH); 78 | 79 | } 80 | } 81 | --------------------------------------------------------------------------------