|
16 | 16 | */ |
17 | 17 | package org.apache.camel.converter.jaxb; |
18 | 18 |
|
| 19 | +import java.io.ByteArrayInputStream; |
19 | 20 | import java.io.ByteArrayOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
20 | 23 | import java.io.OutputStream; |
21 | 24 |
|
| 25 | +import javax.xml.bind.JAXBContext; |
22 | 26 | import javax.xml.bind.JAXBException; |
23 | 27 | import javax.xml.bind.Marshaller; |
| 28 | +import javax.xml.bind.Unmarshaller; |
24 | 29 | import javax.xml.stream.XMLStreamException; |
25 | 30 |
|
26 | 31 | import org.apache.camel.Exchange; |
27 | | -import org.easymock.classextension.EasyMockSupport; |
28 | 32 | import org.junit.Before; |
29 | 33 | import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.mockito.Mock; |
| 36 | +import org.mockito.runners.MockitoJUnitRunner; |
30 | 37 |
|
31 | | -import static org.easymock.EasyMock.expect; |
32 | | -import static org.easymock.EasyMock.isA; |
33 | | -import static org.easymock.EasyMock.same; |
| 38 | + |
| 39 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 40 | +import static org.hamcrest.CoreMatchers.not; |
34 | 41 | import static org.junit.Assert.assertFalse; |
35 | 42 | import static org.junit.Assert.assertTrue; |
36 | | - |
37 | | -public class JaxbDataFormatTest extends EasyMockSupport { |
| 43 | +import static org.mockito.Matchers.any; |
| 44 | +import static org.mockito.Matchers.anyObject; |
| 45 | +import static org.mockito.Matchers.anyString; |
| 46 | +import static org.mockito.Matchers.argThat; |
| 47 | +import static org.mockito.Matchers.isA; |
| 48 | +import static org.mockito.Matchers.same; |
| 49 | +import static org.mockito.Mockito.doCallRealMethod; |
| 50 | +import static org.mockito.Mockito.verify; |
| 51 | +import static org.mockito.Mockito.when; |
| 52 | + |
| 53 | +@RunWith(MockitoJUnitRunner.class) |
| 54 | +public class JaxbDataFormatTest { |
38 | 55 | private JaxbDataFormat jaxbDataFormat; |
| 56 | + @Mock |
39 | 57 | private Exchange exchangeMock; |
| 58 | + @Mock |
40 | 59 | private Marshaller marshallerMock; |
| 60 | + @Mock |
| 61 | + private JaxbDataFormat jaxbDataFormatMock; |
| 62 | + @Mock |
| 63 | + private JAXBContext jaxbContextMock; |
| 64 | + @Mock |
| 65 | + private Unmarshaller unmarshallerMock; |
| 66 | + |
41 | 67 |
|
42 | 68 | @Before |
43 | 69 | public void setUp() { |
44 | 70 | jaxbDataFormat = new JaxbDataFormat(); |
45 | | - marshallerMock = createStrictMock(Marshaller.class); |
46 | | - exchangeMock = createStrictMock(Exchange.class); |
47 | 71 | } |
48 | 72 |
|
| 73 | + @SuppressWarnings("unchecked") |
49 | 74 | @Test |
50 | | - public void testNeedFiltering() { |
| 75 | + public void testNeedFilteringDisabledFiltering() { |
51 | 76 | // tests combinations of data format option and exchange property |
52 | | - expect( |
53 | | - exchangeMock.getProperty(Exchange.FILTER_NON_XML_CHARS, false, |
54 | | - Boolean.class)).andReturn(false); |
55 | | - replayAll(); |
| 77 | + when(exchangeMock.getProperty(anyString(), anyObject(), any(Class.class))) |
| 78 | + .thenReturn(false); |
56 | 79 |
|
57 | | - assertFalse("Not expected filtering here", jaxbDataFormat.needFiltering(exchangeMock)); |
58 | | - verifyAll(); |
59 | | - resetAll(); |
| 80 | + jaxbDataFormat.needFiltering(exchangeMock); |
| 81 | + verify(exchangeMock).getProperty(Exchange.FILTER_NON_XML_CHARS, false, Boolean.class); |
| 82 | + } |
60 | 83 |
|
61 | | - expect( |
62 | | - exchangeMock.getProperty(Exchange.FILTER_NON_XML_CHARS, false, |
63 | | - Boolean.class)).andReturn(true); |
64 | | - replayAll(); |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + @Test |
| 86 | + public void testNeedFilteringEnabledFiltering() { |
| 87 | + when(exchangeMock.getProperty(anyString(), anyObject(), any(Class.class))).thenReturn(true); |
| 88 | + jaxbDataFormat.setFilterNonXmlChars(true); |
| 89 | + jaxbDataFormat.needFiltering(exchangeMock); |
| 90 | + verify(exchangeMock).getProperty(Exchange.FILTER_NON_XML_CHARS, true, Boolean.class); |
| 91 | + } |
65 | 92 |
|
66 | | - assertTrue("Expected filtering here", jaxbDataFormat.needFiltering(exchangeMock)); |
67 | | - verifyAll(); |
68 | | - resetAll(); |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + @Test |
| 95 | + public void testNeedFilteringTruePropagates() { |
| 96 | + // tests combinations of data format option and exchange property |
| 97 | + when(exchangeMock.getProperty(anyString(), anyObject(), any(Class.class))) |
| 98 | + .thenReturn(true); |
69 | 99 |
|
70 | | - jaxbDataFormat.setFilterNonXmlChars(true); |
71 | | - expect( |
72 | | - exchangeMock.getProperty(Exchange.FILTER_NON_XML_CHARS, true, |
73 | | - Boolean.class)).andReturn(false); |
74 | | - replayAll(); |
75 | | - |
76 | | - assertFalse("Not expected filtering here", jaxbDataFormat.needFiltering(exchangeMock)); |
77 | | - verifyAll(); |
78 | | - resetAll(); |
79 | | - |
80 | | - expect( |
81 | | - exchangeMock.getProperty(Exchange.FILTER_NON_XML_CHARS, true, |
82 | | - Boolean.class)).andReturn(true); |
83 | | - replayAll(); |
84 | | - |
85 | | - assertTrue("Expected filtering here", jaxbDataFormat.needFiltering(exchangeMock)); |
86 | | - verifyAll(); |
87 | | - resetAll(); |
| 100 | + assertTrue("Expecting filtering here", jaxbDataFormat.needFiltering(exchangeMock)); |
| 101 | + } |
| 102 | + |
| 103 | + @SuppressWarnings("unchecked") |
| 104 | + @Test |
| 105 | + public void testNeedFilteringFalsePropagates() { |
| 106 | + // tests combinations of data format option and exchange property |
| 107 | + when(exchangeMock.getProperty(anyString(), anyObject(), any(Class.class))) |
| 108 | + .thenReturn(false); |
| 109 | + |
| 110 | + assertFalse("Not expecting filtering here", jaxbDataFormat.needFiltering(exchangeMock)); |
88 | 111 | } |
89 | 112 |
|
90 | 113 | @Test |
91 | | - public void testMarshalFilteringDisabled() throws XMLStreamException, JAXBException { |
92 | | - JaxbDataFormat jaxbDataFormatMock = createMockBuilder(JaxbDataFormat.class) |
93 | | - .addMockedMethod("needFiltering").createStrictMock(); |
| 114 | + public void testMarshalFilteringDisabled() throws IOException, XMLStreamException, JAXBException { |
| 115 | + doCallRealMethod().when(jaxbDataFormatMock).marshal(any(Exchange.class), anyObject(), |
| 116 | + any(OutputStream.class), any(Marshaller.class)); |
| 117 | + when(jaxbDataFormatMock.needFiltering(exchangeMock)).thenReturn(false); |
| 118 | + |
94 | 119 | Object graph = new Object(); |
95 | 120 | OutputStream stream = new ByteArrayOutputStream(); |
96 | 121 |
|
97 | | - expect(jaxbDataFormatMock.needFiltering(exchangeMock)).andReturn(false); |
98 | | - marshallerMock.marshal(same(graph), same(stream)); |
99 | | - replayAll(); |
100 | | - |
101 | 122 | jaxbDataFormatMock.marshal(exchangeMock, graph, stream, marshallerMock); |
102 | | - verifyAll(); |
| 123 | + verify(marshallerMock).marshal(same(graph), same(stream)); |
103 | 124 | } |
104 | 125 |
|
105 | 126 | @Test |
106 | 127 | public void testMarshalFilteringEnabled() throws XMLStreamException, JAXBException { |
107 | | - JaxbDataFormat jaxbDataFormatMock = createMockBuilder(JaxbDataFormat.class) |
108 | | - .addMockedMethod("needFiltering").createStrictMock(); |
| 128 | + doCallRealMethod().when(jaxbDataFormatMock).marshal(any(Exchange.class), anyObject(), |
| 129 | + any(OutputStream.class), any(Marshaller.class)); |
| 130 | + when(jaxbDataFormatMock.needFiltering(exchangeMock)).thenReturn(true); |
109 | 131 |
|
110 | 132 | Object graph = new Object(); |
111 | 133 | OutputStream stream = new ByteArrayOutputStream(); |
112 | 134 |
|
113 | | - expect(jaxbDataFormatMock.needFiltering(exchangeMock)).andReturn(true); |
114 | | - marshallerMock.marshal(same(graph), isA(FilteringXmlStreamWriter.class)); |
115 | | - replayAll(); |
116 | | - |
117 | 135 | jaxbDataFormatMock.marshal(exchangeMock, graph, stream, marshallerMock); |
118 | | - verifyAll(); |
| 136 | + verify(marshallerMock).marshal(same(graph), isA(FilteringXmlStreamWriter.class)); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testUnmarshalFilteringDisabled() throws IOException, JAXBException { |
| 142 | + doCallRealMethod().when(jaxbDataFormatMock).unmarshal(any(Exchange.class), |
| 143 | + any(InputStream.class)); |
| 144 | + |
| 145 | + when(jaxbDataFormatMock.getContext()).thenReturn(jaxbContextMock); |
| 146 | + when(jaxbContextMock.createUnmarshaller()).thenReturn(unmarshallerMock); |
| 147 | + |
| 148 | + when(jaxbDataFormatMock.needFiltering(exchangeMock)).thenReturn(false); |
| 149 | + |
| 150 | + InputStream stream = new ByteArrayInputStream(new byte[] {}); |
| 151 | + |
| 152 | + jaxbDataFormatMock.unmarshal(exchangeMock, stream); |
| 153 | + verify(unmarshallerMock).unmarshal((InputStream) argThat(not(instanceOf(NonXmlFilterReader.class)))); |
119 | 154 | } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testUnmarshalFilteringEnabled() throws IOException, JAXBException { |
| 158 | + doCallRealMethod().when(jaxbDataFormatMock).unmarshal(any(Exchange.class), |
| 159 | + any(InputStream.class)); |
| 160 | + |
| 161 | + when(jaxbDataFormatMock.getContext()).thenReturn(jaxbContextMock); |
| 162 | + when(jaxbContextMock.createUnmarshaller()).thenReturn(unmarshallerMock); |
| 163 | + |
| 164 | + when(jaxbDataFormatMock.needFiltering(exchangeMock)).thenReturn(true); |
| 165 | + |
| 166 | + InputStream stream = new ByteArrayInputStream(new byte[] {}); |
| 167 | + |
| 168 | + jaxbDataFormatMock.unmarshal(exchangeMock, stream); |
| 169 | + verify(unmarshallerMock).unmarshal(any(NonXmlFilterReader.class)); |
| 170 | + } |
| 171 | + |
120 | 172 | } |
0 commit comments