KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > LookupMergerImplTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.freeform;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.TreeMap JavaDoc;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.spi.project.ActionProvider;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.Lookup;
35 import org.openide.util.lookup.Lookups;
36
37 /**
38  * Test for merging action providers.
39  * @author Jesse Glick
40  */

41 public class LookupMergerImplTest extends NbTestCase {
42
43     private static List JavaDoc<String JavaDoc> targetsRun = new ArrayList JavaDoc<String JavaDoc>();
44     static {
45         Actions.TARGET_RUNNER = new Actions.TargetRunner() {
46             public void runTarget(FileObject scriptFile, String JavaDoc[] targetNameArray, Properties JavaDoc props) {
47                 targetsRun.add(scriptFile.getNameExt() + ":" + Arrays.toString(targetNameArray) + ":" + new TreeMap JavaDoc<Object JavaDoc,Object JavaDoc>(props));
48             }
49         };
50     }
51
52     /**
53      * Create test.
54      * @param name test name
55      */

56     public LookupMergerImplTest(String JavaDoc name) {
57         super(name);
58     }
59
60     /**
61      * Clear everything up.
62      * @throws Exception for whatever reason
63      */

64     @Override JavaDoc
65     protected void setUp() throws Exception JavaDoc {
66         super.setUp();
67         targetsRun.clear();
68         clearWorkDir();
69     }
70
71     /**
72      * Test that natures can add action behaviors, but not to the exclusion of the default impl.
73      * @throws Exception for various reasons
74      */

75     public void testActionBindingFromNatures() throws Exception JavaDoc {
76         File JavaDoc base = getWorkDir();
77         File JavaDoc src = new File JavaDoc(base, "src");
78         File JavaDoc x1 = new File JavaDoc(src, "x1");
79         FileObject x1fo = FileUtil.createData(x1);
80         File JavaDoc x2 = new File JavaDoc(src, "x2");
81         FileObject x2fo = FileUtil.createData(x2);
82         File JavaDoc y1 = new File JavaDoc(src, "y1");
83         FileObject y1fo = FileUtil.createData(y1);
84         File JavaDoc y2 = new File JavaDoc(src, "y2");
85         FileObject y2fo = FileUtil.createData(y2);
86         File JavaDoc buildXml = new File JavaDoc(base, "build.xml");
87         FileUtil.createData(buildXml);
88         AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, getName(), buildXml);
89         FreeformProject p = (FreeformProject) ProjectManager.getDefault().findProject(helper.getProjectDirectory());
90         FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
91         final String JavaDoc cmd = "twiddle-file";
92         tm.name = cmd;
93         tm.targets = Arrays.asList("twiddle");
94         FreeformProjectGenerator.TargetMapping.Context context = new FreeformProjectGenerator.TargetMapping.Context();
95         tm.context = context;
96         context.folder = "src";
97         context.format = "relative-path";
98         context.property = "file";
99         context.pattern = "^x";
100         context.separator = null;
101         FreeformProjectGenerator.putTargetMappings(helper, Arrays.asList(tm));
102         final boolean[] ranMockAction = {false};
103         class MockActionProvider implements ActionProvider { // similar to JavaActions
104
public String JavaDoc[] getSupportedActions() {
105                 return new String JavaDoc[] {cmd};
106             }
107             public void invokeAction(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
108                 ranMockAction[0] = true;
109             }
110             public boolean isActionEnabled(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
111                 FileObject f = context.lookup(FileObject.class);
112                 return f != null && !f.getNameExt().contains("2");
113             }
114         }
115         ActionProvider proxy = new LookupMergerImpl().merge(Lookups.fixed(new MockActionProvider(), new Actions(p)));
116         assertTrue(Arrays.asList(proxy.getSupportedActions()).contains(cmd));
117         Lookup selection = Lookups.singleton(x1fo);
118         assertTrue(proxy.isActionEnabled(cmd, selection));
119         proxy.invokeAction(cmd, selection);
120         assertEquals("[build.xml:[twiddle]:{file=x1}]", targetsRun.toString());
121         assertFalse(ranMockAction[0]);
122         targetsRun.clear();
123         selection = Lookups.singleton(x2fo);
124         assertTrue(proxy.isActionEnabled(cmd, selection));
125         proxy.invokeAction(cmd, selection);
126         assertEquals("[build.xml:[twiddle]:{file=x2}]", targetsRun.toString());
127         assertFalse(ranMockAction[0]);
128         targetsRun.clear();
129         selection = Lookups.singleton(y1fo);
130         assertTrue(proxy.isActionEnabled(cmd, selection));
131         proxy.invokeAction(cmd, selection);
132         assertEquals("[]", targetsRun.toString());
133         assertTrue(ranMockAction[0]);
134         selection = Lookups.singleton(y2fo);
135         assertFalse(proxy.isActionEnabled(cmd, selection));
136     }
137
138 }
139
Popular Tags