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

What this is

This file 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.

Other links

The source code

package org.netbeans.modules.xml.text.syntax;

import org.w3c.dom.*;
import java.lang.ref.*;
import java.util.*;
import java.io.*;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.text.*;
import org.netbeans.editor.*;
import org.netbeans.editor.ext.*;
import junit.framework.*;
import org.netbeans.modules.xml.text.syntax.dom.*;

public class XMLSyntaxSupportTest extends TestCase {

    final String data =     " ]>dataa&ref;now";
    
    public XMLSyntaxSupportTest(java.lang.String testName) {
        super(testName);
    }
    
    public static void main(java.lang.String[] args) {
        junit.textui.TestRunner.run(suite());
    }
    
    public static Test suite() {
        TestSuite suite = new TestSuite(XMLSyntaxSupportTest.class);
        
        return suite;
    }

    /** 
     * //!!! Test Elements can be retrieved for arbitrary document location.
     */
    public void testGetElementChain() throws Exception {

        // at first get tokens for invalid documents
        

        final String[] cases = new String[] {
            "<|",
            "|",           // pick up content value
            "&|",
            "&pre|",          // pick up entityref name
            "" + token);
                token = token.getNext();
            }

            SyntaxElement el = sup.getElementChain(caret);
            assertTrue("No syntax element found at " + caret + " " + cases[i], el != null);

//          System.out.println("@" + caret + " is " + el);
        }
        
        
        
    }
    

    /**
     * Scan chain in forward direction, then in backward direction
     * and at least try indexed approach. Ideally it produces same
     * results.
     */
    public void testElementChainIntegrity() throws Exception {
        
        System.out.println("testElementChainIntegrity");        
        
        // test elements
        
        BaseDocument doc = new BaseDocument(XMLKit.class, false);
        doc.insertString(0, data, null);
        XMLSyntaxSupport sup = new XMLSyntaxSupport(doc);

        // dump token chain
        TokenItem token = sup.getTokenChain(0, doc.getLength());
        assertTrue("No token found!", token != null);
        while (token != null) {
            System.out.println(">" + token);
            token = token.getNext();
        }


        assertTrue("At BOD must not be a element!", sup.getElementChain(0) == null);

        // getElementChain()* must return same chain as getNext()* and getPrevious()*

        Set indexChain = new TreeSet();
        Set nextChain = new TreeSet();
        Set prevChain = new TreeSet();

        for (int i = 1; i<=data.length(); i++) {
            SyntaxElement next = sup.getElementChain(i);
            assertTrue("Cannot get element at " + i, next != null);
            indexChain.add(new Integer(next.getElementOffset()));
        }

        SyntaxElement next = sup.getElementChain(1);
        while (next != null) {
            nextChain.add(new Integer(next.getElementOffset()));
            next = next.getNext();
        }

        SyntaxElement prev = sup.getElementChain(doc.getLength());
        while (prev != null) {
            prevChain.add(new Integer(prev.getElementOffset()));
            prev = prev.getPrevious();
        }

        System.out.println("Next()ed chain:" + nextChain);
        System.out.println("Indexed chain :" + indexChain);            
        System.out.println("Prev()ed chain:" + prevChain);

        // pointing to random locations and asking for syntax elements may
        // return more elements that next()ed chain            
        assertTrue("Indexed must contain all next elements!", indexChain.containsAll(nextChain));
        assertTrue("Indexed must contain all prev elements!", indexChain.containsAll(prevChain));

        //!!! Opposite assert may fail but should

//            // try siblings
//            if (el instanceof SyntaxNode) {
//                System.out.println("Retrieving previous siblings for: " + el + "...");
//                Node prev = ((Node)el).getPreviousSibling();
//                while (prev != null) {
//                    System.out.println("\t" + prev);                                
//                    prev = prev.getPreviousSibling();
//                }                        
//            }
                    
        
    }
    
    
    /** 
     * Test that token can be retriesved at any walid document location but offset 0.
     * Test that all invalid locations throw BadLocationException.
     * Test that returned token form one token chain.
     */
    public void testGetPreviousToken() throws Exception {
        System.out.println("testGetPreviousToken");

        BaseDocument doc = null;
        XMLSyntaxSupport sup = null;
        
        doc = new BaseDocument(XMLKit.class, false);
        doc.insertString(0, data, null);
        sup = new XMLSyntaxSupport(doc);

        TokenItem last = null;
        for (int i = 0; i<=data.length(); i++) {
            TokenItem token = sup.getPreviousToken(i);
            if (token == null && i>0) fail("Cannot get token at " + i);
            if (token != null) {
                if (last != null && last.getOffset() < token.getOffset()) {
                    TokenItem next = last.getNext();
                    if (token.getImage().equals(next.getImage()) == false) { //no equals :-(
                        fail("Following tokens must be same:\n1:" + token + "\n2:" + next);
                    }
                }                    
                last = token;                        
            }
        }
                    
        try {
            sup.getPreviousToken(doc.getLength() + 1);
            fail("Adressing behing document end must throw BadLocationException!");
        } catch (BadLocationException ex) {
        }
    }
   
    /**
     * At least correct doctypes must be correctly recognized.
     */
    public void testDoctypes() throws Exception {
        
        System.out.println("testDoctypes");
        
        URL doctypes = getClass().getResource("data/Doctypes.xml");
        InputStreamReader isr = new InputStreamReader(new BufferedInputStream(doctypes.openStream()), "UTF8");
        StringBuffer buf = new StringBuffer();
        for (int c = isr.read(); c!= -1; c = isr.read()) {
            buf.append((char)c);
        }        
        
        BaseDocument doc = null;
        XMLSyntaxSupport sup = null;
        
        doc = new BaseDocument(XMLKit.class, false);
        doc.insertString(0, buf.toString(), null);
        sup = new XMLSyntaxSupport(doc);
        
        SyntaxElement el = sup.getElementChain(1);
        StringBuffer result = new StringBuffer();
        while (el != null) {
            if (el instanceof DocumentTypeImpl) {
                DocumentTypeImpl doctype = (DocumentTypeImpl) el;                
                result.append("DOCTYPE PUBLIC " + doctype.getPublicId() + " SYSTEM " + doctype.getSystemId() + "\n");
            }
            el = el.getNext();
        }
        
        final String golden = 
            "DOCTYPE PUBLIC null SYSTEM null\n" +
            "DOCTYPE PUBLIC null SYSTEM system\n" +
            "DOCTYPE PUBLIC null SYSTEM system-internal\n" +
            "DOCTYPE PUBLIC public ID SYSTEM system ID\n" +
            "DOCTYPE PUBLIC public-internal ID SYSTEM \n";
        
        if (result.toString().equals(golden) == false) {
            System.out.println("Result:\n" + result.toString());
            fail("DOCTYPE test failed!");            
        }            
    }
}
... 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.