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

package org.netbeans.modules.ant.freeform.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.netbeans.junit.NbTestCase;
import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
import org.netbeans.spi.project.support.ant.PropertyProvider;
import org.netbeans.spi.project.support.ant.PropertyUtils;

/**
 * Tests for ProjectModel.
 *
 * @author David Konecny
 */
public class ProjectModelTest extends NbTestCase {
    
    public ProjectModelTest(String testName) {
        super(testName);
    }
    
    public void testCreateEmptyModel() throws Exception {
        File baseFolder = new File(getWorkDir(), "somefolder/");
        File nbProjectFolder = new File(getWorkDir(), "nbprojfolder/");
        Properties p = new Properties();
        p.setProperty("key", "value");
        PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{PropertyUtils.fixedPropertyProvider(p)});
        ProjectModel pm = ProjectModel.createEmptyModel(baseFolder, nbProjectFolder, evaluator);
        assertNotNull(pm);
        assertEquals("Base folder incorrect", baseFolder, pm.getBaseFolder());
        assertEquals("NB project folder incorrect", nbProjectFolder, pm.getNBProjectFolder());
        assertEquals("Evaluator incorrect", evaluator, pm.getEvaluator());
        assertEquals("Evaluator is different", 1, pm.getEvaluator().getProperties().keySet().size());
    }

    // tests: addSourceFolder, removeSourceFolder, setSourceLevel, getCompilationUnit
    public void testBasicFunctionality() throws Exception {
        ProjectModel pm = createEmptyProjectModel();
        pm.setSourceLevel("custom_source_level");
        FreeformProjectGenerator.SourceFolder sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "loc1";
        sf.label = "label_loc1";
        sf.type = "java";
        pm.addSourceFolder(sf);
        assertEquals("Number of source folders does not match", 1, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size());
        List keys = generateKeys(new Object[]{"loc1"}, new String[]{"label_loc1"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
        FreeformProjectGenerator.JavaCompilationUnit cu1 = (FreeformProjectGenerator.JavaCompilationUnit)pm.getJavaCompilationUnits().get(0);
        FreeformProjectGenerator.JavaCompilationUnit cu2 = pm.getCompilationUnit((ProjectModel.CompilationUnitKey)keys.get(0));
        assertEquals("Must be the same instance", cu1, cu2);

        sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "loc2";
        sf.label = "label_loc2";
        sf.type = "java";
        pm.addSourceFolder(sf);
        assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{"loc1", "loc2"}, new String[]{"label_loc1", "label_loc2"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
        cu1 = (FreeformProjectGenerator.JavaCompilationUnit)pm.getJavaCompilationUnits().get(0);
        cu2 = pm.getCompilationUnit((ProjectModel.CompilationUnitKey)keys.get(0));
        assertEquals("Must be the same instance", cu1, cu2);
        cu1 = (FreeformProjectGenerator.JavaCompilationUnit)pm.getJavaCompilationUnits().get(1);
        cu2 = pm.getCompilationUnit((ProjectModel.CompilationUnitKey)keys.get(1));
        assertEquals("Must be the same instance", cu1, cu2);
        
        assertEquals("Source level does not match", "custom_source_level", 
            ((FreeformProjectGenerator.JavaCompilationUnit)(pm.getJavaCompilationUnits().get(0))).sourceLevel);
        assertEquals("Source level does not match", "custom_source_level", 
            ((FreeformProjectGenerator.JavaCompilationUnit)(pm.getJavaCompilationUnits().get(1))).sourceLevel);
        pm.setSourceLevel("jdk15");
        assertEquals("Source level does not match", "jdk15", 
            ((FreeformProjectGenerator.JavaCompilationUnit)(pm.getJavaCompilationUnits().get(0))).sourceLevel);
        assertEquals("Source level does not match", "jdk15", 
            ((FreeformProjectGenerator.JavaCompilationUnit)(pm.getJavaCompilationUnits().get(1))).sourceLevel);
        
        pm.removeSourceFolder(0);
        assertEquals("Number of source folders does not match", 1, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{"loc2"}, new String[]{"label_loc2"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
    }

    public void testAdvancedAddSourceFolder() throws Exception {
        ProjectModel pm = createEmptyProjectModel();
        pm.setSourceLevel("jdk1x");
        FreeformProjectGenerator.SourceFolder sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "loc1";
        sf.label = "label_loc1";
        sf.type = "java";
        pm.addSourceFolder(sf);
        assertEquals("Number of source folders does not match", 1, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size());
        List keys = generateKeys(new Object[]{"loc1"}, new String[]{"label_loc1"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());

        sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "loc2";
        sf.label = "label_loc2";
        sf.type = "java";
        pm.addSourceFolder(sf);
        assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{"loc1", "loc2"}, new String[]{"label_loc1", "label_loc2"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());

        sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "locWEB";
        sf.label = "label_locWEB";
        sf.type = "web";
        pm.addSourceFolder(sf);
        assertEquals("Number of source folders does not match", 3, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{"loc1", "loc2"}, new String[]{"label_loc1", "label_loc2"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());

        pm.removeSourceFolder(2);
        assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 2, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 2, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{"loc1", "loc2"}, new String[]{"label_loc1", "label_loc2"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
        
        pm.updateCompilationUnits(false);
        assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{Arrays.asList(new String[]{"loc1", "loc2"})}, new String[]{null});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
        
        sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "loc3";
        sf.label = "label_loc3";
        sf.type = "java";
        pm.addSourceFolder(sf);
        assertEquals("Number of source folders does not match", 3, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{Arrays.asList(new String[]{"loc1", "loc2", "loc3"})}, new String[]{null});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
        
        sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "locWEB";
        sf.label = "label_locWEB";
        sf.type = "web";
        pm.addSourceFolder(sf);
        assertEquals("Number of source folders does not match", 4, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{Arrays.asList(new String[]{"loc1", "loc2", "loc3"})}, new String[]{null});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
        
        pm.removeSourceFolder(0);
        pm.removeSourceFolder(0);
        assertEquals("Number of source folders does not match", 2, pm.getSourceFoldersCount());
        assertEquals("Number of comp units does not match", 1, pm.getJavaCompilationUnits().size());
        assertEquals("Number of comp unit keys does not match", 1, pm.createCompilationUnitKeys().size());
        keys = generateKeys(new Object[]{"loc3"}, new String[]{"label_loc3"});
        assertKeyEquals(keys, pm.createCompilationUnitKeys());
    }

    public void testCreateCompilationUnitKeys() throws Exception {
        List sources;
        List units;
        List keys;
        List createdKeys;
        
        ProjectModel pm = createEmptyProjectModel();
        
        // case: some source folders; no comp unit
        // expected result: one key for each source folder
        sources = generateSources(new String[]{"src1", "src2", "src3"});
        units = new ArrayList();
        keys = generateKeys(new Object[]{"src1", "src2", "src3"}, new String[]{"src1", "src2", "src3"});
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        createdKeys = pm.createCompilationUnitKeys();
        assertKeyEquals(keys, createdKeys);
        
        // case: one source folder; one comp unit for the source
        // expected result: one key
        sources = generateSources(new String[]{"src1"});
        units = generateUnits(new Object[]{"src1"});
        keys = generateKeys(new Object[]{"src1"}, new String[]{"src1"});
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        createdKeys = pm.createCompilationUnitKeys();
        assertKeyEquals(keys, createdKeys);
        
        // case: two source folders; two comp unit for the sources
        // expected result: two key
        sources = generateSources(new String[]{"src1", "src2"});
        units = generateUnits(new Object[]{"src1", "src2"});
        keys = generateKeys(new Object[]{"src1", "src2"}, new String[]{"src1", "src2"});
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        createdKeys = pm.createCompilationUnitKeys();
        assertKeyEquals(keys, createdKeys);
        
        // case: two source folders; one comp unit for both sources
        // expected result: one key with null as location
        sources = generateSources(new String[]{"src1", "src2"});
        units = generateUnits(new Object[]{Arrays.asList(new String[]{"src1", "src2"})});
        keys = generateKeys(new Object[]{Arrays.asList(new String[]{"src1", "src2"})}, new String[]{null});
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        createdKeys = pm.createCompilationUnitKeys();
        assertKeyEquals(keys, createdKeys);
        
        // case: mixed source folders; mixed comp units
        sources = generateSources(new String[]{"src1", "src2"});
        units = generateUnits(new Object[]{"src3", "src4"});
        // XXX: impl dependency: the result will first contain comp units and then source folders:
        keys = generateKeys(new Object[]{"src3", "src4", "src1", "src2"}, new String[]{null, null, "src1", "src2"});
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        createdKeys = pm.createCompilationUnitKeys();
        assertKeyEquals(keys, createdKeys);
        
        sources = generateSources(new String[]{"src1", "src2"});
        units = generateUnits(new Object[]{Arrays.asList(new String[]{"src2", "src3"})});
        // XXX: impl dependency: the result will first contain comp units and then source folders:
        keys = generateKeys(new Object[]{Arrays.asList(new String[]{"src2", "src3"}), "src1"}, new String[]{null, "src1"});
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        createdKeys = pm.createCompilationUnitKeys();
        assertKeyEquals(keys, createdKeys);
    }
    
    public void testUpdateCompilationUnits() throws Exception {
        List sources;
        List units = new ArrayList();
        List expectedUnits;
        
        ProjectModel pm = createEmptyProjectModel();
        
        sources = generateSources(new String[]{"src1", "src2"});
        FreeformProjectGenerator.JavaCompilationUnit cu = new FreeformProjectGenerator.JavaCompilationUnit();
        cu.packageRoots = Collections.singletonList("src1");
        FreeformProjectGenerator.JavaCompilationUnit.CP cp = new FreeformProjectGenerator.JavaCompilationUnit.CP();
        cp.classpath = "cp1"+File.pathSeparatorChar+"cp2";
        cp.mode = "compile";
        cu.classpath = Collections.singletonList(cp);
        cu.output = Arrays.asList(new String[]{"out1", "out2"});
        units.add(cu);
        cu = new FreeformProjectGenerator.JavaCompilationUnit();
        cu.packageRoots = Collections.singletonList("src2");
        cp = new FreeformProjectGenerator.JavaCompilationUnit.CP();
        cp.classpath = "cp2"+File.pathSeparatorChar+"cp3";
        cp.mode = "compile";
        cu.classpath = Collections.singletonList(cp);
        cu.output = Arrays.asList(new String[]{"out3"});
        units.add(cu);
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        pm.setSourceLevel("S_L_14");
        pm.updateCompilationUnits(false);
        assertEquals("Compilation units has to be merged into one", 1, units.size());
        cu = (FreeformProjectGenerator.JavaCompilationUnit)units.get(0);
        assertEquals("Compilation unit has to have two package roots", 2, cu.packageRoots.size());
        assertTrue("Missing expected package root: src1", cu.packageRoots.contains("src1"));
        assertTrue("Missing expected package root: src2", cu.packageRoots.contains("src2"));
        assertEquals("Compilation unit has to have three classpath items", 
            "cp1"+File.pathSeparatorChar+"cp2"+File.pathSeparatorChar+"cp3", 
            ((FreeformProjectGenerator.JavaCompilationUnit.CP)cu.classpath.get(0)).classpath);
        assertEquals("Compilation unit has to have three output items", 3, cu.output.size());
        assertTrue("Missing expected package root: out1", cu.output.contains("out1"));
        assertTrue("Missing expected package root: out2", cu.output.contains("out2"));
        assertTrue("Missing expected package root: out2", cu.output.contains("out2"));
        assertTrue("Missing expected source level: S_L_14", cu.sourceLevel.equals("S_L_14"));
        
        pm.setSourceFolders(sources);
        pm.setJavaCompilationUnits(units);
        pm.setSourceLevel("S_L_15");
        pm.updateCompilationUnits(true);
        assertEquals("Compilation units has to be cloned into two", 2, units.size());
        cu = (FreeformProjectGenerator.JavaCompilationUnit)units.get(0);
        assertEquals("Compilation unit has to have one package root", 1, cu.packageRoots.size());
        assertTrue("Missing expected package root", cu.packageRoots.contains("src1"));
        assertEquals("Compilation unit has to have three classpath items", 
            "cp1"+File.pathSeparatorChar+"cp2"+File.pathSeparatorChar+"cp3", 
            ((FreeformProjectGenerator.JavaCompilationUnit.CP)cu.classpath.get(0)).classpath);
        assertEquals("Compilation unit has to have three output items", 3, cu.output.size());
        assertTrue("Missing expected package root: out1", cu.output.contains("out1"));
        assertTrue("Missing expected package root: out2", cu.output.contains("out2"));
        assertTrue("Missing expected package root: out2", cu.output.contains("out2"));
        assertTrue("Missing expected source level: S_L_14", cu.sourceLevel.equals("S_L_15"));
        cu = (FreeformProjectGenerator.JavaCompilationUnit)units.get(1);
        assertEquals("Compilation unit has to have one package root", 1, cu.packageRoots.size());
        assertTrue("Missing expected package root", cu.packageRoots.contains("src2"));
        assertEquals("Compilation unit has to have three classpath items", 
            "cp1"+File.pathSeparatorChar+"cp2"+File.pathSeparatorChar+"cp3", 
            ((FreeformProjectGenerator.JavaCompilationUnit.CP)cu.classpath.get(0)).classpath);
        assertEquals("Compilation unit has to have three output items", 3, cu.output.size());
        assertTrue("Missing expected package root: out1", cu.output.contains("out1"));
        assertTrue("Missing expected package root: out2", cu.output.contains("out2"));
        assertTrue("Missing expected package root: out2", cu.output.contains("out2"));
        assertTrue("Missing expected source level: S_L_14", cu.sourceLevel.equals("S_L_15"));
        
    }
    
    public void testUpdatePrincipalSourceFolders() throws Exception {
        ProjectModel pm = createEmptyProjectModel();
        List l = pm.getSourceFolders();
        // base folder and proj folder are different
        List l2 = pm.updatePrincipalSourceFolders(l, true);
        assertEquals("Principal source for base directory must be added", 1, l2.size());
        l2 = pm.updatePrincipalSourceFolders(l, false);
        assertEquals("There are no external java source folders", 0, l2.size());
        
        pm = createEmptyProjectModel();
        FreeformProjectGenerator.SourceFolder sf = new FreeformProjectGenerator.SourceFolder();
        sf.location = "..\\something";
        sf.label = "something";
        sf.type = "java";
        l = pm.getSourceFolders();
        pm.addSourceFolder(sf);
        l2 = pm.updatePrincipalSourceFolders(l, false);
        assertEquals("One principal source must be added", 2, l2.size());
        
        pm = createEmptyProjectModel();
        FreeformProjectGenerator.SourceFolder sf2 = new FreeformProjectGenerator.SourceFolder();
        sf2.location = "..\\something2";
        sf2.label = "something2";
        sf2.type = "java";
        pm.addSourceFolder(sf);
        pm.addSourceFolder(sf2);
        pm.removeSourceFolder(0);
        pm.removeSourceFolder(0);
        pm.addSourceFolder(sf);
        pm.addSourceFolder(sf2);
        pm.removeSourceFolder(0);
        pm.addSourceFolder(sf);
        l = pm.getSourceFolders();
        l2 = pm.updatePrincipalSourceFolders(l, false);
        assertEquals("Two principal sources must be added", 4, l2.size());
        FreeformProjectGenerator.SourceFolder addedSF = (FreeformProjectGenerator.SourceFolder)l2.get(2);
        assertEquals("Added principal source must have the same label", addedSF.label, sf.label);
        assertEquals("Added principal source must have the same location", addedSF.location, sf.location);
        assertNull("Added principal source must have type==null", addedSF.type);
        addedSF = (FreeformProjectGenerator.SourceFolder)l2.get(3);
        assertEquals("Added principal source must have the same label", addedSF.label, sf2.label);
        assertEquals("Added principal source must have the same location", addedSF.location, sf2.location);
        assertNull("Added principal source must have type==null", addedSF.type);
        pm.removeSourceFolder(0);
        l = pm.getSourceFolders();
        l2 = pm.updatePrincipalSourceFolders(l, false);
        assertEquals("One principal source must be removed", 2, l2.size());

        FreeformProjectGenerator.SourceFolder sf2_ = new FreeformProjectGenerator.SourceFolder();
        sf2_.location = "..\\something2";
        sf2_.label = "something2";
        sf2_.type = null;
        pm = createEmptyProjectModel();
        pm.addSourceFolder(sf2);
        pm.addSourceFolder(sf2_);
        l = pm.getSourceFolders();
        l2 = pm.updatePrincipalSourceFolders(l, false);
        assertEquals("No principal sources added in this case because it already exist", l.size(), l2.size());
    }
    
    private List generateSources(String[] locations) {
        List l = new ArrayList(locations.length);
        for (int i=0; i
... 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.