1 /******************************************************************************* 2 * Copyright (c) 2005, 2020 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.text.tests; 15 16 import static org.junit.Assert.assertEquals; 17 18 import java.util.ListResourceBundle; 19 20 import org.junit.After; 21 import org.junit.Before; 22 import org.junit.Rule; 23 import org.junit.Test; 24 import org.junit.rules.ExternalResource; 25 import org.junit.rules.TestName; 26 27 import org.eclipse.jdt.testplugin.JavaProjectHelper; 28 import org.eclipse.jdt.text.tests.performance.EditorTestHelper; 29 import org.eclipse.jdt.text.tests.performance.ResourceTestHelper; 30 31 import org.eclipse.core.runtime.CoreException; 32 33 import org.eclipse.core.resources.IProject; 34 import org.eclipse.core.resources.ResourcesPlugin; 35 36 import org.eclipse.jface.text.IDocument; 37 import org.eclipse.jface.text.source.SourceViewer; 38 39 import org.eclipse.jdt.core.IJavaProject; 40 import org.eclipse.jdt.core.JavaCore; 41 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 42 43 import org.eclipse.jdt.internal.ui.actions.IndentAction; 44 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 45 46 /** 47 * 48 * @since 3.2 49 */ 50 public class IndentActionTest { 51 @Rule 52 public TestName tn= new TestName(); 53 54 private static final String PROJECT= "IndentTests"; 55 56 private final class IndentTestSetup extends ExternalResource { 57 private IJavaProject fJavaProject; 58 59 @Override before()60 protected void before() throws Exception { 61 fJavaProject= EditorTestHelper.createJavaProject(PROJECT, "testResources/indentation"); 62 fJavaProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB); 63 fJavaProject.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.FALSE); 64 fJavaProject.setOption(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.TRUE); 65 fJavaProject.setOption(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.TRUE); 66 } 67 68 @Override after()69 protected void after () { 70 if (fJavaProject != null) 71 try { 72 JavaProjectHelper.delete(fJavaProject); 73 } catch (CoreException e) { 74 e.printStackTrace(); 75 } 76 } 77 getProject()78 public IJavaProject getProject() { 79 IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT); 80 return JavaCore.create(project); 81 } 82 } 83 84 private static final class EmptyBundle extends ListResourceBundle { 85 @Override getContents()86 protected Object[][] getContents() { 87 return new Object[0][]; 88 } 89 } 90 91 @Rule 92 public IndentTestSetup indentTestSetup=new IndentTestSetup(); 93 94 private JavaEditor fEditor; 95 private SourceViewer fSourceViewer; 96 private IDocument fDocument; 97 98 /* 99 * @see junit.framework.TestCase#setUp() 100 */ 101 @Before setUp()102 public void setUp() throws Exception { 103 String filename= createFileName("Before"); 104 fEditor= (JavaEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true); 105 fSourceViewer= EditorTestHelper.getSourceViewer(fEditor); 106 fDocument= fSourceViewer.getDocument(); 107 } 108 109 /* 110 * @see junit.framework.TestCase#tearDown() 111 */ 112 @After tearDown()113 public void tearDown() throws Exception { 114 EditorTestHelper.closeEditor(fEditor); 115 fEditor= null; 116 fSourceViewer= null; 117 } 118 assertIndentResult()119 private void assertIndentResult() throws Exception { 120 String afterFile= createFileName("Modified"); 121 String expected= ResourceTestHelper.read(afterFile).toString(); 122 123 new IndentAction(new EmptyBundle(), "prefix", fEditor, false).run(); 124 125 assertEquals(expected, fDocument.get()); 126 } 127 createFileName(String qualifier)128 private String createFileName(String qualifier) { 129 String name= tn.getMethodName(); 130 name= name.substring(4, 5).toLowerCase() + name.substring(5); 131 return "/" + PROJECT + "/src/" + name + "/" + qualifier + ".java"; 132 } 133 selectAll()134 private void selectAll() { 135 fSourceViewer.setSelectedRange(0, fDocument.getLength()); 136 } 137 138 @Test testUnchanged()139 public void testUnchanged() throws Exception { 140 selectAll(); 141 assertIndentResult(); 142 } 143 144 @Test testBug122261()145 public void testBug122261() throws Exception { 146 selectAll(); 147 assertIndentResult(); 148 } 149 150 @Test testEmptySingleLineComment01()151 public void testEmptySingleLineComment01() throws Exception { 152 selectAll(); 153 assertIndentResult(); 154 } 155 156 @Test testEmptySingleLineComment02()157 public void testEmptySingleLineComment02() throws Exception { 158 IJavaProject project= indentTestSetup.getProject(); 159 project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.TRUE); 160 try { 161 selectAll(); 162 assertIndentResult(); 163 } finally { 164 project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.FALSE); 165 } 166 } 167 168 @Test testEmptySingleLineComment03()169 public void testEmptySingleLineComment03() throws Exception { 170 IJavaProject project= indentTestSetup.getProject(); 171 project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.TRUE); 172 project.setOption(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); 173 project.setOption(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE); 174 try { 175 selectAll(); 176 assertIndentResult(); 177 } finally { 178 project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.FALSE); 179 project.setOption(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.TRUE); 180 project.setOption(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.TRUE); 181 } 182 } 183 184 @Test testBug424772()185 public void testBug424772() throws Exception { 186 selectAll(); 187 assertIndentResult(); 188 } 189 190 @Test testBug428384()191 public void testBug428384() throws Exception { 192 selectAll(); 193 assertIndentResult(); 194 } 195 196 @Test testBug439582_1()197 public void testBug439582_1() throws Exception { 198 IJavaProject project= indentTestSetup.getProject(); 199 String value= project.getOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, true); 200 project.setOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, "1"); 201 try { 202 selectAll(); 203 assertIndentResult(); 204 } finally { 205 project.setOption(DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, value); 206 } 207 } 208 209 @Test testBug439582_2()210 public void testBug439582_2() throws Exception { 211 selectAll(); 212 assertIndentResult(); 213 } 214 215 @Test testBug439582_3()216 public void testBug439582_3() throws Exception { 217 selectAll(); 218 assertIndentResult(); 219 } 220 221 @Test testBug439582_4()222 public void testBug439582_4() throws Exception { 223 selectAll(); 224 assertIndentResult(); 225 } 226 227 @Test testBug439582_5()228 public void testBug439582_5() throws Exception { 229 selectAll(); 230 assertIndentResult(); 231 } 232 233 @Test testBug400670_1()234 public void testBug400670_1() throws Exception { 235 // With formatter profile from https://bugs.eclipse.org/bugs/show_bug.cgi?id=400670#c0 236 String indentOnColumn= DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_NEXT_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN); 237 IJavaProject project= indentTestSetup.getProject(); 238 String value1= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, true); 239 project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, indentOnColumn); 240 String value2= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, true); 241 project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, indentOnColumn); 242 String value3= project.getOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, true); 243 project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, indentOnColumn); 244 try { 245 selectAll(); 246 assertIndentResult(); 247 } finally { 248 project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, value1); 249 project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, value2); 250 project.setOption(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, value3); 251 } 252 } 253 254 @Test testBug400670_2()255 public void testBug400670_2() throws Exception { 256 // With default formatter profile 257 selectAll(); 258 assertIndentResult(); 259 } 260 261 @Test testBug458763()262 public void testBug458763() throws Exception { 263 IJavaProject project= indentTestSetup.getProject(); 264 String value= project.getOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, true); 265 project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, DefaultCodeFormatterConstants.FALSE); 266 try { 267 selectAll(); 268 assertIndentResult(); 269 } finally { 270 project.setOption(DefaultCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH, value); 271 } 272 } 273 } 274