|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2013 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* This is an implementation of an early-draft specification developed under the Java Community Process (JCP) and |
| 9 |
* is made available for testing and evaluation purposes only. |
| 10 |
* The code is not compatible with any specification of the JCP. |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* IBM Corporation - initial API and implementation |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.jdt.ui.tests.quickfix; |
| 16 |
|
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.Hashtable; |
| 19 |
|
| 20 |
import junit.framework.Test; |
| 21 |
import junit.framework.TestSuite; |
| 22 |
|
| 23 |
import org.eclipse.jdt.testplugin.JavaProjectHelper; |
| 24 |
import org.eclipse.jdt.testplugin.TestOptions; |
| 25 |
|
| 26 |
import org.eclipse.jface.preference.IPreferenceStore; |
| 27 |
|
| 28 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 29 |
import org.eclipse.jdt.core.IJavaProject; |
| 30 |
import org.eclipse.jdt.core.IPackageFragment; |
| 31 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 32 |
import org.eclipse.jdt.core.JavaCore; |
| 33 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
| 34 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
| 35 |
|
| 36 |
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; |
| 37 |
import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType; |
| 38 |
|
| 39 |
import org.eclipse.jdt.ui.PreferenceConstants; |
| 40 |
import org.eclipse.jdt.ui.tests.core.Java18ProjectTestSetup; |
| 41 |
import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal; |
| 42 |
|
| 43 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
| 44 |
|
| 45 |
public class QuickFixTest18 extends QuickFixTest { |
| 46 |
|
| 47 |
private static final Class THIS= QuickFixTest18.class; |
| 48 |
|
| 49 |
private IJavaProject fJProject1; |
| 50 |
|
| 51 |
private IPackageFragmentRoot fSourceFolder; |
| 52 |
|
| 53 |
public QuickFixTest18(String name) { |
| 54 |
super(name); |
| 55 |
} |
| 56 |
|
| 57 |
public static Test suite() { |
| 58 |
return new Java18ProjectTestSetup(new TestSuite(THIS)); |
| 59 |
} |
| 60 |
|
| 61 |
public static Test setUpTest(Test test) { |
| 62 |
return new Java18ProjectTestSetup(test); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
protected void setUp() throws Exception { |
| 67 |
Hashtable options= TestOptions.getDefaultOptions(); |
| 68 |
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); |
| 69 |
options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4"); |
| 70 |
options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_LOCAL_VARIABLE, JavaCore.DO_NOT_INSERT); |
| 71 |
|
| 72 |
|
| 73 |
JavaCore.setOptions(options); |
| 74 |
|
| 75 |
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); |
| 76 |
store.setValue(PreferenceConstants.CODEGEN_ADD_COMMENTS, false); |
| 77 |
|
| 78 |
fJProject1= Java18ProjectTestSetup.getProject(); |
| 79 |
|
| 80 |
StubUtility.setCodeTemplate(CodeTemplateContextType.METHODSTUB_ID, "", null); |
| 81 |
StubUtility.setCodeTemplate(CodeTemplateContextType.CONSTRUCTORSTUB_ID, "", null); |
| 82 |
|
| 83 |
fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
@Override |
| 88 |
protected void tearDown() throws Exception { |
| 89 |
JavaProjectHelper.clear(fJProject1, Java18ProjectTestSetup.getDefaultClasspath()); |
| 90 |
} |
| 91 |
|
| 92 |
public void testUnimplementedMethods() throws Exception { |
| 93 |
IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null); |
| 94 |
StringBuffer buf= new StringBuffer(); |
| 95 |
buf.append("package test2;\n"); |
| 96 |
buf.append("import java.io.IOException;\n"); |
| 97 |
buf.append("public interface Inter {\n"); |
| 98 |
buf.append(" int getCount(Object[] o) throws IOException;\n"); |
| 99 |
buf.append(" static int staticMethod(Object[] o) throws IOException{return 10;}\n"); |
| 100 |
buf.append(" default int defaultMethod(Object[] o) throws IOException{return 20;}\n"); |
| 101 |
buf.append("}\n"); |
| 102 |
pack2.createCompilationUnit("Inter.java", buf.toString(), false, null); |
| 103 |
|
| 104 |
|
| 105 |
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); |
| 106 |
buf= new StringBuffer(); |
| 107 |
buf.append("package test1;\n"); |
| 108 |
buf.append("import test2.Inter;\n"); |
| 109 |
buf.append("public class E implements Inter{\n"); |
| 110 |
buf.append("}\n"); |
| 111 |
ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); |
| 112 |
|
| 113 |
CompilationUnit astRoot= getASTRoot(cu); |
| 114 |
ArrayList proposals= collectCorrections(cu, astRoot); |
| 115 |
assertNumberOfProposals(proposals, 2); |
| 116 |
assertCorrectLabels(proposals); |
| 117 |
|
| 118 |
CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(1); |
| 119 |
String preview1= getPreviewContent(proposal); |
| 120 |
|
| 121 |
buf= new StringBuffer(); |
| 122 |
buf.append("package test1;\n"); |
| 123 |
buf.append("import test2.Inter;\n"); |
| 124 |
buf.append("public abstract class E implements Inter{\n"); |
| 125 |
buf.append("}\n"); |
| 126 |
String expected1= buf.toString(); |
| 127 |
|
| 128 |
proposal= (CUCorrectionProposal)proposals.get(0); |
| 129 |
String preview2= getPreviewContent(proposal); |
| 130 |
|
| 131 |
buf= new StringBuffer(); |
| 132 |
buf.append("package test1;\n"); |
| 133 |
buf.append("import java.io.IOException;\n"); |
| 134 |
buf.append("\n"); |
| 135 |
buf.append("import test2.Inter;\n"); |
| 136 |
buf.append("public class E implements Inter{\n"); |
| 137 |
buf.append("\n"); |
| 138 |
buf.append(" @Override\n"); |
| 139 |
buf.append(" public int getCount(Object[] o) throws IOException {\n"); |
| 140 |
buf.append(" return 0;\n"); |
| 141 |
buf.append(" }\n"); |
| 142 |
buf.append("}\n"); |
| 143 |
String expected2= buf.toString(); |
| 144 |
|
| 145 |
assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 }); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
public void testUnimplementedMethods2() throws Exception { |
| 150 |
StringBuffer buf= new StringBuffer(); |
| 151 |
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); |
| 152 |
buf= new StringBuffer(); |
| 153 |
buf.append("package test1;\n"); |
| 154 |
buf.append("public class MyString implements CharSequence{\n"); |
| 155 |
buf.append("}\n"); |
| 156 |
ICompilationUnit cu= pack1.createCompilationUnit("MyString.java", buf.toString(), false, null); |
| 157 |
|
| 158 |
CompilationUnit astRoot= getASTRoot(cu); |
| 159 |
ArrayList proposals= collectCorrections(cu, astRoot, 3); |
| 160 |
assertNumberOfProposals(proposals, 2); |
| 161 |
assertCorrectLabels(proposals); |
| 162 |
|
| 163 |
CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(1); |
| 164 |
String preview1= getPreviewContent(proposal); |
| 165 |
|
| 166 |
buf= new StringBuffer(); |
| 167 |
buf.append("package test1;\n"); |
| 168 |
buf.append("public abstract class MyString implements CharSequence{\n"); |
| 169 |
buf.append("}\n"); |
| 170 |
String expected1= buf.toString(); |
| 171 |
|
| 172 |
proposal= (CUCorrectionProposal)proposals.get(0); |
| 173 |
String preview2= getPreviewContent(proposal); |
| 174 |
|
| 175 |
buf= new StringBuffer(); |
| 176 |
buf.append("package test1;\n"); |
| 177 |
buf.append("public class MyString implements CharSequence{\n"); |
| 178 |
buf.append("\n"); |
| 179 |
buf.append(" @Override\n"); |
| 180 |
buf.append(" public char charAt(int arg0) {\n"); |
| 181 |
buf.append(" return 0;\n"); |
| 182 |
buf.append(" }\n\n"); |
| 183 |
buf.append(" @Override\n"); |
| 184 |
buf.append(" public int length() {\n"); |
| 185 |
buf.append(" return 0;\n"); |
| 186 |
buf.append(" }\n\n"); |
| 187 |
buf.append(" @Override\n"); |
| 188 |
buf.append(" public CharSequence subSequence(int arg0, int arg1) {\n"); |
| 189 |
buf.append(" return null;\n"); |
| 190 |
buf.append(" }\n"); |
| 191 |
buf.append("}\n"); |
| 192 |
String expected2= buf.toString(); |
| 193 |
|
| 194 |
assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 }); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
} |