jaxbcontexttest.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 105 行

JAVA
105
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * 
 */
package org.apache.axis2.jaxws.misc;

import java.util.TreeSet;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.apache.axis2.jaxws.message.databinding.JAXBUtils;
import org.apache.axis2.jaxws.utility.JavaUtils;

import junit.framework.TestCase;

/**
 * Tests Namespace to Package Algorithmh
 *
 */
public class JAXBContextTest extends TestCase {

    /**
     * Test basic functionality of JAXBUtils pooling
     * @throws Exception
     */
    public void test01() throws JAXBException {
        
        // Get a JAXBContext
        TreeSet<String> context1 = new TreeSet<String>();
        context1.add("org.test.addnumbers");
        context1.add("org.test.anytype");
        
        JAXBContext jaxbContext1 = JAXBUtils.getJAXBContext(context1);
        
        // Assert that the JAXBContext was found and the context contains the two valid packages
        assertTrue(jaxbContext1 != null);
        assertTrue(context1.contains("org.test.addnumbers"));
        assertTrue(context1.contains("org.test.anytype"));
        
        // Repeat with the same packages
        TreeSet<String> context2 = new TreeSet<String>();
        context2.add("org.test.addnumbers");
        context2.add("org.test.anytype");
        
        JAXBContext jaxbContext2 = JAXBUtils.getJAXBContext(context2);
        
        // The following assertion is probably true,but GC may have wiped out the weak reference
        //assertTrue(jaxbContext2 == jaxbContext1);
        assertTrue(jaxbContext2 != null);
        assertTrue(jaxbContext2.toString().equals(jaxbContext1.toString()));
        assertTrue(context2.contains("org.test.addnumbers"));
        assertTrue(context2.contains("org.test.anytype"));
        
        // Repeat with the same packages + an invalid package
        TreeSet<String> context3 = new TreeSet<String>();
        context3.add("org.test.addnumbers");
        context3.add("org.test.anytype");
        context3.add("my.grandma.loves.jaxws");
        
        JAXBContext jaxbContext3 = JAXBUtils.getJAXBContext(context3);
        
        // The following assertion is probably true,but GC may have wiped out the weak reference
        //assertTrue(jaxbContext3 == jaxbContext1);
        assertTrue(jaxbContext3 != null);
        assertTrue(jaxbContext1.toString().equals(jaxbContext1.toString()));
        assertTrue(context3.contains("org.test.addnumbers"));
        assertTrue(context3.contains("org.test.anytype")); 
        // TODO FIXME - does not work under m2/surefire
//        assertTrue(!context3.contains("my.grandma.loves.jaxws"));  // invalid package should be silently removed
        
        // Repeat with a subset of packages
        TreeSet<String> context4 = new TreeSet<String>();
        context4.add("org.test.addnumbers");
        
        
        JAXBContext jaxbContext4 = JAXBUtils.getJAXBContext(context4);
        
        assertTrue(jaxbContext4 != null);
        assertTrue(jaxbContext4 != jaxbContext3);
        assertTrue(context4.contains("org.test.addnumbers"));
        
       
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?