Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.refactoring.plugins; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.io.OutputStream; |
47 |
import org.junit.*; |
48 |
import org.netbeans.junit.NbTestCase; |
49 |
import org.netbeans.modules.refactoring.api.AbstractRefactoring; |
50 |
import org.netbeans.modules.refactoring.api.CopyRefactoring; |
51 |
import org.netbeans.modules.refactoring.api.RefactoringSession; |
52 |
import org.netbeans.modules.refactoring.api.SingleCopyRefactoring; |
53 |
import org.netbeans.modules.refactoring.spi.RefactoringPlugin; |
54 |
import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory; |
55 |
import org.openide.filesystems.FileObject; |
56 |
import org.openide.filesystems.FileUtil; |
57 |
import org.openide.util.lookup.Lookups; |
58 |
import org.openide.util.test.MockLookup; |
59 |
|
60 |
/** |
61 |
* |
62 |
* @author Ralph Ruijs |
63 |
*/ |
64 |
public class FilesCopyPluginTest extends NbTestCase { |
65 |
|
66 |
private FileObject f; |
67 |
private FileObject f1; |
68 |
|
69 |
public FilesCopyPluginTest(String name) { |
70 |
super(name); |
71 |
} |
72 |
|
73 |
@BeforeClass |
74 |
public static void setUpClass() throws Exception { |
75 |
MockLookup.setInstances(new RefactoringPluginFactory() { |
76 |
|
77 |
@Override |
78 |
public RefactoringPlugin createInstance(AbstractRefactoring refactoring) { |
79 |
return new FilesCopyPlugin(refactoring); |
80 |
} |
81 |
}); |
82 |
} |
83 |
|
84 |
@AfterClass |
85 |
public static void tearDownClass() throws Exception { |
86 |
} |
87 |
|
88 |
@Before |
89 |
public void setUp() throws IOException { |
90 |
clearWorkDir(); |
91 |
f = FileUtil.createData(FileUtil.toFileObject(getWorkDir()), "test"); |
92 |
OutputStream outputStream = f.getOutputStream(); |
93 |
outputStream.write("test".getBytes()); |
94 |
outputStream.close(); |
95 |
f1 = FileUtil.createData(FileUtil.toFileObject(getWorkDir()), "test1"); |
96 |
outputStream = f1.getOutputStream(); |
97 |
outputStream.write("test1".getBytes()); |
98 |
outputStream.close(); |
99 |
} |
100 |
|
101 |
@After |
102 |
public void tearDown() { |
103 |
} |
104 |
|
105 |
/** |
106 |
* Test of preCheck method, of class FilesCopyPlugin. |
107 |
*/ |
108 |
@Test |
109 |
public void testChecks() { |
110 |
FilesCopyPlugin copyPlugin = new FilesCopyPlugin(null); |
111 |
assertEquals(null, copyPlugin.preCheck()); |
112 |
assertEquals(null, copyPlugin.fastCheckParameters()); |
113 |
assertEquals(null, copyPlugin.checkParameters()); |
114 |
} |
115 |
|
116 |
/** |
117 |
* Test of prepare method, of class FilesCopyPlugin. |
118 |
*/ |
119 |
@Test |
120 |
public void testPrepare() throws Exception { |
121 |
RefactoringSession session = RefactoringSession.create("junit"); |
122 |
CopyRefactoring copyRefactoring = new CopyRefactoring(Lookups.fixed(f)); |
123 |
File target = new File(getWorkDirPath() + File.separatorChar + "junit" + File.separatorChar); |
124 |
copyRefactoring.setTarget(Lookups.fixed(target.toURI().toURL())); |
125 |
copyRefactoring.prepare(session); |
126 |
session.doRefactoring(true); |
127 |
FileObject newOne = copyRefactoring.getContext().lookup(FileObject.class); |
128 |
assertEquals(newOne.asText(), f.asText()); |
129 |
|
130 |
session = RefactoringSession.create("junit1"); |
131 |
target = new File(getWorkDirPath() + File.separatorChar + "junit1" + File.separatorChar); |
132 |
copyRefactoring = new CopyRefactoring(Lookups.fixed(f, f1)); |
133 |
copyRefactoring.setTarget(Lookups.fixed(target.toURI().toURL())); |
134 |
copyRefactoring.prepare(session); |
135 |
session.doRefactoring(true); |
136 |
FileObject[] newOnes = copyRefactoring.getContext().lookup(FileObject[].class); |
137 |
assertEquals(newOnes[0].asText(), f.asText()); |
138 |
assertEquals(newOnes[0].getName(), f.getName()); |
139 |
assertEquals(newOnes[1].asText(), f1.asText()); |
140 |
assertEquals(newOnes[1].getName(), f1.getName()); |
141 |
|
142 |
session = RefactoringSession.create("junit2"); |
143 |
SingleCopyRefactoring singleCopyRefactoring = new SingleCopyRefactoring(Lookups.fixed(f)); |
144 |
target = new File(getWorkDirPath() + File.separatorChar + "junit" + File.separatorChar); |
145 |
singleCopyRefactoring.setTarget(Lookups.fixed(target.toURI().toURL())); |
146 |
String newName = "HelloWorld"; |
147 |
singleCopyRefactoring.setNewName(newName); |
148 |
singleCopyRefactoring.prepare(session); |
149 |
session.doRefactoring(true); |
150 |
FileObject newOneRenamed = singleCopyRefactoring.getContext().lookup(FileObject.class); |
151 |
assertEquals(newOneRenamed.asText(), f.asText()); |
152 |
assertEquals(newOneRenamed.getName(), newName); |
153 |
} |
154 |
} |