View Javadoc

1   /*
2    * $Id: MtomTestCase.java 22552 2011-07-25 07:18:19Z claude.mamo $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.cxf;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import org.mule.tck.AbstractServiceAndFlowTestCase;
17  import org.mule.tck.junit4.rule.DynamicPort;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.File;
21  import java.io.InputStream;
22  import java.net.URL;
23  import java.util.Arrays;
24  import java.util.Collection;
25  
26  import javax.activation.DataHandler;
27  import javax.activation.FileDataSource;
28  import javax.xml.ws.BindingProvider;
29  import javax.xml.ws.Holder;
30  import javax.xml.ws.soap.SOAPBinding;
31  
32  import org.apache.cxf.BusFactory;
33  import org.apache.cxf.helpers.IOUtils;
34  import org.apache.cxf.mime.TestMtom;
35  import org.apache.cxf.mime.TestMtomService;
36  import org.junit.Rule;
37  import org.junit.Test;
38  import org.junit.runners.Parameterized.Parameters;
39  
40  public class MtomTestCase extends AbstractServiceAndFlowTestCase
41  {
42      @Rule
43      public DynamicPort dynamicPort = new DynamicPort("port1");
44  
45      public MtomTestCase(ConfigVariant variant, String configResources)
46      {
47          super(variant, configResources);
48      }
49  
50      @Parameters
51      public static Collection<Object[]> parameters()
52      {
53          return Arrays.asList(new Object[][]{
54              {ConfigVariant.SERVICE, "mtom-conf-service.xml"},
55              {ConfigVariant.FLOW, "mtom-conf-flow.xml"}
56          });
57      }      
58  
59      @Test
60      public void testEchoService() throws Exception
61      {
62          URL wsdl = getClass().getResource("/wsdl/mtom_xop.wsdl");
63          assertNotNull(wsdl);
64  
65          CxfConfiguration clientConfig = new CxfConfiguration();
66          clientConfig.setMuleContext(muleContext);
67          clientConfig.initialise();
68          BusFactory.setThreadDefaultBus(clientConfig.getCxfBus());
69  
70          TestMtomService svc = new TestMtomService(wsdl);
71  
72          TestMtom port = svc.getTestMtomPort();
73  
74          BindingProvider bp = ((BindingProvider) port);
75          bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
76              "http://localhost:" + dynamicPort.getNumber() + "/services/mtom");
77          ((SOAPBinding) bp.getBinding()).setMTOMEnabled(true);
78          // Client client = ClientProxy.getClient(port);
79          // new LoggingFeature().initialize(client, null);
80  
81          File file = new File("src/test/resources/mtom-conf-service.xml");
82          DataHandler dh = new DataHandler(new FileDataSource(file));
83  
84          Holder<String> name = new Holder<String>("test");
85          Holder<DataHandler> info = new Holder<DataHandler>(dh);
86  
87          port.testXop(name, info);
88  
89          assertEquals("return detail + test", name.value);
90          assertNotNull(info.value);
91  
92          InputStream input = info.value.getInputStream();
93          ByteArrayOutputStream bos = new ByteArrayOutputStream();
94          IOUtils.copy(input, bos);
95          input.close();
96      }
97  }
98