|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 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 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.wst.html.core.tests.cleanup; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
|
| 15 |
import junit.extensions.TestSetup; |
| 16 |
import junit.framework.Test; |
| 17 |
import junit.framework.TestCase; |
| 18 |
import junit.framework.TestSuite; |
| 19 |
|
| 20 |
import org.eclipse.core.resources.IFile; |
| 21 |
import org.eclipse.core.resources.IProject; |
| 22 |
import org.eclipse.core.runtime.CoreException; |
| 23 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 24 |
import org.eclipse.jface.text.IDocument; |
| 25 |
import org.eclipse.wst.html.core.internal.cleanup.HTMLCleanupProcessorImpl; |
| 26 |
import org.eclipse.wst.html.core.tests.ProjectUtil; |
| 27 |
import org.eclipse.wst.sse.core.StructuredModelManager; |
| 28 |
import org.eclipse.wst.sse.core.internal.cleanup.AbstractStructuredCleanupProcessor; |
| 29 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
| 30 |
import org.eclipse.wst.sse.core.utils.StringUtils; |
| 31 |
|
| 32 |
public class TestHTMLCleanupProcessor extends TestCase { |
| 33 |
/** |
| 34 |
* The name of the project that all of these tests will use |
| 35 |
*/ |
| 36 |
private static final String PROJECT_NAME = "TestHTMLCleanupProcessor"; |
| 37 |
|
| 38 |
/** |
| 39 |
* The location of the testing files |
| 40 |
*/ |
| 41 |
private static final String PROJECT_FILES = "/testresources/HTMLCleanupProcessor"; |
| 42 |
|
| 43 |
/** |
| 44 |
* The project that all of the tests use |
| 45 |
*/ |
| 46 |
private static IProject fProject; |
| 47 |
|
| 48 |
/** |
| 49 |
* Default constructor |
| 50 |
*/ |
| 51 |
public TestHTMLCleanupProcessor() { |
| 52 |
super("Test HTML Cleanup Processor"); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor that takes a test name. |
| 57 |
* |
| 58 |
* @param name The name this test run should have. |
| 59 |
*/ |
| 60 |
public TestHTMLCleanupProcessor(String name) { |
| 61 |
super(name); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* <p>Use this method to add these tests to a larger test suite so set up |
| 66 |
* and tear down can be performed</p> |
| 67 |
* |
| 68 |
* @return a {@link TestSetup} that will run all of the tests in this class |
| 69 |
* with set up and tear down. |
| 70 |
*/ |
| 71 |
public static Test suite() { |
| 72 |
TestSuite ts = new TestSuite(TestHTMLCleanupProcessor.class); |
| 73 |
return new TestHTMLCleanupProcessorSetup(ts); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* <p><b>TEST:</b> collapsing empty tags in an html document</p> |
| 79 |
*/ |
| 80 |
public void testCollapseEmptyTagsHTML()throws Exception { |
| 81 |
HTMLCleanupProcessorImpl cleanupProcessor = getProcessorForForEmptyTagsTest(); |
| 82 |
runTest("test1.html", "test1-expected.html", cleanupProcessor); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* <p><b>TEST:</b> collapsing empty tags in an xhtml document</p> |
| 87 |
*/ |
| 88 |
public void testCollapseEmptyTagsXHTML()throws Exception { |
| 89 |
HTMLCleanupProcessorImpl cleanupProcessor = getProcessorForForEmptyTagsTest(); |
| 90 |
runTest("test2.html", "test2-expected.html", cleanupProcessor); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return a configured {@link HTMLCleanupProcessorImpl} for testing compressing empty tags |
| 95 |
*/ |
| 96 |
private static HTMLCleanupProcessorImpl getProcessorForForEmptyTagsTest() { |
| 97 |
HTMLCleanupProcessorImpl cleanupProcessor = new HTMLCleanupProcessorImpl(); |
| 98 |
cleanupProcessor.getCleanupPreferences().setCompressEmptyElementTags(true); |
| 99 |
cleanupProcessor.getCleanupPreferences().setInsertRequiredAttrs(false); |
| 100 |
cleanupProcessor.getCleanupPreferences().setInsertMissingTags(true); |
| 101 |
cleanupProcessor.getCleanupPreferences().setQuoteAttrValues(false); |
| 102 |
cleanupProcessor.getCleanupPreferences().setFormatSource(false); |
| 103 |
cleanupProcessor.getCleanupPreferences().setConvertEOLCodes(false); |
| 104 |
|
| 105 |
return cleanupProcessor; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* <p>Runs an {@link AbstractStructuredCleanupProcessor} test</p> |
| 110 |
* |
| 111 |
* @param originalFile file to clean up |
| 112 |
* @param expectedResultsFile expected results of cleaning up the original file |
| 113 |
* @param configuredCleanupProcessor the processor to use to do the cleaning |
| 114 |
* |
| 115 |
* @throws Exception tests can throw exceptions now and then |
| 116 |
*/ |
| 117 |
private void runTest(String originalFile, String expectedResultsFile, |
| 118 |
AbstractStructuredCleanupProcessor configuredCleanupProcessor) throws Exception { |
| 119 |
|
| 120 |
IStructuredModel model = null; |
| 121 |
IStructuredModel expectedModel = null; |
| 122 |
try { |
| 123 |
model = getModelForEdit(originalFile); |
| 124 |
expectedModel = getModelForEdit(expectedResultsFile); |
| 125 |
|
| 126 |
configuredCleanupProcessor.refreshCleanupPreferences = false; |
| 127 |
configuredCleanupProcessor.cleanupModel(model); |
| 128 |
configuredCleanupProcessor.refreshCleanupPreferences = true; |
| 129 |
|
| 130 |
model.save(); |
| 131 |
|
| 132 |
standardizeLineEndings(model.getStructuredDocument()); |
| 133 |
standardizeLineEndings(expectedModel.getStructuredDocument()); |
| 134 |
|
| 135 |
assertEquals("Clean up results did not match expected results", |
| 136 |
expectedModel.getStructuredDocument().get(), |
| 137 |
model.getStructuredDocument().get()); |
| 138 |
} finally { |
| 139 |
if(model != null) { |
| 140 |
model.releaseFromEdit(); |
| 141 |
} |
| 142 |
|
| 143 |
if(expectedModel != null) { |
| 144 |
expectedModel.releaseFromEdit(); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* <p>Given a file name in <code>fProject</code> attempts to get a model |
| 151 |
* for it, if the file doesn't exist or it can't get the model the test fails.</p> |
| 152 |
* |
| 153 |
* @param filename |
| 154 |
* @return |
| 155 |
* @throws CoreException |
| 156 |
* @throws IOException |
| 157 |
*/ |
| 158 |
private IStructuredModel getModelForEdit(final String filename) throws IOException, CoreException { |
| 159 |
IFile file = fProject.getFile(filename); |
| 160 |
assertTrue("Test file " + file + " can not be found", file.exists()); |
| 161 |
|
| 162 |
IStructuredModel model = StructuredModelManager.getModelManager().getModelForEdit(file); |
| 163 |
assertNotNull("Could not get model for " + file, model); |
| 164 |
|
| 165 |
return model; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* <p>Line endings can be an issue when running tests on different OSs. |
| 171 |
* This function standardizes the line endings to use <code>\n</code></p> |
| 172 |
* |
| 173 |
* <p>It will get the text from the given editor, change the line endings, |
| 174 |
* and then save the editor</p> |
| 175 |
* |
| 176 |
* @param editor standardize the line endings of the text presented in this |
| 177 |
* editor. |
| 178 |
*/ |
| 179 |
private void standardizeLineEndings(IDocument doc) { |
| 180 |
String contents = doc.get(); |
| 181 |
contents = StringUtils.replace(contents, "\r\n", "\n"); |
| 182 |
contents = StringUtils.replace(contents, "\r", "\n"); |
| 183 |
doc.set(contents); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* <p>This inner class is used to do set up and tear down before and |
| 188 |
* after (respectively) all tests in the inclosing class have run.</p> |
| 189 |
*/ |
| 190 |
private static class TestHTMLCleanupProcessorSetup extends TestSetup { |
| 191 |
private static final String WTP_AUTOTEST_NONINTERACTIVE = "wtp.autotest.noninteractive"; |
| 192 |
private static String previousWTPAutoTestNonInteractivePropValue = null; |
| 193 |
|
| 194 |
/** |
| 195 |
* Default constructor |
| 196 |
* |
| 197 |
* @param test do setup for the given test |
| 198 |
*/ |
| 199 |
public TestHTMLCleanupProcessorSetup(Test test) { |
| 200 |
super(test); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* <p>This is run once before all of the tests</p> |
| 205 |
* |
| 206 |
* @see junit.extensions.TestSetup#setUp() |
| 207 |
*/ |
| 208 |
public void setUp() throws Exception { |
| 209 |
fProject = ProjectUtil.createProject(PROJECT_NAME, null, null); |
| 210 |
ProjectUtil.copyBundleEntriesIntoWorkspace(PROJECT_FILES, PROJECT_NAME); |
| 211 |
|
| 212 |
String noninteractive = System.getProperty(WTP_AUTOTEST_NONINTERACTIVE); |
| 213 |
if (noninteractive != null) { |
| 214 |
previousWTPAutoTestNonInteractivePropValue = noninteractive; |
| 215 |
} else { |
| 216 |
previousWTPAutoTestNonInteractivePropValue = "false"; |
| 217 |
} |
| 218 |
System.setProperty(WTP_AUTOTEST_NONINTERACTIVE, "true"); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* <p>This is run once after all of the tests have been run</p> |
| 223 |
* |
| 224 |
* @see junit.extensions.TestSetup#tearDown() |
| 225 |
*/ |
| 226 |
public void tearDown() throws Exception { |
| 227 |
fProject.delete(true, new NullProgressMonitor()); |
| 228 |
|
| 229 |
if (previousWTPAutoTestNonInteractivePropValue != null) { |
| 230 |
System.setProperty(WTP_AUTOTEST_NONINTERACTIVE, previousWTPAutoTestNonInteractivePropValue); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |