Index: .project =================================================================== RCS file: /home/eclipse/org.eclipse.ant.tests.ui/.project,v retrieving revision 1.11 diff -u -r1.11 .project --- .project 26 Feb 2004 16:40:17 -0000 1.11 +++ .project 16 Mar 2004 09:49:57 -0000 @@ -6,22 +6,11 @@ org.apache.ant org.eclipse.ant.core org.eclipse.ant.ui - org.eclipse.core.resources - org.eclipse.core.runtime.compatibility org.eclipse.debug.core org.eclipse.debug.ui - org.eclipse.jdt.core org.eclipse.jdt.debug org.eclipse.jdt.launching - org.eclipse.jface.text - org.eclipse.ui - org.eclipse.ui.editors org.eclipse.ui.externaltools - org.eclipse.ui.ide - org.eclipse.ui.views - org.eclipse.ui.workbench.texteditor - org.eclipse.update.core - org.junit Index: Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/NonParsingXMLFormatterTest.java =================================================================== RCS file: Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/NonParsingXMLFormatterTest.java diff -N Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/NonParsingXMLFormatterTest.java --- Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/NonParsingXMLFormatterTest.java 2 Mar 2004 03:55:35 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,103 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004 John-Mason P. Shackelford and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v10.html - * - * Contributors: - * John-Mason P. Shackelford - initial API and implementation - *******************************************************************************/ -package org.eclipse.ant.tests.ui.editor.formatter; - -import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences; -import org.eclipse.ant.internal.ui.editor.formatter.NonParsingXMLFormatter; -import org.eclipse.ant.tests.ui.testplugin.AbstractAntUITest; - -/** - * - */ -public class NonParsingXMLFormatterTest extends AbstractAntUITest { - - /** - * @param name - */ - public NonParsingXMLFormatterTest(String name) { - super(name); - } - - /** - * General Test - */ - public final void testGeneralFormat() throws Exception { - FormattingPreferences prefs = new FormattingPreferences(){ - public int getTabWidth() { - return 3; - } - public boolean stripBlankLines() { - return false; - } - public boolean useSpacesInsteadOfTabs() { - return true; - } - }; - simpleTest("formatTest_source01.xml","formatTest_target01.xml",prefs); - } - - /** - * Insure that tab width is not hard coded - */ - public final void testTabWidth() throws Exception { - FormattingPreferences prefs = new FormattingPreferences(){ - public int getTabWidth() { - return 7; - } - public boolean stripBlankLines() { - return false; - } - public boolean useSpacesInsteadOfTabs() { - return true; - } - }; - simpleTest("formatTest_source01.xml","formatTest_target02.xml",prefs); - } - - - /** - * Test with tab characters instead of spaces. - */ - public final void testTabsInsteadOfSpaces() throws Exception { - FormattingPreferences prefs = new FormattingPreferences(){ - public int getTabWidth() { - return 3; - } - public boolean stripBlankLines() { - return false; - } - public boolean useSpacesInsteadOfTabs() { - return false; - } - }; - simpleTest("formatTest_source01.xml","formatTest_target03.xml",prefs); - } - - - /** - * @param sourceFileName - file to format - * @param targetFileName - the source file after a properly executed format - * @param prefs - given the included preference instructions - * @throws Exception - */ - private void simpleTest(String sourceFileName, String targetFileName, - FormattingPreferences prefs) throws Exception { - - NonParsingXMLFormatter xmlFormatter = new NonParsingXMLFormatter(); - xmlFormatter.setText(getFileContentAsString(getBuildFile(sourceFileName))); - xmlFormatter.setFormattingPreferences(prefs); - - String result = xmlFormatter.format(); - String expectedResult = getFileContentAsString(getBuildFile(targetFileName)); - - assertEquals(expectedResult, result); - } -} Index: test plugin/org/eclipse/ant/tests/ui/testplugin/AntUITests.java =================================================================== RCS file: /home/eclipse/org.eclipse.ant.tests.ui/test plugin/org/eclipse/ant/tests/ui/testplugin/AntUITests.java,v retrieving revision 1.14 diff -u -r1.14 AntUITests.java --- test plugin/org/eclipse/ant/tests/ui/testplugin/AntUITests.java 2 Mar 2004 03:56:47 -0000 1.14 +++ test plugin/org/eclipse/ant/tests/ui/testplugin/AntUITests.java 16 Mar 2004 09:50:01 -0000 @@ -21,8 +21,9 @@ import org.eclipse.ant.tests.ui.editor.AntEditorContentOutlineTests; import org.eclipse.ant.tests.ui.editor.CodeCompletionTest; import org.eclipse.ant.tests.ui.editor.TaskDescriptionProviderTest; +import org.eclipse.ant.tests.ui.editor.formatter.XmlTagFormatterTest; import org.eclipse.ant.tests.ui.editor.formatter.FormattingPreferencesTest; -import org.eclipse.ant.tests.ui.editor.formatter.NonParsingXMLFormatterTest; +import org.eclipse.ant.tests.ui.editor.formatter.XmlDocumentFormatterTest; import org.eclipse.ant.tests.ui.externaltools.MigrationTests; import org.eclipse.ant.tests.ui.separateVM.SeparateVMTests; @@ -43,7 +44,8 @@ suite.addTest(new TestSuite(AntEditorContentOutlineTests.class)); suite.addTest(new TestSuite(MigrationTests.class)); suite.addTest(new TestSuite(FormattingPreferencesTest.class)); - suite.addTest(new TestSuite(NonParsingXMLFormatterTest.class)); + suite.addTest(new TestSuite(XmlDocumentFormatterTest.class)); + suite.addTest(new TestSuite(XmlTagFormatterTest.class)); return suite; } } Index: Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlDocumentFormatterTest.java =================================================================== RCS file: Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlDocumentFormatterTest.java diff -N Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlDocumentFormatterTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlDocumentFormatterTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2004 John-Mason P. Shackelford and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * John-Mason P. Shackelford - initial API and implementation + *******************************************************************************/ +package org.eclipse.ant.tests.ui.editor.formatter; + +import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences; +import org.eclipse.ant.internal.ui.editor.formatter.XmlDocumentFormatter; +import org.eclipse.ant.tests.ui.testplugin.AbstractAntUITest; + +/** + * + */ +public class XmlDocumentFormatterTest extends AbstractAntUITest { + + /** + * @param name + */ + public XmlDocumentFormatterTest(String name) { + super(name); + } + + /** + * General Test + */ + public final void testGeneralFormat() throws Exception { + FormattingPreferences prefs = new FormattingPreferences(){ + public int getTabWidth() { + return 3; + } + public boolean stripBlankLines() { + return false; + } + public boolean useSpacesInsteadOfTabs() { + return true; + } + }; + simpleTest("formatTest_source01.xml","formatTest_target01.xml",prefs); + } + + /** + * Insure that tab width is not hard coded + */ + public final void testTabWidth() throws Exception { + FormattingPreferences prefs = new FormattingPreferences(){ + public int getTabWidth() { + return 7; + } + public boolean stripBlankLines() { + return false; + } + public boolean useSpacesInsteadOfTabs() { + return true; + } + }; + simpleTest("formatTest_source01.xml","formatTest_target02.xml",prefs); + } + + + /** + * Test with tab characters instead of spaces. + */ + public final void testTabsInsteadOfSpaces() throws Exception { + FormattingPreferences prefs = new FormattingPreferences(){ + public int getTabWidth() { + return 3; + } + public boolean stripBlankLines() { + return false; + } + public boolean useSpacesInsteadOfTabs() { + return false; + } + }; + simpleTest("formatTest_source01.xml","formatTest_target03.xml",prefs); + } + + + /** + * @param sourceFileName - file to format + * @param targetFileName - the source file after a properly executed format + * @param prefs - given the included preference instructions + * @throws Exception + */ + private void simpleTest(String sourceFileName, String targetFileName, + FormattingPreferences prefs) throws Exception { + + XmlDocumentFormatter xmlFormatter = new XmlDocumentFormatter(); + xmlFormatter.setText(getFileContentAsString(getBuildFile(sourceFileName))); + xmlFormatter.setFormattingPreferences(prefs); + + String result = xmlFormatter.format(); + String expectedResult = getFileContentAsString(getBuildFile(targetFileName)); + + assertEquals(expectedResult, result); + } +} Index: Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlTagFormatterTest.java =================================================================== RCS file: Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlTagFormatterTest.java diff -N Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlTagFormatterTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Ant Editor Tests/org/eclipse/ant/tests/ui/editor/formatter/XmlTagFormatterTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,379 @@ +/******************************************************************************* + * Copyright (c) 2004 John-Mason P. Shackelford and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * John-Mason P. Shackelford - initial API and implementation + *******************************************************************************/ +package org.eclipse.ant.tests.ui.editor.formatter; + +import java.util.List; + +import org.eclipse.ant.internal.ui.editor.formatter.XmlTagFormatter; +import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences; +import org.eclipse.ant.tests.ui.testplugin.AbstractAntUITest; + +/** + * + */ +public class XmlTagFormatterTest extends AbstractAntUITest { + + // TODO This test is too low level and too tightly coupled to internals. + + /* ---------------- Test Fixture ---------------- */ + // In case anyone wonders why many people recommend against testing + // privates, I produce this example... + private static class InnerClassFactory extends XmlTagFormatter { + + public static class ParseException extends + XmlTagFormatter.ParseException { + + public ParseException(String message) { + super(message); + } + } + + public static class Tag extends XmlTagFormatter.Tag { + } + + public static class TagFormatter extends XmlTagFormatter.TagFormatter { + + public boolean lineRequiresWrap(String line, int lineWidth, + int tabWidth) { + return super.lineRequiresWrap(line, lineWidth, tabWidth); + } + + public int tabExpandedLineWidth(String line, int tabWidth) { + return super.tabExpandedLineWidth(line, tabWidth); + } + + public String wrapTag(Tag tag, FormattingPreferences prefs, + String indent) { + return super.wrapTag(tag, prefs, indent); + } + } + + public static class TagParser extends XmlTagFormatter.TagParser { + + public String getElementName(String tagText) + throws XmlTagFormatter.ParseException { + return super.getElementName(tagText); + } + + public List getAttibutes(String elementText) + throws XmlTagFormatter.ParseException { + return super.getAttibutes(elementText); + } + } + + public static Tag createTag() { + return new Tag(); + } + + public static TagFormatter createTagFormatter() { + return new TagFormatter(); + } + + public static TagParser createTagParser() { + return new TagParser(); + } + + public static void validateAttributePair(Object attributePair, + String attribute, String value) { + XmlTagFormatter.AttributePair pair = (XmlTagFormatter.AttributePair) attributePair; + assertEquals(attribute, pair.getAttribute()); + assertEquals(value, pair.getValue()); + }; + } + + /** + * @param name + */ + public XmlTagFormatterTest(String name) { + super(name); + } + + private FormattingPreferences getPreferences(final boolean wrapLongTags, + final boolean alignCloseChar, final int maxLineWidth) { + + return new FormattingPreferences() { + + public boolean alignElementCloseChar() { + return alignCloseChar; + } + + public boolean wrapLongTags() { + return wrapLongTags; + } + + public int getMaximumLineWidth() { + return maxLineWidth; + } + }; + } + + /** + * + */ + private void simpleTest(String source, String target, + FormattingPreferences prefs, String indent) throws Exception { + + XmlTagFormatter tagFormatter = new XmlTagFormatter(); + String result = tagFormatter.format(source, prefs, indent); + assertEquals(target, result); + } + + /* ---------------- Test Methods ---------------- */ + + public void testParserGetElementName() throws Exception { + + InnerClassFactory.TagParser tagParser = InnerClassFactory + .createTagParser(); + + String elementFixture1 = ""; //$NON-NLS-1$ + assertEquals("myElement", tagParser.getElementName(elementFixture1)); //$NON-NLS-1$ + + String elementFixture2 = ""; //$NON-NLS-1$ + assertEquals("myElement", tagParser.getElementName(elementFixture2)); //$NON-NLS-1$ + + assertEquals("x", tagParser.getElementName("")); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals("x", tagParser.getElementName("")); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals("x:y", tagParser.getElementName("")); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals("x:y", tagParser.getElementName("")); //$NON-NLS-1$ //$NON-NLS-2$ + + Exception e1 = null; + try { + tagParser.getElementName("<>"); //$NON-NLS-1$ + } catch (Exception e) { + e1 = e; + } + assertNotNull(e1); + assertTrue(e1.getClass().isAssignableFrom( + InnerClassFactory.ParseException.class)); + + Exception e2 = null; + try { + tagParser.getElementName("<>"); //$NON-NLS-1$ + } catch (Exception e) { + e2 = e; + } + assertNotNull(e2); + assertTrue(e2.getClass().isAssignableFrom( + InnerClassFactory.ParseException.class)); + + } + + public void testParserGetAttributes() throws Exception { + InnerClassFactory.TagParser tagParser = InnerClassFactory + .createTagParser(); + + List attributePairs; + + // test normal situation + attributePairs = tagParser + .getAttibutes(""); //$NON-NLS-1$ + + assertEquals(2, attributePairs.size()); + InnerClassFactory.validateAttributePair(attributePairs.get(0), + "attribute1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ + InnerClassFactory.validateAttributePair(attributePairs.get(1), + "attribute2", "value2"); //$NON-NLS-1$ //$NON-NLS-2$ + + + // test with extra whitespace and funny quotes + attributePairs = tagParser + .getAttibutes(""); //$NON-NLS-1$ + + assertEquals(2, attributePairs.size()); + InnerClassFactory.validateAttributePair(attributePairs.get(0), + "attribute1", "value1\""); //$NON-NLS-1$ //$NON-NLS-2$ + InnerClassFactory.validateAttributePair(attributePairs.get(1), + "attribute2", "value2'"); //$NON-NLS-1$ //$NON-NLS-2$ + +// TODO attributes which contain whitespace should throw a parse error +// +// // test parse errors - whitespace in attribute name +// Exception e1 = null; +// try { +// attributePairs = tagParser +// .getAttibutes(""); +// } catch (Exception e) { +// e1 = e; +// } +// assertNotNull(e1); +// assertTrue(e1.getClass().isAssignableFrom( +// InnerClassFactory.ParseException.class)); + + + // test parse errors - equals in the wrong place + Exception e2 = null; + try { + attributePairs = tagParser + .getAttibutes(""); //$NON-NLS-1$ + } catch (Exception e) { + e2 = e; + } + assertNotNull(e2); + assertTrue(e2.getClass().isAssignableFrom( + InnerClassFactory.ParseException.class)); + + + // test parse errors - quotes in the wrong place + Exception e3 = null; + try { + attributePairs = tagParser + .getAttibutes(""); //$NON-NLS-1$ + } catch (Exception e) { + e3 = e; + } + assertNotNull(e3); + assertTrue(e3.getClass().isAssignableFrom( + InnerClassFactory.ParseException.class)); + + + } + + /** + * + */ + public void testFormat01() throws Exception { + String indent = "\t"; //$NON-NLS-1$ + String source = ""; //$NON-NLS-1$ + String target = ""; //$NON-NLS-1$ + + simpleTest(source, target, getPreferences(true, false, 60), indent); + } + + /** + * + */ + public void testFormat02() throws Exception { + String indent = "\t"; //$NON-NLS-1$ + String source = ""; //$NON-NLS-1$ + String target = ""; //$NON-NLS-1$ + + simpleTest(source, target, getPreferences(true, true, 60), indent); + } + + /** + * + */ + public void testLineRequiresWrap() throws Exception { + + InnerClassFactory.TagFormatter tagFormatter = InnerClassFactory + .createTagFormatter(); + + boolean shouldWrap = tagFormatter + .lineRequiresWrap( + "\t\t ", //$NON-NLS-1$ + 70, 8); + boolean shouldNotWrap = tagFormatter + .lineRequiresWrap( + "\t\t ", //$NON-NLS-1$ + 70, 8); + assertTrue(shouldWrap); + assertTrue(!shouldNotWrap); + + } + + /** + * + */ + public void testTabExpandedLineWidth() throws Exception { + + InnerClassFactory.TagFormatter tagFormatter = InnerClassFactory + .createTagFormatter(); + + assertEquals(20, tagFormatter.tabExpandedLineWidth("\t 1234567890", 8)); //$NON-NLS-1$ + assertEquals(10, tagFormatter.tabExpandedLineWidth("1234567890", 8)); //$NON-NLS-1$ + assertEquals(19, tagFormatter + .tabExpandedLineWidth("\t1\t2 34567890", 3)); //$NON-NLS-1$ + } + + public void testTabToStringAndMinimumLength() throws Exception { + InnerClassFactory.Tag tag = InnerClassFactory.createTag(); + + tag.setElementName("myElement"); //$NON-NLS-1$ + tag.setClosed(false); + assertEquals("", tag.toString()); //$NON-NLS-1$ + assertEquals(tag.toString().length(), tag.minimumLength()); + + tag.setClosed(true); + assertEquals("", tag.toString()); //$NON-NLS-1$ + assertEquals(tag.toString().length(), tag.minimumLength()); + + tag.addAttribute("attribute1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ + tag.addAttribute("attribute2", "value2"); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals( + "", //$NON-NLS-1$ + tag.toString()); + assertEquals(tag.toString().length(), tag.minimumLength()); + + tag.setClosed(false); + assertEquals("", //$NON-NLS-1$ + tag.toString()); + assertEquals(tag.toString().length(), tag.minimumLength()); + } + + /** + * + */ + public void testWrapTag() throws Exception { + + InnerClassFactory.Tag tag = InnerClassFactory.createTag(); + + InnerClassFactory.TagFormatter tagFormatter = InnerClassFactory + .createTagFormatter(); + + FormattingPreferences dontAlignCloseChar = new FormattingPreferences() { + + public boolean alignElementCloseChar() { + return false; + } + }; + FormattingPreferences doAlignCloseChar = new FormattingPreferences() { + + public boolean alignElementCloseChar() { + return true; + } + }; + + tag.setElementName("myElement"); //$NON-NLS-1$ + tag.addAttribute("attribute1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ + tag.addAttribute("attribute2", "value2"); //$NON-NLS-1$ //$NON-NLS-2$ + + tag.setClosed(true); + + assertEquals("", tagFormatter //$NON-NLS-1$ + .wrapTag(tag, dontAlignCloseChar, "\t\t ")); //$NON-NLS-1$ + + assertEquals("", //$NON-NLS-1$ + tagFormatter.wrapTag(tag, doAlignCloseChar, "\t\t ")); //$NON-NLS-1$ + + tag.setClosed(false); + + assertEquals("", tagFormatter //$NON-NLS-1$ + .wrapTag(tag, dontAlignCloseChar, "\t\t ")); //$NON-NLS-1$ + + assertEquals("", //$NON-NLS-1$ + tagFormatter.wrapTag(tag, doAlignCloseChar, "\t\t ")); //$NON-NLS-1$ + + } + +}