Merge branch 'masterEugen'
This commit is contained in:
@@ -42,3 +42,9 @@ spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
|
|||||||
*.springBeans
|
*.springBeans
|
||||||
|
|
||||||
20171220-JMeter.csv
|
20171220-JMeter.csv
|
||||||
|
|
||||||
|
.factorypath
|
||||||
|
dependency-reduced-pom.xml
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|||||||
+19
-1
@@ -14,6 +14,24 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<version>1.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
@@ -42,4 +60,4 @@
|
|||||||
<weld-se-core.version>2.4.1.Final</weld-se-core.version>
|
<weld-se-core.version>2.4.1.Final</weld-se-core.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -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);
|
||||||
|
}
|
||||||
+27
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+42
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -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 {}
|
||||||
+12
@@ -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 {}
|
||||||
+12
@@ -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,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,70 @@
|
|||||||
|
package com.baeldung.test.dependencyinjection;
|
||||||
|
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor;
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor;
|
||||||
|
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
|
||||||
|
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
|
||||||
|
import com.baeldung.dependencyinjection.loggers.TimeLogger;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.jboss.weld.environment.se.Weld;
|
||||||
|
import org.jboss.weld.environment.se.WeldContainer;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ImageProcessorUnitTest {
|
||||||
|
|
||||||
|
private static ImageFileProcessor imageFileProcessor;
|
||||||
|
private static SimpleDateFormat dateFormat;
|
||||||
|
private static Calendar calendar;
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setImageProcessorInstance() {
|
||||||
|
Weld weld = new Weld();
|
||||||
|
WeldContainer container = weld.initialize();
|
||||||
|
imageFileProcessor = container.select(ImageFileProcessor.class).get();
|
||||||
|
container.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setSimpleDateFormatInstance() {
|
||||||
|
dateFormat = new SimpleDateFormat("HH:mm");
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setCalendarInstance() {
|
||||||
|
calendar = Calendar.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() {
|
||||||
|
assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class);
|
||||||
|
assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() {
|
||||||
|
String currentTime = dateFormat.format(calendar.getTime());
|
||||||
|
assertThat(imageFileProcessor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png at: " + currentTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -220,6 +220,7 @@
|
|||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*LiveTest.java</exclude>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
<exclude>**/*IntegrationTest.java</exclude>
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*IntTest.java</exclude>
|
||||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||||
<exclude>**/*ManualTest.java</exclude>
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
@@ -289,6 +290,7 @@
|
|||||||
</excludes>
|
</excludes>
|
||||||
<includes>
|
<includes>
|
||||||
<include>**/*IntegrationTest.java</include>
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
<include>**/*IntTest.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
|
|||||||
@@ -353,6 +353,7 @@
|
|||||||
</excludes>
|
</excludes>
|
||||||
<includes>
|
<includes>
|
||||||
<include>**/*IntegrationTest.java</include>
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
<include>**/*IntTest.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
|
|||||||
@@ -222,6 +222,7 @@
|
|||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*LiveTest.java</exclude>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
<exclude>**/*IntegrationTest.java</exclude>
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*IntTest.java</exclude>
|
||||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||||
<exclude>**/*ManualTest.java</exclude>
|
<exclude>**/*ManualTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
@@ -387,6 +388,7 @@
|
|||||||
</excludes>
|
</excludes>
|
||||||
<includes>
|
<includes>
|
||||||
<include>**/*IntegrationTest.java</include>
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
<include>**/*IntTest.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.immutableobjects;
|
||||||
|
|
||||||
|
public final class Currency {
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
private Currency(String currencyValue) {
|
||||||
|
value = currencyValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Currency of(String value) {
|
||||||
|
return new Currency(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.immutableobjects;
|
||||||
|
|
||||||
|
// 4. Immutability in Java
|
||||||
|
public final class Money {
|
||||||
|
private final double amount;
|
||||||
|
private final Currency currency;
|
||||||
|
|
||||||
|
public Money(double amount, Currency currency) {
|
||||||
|
this.amount = amount;
|
||||||
|
this.currency = currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Currency getCurrency() {
|
||||||
|
return currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getAmount() {
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,8 +19,7 @@ public class ExtensionUnitTest {
|
|||||||
public void getExtension_whenStringHandle_thenExtensionIsTrue() {
|
public void getExtension_whenStringHandle_thenExtensionIsTrue() {
|
||||||
String expectedExtension = "java";
|
String expectedExtension = "java";
|
||||||
Optional<String> actualExtension = extension.getExtensionByStringHandling("Demo.java");
|
Optional<String> actualExtension = extension.getExtensionByStringHandling("Demo.java");
|
||||||
Assert.assertTrue(actualExtension.isPresent());
|
Assert.assertEquals(expectedExtension, actualExtension.get());
|
||||||
actualExtension.ifPresent(ext -> Assert.assertEquals(expectedExtension,ext));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.immutableobjects;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ImmutableObjectsUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCallingStringReplace_thenStringDoesNotMutate() {
|
||||||
|
// 2. What's an Immutable Object?
|
||||||
|
final String name = "baeldung";
|
||||||
|
final String newName = name.replace("dung", "----");
|
||||||
|
|
||||||
|
assertEquals("baeldung", name);
|
||||||
|
assertEquals("bael----", newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void whenReassignFinalValue_thenCompilerError() {
|
||||||
|
// 3. The final Keyword in Java (1)
|
||||||
|
final String name = "baeldung";
|
||||||
|
// name = "bael...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddingElementToList_thenSizeChange() {
|
||||||
|
// 3. The final Keyword in Java (2)
|
||||||
|
final List<String> strings = new ArrayList<>();
|
||||||
|
assertEquals(0, strings.size());
|
||||||
|
strings.add("baeldung");
|
||||||
|
assertEquals(1, strings.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -11,6 +11,7 @@ public class UnitTestNamingConventionRule extends AbstractJavaRule {
|
|||||||
|
|
||||||
private static List<String> allowedEndings = Arrays.asList(
|
private static List<String> allowedEndings = Arrays.asList(
|
||||||
"IntegrationTest",
|
"IntegrationTest",
|
||||||
|
"IntTest",
|
||||||
"ManualTest",
|
"ManualTest",
|
||||||
"JdbcTest",
|
"JdbcTest",
|
||||||
"LiveTest",
|
"LiveTest",
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
|
import javax.servlet.annotation.*;
|
||||||
|
import javax.servlet.http.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static javax.servlet.RequestDispatcher.*;
|
||||||
|
|
||||||
|
@WebServlet(urlPatterns = "/errorHandler")
|
||||||
|
public class ErrorHandlerServlet extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||||
|
throws IOException {
|
||||||
|
resp.setContentType("text/html; charset=utf-8");
|
||||||
|
try (PrintWriter writer = resp.getWriter()) {
|
||||||
|
writer.write("<html><head><title>Error description</title></head><body>");
|
||||||
|
writer.write("<h2>Error description</h2>");
|
||||||
|
writer.write("<ul>");
|
||||||
|
Arrays.asList(ERROR_STATUS_CODE, ERROR_EXCEPTION_TYPE, ERROR_MESSAGE)
|
||||||
|
.forEach(e ->
|
||||||
|
writer.write("<li>" + e + ":" + req.getAttribute(e) + " </li>")
|
||||||
|
);
|
||||||
|
writer.write("</ul>");
|
||||||
|
writer.write("</html></body>");
|
||||||
|
}
|
||||||
|
|
||||||
|
Exception exception = (Exception) req.getAttribute(ERROR_EXCEPTION);
|
||||||
|
if (IllegalArgumentException.class.isInstance(exception)) {
|
||||||
|
getServletContext().log("Error on an application argument", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
|
import javax.servlet.annotation.*;
|
||||||
|
import javax.servlet.http.*;
|
||||||
|
|
||||||
|
@WebServlet(urlPatterns = "/randomError")
|
||||||
|
public class RandomErrorServlet extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, final HttpServletResponse resp) {
|
||||||
|
throw new IllegalStateException("Random error");
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user