|
1 | 1 | /*
|
2 |
| - * Copyright 2008-2022 the original author or authors. |
| 2 | + * Copyright 2008-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import org.hamcrest.Matchers;
|
20 | 20 | import org.junit.jupiter.api.BeforeEach;
|
21 | 21 | import org.junit.jupiter.api.Test;
|
| 22 | + |
22 | 23 | import org.springframework.batch.item.ExecutionContext;
|
23 | 24 | import org.springframework.batch.item.ItemCountAware;
|
24 | 25 | import org.springframework.batch.item.ItemStreamException;
|
|
36 | 37 | import javax.xml.namespace.QName;
|
37 | 38 | import javax.xml.stream.FactoryConfigurationError;
|
38 | 39 | import javax.xml.stream.XMLEventReader;
|
| 40 | +import javax.xml.stream.XMLInputFactory; |
39 | 41 | import javax.xml.stream.XMLStreamException;
|
40 | 42 | import javax.xml.stream.events.EndElement;
|
| 43 | +import javax.xml.stream.events.StartDocument; |
41 | 44 | import javax.xml.stream.events.StartElement;
|
42 | 45 | import javax.xml.stream.events.XMLEvent;
|
43 | 46 | import javax.xml.transform.Source;
|
|
58 | 61 | import static org.junit.jupiter.api.Assertions.assertSame;
|
59 | 62 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
60 | 63 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
| 64 | +import static org.mockito.Mockito.mock; |
| 65 | +import static org.mockito.Mockito.verify; |
| 66 | +import static org.mockito.Mockito.when; |
61 | 67 |
|
62 | 68 | /**
|
63 | 69 | * Tests for {@link StaxEventItemReader}.
|
@@ -159,6 +165,37 @@ void testCustomEncoding() throws Exception {
|
159 | 165 | source.close();
|
160 | 166 | }
|
161 | 167 |
|
| 168 | + @Test |
| 169 | + void testNullEncoding() throws Exception { |
| 170 | + // given |
| 171 | + XMLEventReader eventReader = mock(XMLEventReader.class); |
| 172 | + when(eventReader.peek()).thenReturn(mock(StartDocument.class)); |
| 173 | + |
| 174 | + Resource resource = mock(Resource.class); |
| 175 | + InputStream inputStream = mock(InputStream.class); |
| 176 | + when(resource.getInputStream()).thenReturn(inputStream); |
| 177 | + when(resource.isReadable()).thenReturn(true); |
| 178 | + when(resource.exists()).thenReturn(true); |
| 179 | + XMLInputFactory xmlInputFactory = mock(XMLInputFactory.class); |
| 180 | + when(xmlInputFactory.createXMLEventReader(inputStream)).thenReturn(eventReader); |
| 181 | + |
| 182 | + StaxEventItemReader<Object> reader = new StaxEventItemReader<>(); |
| 183 | + reader.setUnmarshaller(new MockFragmentUnmarshaller()); |
| 184 | + reader.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT); |
| 185 | + reader.setResource(resource); |
| 186 | + reader.setEncoding(null); |
| 187 | + reader.setStrict(false); |
| 188 | + reader.setXmlInputFactory(xmlInputFactory); |
| 189 | + reader.afterPropertiesSet(); |
| 190 | + |
| 191 | + // when |
| 192 | + reader.open(new ExecutionContext()); |
| 193 | + |
| 194 | + // then |
| 195 | + verify(xmlInputFactory).createXMLEventReader(inputStream); |
| 196 | + reader.close(); |
| 197 | + } |
| 198 | + |
162 | 199 | @Test
|
163 | 200 | void testItemCountAwareFragment() throws Exception {
|
164 | 201 | StaxEventItemReader<ItemCountAwareFragment> source = createNewItemCountAwareInputSource();
|
|
0 commit comments