KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > refactoring > move > MoveTestCase


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

19 package org.netbeans.test.j2ee.refactoring.move;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import org.netbeans.jmi.javamodel.Resource;
25 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
26 import org.netbeans.modules.refactoring.api.MoveClassRefactoring;
27 import org.netbeans.modules.refactoring.api.RefactoringSession;
28 import org.netbeans.test.j2ee.refactoring.RefactoringTestCase;
29 import org.netbeans.test.j2ee.refactoring.Utility;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileUtil;
32
33 /**
34  *
35  * @author eh103527
36  */

37 public class MoveTestCase extends RefactoringTestCase {
38
39     protected static final String JavaDoc DESTINATION_PACKAGE_1 = "org.netbeans.test.examples.movetest";
40     
41     protected static final String JavaDoc DESTINATION_PACKAGE_2 = "org.netbeans.test.examples.movetest.subpkg";
42     
43     /** Creates a new instance of MoveTestCase */
44     public MoveTestCase(String JavaDoc name) {
45         super(name);
46     }
47     
48     protected void moveClass(String JavaDoc name, String JavaDoc newPackage, boolean undo) {
49         setJavaClass(name);
50         ref("Move class "+name+" to "+newPackage);
51         
52         HashMap JavaDoc files=null;
53         RefactoringSession result = RefactoringSession.create("Move Class");
54         boolean finish=false;
55         Utility.prepareTest();
56         try {
57             Resource resource = (Resource) jc.refImmediateComposite();
58             FileObject classPathWorkDirFO = FileUtil.toFileObject(classPathWorkDir);
59             Collection JavaDoc list = new ArrayList JavaDoc(1);
60             list.add(resource);
61             MoveClassRefactoring refactoring = new MoveClassRefactoring(list);
62             if (refProblems( refactoring.preCheck() )) throw new Exception JavaDoc("Fatal problem");
63             refactoring.setTargetClassPathRoot(classPathWorkDirFO);
64             refactoring.setTargetPackageName(newPackage);
65             if (refProblems(refactoring.checkParameters())) throw new Exception JavaDoc("Fatal problem");
66             if (refProblems(refactoring.prepare(result) )) throw new Exception JavaDoc("Fatal problem");
67             files=getResources(result);
68             if (refProblems( result.doRefactoring(true) )) throw new Exception JavaDoc("Fatal problem");
69             finish=true;
70         } catch (Throwable JavaDoc t) {
71             if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) {
72                 t.printStackTrace(getLogStream());
73                 assertTrue(t.getMessage(), false);
74             }
75         } finally {
76             Utility.finishTest();
77         }
78         if (finish) {
79             try {
80                 Thread.sleep(5000);
81                 if (undo) {
82                     JavaMetamodel.getUndoManager().undo();
83                     Thread.sleep(10000);
84                 }
85                 compareResources(files, result, name, newPackage+"."+((name.indexOf('.') > -1) ? name.substring(name.lastIndexOf('.')+1):name));
86             } catch (Exception JavaDoc ex) {
87                 ex.printStackTrace(getLogStream());
88                 assertTrue(ex.getMessage(), false);
89             }
90         }
91     }
92 }
93
Popular Tags