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

Struts example source code file (LocationAttributesTest.java)

This example Struts source code file (LocationAttributesTest.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 - Struts tags/keywords

attributesimpl, document, document, documentbuilder, documentbuilderfactory, dom, element, element, exception, exception, locationattributestest, locator, parser, sax, string, string, testcase, xml

The Struts LocationAttributesTest.java source code

/*
 * Copyright 2006,2009 The Apache Software Foundation.
 * 
 * Licensed 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 com.opensymphony.xwork2.util.location;

import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.Locator;
import org.xml.sax.helpers.AttributesImpl;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class LocationAttributesTest extends TestCase {
    
    public LocationAttributesTest(String name) {
        super(name);
    }
    
    public void testAddLocationAttributes() throws Exception {
        AttributesImpl attrs = new AttributesImpl();
        LocationAttributes.addLocationAttributes(new Locator() {
            public int getColumnNumber() { return 40; }
            public int getLineNumber() { return 1; }
            public String getSystemId() { return "path/to/file.xml"; }
            public String getPublicId() { return "path/to/file.xml"; }
        }, attrs);

        assertTrue("path/to/file.xml".equals(attrs.getValue("loc:src")));
        assertTrue("1".equals(attrs.getValue("loc:line")));
        assertTrue("40".equals(attrs.getValue("loc:column")));
    }
 
    public void testRecursiveRemove() throws Exception {
        Document doc = getDoc("xml-with-location.xml");

        Element root = doc.getDocumentElement();
        LocationAttributes.remove(root, true);

        assertNull(root.getAttributeNode("loc:line"));
        assertNull(root.getAttributeNode("loc:column"));
        assertNull(root.getAttributeNode("loc:src"));

        Element kid = (Element)doc.getElementsByTagName("bar").item(0);
        assertNull(kid.getAttributeNode("loc:line"));
        assertNull(kid.getAttributeNode("loc:column"));
        assertNull(kid.getAttributeNode("loc:src"));
    }    

    public void testNonRecursiveRemove() throws Exception {
        Document doc = getDoc("xml-with-location.xml");

        Element root = doc.getDocumentElement();
        LocationAttributes.remove(root, false);

        assertNull(root.getAttributeNode("loc:line"));
        assertNull(root.getAttributeNode("loc:column"));
        assertNull(root.getAttributeNode("loc:src"));

        Element kid = (Element)doc.getElementsByTagName("bar").item(0);
        assertNotNull(kid.getAttributeNode("loc:line"));
        assertNotNull(kid.getAttributeNode("loc:column"));
        assertNotNull(kid.getAttributeNode("loc:src"));
    }    

    private Document getDoc(String path) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(LocationAttributesTest.class.getResourceAsStream(path));


    }
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts LocationAttributesTest.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.