[358545] Formatting a JSP file, containing Java snippets, leads to misformatted or even lost code
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/JSPUITestSuite.java b/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/JSPUITestSuite.java index 9858388..06e701f 100644 --- a/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/JSPUITestSuite.java +++ b/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/JSPUITestSuite.java
@@ -22,6 +22,7 @@ import org.eclipse.jst.jsp.ui.tests.contentassist.TestJSPContentAssistComputers; import org.eclipse.jst.jsp.ui.tests.contentdescription.TestContentDescription; import org.eclipse.jst.jsp.ui.tests.document.FileBufferDocumentTester; +import org.eclipse.jst.jsp.ui.tests.format.TestContentFormatter; import org.eclipse.jst.jsp.ui.tests.model.TestModelsFromFiles; import org.eclipse.jst.jsp.ui.tests.modelquery.ModelQueryTester; import org.eclipse.jst.jsp.ui.tests.other.ScannerUnitTests; @@ -47,6 +48,7 @@ addTest(new TestSuite(ScannerUnitTests.class, "ScannerUnitTests")); addTest(new TestSuite(UnitTests.class, "UnitTests")); addTest(new TestSuite(TestStructuredPartitionerJSP.class, "TestStructuredPartioner")); + addTest(new TestSuite(TestContentFormatter.class, "Content Formatter Tests")); addTest(new TestSuite(ModelQueryTester.class, "ModelQueryTester")); addTest(new TestSuite(JSPJavaTranslatorTest.class, "Mixed JavaScript Translator Tests")); addTest(new TestSuite(TestEmailNotice.class, "TestEmailNotice"));
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/format/TestContentFormatter.java b/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/format/TestContentFormatter.java index 173ef6f..231957e 100644 --- a/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/format/TestContentFormatter.java +++ b/tests/org.eclipse.jst.jsp.ui.tests/src/org/eclipse/jst/jsp/ui/tests/format/TestContentFormatter.java
@@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 IBM Corporation and others. + * Copyright (c) 2007, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -23,10 +23,7 @@ import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Preferences; import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jface.text.DocumentRewriteSession; -import org.eclipse.jface.text.DocumentRewriteSessionType; import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IDocumentExtension4; import org.eclipse.jface.text.Region; import org.eclipse.jface.text.formatter.FormattingContext; import org.eclipse.jface.text.formatter.FormattingContextProperties; @@ -35,12 +32,16 @@ import org.eclipse.jface.text.source.SourceViewerConfiguration; import org.eclipse.jst.jsp.ui.StructuredTextViewerConfigurationJSP; import org.eclipse.jst.jsp.ui.tests.util.ProjectUtil; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.ide.IDE; import org.eclipse.wst.html.core.internal.HTMLCorePlugin; import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames; import org.eclipse.wst.sse.core.StructuredModelManager; import org.eclipse.wst.sse.core.internal.provisional.IModelManager; import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; import org.eclipse.wst.sse.core.utils.StringUtils; +import org.eclipse.wst.sse.ui.StructuredTextEditor; +import org.eclipse.wst.sse.ui.internal.StructuredTextViewer; public class TestContentFormatter extends TestCase { String wtp_autotest_noninteractive = null; @@ -96,15 +97,15 @@ afterModel.save(afterBytes); String expectedContents = new String(afterBytes.toByteArray(), UTF_8); - expectedContents = StringUtils.replace(expectedContents, "\r\n", "\n"); + expectedContents = StringUtils.replace(expectedContents, "\r\n", "\r"); expectedContents = StringUtils.replace(expectedContents, "\r", "\n"); String actualContents = new String(formattedBytes.toByteArray(), UTF_8); - actualContents = StringUtils.replace(actualContents, "\r\n", "\n"); + actualContents = StringUtils.replace(actualContents, "\r\n", "\r"); actualContents = StringUtils.replace(actualContents, "\r", "\n"); - assertEquals("Formatted document differs from the expected.", expectedContents, actualContents); assertTrue(onlyWhiteSpaceDiffers(expectedContents, actualContents)); + assertEquals("Formatted document differs from the expected.", expectedContents, actualContents); } finally { if (beforeModel != null) @@ -115,41 +116,16 @@ } private void formatAndAssertSignificantEquals(String beforePath, boolean resetPreferences) throws UnsupportedEncodingException, IOException, CoreException { - IStructuredModel beforeModel = null; + StructuredTextEditor editor = (StructuredTextEditor) IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(beforePath)), "org.eclipse.jst.jsp.core.jspsource.source", true); try { - beforeModel = getModelForEdit(beforePath); - assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel); - - if (resetPreferences) { - resetPreferencesToDefault(); - } + String before = editor.getDocumentProvider().getDocument(editor.getEditorInput()).get(); + editor.getTextViewer().doOperation(StructuredTextViewer.FORMAT_DOCUMENT); - SourceViewerConfiguration configuration = new StructuredTextViewerConfigurationJSP(); - IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(null); - - IDocument document = beforeModel.getStructuredDocument(); - String before = document.get(); - Region region = new Region(0, document.getLength()); - fContext.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE); - fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region); - DocumentRewriteSession rewriteSession = null; - if (document instanceof IDocumentExtension4) { - IDocumentExtension4 extension = (IDocumentExtension4) document; - DocumentRewriteSessionType type = DocumentRewriteSessionType.UNRESTRICTED; - rewriteSession = (extension.getActiveRewriteSession() != null) ? null : extension.startRewriteSession(type); - } - formatter.format(document, fContext); - String after = document.get(); - if (document instanceof IDocumentExtension4 && rewriteSession != null) { - IDocumentExtension4 extension = (IDocumentExtension4) document; - extension.stopRewriteSession(rewriteSession); - } - + String after = editor.getDocumentProvider().getDocument(editor.getEditorInput()).get(); assertTrue(onlyWhiteSpaceDiffers(before, after)); } finally { - if (beforeModel != null) - beforeModel.releaseFromEdit(); + editor.close(false); } } @@ -202,11 +178,8 @@ writer2.close(); char[] expectedCompacted = writer1.toCharArray(); - char[] actualCompacted = writer1.toCharArray(); - assertEquals("significant character lengths are not the same", expectedCompacted.length, actualCompacted.length); - for (int i = 0; i < actualCompacted.length; i++) { - assertEquals("significant character differs", expectedCompacted[i], actualCompacted[i]); - } + char[] actualCompacted = writer2.toCharArray(); + assertEquals("significant character differs", new String(expectedCompacted), new String(actualCompacted)); return true; } @@ -250,7 +223,10 @@ formatAndAssertEquals(beforePath, afterPath, true); } - public void testFormatBug358545() throws UnsupportedEncodingException, IOException, CoreException { + public void testFormatBug358545a() throws UnsupportedEncodingException, IOException, CoreException { formatAndAssertSignificantEquals("/" + PROJECT_NAME + "/WebContent/formatbug358545.jsp", true); } + public void testFormatBug358545b() throws UnsupportedEncodingException, IOException, CoreException { + formatAndAssertSignificantEquals("/" + PROJECT_NAME + "/WebContent/formatbug358545b.jsp", true); + } }
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495-fmt.jsp b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495-fmt.jsp index b626c91..6ab463d 100644 --- a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495-fmt.jsp +++ b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495-fmt.jsp
@@ -10,23 +10,26 @@ <title>Insert title here</title> </head> <body> -<%-- jsp expression must be followed directly by ! because no whitespace exists between --%> -Welcome to WTP, -<%=message%>! -<p>Welcome to WTP, <%=pal%>!</p> -<%-- jsp expression should be treated like any other attribute --%> -<table <%="align='left'"%>> - <tr> - <%-- no whitespace exists before/after jsp tag so keep it that way --%> - <th>Column<jsp:setProperty name="myname" property="myproperty" />Heading</th> - <%-- whitespace exists before/after jsp tag so newlines are okay --%> - <th>Column <jsp:setProperty name="myname" property="myproperty" /> - Heading</th> - </tr> - <tr> - <td>Row 1: Col 1</td> - <td>Row 1: Col 2</td> - </tr> -</table> + <%-- jsp expression must be followed directly by ! because no whitespace exists between --%> + Welcome to WTP, + <%=message%>! + <p> + Welcome to WTP, + <%=pal%>! + </p> + <%-- jsp expression should be treated like any other attribute --%> + <table <%="align='left'"%>> + <tr> + <%-- no whitespace exists before/after jsp tag so keep it that way --%> + <th>Column<jsp:setProperty name="myname" property="myproperty" />Heading</th> + <%-- whitespace exists before/after jsp tag so newlines are okay --%> + <th>Column <jsp:setProperty name="myname" property="myproperty" /> + Heading</th> + </tr> + <tr> + <td>Row 1: Col 1</td> + <td>Row 1: Col 2</td> + </tr> + </table> </body> </html> \ No newline at end of file
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_2-fmt.jsp b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_2-fmt.jsp index ce2159c..17487d8 100644 --- a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_2-fmt.jsp +++ b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_2-fmt.jsp
@@ -9,9 +9,12 @@ <title>Insert title here</title> </head> <body> -<%-- jsp expression must be followed directly by ! because no whitespace exists between --%> -Welcome to WTP, -<%=message%>! -<p>Welcome to WTP, <%=pal%>!</p> + <%-- jsp expression must be followed directly by ! because no whitespace exists between --%> + Welcome to WTP, + <%=message%>! + <p> + Welcome to WTP, + <%=pal%>! + </p> </body> </html> \ No newline at end of file
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_3-fmt.jsp b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_3-fmt.jsp index 287f804..a5fed8e 100644 --- a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_3-fmt.jsp +++ b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_3-fmt.jsp
@@ -7,8 +7,8 @@ <title>Insert title here</title> </head> <body> -<%-- jsp expression should be treated like any other attribute --%> -<table <%="align='left'"%>> -</table> + <%-- jsp expression should be treated like any other attribute --%> + <table <%="align='left'"%>> + </table> </body> </html> \ No newline at end of file
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_4-fmt.jsp b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_4-fmt.jsp index 454b366..194dcc4 100644 --- a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_4-fmt.jsp +++ b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug102495_4-fmt.jsp
@@ -7,18 +7,18 @@ <title>Insert title here</title> </head> <body> -<table> - <tr> - <%-- no whitespace exists before/after jsp tag so keep it that way --%> - <th>Column<jsp:setProperty name="myname" property="myproperty" />Heading</th> - <%-- whitespace exists before/after jsp tag so newlines are okay --%> - <th>Column <jsp:setProperty name="myname" property="myproperty" /> - Heading</th> - </tr> - <tr> - <td>Row 1: Col 1</td> - <td>Row 1: Col 2</td> - </tr> -</table> + <table> + <tr> + <%-- no whitespace exists before/after jsp tag so keep it that way --%> + <th>Column<jsp:setProperty name="myname" property="myproperty" />Heading</th> + <%-- whitespace exists before/after jsp tag so newlines are okay --%> + <th>Column <jsp:setProperty name="myname" property="myproperty" /> + Heading</th> + </tr> + <tr> + <td>Row 1: Col 1</td> + <td>Row 1: Col 2</td> + </tr> + </table> </body> </html> \ No newline at end of file
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug358545.jsp b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug358545.jsp index 622877c..9c804b5 100644 --- a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug358545.jsp +++ b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug358545.jsp
@@ -89,4 +89,4 @@ </body> -</html> +</html> \ No newline at end of file
diff --git a/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug358545b.jsp b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug358545b.jsp new file mode 100644 index 0000000..2f8879b --- /dev/null +++ b/tests/org.eclipse.jst.jsp.ui.tests/testfiles/jspformatting/WebContent/formatbug358545b.jsp
@@ -0,0 +1,19 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html> +<head> +<script type="text/javascript"> + + function verifyLocalAcessPLI() { +<% if( request.getAttribute( "LOCAL_DIVERSE_ACESS_PLI" ) != null ) {%> + MM_showHideLayers('restrictLocalAccessPLIPopup', '', 'show'); +<% }%> + } +</script> +<title>ePrice Administration - Price Books - Edit Individual Prices</title> +</head> +<body onkeydown="selectSave()" onload="verifyLocalAcessPLI();"> + + + +</body> +</html> \ No newline at end of file