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

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.windows.persistence;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import org.netbeans.junit.NbTest;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;

import org.netbeans.core.windows.persistence.TCRefConfig;

import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.URLMapper;

/** Functionality tests for saving and loading TCRef configuration data
 *
 * @author Marek Slama
 */
public class TCRefParserTest extends NbTestCase {
    
    public TCRefParserTest() {
        super("");
    }
    
    public TCRefParserTest(java.lang.String testName) {
        super(testName);
    }
    
    public static void main(java.lang.String[] args) {
        junit.textui.TestRunner.run(suite());
    }
    
    public static NbTest suite() {
        NbTestSuite suite = new NbTestSuite(TCRefParserTest.class);
        return suite;
    }

    protected void setUp () throws Exception {
    }
    
    ////////////////////////////////
    //Testing CORRECT data
    ////////////////////////////////
    /** Test of loaded data
     */
    public void testLoadTCRef00 () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testLoadTCRef00 START");
        
        TCRefParser tcRefParser = createRefParser("data/valid/Windows/Modes/mode00","tcref00");
        
        TCRefConfig tcRefCfg = tcRefParser.load();
        
        //Check loaded data
        assertNotNull("Could not load data.", tcRefCfg);
        
        assertTrue("TopComponent is opened.", tcRefCfg.opened);
        
        System.out.println("TCRefParserTest.testLoadTCRef00 FINISH");
    }
    
    /** Test of loaded data
     */
    public void testLoadTCRef01 () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testLoadTCRef01 START");
        
        TCRefParser tcRefParser = createRefParser("data/valid/Windows/Modes/mode00","tcref01");
        
        TCRefConfig tcRefCfg = tcRefParser.load();
        
        //Check loaded data
        assertNotNull("Could not load data.", tcRefCfg);
        
        assertFalse("TopComponent is closed.", tcRefCfg.opened);
        
        System.out.println("TCRefParserTest.testLoadTCRef01 FINISH");
    }
    
    /** Test of saving
     */
    public void testSaveTCRef00 () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testSaveTCRef00 START");
        
        TCRefParser tcRefParser = createRefParser("data/valid/Windows/Modes/mode00","tcref00");
        
        TCRefConfig tcRefCfg1 = tcRefParser.load();
        
        tcRefParser.save(tcRefCfg1);
        
        TCRefConfig tcRefCfg2 = tcRefParser.load();
        
        //Compare data
        assertTrue("Compare configuration data",tcRefCfg1.equals(tcRefCfg2));
                
        System.out.println("TCRefParserTest.testSaveTCRef00 FINISH");
    }
    
    /** Test of saving
     */
    public void testSaveTCRef01 () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testSaveTCRef01 START");
        
        TCRefParser tcRefParser = createRefParser("data/valid/Windows/Modes/mode00","tcref01");
        
        TCRefConfig tcRefCfg1 = tcRefParser.load();
        
        tcRefParser.save(tcRefCfg1);
        
        TCRefConfig tcRefCfg2 = tcRefParser.load();
        
        //Compare data
        assertTrue("Compare configuration data",tcRefCfg1.equals(tcRefCfg2));
        
        System.out.println("TCRefParserTest.testSaveTCRef01 FINISH");
    }
    
    ////////////////////////////////
    //Testing INCORRECT data
    ////////////////////////////////
    /** Test of missing file
     */
    public void testLoadTCRef00Invalid () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testLoadTCRef00Invalid START");
        
        TCRefParser tcRefParser = createRefParser("data/invalid/Windows/Modes/mode00","tcref00");
        
        try {
            tcRefParser.load();
        } catch (FileNotFoundException exc) {
            //Missing file detected
            //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
            System.out.println("TCRefParserTest.testLoadTCRef00Invalid FINISH");
            return;
        }
        
        fail("Missing file was not detected.");
    }
    
    /** Test of empty file
     */
    public void testLoadTCRef01Invalid () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testLoadTCRef01Invalid START");
        
        TCRefParser tcRefParser = createRefParser("data/invalid/Windows/Modes/mode00","tcref01");
        
        try {
            tcRefParser.load();
        } catch (IOException exc) {
            //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
            System.out.println("TCRefParserTest.testLoadTCRef01Invalid FINISH");
            return;
        }
        
        fail("Empty file was not detected.");
    }
    
    /** Test of missing required attribute "id" of element "properties".
     */
    public void testLoadTCRef02Invalid () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testLoadTCRef02Invalid START");
        
        TCRefParser tcRefParser = createRefParser("data/invalid/Windows/Modes/mode00","tcref02");
        
        try {
            tcRefParser.load();
        } catch (IOException exc) {
            //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
            System.out.println("TCRefParserTest.testLoadTCRef02Invalid FINISH");
            return;
        }
        
        fail("Missing required attribute \"id\" of element \"properties\" was not detected.");
    }
    
    /** Test of file name and "id" mismatch.
     */
    public void testLoadTCRef03Invalid () throws Exception {
        System.out.println("");
        System.out.println("TCRefParserTest.testLoadTCRef03Invalid START");
        
        TCRefParser tcRefParser = createRefParser("data/invalid/Windows/Modes/mode00","tcref03");
        
        try {
            tcRefParser.load();
        } catch (IOException exc) {
            //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
            System.out.println("TCRefParserTest.testLoadTCRef03Invalid FINISH");
            return;
        }
        
        fail("Mismatch of file name and value of attribute \"id\" of element \"properties\" was not detected.");
    }
    
    private TCRefParser createRefParser (String path, String name) {
        URL url;
        url = TCRefParserTest.class.getResource(path);
        assertNotNull("url not found.",url);
        
        FileObject [] foArray = URLMapper.findFileObjects(url);
        assertNotNull("Test parent folder not found. Array is null.",foArray);
        assertTrue("Test parent folder not found. Array is empty.",foArray.length > 0);
        
        FileObject parentFolder = foArray[0];
        assertNotNull("Test parent folder not found. ParentFolder is null.",parentFolder);
        
        TCRefParser tcRefParser = new TCRefParser(name);
        tcRefParser.setInLocalFolder(true);
        tcRefParser.setLocalParentFolder(parentFolder);
        
        return tcRefParser;
    }
    
}
... 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.