|
1 | 1 | package com.baeldung.test.dependencyinjection; |
2 | 2 |
|
3 | | -import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor; |
4 | | -import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor; |
5 | | -import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor; |
6 | | -import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor; |
7 | | -import com.baeldung.dependencyinjection.loggers.TimeLogger; |
8 | | -import java.text.SimpleDateFormat; |
9 | | -import java.util.Calendar; |
10 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.within; |
| 5 | + |
| 6 | +import java.text.ParseException; |
| 7 | +import java.time.LocalTime; |
| 8 | +import java.time.temporal.ChronoUnit; |
| 9 | + |
11 | 10 | import org.jboss.weld.environment.se.Weld; |
12 | 11 | import org.jboss.weld.environment.se.WeldContainer; |
13 | 12 | import org.junit.BeforeClass; |
14 | 13 | import org.junit.Test; |
15 | 14 |
|
| 15 | +import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor; |
| 16 | +import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor; |
| 17 | +import com.baeldung.dependencyinjection.loggers.TimeLogger; |
| 18 | + |
16 | 19 | public class ImageProcessorUnitTest { |
17 | | - |
| 20 | + |
18 | 21 | private static ImageFileProcessor imageFileProcessor; |
19 | | - private static SimpleDateFormat dateFormat; |
20 | | - private static Calendar calendar; |
21 | | - |
22 | | - |
| 22 | + |
23 | 23 | @BeforeClass |
24 | 24 | public static void setImageProcessorInstance() { |
25 | 25 | Weld weld = new Weld(); |
26 | 26 | WeldContainer container = weld.initialize(); |
27 | | - imageFileProcessor = container.select(ImageFileProcessor.class).get(); |
| 27 | + imageFileProcessor = container.select(ImageFileProcessor.class) |
| 28 | + .get(); |
28 | 29 | container.shutdown(); |
29 | 30 | } |
30 | | - |
31 | | - @BeforeClass |
32 | | - public static void setSimpleDateFormatInstance() { |
33 | | - dateFormat = new SimpleDateFormat("HH:mm"); |
34 | | - } |
35 | | - |
36 | | - @BeforeClass |
37 | | - public static void setCalendarInstance() { |
38 | | - calendar = Calendar.getInstance(); |
39 | | - } |
40 | | - |
| 31 | + |
41 | 32 | @Test |
42 | 33 | public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() { |
43 | 34 | assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class); |
44 | 35 | assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class); |
45 | 36 | } |
46 | | - |
| 37 | + |
47 | 38 | @Test |
48 | | - public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() { |
49 | | - String currentTime = dateFormat.format(calendar.getTime()); |
50 | | - assertThat(imageFileProcessor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png at: " + currentTime); |
| 39 | + public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() throws ParseException { |
| 40 | + LocalTime currentTime = LocalTime.now(); |
| 41 | + |
| 42 | + String openFileLog = imageFileProcessor.openFile("file1.png"); |
| 43 | + assertThat(openFileLog).contains("Opening PNG file file1.png at: "); |
| 44 | + |
| 45 | + LocalTime loggedTime = getLoggedTime(openFileLog); |
| 46 | + assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); |
51 | 47 | } |
52 | | - |
| 48 | + |
53 | 49 | @Test |
54 | | - public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() { |
55 | | - String currentTime = dateFormat.format(calendar.getTime()); |
56 | | - assertThat(imageFileProcessor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png at: " + currentTime); |
| 50 | + public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() throws ParseException { |
| 51 | + LocalTime currentTime = LocalTime.now(); |
| 52 | + |
| 53 | + String editFileLog = imageFileProcessor.editFile("file1.png"); |
| 54 | + assertThat(editFileLog).contains("Editing PNG file file1.png at: "); |
| 55 | + |
| 56 | + LocalTime loggedTime = getLoggedTime(editFileLog); |
| 57 | + assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); |
57 | 58 | } |
58 | | - |
| 59 | + |
59 | 60 | @Test |
60 | | - public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() { |
61 | | - String currentTime = dateFormat.format(calendar.getTime()); |
62 | | - assertThat(imageFileProcessor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png at: " + currentTime); |
| 61 | + public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() throws ParseException { |
| 62 | + LocalTime currentTime = LocalTime.now(); |
| 63 | + |
| 64 | + String writeFileLog = imageFileProcessor.writeFile("file1.png"); |
| 65 | + assertThat(writeFileLog).contains("Writing PNG file file1.png at: "); |
| 66 | + |
| 67 | + LocalTime loggedTime = getLoggedTime(writeFileLog); |
| 68 | + assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); |
63 | 69 | } |
64 | | - |
| 70 | + |
65 | 71 | @Test |
66 | | - public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() { |
67 | | - String currentTime = dateFormat.format(calendar.getTime()); |
68 | | - assertThat(imageFileProcessor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png at: " + currentTime); |
| 72 | + public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() throws ParseException { |
| 73 | + LocalTime currentTime = LocalTime.now(); |
| 74 | + |
| 75 | + String saveFileLog = imageFileProcessor.saveFile("file1.png"); |
| 76 | + assertThat(saveFileLog).contains("Saving PNG file file1.png at: "); |
| 77 | + |
| 78 | + LocalTime loggedTime = getLoggedTime(saveFileLog); |
| 79 | + assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); |
| 80 | + } |
| 81 | + |
| 82 | + private LocalTime getLoggedTime(String log) throws ParseException { |
| 83 | + String logTimeString = log.split("at: ")[1]; |
| 84 | + |
| 85 | + int hour = Integer.valueOf(logTimeString.split(":")[0]); |
| 86 | + int minutes = Integer.valueOf(logTimeString.split(":")[1]); |
| 87 | + |
| 88 | + LocalTime loggedTime = LocalTime.of(hour, minutes); |
| 89 | + return loggedTime; |
69 | 90 | } |
70 | 91 | } |
0 commit comments