1 19 20 package org.netbeans.modules.j2ee.refactoring.test; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.junit.NbTestSuite; 24 import org.netbeans.modules.refactoring.api.MoveClassRefactoring; 25 import org.netbeans.modules.refactoring.api.RefactoringSession; 26 import org.netbeans.modules.web.taglib.TLDDataObject; 27 import org.openide.cookies.EditorCookie; 28 import org.openide.cookies.OpenCookie; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileStateInvalidException; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.loaders.DataObject; 33 34 import java.io.IOException ; 35 36 40 public class J2EEMoveClassRefactoringTest extends J2EERefactoringTest { 41 private static final String PROJECT_WEBAPP = "webapp"; 42 private static final String PROJECT_WEBAPP2 = "webapp2"; 43 private static final String PROJECT_EJBMOD2 = "ejbmod2"; 44 private static final String PROJECT_WEBAPPEMPTY = "webapp-empty"; 45 46 public J2EEMoveClassRefactoringTest(String testName) { 47 super(testName); 48 } 49 50 public static NbTestSuite suite() { 51 NbTestSuite suite = new NbTestSuite("J2EEMoveClassRefactoringTest"); 52 suite.addTest(new J2EEMoveClassRefactoringTest("testPrepare")); 53 suite.addTest(new J2EEMoveClassRefactoringTest("testTldMoveClass")); 54 suite.addTest(new J2EEMoveClassRefactoringTest("testWebMoveClass")); 55 suite.addTest(new J2EEMoveClassRefactoringTest("testTldMoveClassOtherProject")); 56 suite.addTest(new J2EEMoveClassRefactoringTest("testWebMoveClassOtherProject")); 57 suite.addTest(new J2EEMoveClassRefactoringTest("testTldMoveClassOtherEmptyProject")); 58 suite.addTest(new J2EEMoveClassRefactoringTest("testEjbMoveClass")); 59 suite.addTest(new J2EEMoveClassRefactoringTest("testWebsvcMoveClass")); 60 suite.addTest(new J2EEMoveClassRefactoringTest("testCleanup")); 61 return suite; 62 } 63 64 67 public void testPrepare() { 68 openProject(PROJECT_WEBAPP); 69 openProject(PROJECT_WEBAPP2); 70 openProject(PROJECT_WEBAPPEMPTY); 71 openProject(PROJECT_EJBMOD2); 72 } 73 74 78 public void testTldMoveClass() throws FileStateInvalidException, IOException { 79 80 FileObject fo = FileUtil.toFileObject(getProjectFile(PROJECT_WEBAPP, "web/WEB-INF/tlds/mytld.tld")); 82 TLDDataObject tdo = ((TLDDataObject)DataObject.find(fo)); 83 ((OpenCookie)tdo.getCookie(OpenCookie.class)).open(); 84 86 doMoveClassRefactoring(PROJECT_WEBAPP, "abc.NewTagHandler", null, "xyz.ccc", "move tag handler"); 87 88 EditorCookie ec = (EditorCookie)tdo.getCookie(EditorCookie.class); 90 try { 91 ec.getDocument().insertString(0, " ", null); 92 ec.saveDocument(); 93 ec.getDocument().remove(0, 1); 94 ec.saveDocument(); 95 } catch (Exception e) { 96 fail(e.toString()); 97 } 98 100 try { 102 checkGoldenFile(PROJECT_WEBAPP, "src/java/xyz/ccc/NewTagHandler.java", "NewTagHandler.pass"); 103 104 checkFile(PROJECT_WEBAPP, "web/WEB-INF/tlds/mytld.tld", new String []{">xyz.ccc.NewTagHandler<"}, 106 new String []{">abc.NewTagHandler<"}); 107 108 } catch (FileStateInvalidException e) { 109 fail(e.getMessage()); 110 } catch (IOException e) { 111 fail(e.getMessage()); 112 } 113 } 114 115 121 public void testWebMoveClass() throws FileStateInvalidException, IOException { 122 123 doMoveClassRefactoring(PROJECT_WEBAPP, "abc.NewServlet", "abc.NewSimpleFilter", "abc.NewServletListener", "xyz.ccc", 124 "move classes", null); 125 126 try { 128 checkGoldenFile(PROJECT_WEBAPP, "src/java/xyz/ccc/NewServlet.java", "NewServlet.pass"); 129 checkGoldenFile(PROJECT_WEBAPP, "src/java/xyz/ccc/NewSimpleFilter.java", "NewSimpleFilter.pass"); 130 checkGoldenFile(PROJECT_WEBAPP, "src/java/xyz/ccc/NewServletListener.java", "NewServletListener.pass"); 131 checkFile(PROJECT_WEBAPP, "web/WEB-INF/web.xml", 133 new String []{">xyz.ccc.NewServlet<", ">xyz.ccc.NewSimpleFilter<", ">xyz.ccc.NewServletListener<"}, 134 new String []{">abc.NewServlet<", ">abc.NewSimpleFilter<", ">abc.NewServletListener<"}); 135 } catch (FileStateInvalidException e) { 136 fail(e.getMessage()); 137 } catch (IOException e) { 138 fail(e.getMessage()); 139 } 140 } 141 142 146 public void testTldMoveClassOtherProject() throws FileStateInvalidException, IOException { 147 148 149 doMoveClassRefactoring(PROJECT_WEBAPP, "xyz.ccc.NewTagHandler", PROJECT_WEBAPP2, "pkg.subpkg", "move tag handler to other project" 150 ); 151 152 try { 154 checkGoldenFile(PROJECT_WEBAPP2, "src/java/pkg/subpkg/NewTagHandler.java", "NewTagHandlerOtherProject.pass"); 155 156 checkFile(PROJECT_WEBAPP, "web/WEB-INF/tlds/mytld.tld", null, 158 new String []{">xyz.ccc.NewTagHandler<", "NewTagHandler"}); 159 160 checkFile(PROJECT_WEBAPP2, "web/WEB-INF/tlds/tld2.tld", 162 new String []{">pkg.subpkg.NewTagHandler<", "NewTagHandler<"}, null); 163 } catch (FileStateInvalidException e) { 164 fail(e.getMessage()); 165 } catch (IOException e) { 166 fail(e.getMessage()); 167 } 168 } 169 170 174 public void testTldMoveClassOtherEmptyProject() throws FileStateInvalidException, IOException { 175 RefactoringSession result = RefactoringSession.create("move tag handler to other project where no tld exists"); 178 MoveClassRefactoring refactoring = createMoveClassRefactoring(PROJECT_WEBAPP2, "pkg.subpkg.NewTagHandler", PROJECT_WEBAPPEMPTY, 179 "pkg1.pkg2" 180 ); 181 assertNotNull(refactoring.prepare(result)); 182 } 183 184 185 191 public void testWebMoveClassOtherProject() throws FileStateInvalidException, IOException { 192 193 doMoveClassRefactoring(PROJECT_WEBAPP, "xyz.ccc.NewServlet", "xyz.ccc.NewSimpleFilter", "xyz.ccc.NewServletListener", 194 "pkg.subpkg", "move classes to other project", PROJECT_WEBAPP2); 195 196 try { 198 checkGoldenFile(PROJECT_WEBAPP2, "src/java/pkg/subpkg/NewServlet.java", "NewServletOtherProject.pass"); 199 checkGoldenFile(PROJECT_WEBAPP2, "src/java/pkg/subpkg/NewSimpleFilter.java", 200 "NewSimpleFilterOtherProject.pass"); 201 checkGoldenFile(PROJECT_WEBAPP2, "src/java/pkg/subpkg/NewServletListener.java", 202 "NewServletListenerOtherProject.pass"); 203 204 checkFile(PROJECT_WEBAPP, "web/WEB-INF/web.xml", null, new String []{">xyz.ccc.NewServlet<", 206 ">xyz.ccc.NewSimpleFilter<", ">xyz.ccc.NewServletListener<", "NewSimpleFilter", "ServletContextListener", 207 "NewServlet"}); 208 checkFile(PROJECT_WEBAPP2, "web/WEB-INF/web.xml", new String []{">pkg.subpkg.NewServlet<", 210 ">pkg.subpkg.NewSimpleFilter<", ">pkg.subpkg.NewServletListener<", "NewSimpleFilter", 211 "ServletContextListener", "NewServlet"}, null); 212 } catch (FileStateInvalidException e) { 213 fail(e.getMessage()); 214 } catch (IOException e) { 215 fail(e.getMessage()); 216 } 217 } 218 219 235 public void testEjbMoveClass() throws FileStateInvalidException, IOException { 236 237 } 460 461 466 public void testWebsvcMoveClass() throws FileStateInvalidException, IOException { 467 468 doMoveClassRefactoring(PROJECT_EJBMOD2, "my.ws.server.NewWebServiceBean", null, "xyz.ccc", "move ws impl bean"); 469 470 try { 472 checkGoldenFile(PROJECT_EJBMOD2, "src/java/xyz/ccc/NewWebServiceBean.java", "NewWebServiceBean.pass"); 473 474 checkFile(PROJECT_EJBMOD2, "src/java/my/ws/server/NewWebService-config.xml", 476 new String []{"xyz.ccc.NewWebServiceBean", "packageName=\"my.ws.server\""}, 477 new String []{"my.ws.server.NewWebServiceBean"}); 478 479 checkFile(PROJECT_EJBMOD2, "src/conf/ejb-jar.xml", new String []{">xyz.ccc.NewWebServiceBean<"}, 481 new String []{">my.ws.server.NewWebServiceBean<"}); 482 483 } catch (FileStateInvalidException e) { 484 fail(e.getMessage()); 485 } catch (IOException e) { 486 fail(e.getMessage()); 487 } 488 489 doMoveClassRefactoring(PROJECT_EJBMOD2, "my.ws.server.NewWebServiceSEI", null, "xyz.ccc", "move ws SEI"); 490 491 try { 493 checkGoldenFile(PROJECT_EJBMOD2, "src/java/xyz/ccc/NewWebServiceSEI.java", "NewWebServiceSEI.pass"); 494 495 checkFile(PROJECT_EJBMOD2, "src/java/xyz/ccc/NewWebService-config.xml", 497 new String []{"xyz.ccc.NewWebServiceSEI", "packageName=\"xyz.ccc\""}, 498 new String []{"my.ws.server.NewWebServiceSEI"}); 499 500 checkFile(PROJECT_EJBMOD2, "src/conf/webservices.xml", new String []{">xyz.ccc.NewWebServiceSEI<"}, 502 new String []{">my.ws.server.NewWebServiceSEI<"}); 503 504 } catch (FileStateInvalidException e) { 505 fail(e.getMessage()); 506 } catch (IOException e) { 507 fail(e.getMessage()); 508 } 509 510 } 511 512 515 public void testCleanup() { 516 closeProject(PROJECT_WEBAPP); 517 closeProject(PROJECT_WEBAPP2); 518 closeProject(PROJECT_WEBAPPEMPTY); 519 closeProject(PROJECT_EJBMOD2); 520 } 521 522 527 public static void main(String [] args) { 528 TestRunner.run(suite()); 529 } 530 } 531 | Popular Tags |