001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.test.syntax;
042:
043: import java.awt.event.KeyEvent;
044: import java.io.File;
045: import junit.framework.Test;
046: import org.netbeans.junit.AssertionFailedErrorException;
047: import org.netbeans.editor.BaseDocument;
048: import org.netbeans.jellytools.EditorOperator;
049: import org.netbeans.test.web.FileObjectFilter;
050: import org.netbeans.test.web.RecurrentSuiteFactory;
051: import org.openide.filesystems.FileObject;
052:
053: /**
054: *
055: * @author Jindrich Sedek
056: */
057: public class IndentationTest extends CompletionTest {
058:
059: private static final String[] START_STEPS = { "<!--CC", "/*CC" };
060: private static final String[] END_STEPS = { "-->", "*/" };
061: private boolean debugMode = false;
062:
063: /** Creates a new instance of IndentationTest */
064: public IndentationTest(String name, FileObject testFileObj) {
065: super (name, testFileObj);
066: }
067:
068: public static Test suite() {
069: // find folder with test projects and define file objects filter
070: File datadir = new IndentationTest(null, null).getDataDir();
071: File projectsDir = new File(datadir, "IndentationTestProjects");
072: FileObjectFilter filter = new FileObjectFilter() {
073:
074: public boolean accept(FileObject fObject) {
075: String ext = fObject.getExt();
076: String name = fObject.getName();
077: return name.startsWith("test")
078: && (XML_EXTS.contains(ext) || JSP_EXTS
079: .contains(ext));
080: }
081: };
082: return RecurrentSuiteFactory.createSuite(IndentationTest.class,
083: projectsDir, filter);
084: }
085:
086: @Override
087: public void runTest() throws Exception {
088: try {
089: BaseDocument doc = openFile(testFileObj);
090: String text = doc.getText(0, doc.getLength());
091: Possition actualPossition = getNextPossition(text, 0);
092: EditorOperator eOperator;
093: while (actualPossition != null) {//go through all cases
094: doc.remove(actualPossition.start, actualPossition.len);
095: eOperator = new EditorOperator(testFileObj.getName());
096: eOperator.txtEditorPane().getCaret().setDot(
097: actualPossition.start);
098: eOperator.save();
099: eOperator.pushKey(KeyEvent.VK_ENTER);
100: eOperator.waitModified(true);
101: int shift = eOperator.txtEditorPane()
102: .getCaretPosition()
103: - actualPossition.start;
104: ref("line " + eOperator.getLineNumber() + ": " + shift);
105: if (debugMode) {
106: doc.insertString(eOperator.txtEditorPane()
107: .getCaretPosition(), "|", null);
108: Thread.sleep(2000);
109: }
110: actualPossition = getNextPossition(eOperator.getText(),
111: actualPossition.start + 1);
112: }
113: if (debugMode) {
114: ref(new EditorOperator(testFileObj.getName()).getText());
115: }
116: } catch (Exception ex) {
117: throw new AssertionFailedErrorException(ex);
118: }
119: ending();
120: }
121:
122: private Possition getNextPossition(String text, int actual) {
123: int minStart = Integer.MAX_VALUE, len = -1;
124: for (int i = 0; i < START_STEPS.length; i++) {
125: int pos = text.indexOf(START_STEPS[i], actual);
126: if ((pos != -1) && (pos < minStart)) {
127: minStart = pos;
128: int minEnd = text.indexOf(END_STEPS[i], actual);
129: len = minEnd - minStart + END_STEPS[i].length();
130: }
131: }
132: if (minStart != Integer.MAX_VALUE) {
133: return new Possition(minStart, len);
134: } else {
135: return null;
136: }
137: }
138:
139: private class Possition {
140:
141: public int start, len;
142:
143: Possition(int start, int len) {
144: this .start = start;
145: this .len = len;
146: }
147:
148: @Override
149: public String toString() {
150: return "Od " + start + " a delky " + len;
151: }
152: }
153: }
|