1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.List ; 26 import java.util.Properties ; 27 import java.util.TreeMap ; 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 41 public class LookupMergerImplTest extends NbTestCase { 42 43 private static List <String > targetsRun = new ArrayList <String >(); 44 static { 45 Actions.TARGET_RUNNER = new Actions.TargetRunner() { 46 public void runTarget(FileObject scriptFile, String [] targetNameArray, Properties props) { 47 targetsRun.add(scriptFile.getNameExt() + ":" + Arrays.toString(targetNameArray) + ":" + new TreeMap <Object ,Object >(props)); 48 } 49 }; 50 } 51 52 56 public LookupMergerImplTest(String name) { 57 super(name); 58 } 59 60 64 @Override 65 protected void setUp() throws Exception { 66 super.setUp(); 67 targetsRun.clear(); 68 clearWorkDir(); 69 } 70 71 75 public void testActionBindingFromNatures() throws Exception { 76 File base = getWorkDir(); 77 File src = new File (base, "src"); 78 File x1 = new File (src, "x1"); 79 FileObject x1fo = FileUtil.createData(x1); 80 File x2 = new File (src, "x2"); 81 FileObject x2fo = FileUtil.createData(x2); 82 File y1 = new File (src, "y1"); 83 FileObject y1fo = FileUtil.createData(y1); 84 File y2 = new File (src, "y2"); 85 FileObject y2fo = FileUtil.createData(y2); 86 File buildXml = new File (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 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 { public String [] getSupportedActions() { 105 return new String [] {cmd}; 106 } 107 public void invokeAction(String command, Lookup context) throws IllegalArgumentException { 108 ranMockAction[0] = true; 109 } 110 public boolean isActionEnabled(String command, Lookup context) throws IllegalArgumentException { 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 |