11package com .baeldung .test .dependencyinjection ;
22
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 ;
103import 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+
1110import org .jboss .weld .environment .se .Weld ;
1211import org .jboss .weld .environment .se .WeldContainer ;
1312import org .junit .BeforeClass ;
1413import org .junit .Test ;
1514
15+ import com .baeldung .dependencyinjection .imagefileeditors .PngFileEditor ;
16+ import com .baeldung .dependencyinjection .imageprocessors .ImageFileProcessor ;
17+ import com .baeldung .dependencyinjection .loggers .TimeLogger ;
18+
1619public class ImageProcessorUnitTest {
17-
20+
1821 private static ImageFileProcessor imageFileProcessor ;
19- private static SimpleDateFormat dateFormat ;
20- private static Calendar calendar ;
21-
22-
22+
2323 @ BeforeClass
2424 public static void setImageProcessorInstance () {
2525 Weld weld = new Weld ();
2626 WeldContainer container = weld .initialize ();
27- imageFileProcessor = container .select (ImageFileProcessor .class ).get ();
27+ imageFileProcessor = container .select (ImageFileProcessor .class )
28+ .get ();
2829 container .shutdown ();
2930 }
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+
4132 @ Test
4233 public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions () {
4334 assertThat (imageFileProcessor .getImageFileditor ()).isInstanceOf (PngFileEditor .class );
4435 assertThat (imageFileProcessor .getTimeLogger ()).isInstanceOf (TimeLogger .class );
4536 }
46-
37+
4738 @ 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 ));
5147 }
52-
48+
5349 @ 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 ));
5758 }
58-
59+
5960 @ 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 ));
6369 }
64-
70+
6571 @ 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 ;
6990 }
7091}
0 commit comments