com.tangfan.test.UserServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for com.tangfan.test.UserServiceTest.java

Source

/*******************************************************************
 * Copyright (c) 2013 tangfan and others
 * All rights reserved.
 *
 * Contributors:
 * tangfan's Systems (Shanghai) fan.T, Ltd.
 * 
 ******************************************************************/
package com.tangfan.test;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.MTOMFeature;
import javax.xml.ws.soap.SOAPBinding;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

import com.sun.xml.ws.api.message.Headers;
import com.sun.xml.ws.developer.WSBindingProvider;
import com.tangfan.client.user.LicenseInfo;
import com.tangfan.client.user.User;
import com.tangfan.client.user.UserException_Exception;
import com.tangfan.client.user.UserService;
import com.tangfan.client.user.UserService_Service;

/**
 * UserService
 *
 * @author TangFan
 *
 * @version 2015413
 *
 */
public class UserServiceTest {

    private UserService port;
    private UserService_Service ws;
    private String ns = "http://www.tangfan.org/user";

    @Before
    public void init() {
        try {
            URL url = new URL("http://localhost:8085/soap/us?wsdl");
            QName qName = new QName(ns, "UserService");
            ws = new UserService_Service(url, qName);
            //         port = ws.getUserServicePort(new MTOMFeature());
            port = ws.getUserServicePort();
            BindingProvider bp = (BindingProvider) port;
            SOAPBinding binding = (SOAPBinding) bp.getBinding();
            binding.setMTOMEnabled(true);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    /**
     * list web?
     */
    @Test
    public void list() {
        List<User> list = port.list();
        for (User u : list) {
            System.out.println(u.getNickname());
        }
    }

    /**
     * add jaxws-ri???
     */
    @Test
    public void add() {
        try {
            //1.?xml
            JAXBContext jaxb = JAXBContext.newInstance(LicenseInfo.class);
            User lu = new User();
            lu.setId(0);
            lu.setNickname("");
            lu.setUsername("admin");
            lu.setPassword("123");
            LicenseInfo info = new LicenseInfo();
            info.setLoginUser(lu);

            QName qName = new QName(ns, "licenseInfo");
            JAXBElement<LicenseInfo> jele = new JAXBElement<LicenseInfo>(qName, LicenseInfo.class, info);

            Marshaller mars = jaxb.createMarshaller();
            mars.setProperty(Marshaller.JAXB_FRAGMENT, true);//xml
            mars.setProperty(Marshaller.JAXB_ENCODING, "utf-8");//?

            //2.?dom
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            mars.marshal(jele, doc);

            //3.Headers.create?header
            WSBindingProvider wsb = (WSBindingProvider) port;
            wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement()));

            User u = new User();
            u.setId(2);
            u.setUsername("master");
            u.setNickname("jboss?");
            u.setPassword("123456");
            port.add(u);
        } catch (UserException_Exception e) {
            System.out.println(e.getMessage());
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    /**
     * testUpload webservice?
     */
    @Test
    public void testUpload() {
        try {
            byte[] file = FileUtils
                    .readFileToByteArray(new File("C:/Users/Administrator/Pictures/QQ20150206132707.jpg"));
            port.upload(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * testBinary webservice?
     */
    @Test
    public void testBinary() {
        DataHandler file = new DataHandler(
                new FileDataSource(new File("C:/Users/Administrator/Pictures/QQ20150206132707.jpg")));
        port.binary(file);
    }

}