001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: /**
021: *
022: */package org.apache.axis2.jaxws.misc;
023:
024: import java.util.TreeSet;
025: import java.util.Set;
026:
027: import javax.xml.bind.JAXBContext;
028: import javax.xml.bind.JAXBException;
029:
030: import org.apache.axis2.jaxws.message.databinding.JAXBUtils;
031: import org.apache.axis2.jaxws.utility.JavaUtils;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * Tests Namespace to Package Algorithmh
037: *
038: */
039: public class JAXBContextTest extends TestCase {
040:
041: /**
042: * Test basic functionality of JAXBUtils pooling
043: * @throws Exception
044: */
045: public void test01() throws JAXBException {
046:
047: // Get a JAXBContext
048: TreeSet<String> context1 = new TreeSet<String>();
049: context1.add("org.test.addnumbers");
050: context1.add("org.test.anytype");
051:
052: JAXBContext jaxbContext1 = JAXBUtils.getJAXBContext(context1);
053:
054: // Assert that the JAXBContext was found and the context contains the two valid packages
055: assertTrue(jaxbContext1 != null);
056: assertTrue(context1.contains("org.test.addnumbers"));
057: assertTrue(context1.contains("org.test.anytype"));
058:
059: // Repeat with the same packages
060: TreeSet<String> context2 = new TreeSet<String>();
061: context2.add("org.test.addnumbers");
062: context2.add("org.test.anytype");
063:
064: JAXBContext jaxbContext2 = JAXBUtils.getJAXBContext(context2);
065:
066: // The following assertion is probably true,but GC may have wiped out the weak reference
067: //assertTrue(jaxbContext2 == jaxbContext1);
068: assertTrue(jaxbContext2 != null);
069: assertTrue(jaxbContext2.toString().equals(
070: jaxbContext1.toString()));
071: assertTrue(context2.contains("org.test.addnumbers"));
072: assertTrue(context2.contains("org.test.anytype"));
073:
074: // Repeat with the same packages + an invalid package
075: TreeSet<String> context3 = new TreeSet<String>();
076: context3.add("org.test.addnumbers");
077: context3.add("org.test.anytype");
078: context3.add("my.grandma.loves.jaxws");
079:
080: JAXBContext jaxbContext3 = JAXBUtils.getJAXBContext(context3);
081:
082: // The following assertion is probably true,but GC may have wiped out the weak reference
083: //assertTrue(jaxbContext3 == jaxbContext1);
084: assertTrue(jaxbContext3 != null);
085: assertTrue(jaxbContext1.toString().equals(
086: jaxbContext1.toString()));
087: assertTrue(context3.contains("org.test.addnumbers"));
088: assertTrue(context3.contains("org.test.anytype"));
089: // TODO FIXME - does not work under m2/surefire
090: // assertTrue(!context3.contains("my.grandma.loves.jaxws")); // invalid package should be silently removed
091:
092: // Repeat with a subset of packages
093: TreeSet<String> context4 = new TreeSet<String>();
094: context4.add("org.test.addnumbers");
095:
096: JAXBContext jaxbContext4 = JAXBUtils.getJAXBContext(context4);
097:
098: assertTrue(jaxbContext4 != null);
099: assertTrue(jaxbContext4 != jaxbContext3);
100: assertTrue(context4.contains("org.test.addnumbers"));
101:
102: }
103: }
|