2525import org .mockito .Mock ;
2626import org .mockito .Mockito ;
2727import org .mockito .junit .jupiter .MockitoExtension ;
28+ import tools .jackson .databind .json .JsonMapper ;
2829
2930import org .springframework .batch .infrastructure .item .Chunk ;
3031import org .springframework .batch .infrastructure .item .ExecutionContext ;
31- import org .springframework .batch .infrastructure .item .json .JsonFileItemWriter ;
32- import org .springframework .batch .infrastructure .item .json .JsonObjectMarshaller ;
3332import org .springframework .core .io .FileSystemResource ;
3433import org .springframework .core .io .WritableResource ;
3534
36- import static org .junit .jupiter .api .Assertions .assertThrows ;
35+ import static org .junit .jupiter .api .Assertions .* ;
3736
3837/**
3938 * @author Mahmoud Ben Hassine
39+ * @author Yanming Zhou
4040 */
4141@ ExtendWith (MockitoExtension .class )
4242class JsonFileItemWriterTests {
@@ -49,6 +49,7 @@ class JsonFileItemWriterTests {
4949 @ BeforeEach
5050 void setUp () throws Exception {
5151 File file = Files .createTempFile ("test" , "json" ).toFile ();
52+ file .deleteOnExit ();
5253 this .resource = new FileSystemResource (file );
5354 }
5455
@@ -72,4 +73,73 @@ void itemsShouldBeMarshalledToJsonWithTheJsonObjectMarshaller() throws Exception
7273 Mockito .verify (this .jsonObjectMarshaller ).marshal ("bar" );
7374 }
7475
76+ @ Test
77+ void appendAllowed () throws Exception {
78+ JsonFileItemWriter <String > writer = new JsonFileItemWriter <>(this .resource ,
79+ new JacksonJsonObjectMarshaller <>());
80+ writer .setAppendAllowed (true );
81+
82+ writer .open (new ExecutionContext ());
83+ writer .close ();
84+
85+ resourceShouldContains ();
86+
87+ writer .open (new ExecutionContext ());
88+ writer .write (Chunk .of ("aaa" ));
89+ writer .write (Chunk .of ("bbb" ));
90+ writer .close ();
91+
92+ resourceShouldContains ("aaa" , "bbb" );
93+
94+ writer .open (new ExecutionContext ());
95+ writer .close ();
96+
97+ resourceShouldContains ("aaa" , "bbb" );
98+
99+ writer .open (new ExecutionContext ());
100+ writer .write (Chunk .of ("ccc" ));
101+ writer .close ();
102+
103+ resourceShouldContains ("aaa" , "bbb" , "ccc" );
104+ }
105+
106+ @ Test
107+ void appendNotAllowed () throws Exception {
108+ JsonFileItemWriter <String > writer = new JsonFileItemWriter <>(this .resource ,
109+ new JacksonJsonObjectMarshaller <>());
110+
111+ writer .open (new ExecutionContext ());
112+ writer .close ();
113+
114+ resourceShouldContains ();
115+
116+ writer .open (new ExecutionContext ());
117+ writer .write (Chunk .of ("aaa" ));
118+ writer .write (Chunk .of ("bbb" ));
119+ writer .close ();
120+
121+ resourceShouldContains ("aaa" , "bbb" );
122+
123+ writer .open (new ExecutionContext ());
124+ writer .close ();
125+
126+ resourceShouldContains ();
127+
128+ writer .open (new ExecutionContext ());
129+ writer .write (Chunk .of ("ccc" ));
130+ writer .close ();
131+
132+ resourceShouldContains ("ccc" );
133+
134+ writer .open (new ExecutionContext ());
135+ writer .write (Chunk .of ("ddd" ));
136+ writer .close ();
137+
138+ resourceShouldContains ("ddd" );
139+ }
140+
141+ private void resourceShouldContains (String ... array ) throws Exception {
142+ assertArrayEquals (array , new JsonMapper ().readValue (this .resource .getContentAsByteArray (), String [].class ));
143+ }
144+
75145}
0 commit comments