JAVA-15787 Created new di-modules and server-modules
- Moved spring-freemarker to spring-web-modules
This commit is contained in:
9
di-modules/cdi/README.md
Normal file
9
di-modules/cdi/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
## CDI
|
||||
|
||||
This module contains articles about Contexts and Dependency Injection (CDI)
|
||||
|
||||
### Relevant Articles:
|
||||
- [CDI Interceptor vs Spring AspectJ](https://www.baeldung.com/cdi-interceptor-vs-spring-aspectj)
|
||||
- [An Introduction to CDI (Contexts and Dependency Injection) in Java](https://www.baeldung.com/java-ee-cdi)
|
||||
- [Introduction to the Event Notification Model in CDI 2.0](https://www.baeldung.com/cdi-event-notification)
|
||||
|
||||
51
di-modules/cdi/pom.xml
Normal file
51
di-modules/cdi/pom.xml
Normal file
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cdi</artifactId>
|
||||
<name>cdi</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
<artifactId>cdi-api</artifactId>
|
||||
<version>${cdi-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jboss.weld.se</groupId>
|
||||
<artifactId>weld-se-core</artifactId>
|
||||
<version>${weld-se-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectjweaver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<cdi-api.version>2.0.SP1</cdi-api.version>
|
||||
<weld-se-core.version>3.0.5.Final</weld-se-core.version>
|
||||
<aspectjweaver.version>1.9.2</aspectjweaver.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.cdi2observers.application;
|
||||
|
||||
import com.baeldung.cdi2observers.events.ExampleEvent;
|
||||
|
||||
import javax.enterprise.inject.se.SeContainer;
|
||||
import javax.enterprise.inject.se.SeContainerInitializer;
|
||||
|
||||
public class BootstrappingApplication {
|
||||
|
||||
public static void main(String... args) {
|
||||
SeContainerInitializer containerInitializer = SeContainerInitializer.newInstance();
|
||||
try (SeContainer container = containerInitializer.initialize()) {
|
||||
container.getBeanManager().fireEvent(new ExampleEvent("Welcome to Baeldung!"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.cdi2observers.events;
|
||||
|
||||
public class ExampleEvent {
|
||||
|
||||
private final String eventMessage;
|
||||
|
||||
public ExampleEvent(String eventMessage) {
|
||||
this.eventMessage = eventMessage;
|
||||
}
|
||||
|
||||
public String getEventMessage() {
|
||||
return eventMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.cdi2observers.events;
|
||||
|
||||
import javax.enterprise.event.Event;
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class ExampleEventSource {
|
||||
|
||||
@Inject
|
||||
Event<ExampleEvent> exampleEvent;
|
||||
|
||||
public void fireEvent() {
|
||||
exampleEvent.fireAsync(new ExampleEvent("Welcome to Baeldung!"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.cdi2observers.observers;
|
||||
|
||||
import com.baeldung.cdi2observers.events.ExampleEvent;
|
||||
|
||||
import javax.annotation.Priority;
|
||||
import javax.enterprise.event.Observes;
|
||||
|
||||
public class AnotherExampleEventObserver {
|
||||
|
||||
public String onEvent(@Observes @Priority(2) ExampleEvent event) {
|
||||
return event.getEventMessage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.cdi2observers.observers;
|
||||
|
||||
import com.baeldung.cdi2observers.events.ExampleEvent;
|
||||
import com.baeldung.cdi2observers.services.TextService;
|
||||
import javax.annotation.Priority;
|
||||
import javax.enterprise.event.Observes;
|
||||
|
||||
public class ExampleEventObserver {
|
||||
|
||||
public String onEvent(@Observes @Priority(1) ExampleEvent event, TextService textService) {
|
||||
return textService.parseText(event.getEventMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.cdi2observers.services;
|
||||
|
||||
public class TextService {
|
||||
|
||||
public String parseText(String text) {
|
||||
return text.toUpperCase();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.dependencyinjection.application;
|
||||
|
||||
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
|
||||
import org.jboss.weld.environment.se.Weld;
|
||||
import org.jboss.weld.environment.se.WeldContainer;
|
||||
|
||||
public class FileApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Weld weld = new Weld();
|
||||
WeldContainer container = weld.initialize();
|
||||
ImageFileProcessor imageFileProcessor = container.select(ImageFileProcessor.class).get();
|
||||
System.out.println(imageFileProcessor.openFile("file1.png"));
|
||||
System.out.println(imageFileProcessor.writeFile("file1.png"));
|
||||
System.out.println(imageFileProcessor.saveFile("file1.png"));
|
||||
container.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.dependencyinjection.factories;
|
||||
|
||||
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import javax.enterprise.inject.Produces;
|
||||
|
||||
public class TimeLoggerFactory {
|
||||
|
||||
@Produces
|
||||
public TimeLogger getTimeLogger() {
|
||||
return new TimeLogger(new SimpleDateFormat("HH:mm"), Calendar.getInstance());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||
|
||||
import com.baeldung.dependencyinjection.qualifiers.GifFileEditorQualifier;
|
||||
|
||||
@GifFileEditorQualifier
|
||||
public class GifFileEditor implements ImageFileEditor {
|
||||
|
||||
@Override
|
||||
public String openFile(String fileName) {
|
||||
return "Opening GIF file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String editFile(String fileName) {
|
||||
return "Editing GIF file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String writeFile(String fileName) {
|
||||
return "Writing GIF file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String saveFile(String fileName) {
|
||||
return "Saving GIF file " + fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||
|
||||
public interface ImageFileEditor {
|
||||
|
||||
String openFile(String fileName);
|
||||
|
||||
String editFile(String fileName);
|
||||
|
||||
String writeFile(String fileName);
|
||||
|
||||
String saveFile(String fileName);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||
|
||||
import com.baeldung.dependencyinjection.qualifiers.JpgFileEditorQualifier;
|
||||
|
||||
@JpgFileEditorQualifier
|
||||
public class JpgFileEditor implements ImageFileEditor {
|
||||
|
||||
@Override
|
||||
public String openFile(String fileName) {
|
||||
return "Opening JPG file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String editFile(String fileName) {
|
||||
return "Editing JPG file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String writeFile(String fileName) {
|
||||
return "Writing JPG file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String saveFile(String fileName) {
|
||||
return "Saving JPG file " + fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.dependencyinjection.imagefileeditors;
|
||||
|
||||
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
|
||||
|
||||
@PngFileEditorQualifier
|
||||
public class PngFileEditor implements ImageFileEditor {
|
||||
|
||||
@Override
|
||||
public String openFile(String fileName) {
|
||||
return "Opening PNG file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String editFile(String fileName) {
|
||||
return "Editing PNG file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String writeFile(String fileName) {
|
||||
return "Writing PNG file " + fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String saveFile(String fileName) {
|
||||
return "Saving PNG file " + fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.dependencyinjection.imageprocessors;
|
||||
|
||||
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
|
||||
import javax.inject.Inject;
|
||||
import com.baeldung.dependencyinjection.imagefileeditors.ImageFileEditor;
|
||||
|
||||
public class ImageFileProcessor {
|
||||
|
||||
private final ImageFileEditor imageFileEditor;
|
||||
private final TimeLogger timeLogger;
|
||||
|
||||
@Inject
|
||||
public ImageFileProcessor(@PngFileEditorQualifier ImageFileEditor imageFileEditor, TimeLogger timeLogger) {
|
||||
this.imageFileEditor = imageFileEditor;
|
||||
this.timeLogger = timeLogger;
|
||||
}
|
||||
|
||||
public ImageFileEditor getImageFileditor() {
|
||||
return imageFileEditor;
|
||||
}
|
||||
|
||||
public TimeLogger getTimeLogger() {
|
||||
return timeLogger;
|
||||
}
|
||||
|
||||
public String openFile(String fileName) {
|
||||
return imageFileEditor.openFile(fileName) + " at: " + timeLogger.getTime();
|
||||
}
|
||||
|
||||
public String editFile(String fileName) {
|
||||
return imageFileEditor.editFile(fileName) + " at: " + timeLogger.getTime();
|
||||
}
|
||||
|
||||
public String writeFile(String fileName) {
|
||||
return imageFileEditor.writeFile(fileName) + " at: " + timeLogger.getTime();
|
||||
}
|
||||
|
||||
public String saveFile(String fileName) {
|
||||
return imageFileEditor.saveFile(fileName)+ " at: " + timeLogger.getTime();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.dependencyinjection.loggers;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class TimeLogger {
|
||||
|
||||
private final SimpleDateFormat dateFormat;
|
||||
private final Calendar calendar;
|
||||
|
||||
public TimeLogger(SimpleDateFormat dateFormat, Calendar calendar) {
|
||||
this.dateFormat = dateFormat;
|
||||
this.calendar = calendar;
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return dateFormat.format(calendar.getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.dependencyinjection.qualifiers;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
|
||||
public @interface GifFileEditorQualifier {}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.dependencyinjection.qualifiers;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
|
||||
public @interface JpgFileEditorQualifier {}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.dependencyinjection.qualifiers;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
|
||||
public @interface PngFileEditorQualifier {}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.interceptor;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.interceptor.InterceptorBinding;
|
||||
|
||||
@InterceptorBinding
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Audited {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.interceptor;
|
||||
|
||||
import javax.interceptor.AroundInvoke;
|
||||
import javax.interceptor.Interceptor;
|
||||
import javax.interceptor.InvocationContext;
|
||||
|
||||
@Audited
|
||||
@Interceptor
|
||||
public class AuditedInterceptor {
|
||||
public static boolean calledBefore = false;
|
||||
public static boolean calledAfter = false;
|
||||
|
||||
@AroundInvoke
|
||||
public Object auditMethod(InvocationContext ctx) throws Exception {
|
||||
calledBefore = true;
|
||||
Object result = ctx.proceed();
|
||||
calledAfter = true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.service;
|
||||
|
||||
import com.baeldung.interceptor.Audited;
|
||||
|
||||
public class SuperService {
|
||||
@Audited
|
||||
public String deliverService(String uid) {
|
||||
return uid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.spring.aspect;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@Aspect
|
||||
public class SpringTestAspect {
|
||||
@Autowired
|
||||
private List<String> accumulator;
|
||||
|
||||
@Around("execution(* com.baeldung.spring.service.SpringSuperService.*(..))")
|
||||
public Object auditMethod(ProceedingJoinPoint jp) throws Throwable {
|
||||
String methodName = jp.getSignature().getName();
|
||||
accumulator.add("Call to " + methodName);
|
||||
Object obj = jp.proceed();
|
||||
accumulator.add("Method called successfully: " + methodName);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.spring.configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
import com.baeldung.spring.aspect.SpringTestAspect;
|
||||
import com.baeldung.spring.service.SpringSuperService;
|
||||
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy
|
||||
public class AppConfig {
|
||||
@Bean
|
||||
public SpringSuperService springSuperService() {
|
||||
return new SpringSuperService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringTestAspect springTestAspect() {
|
||||
return new SpringTestAspect();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public List<String> getAccumulator() {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.spring.service;
|
||||
|
||||
public class SpringSuperService {
|
||||
public String getInfoFromService(String code) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
9
di-modules/cdi/src/main/resources/META-INF/beans.xml
Normal file
9
di-modules/cdi/src/main/resources/META-INF/beans.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
|
||||
bean-discovery-mode="all">
|
||||
<interceptors>
|
||||
<class>com.baeldung.interceptor.AuditedInterceptor</class>
|
||||
</interceptors>
|
||||
</beans>
|
||||
13
di-modules/cdi/src/main/resources/logback.xml
Normal file
13
di-modules/cdi/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import org.jboss.weld.environment.se.Weld;
|
||||
import org.jboss.weld.environment.se.WeldContainer;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.interceptor.AuditedInterceptor;
|
||||
import com.baeldung.service.SuperService;
|
||||
|
||||
public class InterceptorIntegrationTest {
|
||||
Weld weld;
|
||||
WeldContainer container;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
weld = new Weld();
|
||||
container = weld.initialize();
|
||||
}
|
||||
|
||||
@After
|
||||
public void shutdown() {
|
||||
weld.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheService_whenMethodAndInterceptorExecuted_thenOK() {
|
||||
SuperService superService = container.select(SuperService.class).get();
|
||||
String code = "123456";
|
||||
superService.deliverService(code);
|
||||
|
||||
Assert.assertTrue(AuditedInterceptor.calledBefore);
|
||||
Assert.assertTrue(AuditedInterceptor.calledAfter);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.spring.configuration.AppConfig;
|
||||
import com.baeldung.spring.service.SpringSuperService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { AppConfig.class })
|
||||
public class SpringInterceptorIntegrationTest {
|
||||
@Autowired
|
||||
SpringSuperService springSuperService;
|
||||
|
||||
@Autowired
|
||||
private List<String> accumulator;
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
public void givenService_whenServiceAndAspectExecuted_thenOk() {
|
||||
String code = "123456";
|
||||
String result = springSuperService.getInfoFromService(code);
|
||||
|
||||
Assert.assertThat(accumulator.size(), is(2));
|
||||
Assert.assertThat(accumulator.get(0), is("Call to getInfoFromService"));
|
||||
Assert.assertThat(accumulator.get(1), is("Method called successfully: getInfoFromService"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.test.cdi2observers.tests;
|
||||
|
||||
import com.baeldung.cdi2observers.services.TextService;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TextServiceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTextServiceInstance_whenCalledparseText_thenCorrect() {
|
||||
TextService textService = new TextService();
|
||||
assertThat(textService.parseText("Baeldung")).isEqualTo("BAELDUNG");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.test.dependencyinjection;
|
||||
|
||||
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GifFileEditorUnitTest {
|
||||
|
||||
private static GifFileEditor gifFileEditor;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setGifFileEditorInstance() {
|
||||
gifFileEditor = new GifFileEditor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGifFileEditorlInstance_whenCalledopenFile_thenOneAssertion() {
|
||||
assertThat(gifFileEditor.openFile("file1.gif")).isEqualTo("Opening GIF file file1.gif");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGifFileEditorlInstance_whenCallededitFile_thenOneAssertion() {
|
||||
assertThat(gifFileEditor.editFile("file1.gif")).isEqualTo("Editing GIF file file1.gif");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGifFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||
assertThat(gifFileEditor.writeFile("file1.gif")).isEqualTo("Writing GIF file file1.gif");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGifFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||
assertThat(gifFileEditor.saveFile("file1.gif")).isEqualTo("Saving GIF file file1.gif");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.test.dependencyinjection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import org.jboss.weld.environment.se.Weld;
|
||||
import org.jboss.weld.environment.se.WeldContainer;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
|
||||
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
|
||||
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||
|
||||
public class ImageProcessorUnitTest {
|
||||
|
||||
private static ImageFileProcessor imageFileProcessor;
|
||||
|
||||
@BeforeClass
|
||||
public static void setImageProcessorInstance() {
|
||||
Weld weld = new Weld();
|
||||
WeldContainer container = weld.initialize();
|
||||
imageFileProcessor = container.select(ImageFileProcessor.class)
|
||||
.get();
|
||||
container.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() {
|
||||
assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class);
|
||||
assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() throws ParseException {
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
|
||||
String openFileLog = imageFileProcessor.openFile("file1.png");
|
||||
assertThat(openFileLog).contains("Opening PNG file file1.png at: ");
|
||||
|
||||
LocalTime loggedTime = getLoggedTime(openFileLog);
|
||||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() throws ParseException {
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
|
||||
String editFileLog = imageFileProcessor.editFile("file1.png");
|
||||
assertThat(editFileLog).contains("Editing PNG file file1.png at: ");
|
||||
|
||||
LocalTime loggedTime = getLoggedTime(editFileLog);
|
||||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() throws ParseException {
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
|
||||
String writeFileLog = imageFileProcessor.writeFile("file1.png");
|
||||
assertThat(writeFileLog).contains("Writing PNG file file1.png at: ");
|
||||
|
||||
LocalTime loggedTime = getLoggedTime(writeFileLog);
|
||||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() throws ParseException {
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
|
||||
String saveFileLog = imageFileProcessor.saveFile("file1.png");
|
||||
assertThat(saveFileLog).contains("Saving PNG file file1.png at: ");
|
||||
|
||||
LocalTime loggedTime = getLoggedTime(saveFileLog);
|
||||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
|
||||
}
|
||||
|
||||
private LocalTime getLoggedTime(String log) throws ParseException {
|
||||
String logTimeString = log.split("at: ")[1];
|
||||
|
||||
int hour = Integer.valueOf(logTimeString.split(":")[0]);
|
||||
int minutes = Integer.valueOf(logTimeString.split(":")[1]);
|
||||
|
||||
LocalTime loggedTime = LocalTime.of(hour, minutes);
|
||||
return loggedTime;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.test.dependencyinjection;
|
||||
|
||||
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JpgFileEditorUnitTest {
|
||||
|
||||
private static JpgFileEditor jpgFileUtil;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setJpgFileEditorInstance() {
|
||||
jpgFileUtil = new JpgFileEditor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJpgFileEditorInstance_whenCalledopenFile_thenOneAssertion() {
|
||||
assertThat(jpgFileUtil.openFile("file1.jpg")).isEqualTo("Opening JPG file file1.jpg");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJpgFileEditorlInstance_whenCallededitFile_thenOneAssertion() {
|
||||
assertThat(jpgFileUtil.editFile("file1.gif")).isEqualTo("Editing JPG file file1.gif");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJpgFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||
assertThat(jpgFileUtil.writeFile("file1.jpg")).isEqualTo("Writing JPG file file1.jpg");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJpgFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||
assertThat(jpgFileUtil.saveFile("file1.jpg")).isEqualTo("Saving JPG file file1.jpg");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.test.dependencyinjection;
|
||||
|
||||
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
|
||||
import com.baeldung.dependencyinjection.qualifiers.PngFileEditorQualifier;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
@PngFileEditorQualifier
|
||||
public class PngFileEditorUnitTest {
|
||||
|
||||
private static PngFileEditor pngFileEditor;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setPngFileEditorInstance() {
|
||||
pngFileEditor = new PngFileEditor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPngFileEditorInstance_whenCalledopenFile_thenOneAssertion() {
|
||||
assertThat(pngFileEditor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPngFileEditorInstance_whenCallededitFile_thenOneAssertion() {
|
||||
assertThat(pngFileEditor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPngFileEditorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||
assertThat(pngFileEditor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPngFileEditorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||
assertThat(pngFileEditor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.test.dependencyinjection;
|
||||
|
||||
import com.baeldung.dependencyinjection.factories.TimeLoggerFactory;
|
||||
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TimeLoggerFactoryUnitTest {
|
||||
@Test
|
||||
public void givenTimeLoggerFactory_whenCalledgetTimeLogger_thenOneAssertion() {
|
||||
TimeLoggerFactory timeLoggerFactory = new TimeLoggerFactory();
|
||||
assertThat(timeLoggerFactory.getTimeLogger()).isInstanceOf(TimeLogger.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.test.dependencyinjection;
|
||||
|
||||
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TimeLoggerUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTimeLoggerInstance_whenCalledgetLogTime_thenOneAssertion() {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
TimeLogger timeLogger = new TimeLogger(dateFormat, calendar);
|
||||
String currentTime = dateFormat.format(calendar.getTime());
|
||||
assertThat(timeLogger.getTime()).isEqualTo(currentTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user