|
Lines 10-16
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.wst.jsdt.web.ui.tests.format; |
11 |
package org.eclipse.wst.jsdt.web.ui.tests.format; |
| 12 |
|
12 |
|
| 13 |
import java.io.ByteArrayOutputStream; |
|
|
| 14 |
import java.io.IOException; |
13 |
import java.io.IOException; |
| 15 |
import java.io.UnsupportedEncodingException; |
14 |
import java.io.UnsupportedEncodingException; |
| 16 |
|
15 |
|
|
Lines 23-28
Link Here
|
| 23 |
import org.eclipse.core.resources.IProject; |
22 |
import org.eclipse.core.resources.IProject; |
| 24 |
import org.eclipse.core.runtime.CoreException; |
23 |
import org.eclipse.core.runtime.CoreException; |
| 25 |
import org.eclipse.core.runtime.NullProgressMonitor; |
24 |
import org.eclipse.core.runtime.NullProgressMonitor; |
|
|
25 |
import org.eclipse.jface.text.IDocument; |
| 26 |
import org.eclipse.jface.text.Region; |
26 |
import org.eclipse.jface.text.Region; |
| 27 |
import org.eclipse.jface.text.formatter.FormattingContext; |
27 |
import org.eclipse.jface.text.formatter.FormattingContext; |
| 28 |
import org.eclipse.jface.text.formatter.FormattingContextProperties; |
28 |
import org.eclipse.jface.text.formatter.FormattingContextProperties; |
|
Lines 50-57
Link Here
|
| 50 |
* |
50 |
* |
| 51 |
*/ |
51 |
*/ |
| 52 |
public class FormattingTests extends TestCase { |
52 |
public class FormattingTests extends TestCase { |
| 53 |
private static final String UTF_8 = "UTF-8"; |
|
|
| 54 |
|
| 55 |
/** |
53 |
/** |
| 56 |
* The name of the project that all of these tests will use |
54 |
* The name of the project that all of these tests will use |
| 57 |
*/ |
55 |
*/ |
|
Lines 67-75
Link Here
|
| 67 |
*/ |
65 |
*/ |
| 68 |
private static IProject fProject; |
66 |
private static IProject fProject; |
| 69 |
|
67 |
|
| 70 |
/** the viewer to use during the tests */ |
|
|
| 71 |
private static ISourceViewer fViewer; |
| 72 |
|
| 73 |
/** |
68 |
/** |
| 74 |
* <p>Default constructor<p> |
69 |
* <p>Default constructor<p> |
| 75 |
* <p>Use {@link #suite()}</p> |
70 |
* <p>Use {@link #suite()}</p> |
|
Lines 145-150
Link Here
|
| 145 |
formatAndAssertEquals("test9.html", "test9-fmt.html", new StructuredTextViewerConfigurationJSDT()); |
140 |
formatAndAssertEquals("test9.html", "test9-fmt.html", new StructuredTextViewerConfigurationJSDT()); |
| 146 |
} |
141 |
} |
| 147 |
|
142 |
|
|
|
143 |
public void testFormatHTMLScriptRegionWrappedWithHTMLComment_TextInLeadingComment() throws UnsupportedEncodingException, IOException, CoreException { |
| 144 |
formatAndAssertEquals("test10.html", "test10-fmt.html", new StructuredTextViewerConfigurationJSDT()); |
| 145 |
} |
| 146 |
|
| 147 |
public void testFormatHTMLScriptRegionWrappedWithHTMLComment_TextInTrailingComment() throws UnsupportedEncodingException, IOException, CoreException { |
| 148 |
formatAndAssertEquals("test11.html", "test11-fmt.html", new StructuredTextViewerConfigurationJSDT()); |
| 149 |
} |
| 150 |
|
| 151 |
public void testFormatHTMLScriptRegionWrappedWithHTMLComment_TextInLeadingComment_and_TextInTrailingComment() throws UnsupportedEncodingException, IOException, CoreException { |
| 152 |
formatAndAssertEquals("test12.html", "test12-fmt.html", new StructuredTextViewerConfigurationJSDT()); |
| 153 |
} |
| 154 |
|
| 155 |
public void testFormatHTMLScriptRegion_AllOnOneLine() throws UnsupportedEncodingException, IOException, CoreException { |
| 156 |
formatAndAssertEquals("test13.html", "test13-fmt.html", new StructuredTextViewerConfigurationJSDT()); |
| 157 |
} |
| 158 |
|
| 159 |
public void testFormatHTMLScriptRegion_AllOnOneLine_LeadingComment() throws UnsupportedEncodingException, IOException, CoreException { |
| 160 |
formatAndAssertEquals("test14.html", "test14-fmt.html", new StructuredTextViewerConfigurationJSDT()); |
| 161 |
} |
| 162 |
|
| 148 |
/** |
163 |
/** |
| 149 |
* @param beforePath |
164 |
* @param beforePath |
| 150 |
* @param afterPath |
165 |
* @param afterPath |
|
Lines 172-198
Link Here
|
| 172 |
normalizedContents = StringUtils.replace(normalizedContents, "\r", "\n"); |
187 |
normalizedContents = StringUtils.replace(normalizedContents, "\r", "\n"); |
| 173 |
document.set(normalizedContents); |
188 |
document.set(normalizedContents); |
| 174 |
|
189 |
|
| 175 |
//setup dummy viewer |
190 |
ISourceViewer viewer = getConfiguredViewer(document, configuration); |
| 176 |
fViewer.setDocument(document); |
191 |
assertNotNull("Could not get viewer to run test", viewer); |
| 177 |
fViewer.configure(configuration); |
|
|
| 178 |
|
192 |
|
| 179 |
//do the format |
193 |
//do the format |
| 180 |
IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(fViewer); |
194 |
IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(viewer); |
| 181 |
IFormattingContext fContext = new FormattingContext(); |
195 |
IFormattingContext fContext = new FormattingContext(); |
| 182 |
Region region = new Region(0, document.getLength()); |
196 |
Region region = new Region(0, document.getLength()); |
| 183 |
fContext.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true)); |
197 |
fContext.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true)); |
| 184 |
fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region); |
198 |
fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region); |
| 185 |
formatter.format(document, fContext); |
199 |
formatter.format(document, fContext); |
| 186 |
|
200 |
|
| 187 |
//save the models |
201 |
//get the contents |
| 188 |
ByteArrayOutputStream formattedBytes = new ByteArrayOutputStream(); |
202 |
String actualContents = beforeModel.getStructuredDocument().get(); |
| 189 |
beforeModel.save(formattedBytes); // "beforeModel" should now be after the formatter |
203 |
String expectedContents = afterModel.getStructuredDocument().get(); |
| 190 |
|
|
|
| 191 |
ByteArrayOutputStream afterBytes = new ByteArrayOutputStream(); |
| 192 |
afterModel.save(afterBytes); |
| 193 |
|
| 194 |
String expectedContents = new String(afterBytes.toByteArray(), UTF_8); |
| 195 |
String actualContents = new String(formattedBytes.toByteArray(), UTF_8); |
| 196 |
|
204 |
|
| 197 |
/* Make some adjustments to ignore cross platform line delimiter issues */ |
205 |
/* Make some adjustments to ignore cross platform line delimiter issues */ |
| 198 |
expectedContents = StringUtils.replace(expectedContents, "\r\n", "\n"); |
206 |
expectedContents = StringUtils.replace(expectedContents, "\r\n", "\n"); |
|
Lines 206-215
Link Here
|
| 206 |
} |
214 |
} |
| 207 |
finally { |
215 |
finally { |
| 208 |
if (beforeModel != null) { |
216 |
if (beforeModel != null) { |
| 209 |
beforeModel.releaseFromEdit(); |
217 |
try { |
|
|
218 |
beforeModel.releaseFromEdit(); |
| 219 |
} catch(Exception e) { |
| 220 |
//ignore |
| 221 |
} |
| 210 |
} |
222 |
} |
| 211 |
if (afterModel != null) { |
223 |
if (afterModel != null) { |
| 212 |
afterModel.releaseFromEdit(); |
224 |
try { |
|
|
225 |
afterModel.releaseFromEdit(); |
| 226 |
} catch(Exception e) { |
| 227 |
//ignore |
| 228 |
} |
| 213 |
} |
229 |
} |
| 214 |
} |
230 |
} |
| 215 |
} |
231 |
} |
|
Lines 251-256
Link Here
|
| 251 |
} |
267 |
} |
| 252 |
|
268 |
|
| 253 |
/** |
269 |
/** |
|
|
270 |
* @param document {@link IDocument} to display in the dummy viewer |
| 271 |
* @param configuration {@link SourceViewerConfiguration} to configure the dummy viewer with |
| 272 |
* @return a configured {@link ISourceViewer} using the given parameters |
| 273 |
*/ |
| 274 |
private static ISourceViewer getConfiguredViewer(IDocument document, SourceViewerConfiguration configuration) { |
| 275 |
ISourceViewer viewer = null; |
| 276 |
assertNotNull("Could not get current display to run test with.", Display.getCurrent()); |
| 277 |
|
| 278 |
Shell shell = null; |
| 279 |
|
| 280 |
if (PlatformUI.isWorkbenchRunning()) { |
| 281 |
shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 282 |
} |
| 283 |
else { |
| 284 |
shell = new Shell(Display.getCurrent()); |
| 285 |
} |
| 286 |
Composite parent = new Composite(shell, SWT.NONE); |
| 287 |
viewer = new StructuredTextViewer(parent, null, null, false, SWT.NONE); |
| 288 |
viewer.setDocument(document); |
| 289 |
viewer.configure(configuration); |
| 290 |
|
| 291 |
return viewer; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 254 |
* <p>This inner class is used to do set up and tear down before and |
295 |
* <p>This inner class is used to do set up and tear down before and |
| 255 |
* after (respectively) all tests in the inclosing class have run.</p> |
296 |
* after (respectively) all tests in the inclosing class have run.</p> |
| 256 |
*/ |
297 |
*/ |
|
Lines 277-299
Link Here
|
| 277 |
fProject = ProjectUtil.createProject(PROJECT_NAME, null, new String[] {JavaScriptCore.NATURE_ID}); |
318 |
fProject = ProjectUtil.createProject(PROJECT_NAME, null, new String[] {JavaScriptCore.NATURE_ID}); |
| 278 |
ProjectUtil.copyBundleEntriesIntoWorkspace(PROJECT_FILES, PROJECT_NAME); |
319 |
ProjectUtil.copyBundleEntriesIntoWorkspace(PROJECT_FILES, PROJECT_NAME); |
| 279 |
|
320 |
|
| 280 |
if (Display.getCurrent() != null) { |
|
|
| 281 |
|
| 282 |
Shell shell = null; |
| 283 |
Composite parent = null; |
| 284 |
|
| 285 |
if (PlatformUI.isWorkbenchRunning()) { |
| 286 |
shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 287 |
} |
| 288 |
else { |
| 289 |
shell = new Shell(Display.getCurrent()); |
| 290 |
} |
| 291 |
parent = new Composite(shell, SWT.NONE); |
| 292 |
|
| 293 |
// dummy viewer |
| 294 |
fViewer = new StructuredTextViewer(parent, null, null, false, SWT.NONE); |
| 295 |
} |
| 296 |
|
| 297 |
//set non-interactive |
321 |
//set non-interactive |
| 298 |
String noninteractive = System.getProperty(WTP_AUTOTEST_NONINTERACTIVE); |
322 |
String noninteractive = System.getProperty(WTP_AUTOTEST_NONINTERACTIVE); |
| 299 |
if (noninteractive != null) { |
323 |
if (noninteractive != null) { |