alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Apache CXF example source code file (XMLSourceTest.java)

This example Apache CXF source code file (XMLSourceTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Apache CXF tags/keywords

bar, bar2, bytearrayinputstream, inputstream, inputstream, io, linkedhashmap, map, net, network, string, string, test, test, uri, util, xmlsource, xmlsource

The Apache CXF XMLSourceTest.java source code

/**
 * 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.cxf.jaxrs.ext.xml;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;

import org.apache.cxf.common.xmlschema.XmlSchemaConstants;

import org.junit.Assert;
import org.junit.Test;

public class XMLSourceTest extends Assert {

    
    @Test
    public void testNodeStringValue() {
        InputStream is = getClass().getResourceAsStream("/book1.xsd");
        XMLSource xp = new XMLSource(is);
        Map<String, String> nsMap = 
            Collections.singletonMap("xs", XmlSchemaConstants.XSD_NAMESPACE_URI);
        String value = xp.getNode("/xs:schema", nsMap, String.class);
        assertFalse(value.contains("<?xml"));
        assertTrue(value.startsWith("<xs:schema"));
    }
    
    @Test
    public void testAttributeValue() {
        InputStream is = new ByteArrayInputStream("<foo>barValue".getBytes());
        XMLSource xp = new XMLSource(is);
        assertEquals("baz", xp.getValue("/foo/bar/@attr"));
    }
    
    @Test
    public void testNodeTextValue() {
        InputStream is = new ByteArrayInputStream("<foo>barValue".getBytes());
        XMLSource xp = new XMLSource(is);
        assertEquals("barValue", xp.getValue("/foo/bar"));
    }
    
    @Test
    public void testAttributeValues() {
        InputStream is = new ByteArrayInputStream(
            "<foo>bar1bar2".getBytes());
        XMLSource xp = new XMLSource(is);
        List<String> values = Arrays.asList(xp.getValues("/foo/bar/@attr"));
        assertEquals(2, values.size());
        assertTrue(values.contains("baz"));
        assertTrue(values.contains("baz2"));
    }
    
    @Test
    public void testNodeTextValues() {
        InputStream is = new ByteArrayInputStream(
            "<foo>bar1bar2".getBytes());
        XMLSource xp = new XMLSource(is);
        List<String> values = Arrays.asList(xp.getValues("/foo/bar/text()"));
        assertEquals(2, values.size());
        assertTrue(values.contains("bar1"));
        assertTrue(values.contains("bar2"));
    }
    
    @Test
    public void testIntegerValues() {
        InputStream is = new ByteArrayInputStream(
            "<foo>".getBytes());
        XMLSource xp = new XMLSource(is);
        Integer[] values = xp.getNodes("/foo/bar/@attr", Integer.class);
        assertEquals(2, values.length);
        assertTrue(values[0] == 1 && values[1] == 2 || values[0] == 2 && values[1] == 1);
    }
    
    @Test
    public void testGetNodeNoNamespace() {
        InputStream is = new ByteArrayInputStream("<foo>".getBytes());
        XMLSource xp = new XMLSource(is);
        Bar bar = xp.getNode("/foo/bar", Bar.class);
        assertNotNull(bar);
    }
    
    @Test
    public void testGetNodeNamespace() {
        String data = "<x:foo xmlns:x=\"http://baz\">"; 
        InputStream is = new ByteArrayInputStream(data.getBytes());
        XMLSource xp = new XMLSource(is);
        Map<String, String> map = new LinkedHashMap();
        map.put("x", "http://baz");
        Bar2 bar = xp.getNode("/x:foo/x:bar", map, Bar2.class);
        assertNotNull(bar);
    }
    
    @Test
    public void testGetNodeBuffering() {
        String data = "<x:foo xmlns:x=\"http://baz\">"; 
        InputStream is = new ByteArrayInputStream(data.getBytes());
        XMLSource xp = new XMLSource(is);
        xp.setBuffering(true);
        Map<String, String> map = new LinkedHashMap();
        map.put("x", "http://baz");
        Bar2 bar = xp.getNode("/x:foo/x:bar", map, Bar2.class);
        assertNotNull(bar);
        bar = xp.getNode("/x:foo/x:bar", map, Bar2.class);
        assertNotNull(bar);
    }
    
    @Test
    public void testGetNodeNamespace2() {
        String data = "<z:foo xmlns:z=\"http://baz\">"; 
        InputStream is = new ByteArrayInputStream(data.getBytes());
        XMLSource xp = new XMLSource(is);
        Map<String, String> map = new LinkedHashMap();
        map.put("x", "http://baz");
        Bar2 bar = xp.getNode("/x:foo/x:bar", map, Bar2.class);
        assertNotNull(bar);
    }
    
    @Test
    public void testGetNodeNamespace3() {
        String data = "<x:foo xmlns:x=\"http://foo\" xmlns:z=\"http://baz\">"; 
        InputStream is = new ByteArrayInputStream(data.getBytes());
        XMLSource xp = new XMLSource(is);
        Map<String, String> map = new LinkedHashMap();
        map.put("x", "http://foo");
        map.put("y", "http://baz");
        Bar2 bar = xp.getNode("/x:foo/y:bar", map, Bar2.class);
        assertNotNull(bar);
    }
    
    @Test
    public void testGetNodeDefaultNamespace() {
        String data = "<foo xmlns=\"http://baz\">"; 
        InputStream is = new ByteArrayInputStream(data.getBytes());
        XMLSource xp = new XMLSource(is);
        Map<String, String> map = new LinkedHashMap();
        map.put("x", "http://baz");
        Bar2 bar = xp.getNode("/x:foo/x:bar", map, Bar2.class);
        assertNotNull(bar);
    }

    @Test
    public void testGetNodesNoNamespace() {
        InputStream is = new ByteArrayInputStream("<foo>".getBytes());
        XMLSource xp = new XMLSource(is);
        Bar[] bars = xp.getNodes("/foo/bar", Bar.class);
        assertNotNull(bars);
        assertEquals(2, bars.length);
        assertNotSame(bars[0], bars[1]);
    }
    
    @Test
    public void testGetNodesNamespace() {
        String data = "<x:foo xmlns:x=\"http://baz\">"; 
        InputStream is = new ByteArrayInputStream(data.getBytes());
        XMLSource xp = new XMLSource(is);
        Map<String, String> map = new LinkedHashMap();
        map.put("x", "http://baz");
        Bar2[] bars = xp.getNodes("/x:foo/x:bar", map, Bar2.class);
        assertNotNull(bars);
        assertNotNull(bars);
        assertEquals(2, bars.length);
        assertNotSame(bars[0], bars[1]);
    }
    
    @Test
    public void testGetStringValue() {
        InputStream is = new ByteArrayInputStream("<foo>".getBytes());
        XMLSource xp = new XMLSource(is);
        String value = xp.getValue("/foo/bar/@id");
        assertEquals("2", value);
    }
    
    @Test
    public void testGetRelativeLink() {
        InputStream is = new ByteArrayInputStream("<foo>".getBytes());
        XMLSource xp = new XMLSource(is);
        URI value = xp.getLink("/foo/bar/@href");
        assertEquals("/2", value.toString());
    }
    
    @Test
    public void testBaseURI() {
        InputStream is = new ByteArrayInputStream(
            "<foo xml:base=\"http://bar\">".getBytes());
        XMLSource xp = new XMLSource(is);
        URI value = xp.getBaseURI();
        assertEquals("http://bar", value.toString());
    }
    
    @XmlRootElement
    private static class Bar {
        
    }
    
    @XmlRootElement(name = "bar", namespace = "http://baz")
    private static class Bar2 {
        
    }
}

Other Apache CXF examples (source code examples)

Here is a short list of links related to this Apache CXF XMLSourceTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.