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:
042: package org.netbeans.modules.java.guards;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.List;
047: import javax.swing.text.BadLocationException;
048: import junit.framework.TestCase;
049: import org.netbeans.api.editor.guards.GuardedSection;
050:
051: /**
052: *
053: * @author Jan Pokorsky
054: */
055: public class JavaGuardedWriterTest extends TestCase {
056:
057: public JavaGuardedWriterTest(String testName) {
058: super (testName);
059: }
060:
061: protected void setUp() throws Exception {
062: }
063:
064: protected void tearDown() throws Exception {
065: }
066:
067: /**
068: * Test of translate method, of class org.netbeans.modules.java.guards.JavaGuardedWriter.
069: */
070: public void testTranslatePlain() {
071: System.out.println("write plain");
072:
073: char[] writeBuff = "\nclass A {\n}\n".toCharArray();
074: JavaGuardedWriter instance = new JavaGuardedWriter();
075: List<GuardedSection> sections = Collections
076: .<GuardedSection> emptyList();
077:
078: char[] expResult = writeBuff;
079: instance.setGuardedSection(sections);
080: char[] result = instance.translate(writeBuff);
081: assertEquals(String.valueOf(expResult), String.valueOf(result));
082: }
083:
084: public void testTranslateLINE() throws BadLocationException {
085: System.out.println("write //" + "GEN-LINE:");
086:
087: String writeStr = "\nclass A {" + "\n}\n";
088: String expStr = "\nclass A {//" + "GEN-LINE:hu\n}\n";
089: char[] writeBuff = writeStr.toCharArray();
090:
091: JavaGuardedWriter instance = new JavaGuardedWriter();
092: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
093: new Editor());
094: List<GuardedSection> sections = new ArrayList<GuardedSection>();
095: sections.add(provider.createSimpleSection("hu", 1, writeStr
096: .indexOf("\n}")));
097:
098: instance.setGuardedSection(sections);
099: char[] result = instance.translate(writeBuff);
100: assertEquals(expStr, String.valueOf(result));
101: }
102:
103: public void testTranslateLINEWithSpaces()
104: throws BadLocationException {
105: System.out.println("write with spaces //" + "GEN-LINE:");
106:
107: String writeStr = "\nclass A { " + " \n}\n";
108: String expStr = "\nclass A {//" + "GEN-LINE:hu\n}\n";
109: char[] writeBuff = writeStr.toCharArray();
110:
111: JavaGuardedWriter instance = new JavaGuardedWriter();
112: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
113: new Editor());
114: List<GuardedSection> sections = new ArrayList<GuardedSection>();
115: sections.add(provider.createSimpleSection("hu", 1, writeStr
116: .indexOf("\n}")));
117:
118: instance.setGuardedSection(sections);
119: char[] result = instance.translate(writeBuff);
120: assertEquals(expStr, String.valueOf(result));
121: }
122:
123: public void testTranslateBEGIN_END() throws BadLocationException {
124: System.out.println("write //" + "GEN-BEGIN_END:");
125:
126: String writeStr = "\nclass A {" + "\n\n}" + "\n";
127: String expStr = "\nclass A {//" + "GEN-BEGIN:hu\n\n}//"
128: + "GEN-END:hu\n";
129: char[] writeBuff = writeStr.toCharArray();
130:
131: JavaGuardedWriter instance = new JavaGuardedWriter();
132: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
133: new Editor());
134: List<GuardedSection> sections = new ArrayList<GuardedSection>();
135: sections.add(provider.createSimpleSection("hu", 1, writeStr
136: .length() - 1));
137:
138: instance.setGuardedSection(sections);
139: char[] result = instance.translate(writeBuff);
140: assertEquals(expStr, String.valueOf(result));
141: }
142:
143: public void testTranslateBEGIN_ENDWithSpaces()
144: throws BadLocationException {
145: System.out.println("write with spaces //" + "GEN-BEGIN_END:");
146:
147: String writeStr = "\nclass A { " + " \n\n} "
148: + " \n";
149: String expStr = "\nclass A {//" + "GEN-BEGIN:hu\n\n}//"
150: + "GEN-END:hu\n";
151: char[] writeBuff = writeStr.toCharArray();
152:
153: JavaGuardedWriter instance = new JavaGuardedWriter();
154: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
155: new Editor());
156: List<GuardedSection> sections = new ArrayList<GuardedSection>();
157: sections.add(provider.createSimpleSection("hu", 1, writeStr
158: .length() - 1));
159:
160: instance.setGuardedSection(sections);
161: char[] result = instance.translate(writeBuff);
162: assertEquals(expStr, String.valueOf(result));
163: }
164:
165: public void testTranslateFIRST_LAST() throws BadLocationException {
166: System.out.println("write //" + "GEN-FIRST_LAST:");
167:
168: String writeStr = "\nclass A { " + "\n statement;\n}" + "\n";
169: String expStr = "\nclass A {//"
170: + "GEN-FIRST:hu\n statement;\n}//" + "GEN-LAST:hu\n";
171: char[] writeBuff = writeStr.toCharArray();
172:
173: JavaGuardedWriter instance = new JavaGuardedWriter();
174: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
175: new Editor());
176: List<GuardedSection> sections = new ArrayList<GuardedSection>();
177: sections.add(provider.createInteriorSection("hu", 1, writeStr
178: .indexOf("\n statement;"), writeStr.indexOf("}"),
179: writeStr.length() - 1));
180:
181: instance.setGuardedSection(sections);
182: char[] result = instance.translate(writeBuff);
183: assertEquals(expStr, String.valueOf(result));
184: }
185:
186: public void testTranslateFIRST_LASTWithSpaces()
187: throws BadLocationException {
188: System.out.println("write with spaces //" + "GEN-FIRST_LAST:");
189:
190: String writeStr = "\nclass A { "
191: + " \n statement;\n} " + " \n";
192: String expStr = "\nclass A {//"
193: + "GEN-FIRST:hu\n statement;\n}//" + "GEN-LAST:hu\n";
194: char[] writeBuff = writeStr.toCharArray();
195:
196: JavaGuardedWriter instance = new JavaGuardedWriter();
197: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
198: new Editor());
199: List<GuardedSection> sections = new ArrayList<GuardedSection>();
200: sections.add(provider.createInteriorSection("hu", 1, writeStr
201: .indexOf("\n statement;"), writeStr.indexOf("}"),
202: writeStr.length() - 1));
203:
204: instance.setGuardedSection(sections);
205: char[] result = instance.translate(writeBuff);
206: assertEquals(expStr, String.valueOf(result));
207: }
208:
209: public void testTranslateFIRST_HEADEREND_LAST()
210: throws BadLocationException {
211: System.out.println("write //" + "GEN-FIRST_HEADEREND_LAST:");
212:
213: String writeStr = "\nclass A " + " \n{ "
214: + " \n statement;\n} "
215: + " \n";
216: String expStr = "\nclass A//" + "GEN-FIRST:hu\n{//"
217: + "GEN-HEADEREND:hu\n statement;\n}//"
218: + "GEN-LAST:hu\n";
219: char[] writeBuff = writeStr.toCharArray();
220:
221: JavaGuardedWriter instance = new JavaGuardedWriter();
222: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
223: new Editor());
224: List<GuardedSection> sections = new ArrayList<GuardedSection>();
225: sections.add(provider.createInteriorSection("hu", 1, writeStr
226: .indexOf("\n statement;"), writeStr.indexOf("}"),
227: writeStr.length() - 1));
228:
229: instance.setGuardedSection(sections);
230: char[] result = instance.translate(writeBuff);
231: assertEquals(expStr, String.valueOf(result));
232: }
233:
234: public void testTranslateFIRST_HEADEREND_LASTWithSpaces()
235: throws BadLocationException {
236: System.out.println("write with spaces //"
237: + "GEN-FIRST_HEADEREND_LAST:");
238:
239: String writeStr = "\nclass A" + "\n{" + "\n statement;\n}"
240: + "\n";
241: String expStr = "\nclass A//" + "GEN-FIRST:hu\n{//"
242: + "GEN-HEADEREND:hu\n statement;\n}//"
243: + "GEN-LAST:hu\n";
244: char[] writeBuff = writeStr.toCharArray();
245:
246: JavaGuardedWriter instance = new JavaGuardedWriter();
247: JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(
248: new Editor());
249: List<GuardedSection> sections = new ArrayList<GuardedSection>();
250: sections.add(provider.createInteriorSection("hu", 1, writeStr
251: .indexOf("\n statement;"), writeStr.indexOf("}"),
252: writeStr.length() - 1));
253:
254: instance.setGuardedSection(sections);
255: char[] result = instance.translate(writeBuff);
256: assertEquals(expStr, String.valueOf(result));
257: }
258:
259: }
|