BAEL-8506 Test in CDI project failing

- Fixed assertion of local time logic
This commit is contained in:
Dhawal Kapil 2018-08-30 23:33:42 +05:30
parent dc8676630d
commit db704cbe4d
1 changed files with 61 additions and 40 deletions

View File

@ -1,70 +1,91 @@
package com.baeldung.test.dependencyinjection; 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 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.Weld;
import org.jboss.weld.environment.se.WeldContainer; import org.jboss.weld.environment.se.WeldContainer;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; 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 { public class ImageProcessorUnitTest {
private static ImageFileProcessor imageFileProcessor; private static ImageFileProcessor imageFileProcessor;
private static SimpleDateFormat dateFormat;
private static Calendar calendar;
@BeforeClass @BeforeClass
public static void setImageProcessorInstance() { public static void setImageProcessorInstance() {
Weld weld = new Weld(); Weld weld = new Weld();
WeldContainer container = weld.initialize(); WeldContainer container = weld.initialize();
imageFileProcessor = container.select(ImageFileProcessor.class).get(); imageFileProcessor = container.select(ImageFileProcessor.class)
.get();
container.shutdown(); container.shutdown();
} }
@BeforeClass
public static void setSimpleDateFormatInstance() {
dateFormat = new SimpleDateFormat("HH:mm");
}
@BeforeClass
public static void setCalendarInstance() {
calendar = Calendar.getInstance();
}
@Test @Test
public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() { public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() {
assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class); assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class);
assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class); assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class);
} }
@Test @Test
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() { public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() throws ParseException {
String currentTime = dateFormat.format(calendar.getTime()); LocalTime currentTime = LocalTime.now();
assertThat(imageFileProcessor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png at: " + currentTime);
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 @Test
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() { public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() throws ParseException {
String currentTime = dateFormat.format(calendar.getTime()); LocalTime currentTime = LocalTime.now();
assertThat(imageFileProcessor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png at: " + currentTime);
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 @Test
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() { public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() throws ParseException {
String currentTime = dateFormat.format(calendar.getTime()); LocalTime currentTime = LocalTime.now();
assertThat(imageFileProcessor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png at: " + currentTime);
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 @Test
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() { public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() throws ParseException {
String currentTime = dateFormat.format(calendar.getTime()); LocalTime currentTime = LocalTime.now();
assertThat(imageFileProcessor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png at: " + currentTime);
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;
} }
} }