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-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.loaders;

import java.lang.ref.WeakReference;
import java.util.*;
import junit.textui.TestRunner;
import org.openide.filesystems.FileSystem;
import java.util.Enumeration;
import org.openide.nodes.Node;
import org.openide.cookies.InstanceCookie;
import org.openide.filesystems.Repository;
import org.netbeans.junit.*;
import org.openide.filesystems.*;

/** Creation of data object is said to be slow due to
 * poor implementation of BrokenDataShadow validate functionality.
 * @author Jaroslav Tulach
 */
public class DataShadowBrokenSlownessTest extends NbTestCase {
    /** List of DataObject */
    private List shadows, brokenShadows;
    /** folder to work with */
    private DataFolder folder;
    /** fs we work on */
    private FileSystem lfs;
    /** start time of the test */
    private long time;
    /** table with test resutls Integer -> Long */
    private static HashMap times = new HashMap ();
    
    public DataShadowBrokenSlownessTest (String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(DataShadowBrokenSlownessTest.class));
        // Kill process since currently this pops up GUI windows or something:
        System.exit(0);
    }
    
    protected void setUp() throws Exception {
        TestUtilHid.destroyLocalFileSystem(getName());
        lfs = TestUtilHid.createLocalFileSystem(getName(), new String[] {
            "shadows/",
            "brokenshadows",
            "folder/original.txt",
            "folder/orig.txt",
            "modify/"
        });
        Repository.getDefault ().addFileSystem (lfs);
        
        int count = getNumber ().intValue ();
        
		shadows = createShadows (
            DataObject.find (lfs.findResource("folder/original.txt")), 
            DataFolder.findFolder (lfs.findResource ("shadows")),
            count
        );
        
		brokenShadows = /*Collections.EMPTY_LIST; */createShadows (
            DataObject.find (lfs.findResource("folder/orig.txt")), 
            DataFolder.findFolder (lfs.findResource ("shadows")),
            count
        );
        
        DataObject.find (lfs.findResource("folder/orig.txt")).delete ();
        
        ListIterator it = brokenShadows.listIterator ();
        while (it.hasNext ()) {
            DataObject obj = (DataObject)it.next ();
            assertFalse ("Is not valid", obj.isValid ());
            assertTrue ("Used to be shadow", obj instanceof DataShadow);
            DataObject newObj = DataObject.find (obj.getPrimaryFile ());
            assertTrue ("They are different", newObj != obj);
            assertFalse ("It is not shadow, as it is broken", newObj instanceof DataShadow);
            
            it.set (newObj);
        }
        
        FileObject files = lfs.findResource ("modify");
        for (int i = 0; i < 200; i++) {
            FileUtil.createData (files, "empty" + i + ".txt");
        }
        
        assertEquals ("Children created", 200, files.getChildren ().length);
        
        folder = DataFolder.findFolder (files);
        time = System.currentTimeMillis ();
    }
    
    /** Returns number of this test */
    private Integer getNumber () {
        try {
            java.util.regex.Matcher m = java.util.regex.Pattern.compile ("test[a-zA-Z]*([0-9]+)").matcher (getName ());
            assertTrue ("Name does not contain numbers: " + getName (), m.find ());
            return Integer.valueOf (m.group (1));
        } catch (Exception ex) {
            ex.printStackTrace();
            fail ("Name: " + getName () + " does not represent number");
            return null;
        }
    }
    
    private static List createShadows (DataObject original, DataFolder target, int count) throws java.io.IOException {
        ArrayList list = new ArrayList (count);
        for (int i = 0; i < count; i++) {
            DataShadow shad = DataShadow.create(target, original.getName()+i, original, "shadow");
            list.add (shad);
        }
        return list;
    }
    
    protected void tearDown() throws Exception {
        long now = System.currentTimeMillis ();
        
        Repository.getDefault ().removeFileSystem (lfs);
        TestUtilHid.destroyLocalFileSystem(getName());
        
        times.put (getNumber (), new Long (now - time));
                
        // and verify
        assertNumbersAreSane ();
    }
    
    public void test0 () throws java.io.IOException {
        folder.delete ();
    }
    
    public void test10 () throws java.io.IOException {
        folder.delete ();
    }
    
    public void test99 () throws java.io.IOException {
        folder.delete ();
    }

    public void test245 () throws java.io.IOException {
        folder.delete ();
    }
    
    public void test552 () throws java.io.IOException {
        folder.delete ();
    }
    
    public void test987 () throws java.io.IOException {
        folder.delete ();
    }
    
    /** Compares that the numbers are in sane bounds */
    private void assertNumbersAreSane () {
        StringBuffer error = new StringBuffer ();
        {
            Iterator it = times.entrySet ().iterator ();
            while (it.hasNext ()) {
                Map.Entry en = (Map.Entry)it.next ();
                error.append ("Test "); error.append (en.getKey ());
                error.append (" took "); error.append (en.getValue ());
                error.append (" ms\n");
            }
        }
        
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        
        {
            Iterator it = times.values ().iterator ();
            while (it.hasNext ()) {
                Long l = (Long)it.next ();
                if (l.longValue () > max) max = l.longValue ();
                if (l.longValue () < min) min = l.longValue ();
            }
        }
        
        if (min * 10 < max) {
            fail ("Too big differences when various number of shadows is used:\n" + error.toString ());
        }
        
        System.err.println(error.toString ());
    }
}
... 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.