1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.swing.Icon ; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.api.project.ProjectManager; 29 import org.netbeans.junit.MockServices; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.modules.project.ui.actions.ProjectActionTest.ActionCreator; 32 import org.netbeans.spi.project.ActionProvider; 33 import org.netbeans.spi.project.ui.support.ProjectActionPerformer; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.loaders.DataObject; 37 import org.openide.util.Lookup; 38 import org.openide.util.lookup.Lookups; 39 40 public class FileCommandActionTest extends NbTestCase { 41 42 public FileCommandActionTest(String name) { 43 super( name ); 44 } 45 46 private FileObject p1; 47 private FileObject p2; 48 private FileObject f1_1; 49 private FileObject f1_2; 50 private FileObject f2_1; 51 private FileObject f2_2; 52 private DataObject d1_1; 53 private DataObject d1_2; 54 private DataObject d2_1; 55 private DataObject d2_2; 56 private TestSupport.TestProject project1; 57 private TestSupport.TestProject project2; 58 59 protected void setUp() throws Exception { 60 super.setUp(); 61 MockServices.setServices(TestSupport.TestProjectFactory.class); 62 clearWorkDir(); 63 FileObject workDir = FileUtil.toFileObject(getWorkDir()); 64 65 66 p1 = TestSupport.createTestProject( workDir, "project1" ); 67 f1_1 = p1.createData("f1_1.java"); 68 f1_2 = p1.createData("f1_2.krava"); 69 d1_1 = DataObject.find(f1_1); 70 d1_2 = DataObject.find(f1_2); 71 72 project1 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p1 ); 73 project1.setLookup( Lookups.fixed( new Object [] { new TestActionProvider() } ) ); 74 75 p2 = TestSupport.createTestProject( workDir, "project2" ); 76 f2_1 = p2.createData("f2_1.java"); 77 f2_2 = p2.createData("f2_2.krava"); 78 d2_1 = DataObject.find(f2_1); 79 d2_2 = DataObject.find(f2_2); 80 81 project2 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p2 ); 82 83 } 84 85 protected void tearDown() throws Exception { 86 clearWorkDir(); 87 super.tearDown(); 88 } 89 90 public boolean runInEQ () { 91 return true; 92 } 93 94 public void testCommandEnablement() { 95 TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); 96 FileCommandAction action = new FileCommandAction( "COMMAND", "TestFileCommandAction", (Icon )null, lookup ); 97 98 assertFalse( "Action should NOT be enabled", action.isEnabled() ); 99 100 lookup.change(d1_1); 101 assertTrue( "Action should be enabled", action.isEnabled() ); 102 103 lookup.change(d1_1, d1_2); 104 assertFalse( "Action should NOT be enabled", action.isEnabled() ); 105 106 lookup.change(d1_1, d2_1); 107 assertFalse( "Action should NOT be enabled", action.isEnabled() ); 108 109 } 110 111 public void testAcceleratorsPropagated() { 112 ProjectActionTest.doTestAcceleratorsPropagated(new ActionCreator() { 113 public ProjectAction create(Lookup l) { 114 return new FileCommandAction("command", "TestProjectAction", (Icon ) null, l); 115 } 116 }, true); 117 } 118 119 private static class TestActionProvider implements ActionProvider { 120 121 public String COMMAND = "COMMAND"; 122 123 private String [] ACTIONS = new String [] { COMMAND }; 124 125 private List <String > invocations = new ArrayList <String >(); 126 127 public String [] getSupportedActions() { 128 return ACTIONS; 129 } 130 131 public void invokeAction( String command, Lookup context ) throws IllegalArgumentException { 132 133 if ( COMMAND.equals( command ) ) { 134 invocations.add( command ); 135 } 136 else { 137 throw new IllegalArgumentException (); 138 } 139 140 } 141 142 public boolean isActionEnabled( String command, Lookup context) throws IllegalArgumentException { 143 144 if ( COMMAND.equals( command ) ) { 145 Collection dobjs = context.lookupAll(DataObject.class); 146 for ( Iterator it = dobjs.iterator(); it.hasNext(); ) { 147 DataObject dobj = (DataObject)it.next(); 148 if ( !dobj.getPrimaryFile().getNameExt().endsWith( ".java" ) ) { 149 return false; 150 } 151 } 152 return true; 153 } 154 else { 155 throw new IllegalArgumentException (); 156 } 157 158 } 159 160 161 } 162 163 private static class TestActionPerformer implements ProjectActionPerformer { 164 165 private int enableCount; 166 private Project project; 167 private boolean on; 168 169 public boolean enable( Project project ) { 170 enableCount ++; 171 this.project = project; 172 return on; 173 } 174 175 public void perform( Project project ) { 176 this.project = project; 177 } 178 179 public void on( boolean on ) { 180 this.on = on; 181 } 182 183 public void clear() { 184 this.project = null; 185 enableCount = 0; 186 } 187 188 189 } 190 191 } 192 | Popular Tags |