1 package org.netbeans.test.refactoring.cleanup; 2 3 import java.util.Collections ; 4 import org.netbeans.jmi.javamodel.JavaClass; 5 import org.netbeans.jmi.javamodel.Resource; 6 import org.netbeans.junit.NbTestCase; 7 import org.netbeans.modules.refactoring.api.Problem; 8 import org.netbeans.modules.refactoring.api.RefactoringSession; 9 import org.netbeans.modules.refactoring.experimental.CleanUpRefactoring; 10 import org.netbeans.test.refactoring.experimental.Utility; 11 12 15 public class CleanUpRefactoringTest extends NbTestCase { 16 17 public CleanUpRefactoringTest() { 18 super(CleanUpRefactoringTest.class.getName()); 19 } 20 21 private void executeTest(CleanUpRefactoring ref, String clazz, 22 String descr, String goldenFile) throws Exception { 23 JavaClass jcls = Utility.findClass(clazz); 24 Resource res = (Resource) jcls.refImmediateComposite(); 25 ref.setResources(Collections.singletonList(res)); 26 RefactoringSession rs = RefactoringSession.create(descr); 27 Problem p = ref.prepare(rs); 28 assertTrue("Fatal problem while preparing " + descr, p == null || !p.isFatal()); 29 assertNull("Problem while doing refactoring " + descr, rs.doRefactoring(true)); 30 assertFile( 31 Utility.getFile(getDataDir(), clazz.replaceAll("\\.", "/") + ".java"), 32 getGoldenFile(goldenFile), getWorkDir()); 33 } 34 35 private CleanUpRefactoring createCompleteRefactoring() { 36 CleanUpRefactoring ref = new CleanUpRefactoring(); 37 ref.setRemoveUnusedCallableFeatures(true); 38 ref.setRemoveUnusedClasses(true); 39 ref.setRemoveUnusedFields(true); 40 ref.setRemoveUnusedLocalVars(true); 41 ref.setRemoveUnusedImports(true); 42 return ref; 43 } 44 45 public void testRemoveRedundantCasts() throws Exception { 46 CleanUpRefactoring ref = new CleanUpRefactoring(); 47 ref.setRemoveRedundantCasts(true); 48 executeTest( 49 ref, 50 "org.netbeans.test.cleanup.RemoveRedundantCasts", 51 "remove redundant casts", 52 "testRemoveRedundantCasts.pass"); 53 } 54 55 public void testRemoveUnusedElements() throws Exception { 56 CleanUpRefactoring ref = createCompleteRefactoring(); 57 ref.setRemoveUnusedImports(false); 58 executeTest( 59 ref, 60 "org.netbeans.test.cleanup.RemoveUnusedElements", 61 "remove unused elements", 62 "testRemoveUnusedElements.pass"); 63 } 64 65 public void testRemoveGenericsImports() throws Exception { 66 CleanUpRefactoring ref = createCompleteRefactoring(); 67 executeTest( 68 ref, 69 "org.netbeans.test.cleanup.RemoveGenericsImports", 70 "remove generics imports", 71 "testRemoveGenericsImports.pass"); 72 } 73 74 public void testRemoveJavaLangImports() throws Exception { 75 CleanUpRefactoring ref = createCompleteRefactoring(); 76 executeTest( 77 ref, 78 "org.netbeans.test.cleanup.RemoveJavaLangImports", 79 "remove java.lang imports", 80 "testRemoveJavaLangImports.pass"); 81 } 82 83 84 public void testRemoveRemovedElementsImports() throws Exception { 85 CleanUpRefactoring ref = createCompleteRefactoring(); 86 executeTest( 87 ref, 88 "org.netbeans.test.cleanup.RemoveRemovedElementsImports", 89 "remove removed elements imports", 90 "testRemoveRemovedElementsImports.pass"); 91 } 92 93 public void testRemovePackageImports() throws Exception { 94 CleanUpRefactoring ref = createCompleteRefactoring(); 95 executeTest( 96 ref, 97 "org.netbeans.test.cleanup.RemovePackageImports", 98 "remove package imports", 99 "testRemovePackageImports.pass"); 100 } 101 102 public void testRemoveSamePackageImports() throws Exception { 103 CleanUpRefactoring ref = createCompleteRefactoring(); 104 executeTest( 105 ref, 106 "org.netbeans.test.cleanup.RemoveSamePackageImports", 107 "remove same package imports", 108 "testRemoveSamePackageImports.pass"); 109 } 110 111 public void testRemoveUnusedVariablesWithInitialValue() throws Exception { 112 CleanUpRefactoring ref = createCompleteRefactoring(); 113 executeTest( 114 ref, 115 "org.netbeans.test.cleanup.RemoveUnusedWithInitialValue", 116 "remove unused with initial value", 117 "testRemoveUnusedWithInitialValue.pass"); 118 } 119 } 120 | Popular Tags |