Skip to content

Commit f0144f2

Browse files
committed
CAMEL-2330 applied patch with thanks to Pavel
git-svn-id: https://svn.apache.org/repos/asf/camel/trunk@900649 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0e47b69 commit f0144f2

7 files changed

Lines changed: 207 additions & 166 deletions

File tree

components/camel-jaxb/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
<scope>test</scope>
101101
</dependency>
102102
<dependency>
103-
<groupId>org.easymock</groupId>
104-
<artifactId>easymockclassextension</artifactId>
103+
<groupId>org.mockito</groupId>
104+
<artifactId>mockito-core</artifactId>
105105
<scope>test</scope>
106106
</dependency>
107107
</dependencies>

components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void marshal(Exchange exchange, Object graph, OutputStream stream, Marshaller ma
9696
}
9797
}
9898

99-
public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, ClassNotFoundException {
99+
public Object unmarshal(Exchange exchange, InputStream stream) throws IOException {
100100
try {
101101
// must create a new instance of unmarshaller as its not thread safe
102102
Object answer;

components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/FilteringXmlStreamWriterTest.java

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,98 +19,77 @@
1919
import javax.xml.stream.XMLStreamException;
2020
import javax.xml.stream.XMLStreamWriter;
2121

22-
import org.easymock.classextension.EasyMockSupport;
2322
import org.junit.Before;
2423
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.runners.MockitoJUnitRunner;
2527

26-
import static org.easymock.EasyMock.eq;
27-
import static org.easymock.EasyMock.expect;
28-
import static org.easymock.EasyMock.same;
28+
import static org.mockito.Matchers.eq;
29+
import static org.mockito.Matchers.same;
30+
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.when;
2932

30-
public class FilteringXmlStreamWriterTest extends EasyMockSupport {
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class FilteringXmlStreamWriterTest {
3135
private FilteringXmlStreamWriter filteringXmlStreamWriter;
36+
@Mock
3237
private NonXmlCharFilterer nonXmlCharFiltererMock;
38+
@Mock
3339
private XMLStreamWriter xmlStreamWriterMock;
3440

3541
// only testing non-generated methods, those that do apply filtering
3642

3743
@Before
3844
public void setUp() {
39-
xmlStreamWriterMock = createStrictMock(XMLStreamWriter.class);
40-
nonXmlCharFiltererMock = createStrictMock(NonXmlCharFilterer.class);
4145
filteringXmlStreamWriter = new FilteringXmlStreamWriter(xmlStreamWriterMock);
4246
filteringXmlStreamWriter.nonXmlCharFilterer = nonXmlCharFiltererMock;
47+
48+
when(nonXmlCharFiltererMock.filter("value")).thenReturn("filteredValue");
4349
}
4450

4551
@Test
4652
public void testWriteAttribute2Args() throws XMLStreamException {
47-
expect(nonXmlCharFiltererMock.filter("value")).andReturn("filteredValue");
48-
xmlStreamWriterMock.writeAttribute("localName", "filteredValue");
49-
replayAll();
50-
5153
filteringXmlStreamWriter.writeAttribute("localName", "value");
52-
verifyAll();
54+
verify(xmlStreamWriterMock).writeAttribute("localName", "filteredValue");
5355
}
5456

5557
@Test
5658
public void testWriteAttribute3Args() throws XMLStreamException {
57-
expect(nonXmlCharFiltererMock.filter("value")).andReturn("filteredValue");
58-
xmlStreamWriterMock.writeAttribute("namespaceURI", "localName", "filteredValue");
59-
replayAll();
60-
6159
filteringXmlStreamWriter.writeAttribute("namespaceURI", "localName", "value");
62-
verifyAll();
60+
verify(xmlStreamWriterMock).writeAttribute("namespaceURI", "localName", "filteredValue");
6361
}
6462

6563
@Test
6664
public void testWriteAttribute4Args() throws XMLStreamException {
67-
expect(nonXmlCharFiltererMock.filter("value")).andReturn("filteredValue");
68-
xmlStreamWriterMock.writeAttribute("prefix", "namespaceURI", "localName", "filteredValue");
69-
replayAll();
70-
7165
filteringXmlStreamWriter.writeAttribute("prefix", "namespaceURI", "localName", "value");
72-
verifyAll();
66+
verify(xmlStreamWriterMock).writeAttribute("prefix", "namespaceURI", "localName", "filteredValue");
7367
}
7468

7569
@Test
7670
public void testWriteCData() throws XMLStreamException {
77-
expect(nonXmlCharFiltererMock.filter("value")).andReturn("filteredValue");
78-
xmlStreamWriterMock.writeCData("filteredValue");
79-
replayAll();
80-
8171
filteringXmlStreamWriter.writeCData("value");
82-
verifyAll();
72+
verify(xmlStreamWriterMock).writeCData("filteredValue");
8373
}
8474

8575
@Test
8676
public void testWriteCharacters1Arg() throws XMLStreamException {
87-
expect(nonXmlCharFiltererMock.filter("value")).andReturn("filteredValue");
88-
xmlStreamWriterMock.writeCharacters("filteredValue");
89-
replayAll();
90-
9177
filteringXmlStreamWriter.writeCharacters("value");
92-
verifyAll();
78+
verify(xmlStreamWriterMock).writeCharacters("filteredValue");
9379
}
9480

9581
@Test
9682
public void testWriteComment() throws XMLStreamException {
97-
expect(nonXmlCharFiltererMock.filter("value")).andReturn("filteredValue");
98-
xmlStreamWriterMock.writeComment("filteredValue");
99-
replayAll();
100-
10183
filteringXmlStreamWriter.writeComment("value");
102-
verifyAll();
84+
verify(xmlStreamWriterMock).writeComment("filteredValue");
10385
}
10486

10587
@Test
10688
public void testWriteCharacters3Args() throws XMLStreamException {
10789
char[] buffer = new char[] {'a', 'b', 'c'};
108-
expect(nonXmlCharFiltererMock.filter(same(buffer), eq(2), eq(3))).andReturn(true);
109-
xmlStreamWriterMock.writeCharacters(same(buffer), eq(2), eq(3));
110-
replayAll();
111-
11290
filteringXmlStreamWriter.writeCharacters(buffer, 2, 3);
113-
verifyAll();
91+
verify(nonXmlCharFiltererMock).filter(same(buffer), eq(2), eq(3));
92+
verify(xmlStreamWriterMock).writeCharacters(same(buffer), eq(2), eq(3));
11493
}
11594

11695
}

components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatTest.java

Lines changed: 108 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,105 +16,157 @@
1616
*/
1717
package org.apache.camel.converter.jaxb;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.io.InputStream;
2023
import java.io.OutputStream;
2124

25+
import javax.xml.bind.JAXBContext;
2226
import javax.xml.bind.JAXBException;
2327
import javax.xml.bind.Marshaller;
28+
import javax.xml.bind.Unmarshaller;
2429
import javax.xml.stream.XMLStreamException;
2530

2631
import org.apache.camel.Exchange;
27-
import org.easymock.classextension.EasyMockSupport;
2832
import org.junit.Before;
2933
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.mockito.Mock;
36+
import org.mockito.runners.MockitoJUnitRunner;
3037

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;
3441
import static org.junit.Assert.assertFalse;
3542
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 {
3855
private JaxbDataFormat jaxbDataFormat;
56+
@Mock
3957
private Exchange exchangeMock;
58+
@Mock
4059
private Marshaller marshallerMock;
60+
@Mock
61+
private JaxbDataFormat jaxbDataFormatMock;
62+
@Mock
63+
private JAXBContext jaxbContextMock;
64+
@Mock
65+
private Unmarshaller unmarshallerMock;
66+
4167

4268
@Before
4369
public void setUp() {
4470
jaxbDataFormat = new JaxbDataFormat();
45-
marshallerMock = createStrictMock(Marshaller.class);
46-
exchangeMock = createStrictMock(Exchange.class);
4771
}
4872

73+
@SuppressWarnings("unchecked")
4974
@Test
50-
public void testNeedFiltering() {
75+
public void testNeedFilteringDisabledFiltering() {
5176
// 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);
5679

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+
}
6083

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+
}
6592

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);
6999

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));
88111
}
89112

90113
@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+
94119
Object graph = new Object();
95120
OutputStream stream = new ByteArrayOutputStream();
96121

97-
expect(jaxbDataFormatMock.needFiltering(exchangeMock)).andReturn(false);
98-
marshallerMock.marshal(same(graph), same(stream));
99-
replayAll();
100-
101122
jaxbDataFormatMock.marshal(exchangeMock, graph, stream, marshallerMock);
102-
verifyAll();
123+
verify(marshallerMock).marshal(same(graph), same(stream));
103124
}
104125

105126
@Test
106127
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);
109131

110132
Object graph = new Object();
111133
OutputStream stream = new ByteArrayOutputStream();
112134

113-
expect(jaxbDataFormatMock.needFiltering(exchangeMock)).andReturn(true);
114-
marshallerMock.marshal(same(graph), isA(FilteringXmlStreamWriter.class));
115-
replayAll();
116-
117135
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))));
119154
}
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+
120172
}

0 commit comments

Comments
 (0)