KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > guards > JavaGuardedWriterTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.guards;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import junit.framework.TestCase;
27 import org.netbeans.api.editor.guards.GuardedSection;
28
29 /**
30  *
31  * @author Jan Pokorsky
32  */

33 public class JavaGuardedWriterTest extends TestCase {
34     
35     public JavaGuardedWriterTest(String JavaDoc testName) {
36         super(testName);
37     }
38     
39     protected void setUp() throws Exception JavaDoc {
40     }
41     
42     protected void tearDown() throws Exception JavaDoc {
43     }
44     
45     /**
46      * Test of translate method, of class org.netbeans.modules.java.guards.JavaGuardedWriter.
47      */

48     public void testTranslatePlain() {
49         System.out.println("write plain");
50         
51         char[] writeBuff = "\nclass A {\n}\n".toCharArray();
52         JavaGuardedWriter instance = new JavaGuardedWriter();
53         List JavaDoc<GuardedSection> sections = Collections.<GuardedSection>emptyList();
54         
55         char[] expResult = writeBuff;
56         instance.setGuardedSection(sections);
57         char[] result = instance.translate(writeBuff);
58         assertEquals(String.valueOf(expResult), String.valueOf(result));
59     }
60     
61     public void testTranslateLINE() throws BadLocationException JavaDoc {
62         System.out.println("write //" + "GEN-LINE:");
63         
64         String JavaDoc writeStr = "\nclass A {" + "\n}\n";
65         String JavaDoc expStr = "\nclass A {//" + "GEN-LINE:hu\n}\n";
66         char[] writeBuff = writeStr.toCharArray();
67         
68         JavaGuardedWriter instance = new JavaGuardedWriter();
69         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
70         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
71         sections.add(provider.createSimpleSection("hu", 1, writeStr.indexOf("\n}")));
72         
73         instance.setGuardedSection(sections);
74         char[] result = instance.translate(writeBuff);
75         assertEquals(expStr, String.valueOf(result));
76     }
77     
78     public void testTranslateLINEWithSpaces() throws BadLocationException JavaDoc {
79         System.out.println("write with spaces //" + "GEN-LINE:");
80         
81         String JavaDoc writeStr = "\nclass A { " + " \n}\n";
82         String JavaDoc expStr = "\nclass A {//" + "GEN-LINE:hu\n}\n";
83         char[] writeBuff = writeStr.toCharArray();
84         
85         JavaGuardedWriter instance = new JavaGuardedWriter();
86         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
87         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
88         sections.add(provider.createSimpleSection("hu", 1, writeStr.indexOf("\n}")));
89         
90         instance.setGuardedSection(sections);
91         char[] result = instance.translate(writeBuff);
92         assertEquals(expStr, String.valueOf(result));
93     }
94     
95     public void testTranslateBEGIN_END() throws BadLocationException JavaDoc {
96         System.out.println("write //" + "GEN-BEGIN_END:");
97         
98         String JavaDoc writeStr = "\nclass A {" + "\n\n}" + "\n";
99         String JavaDoc expStr = "\nclass A {//" + "GEN-BEGIN:hu\n\n}//" + "GEN-END:hu\n";
100         char[] writeBuff = writeStr.toCharArray();
101         
102         JavaGuardedWriter instance = new JavaGuardedWriter();
103         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
104         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
105         sections.add(provider.createSimpleSection("hu", 1, writeStr.length() - 1));
106         
107         instance.setGuardedSection(sections);
108         char[] result = instance.translate(writeBuff);
109         assertEquals(expStr, String.valueOf(result));
110     }
111     
112     public void testTranslateBEGIN_ENDWithSpaces() throws BadLocationException JavaDoc {
113         System.out.println("write with spaces //" + "GEN-BEGIN_END:");
114         
115         String JavaDoc writeStr = "\nclass A { " + " \n\n} " + " \n";
116         String JavaDoc expStr = "\nclass A {//" + "GEN-BEGIN:hu\n\n}//" + "GEN-END:hu\n";
117         char[] writeBuff = writeStr.toCharArray();
118         
119         JavaGuardedWriter instance = new JavaGuardedWriter();
120         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
121         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
122         sections.add(provider.createSimpleSection("hu", 1, writeStr.length() - 1));
123         
124         instance.setGuardedSection(sections);
125         char[] result = instance.translate(writeBuff);
126         assertEquals(expStr, String.valueOf(result));
127     }
128     
129     public void testTranslateFIRST_LAST() throws BadLocationException JavaDoc {
130         System.out.println("write //" + "GEN-FIRST_LAST:");
131         
132         String JavaDoc writeStr = "\nclass A { " + "\n statement;\n}" + "\n";
133         String JavaDoc expStr = "\nclass A {//" + "GEN-FIRST:hu\n statement;\n}//" + "GEN-LAST:hu\n";
134         char[] writeBuff = writeStr.toCharArray();
135         
136         JavaGuardedWriter instance = new JavaGuardedWriter();
137         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
138         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
139         sections.add(provider.createInteriorSection("hu",
140                 1, writeStr.indexOf("\n statement;"),
141                 writeStr.indexOf("}"), writeStr.length() - 1));
142         
143         instance.setGuardedSection(sections);
144         char[] result = instance.translate(writeBuff);
145         assertEquals(expStr, String.valueOf(result));
146     }
147     
148     public void testTranslateFIRST_LASTWithSpaces() throws BadLocationException JavaDoc {
149         System.out.println("write with spaces //" + "GEN-FIRST_LAST:");
150         
151         String JavaDoc writeStr = "\nclass A { " + " \n statement;\n} " + " \n";
152         String JavaDoc expStr = "\nclass A {//" + "GEN-FIRST:hu\n statement;\n}//" + "GEN-LAST:hu\n";
153         char[] writeBuff = writeStr.toCharArray();
154         
155         JavaGuardedWriter instance = new JavaGuardedWriter();
156         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
157         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
158         sections.add(provider.createInteriorSection("hu",
159                 1, writeStr.indexOf("\n statement;"),
160                 writeStr.indexOf("}"), writeStr.length() - 1));
161         
162         instance.setGuardedSection(sections);
163         char[] result = instance.translate(writeBuff);
164         assertEquals(expStr, String.valueOf(result));
165     }
166     
167     public void testTranslateFIRST_HEADEREND_LAST() throws BadLocationException JavaDoc {
168         System.out.println("write //" + "GEN-FIRST_HEADEREND_LAST:");
169         
170         String JavaDoc writeStr = "\nclass A " + " \n{ " + " \n statement;\n} " + " \n";
171         String JavaDoc expStr = "\nclass A//" + "GEN-FIRST:hu\n{//" + "GEN-HEADEREND:hu\n statement;\n}//" + "GEN-LAST:hu\n";
172         char[] writeBuff = writeStr.toCharArray();
173         
174         JavaGuardedWriter instance = new JavaGuardedWriter();
175         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
176         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
177         sections.add(provider.createInteriorSection("hu",
178                 1, writeStr.indexOf("\n statement;"),
179                 writeStr.indexOf("}"), 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_HEADEREND_LASTWithSpaces() throws BadLocationException JavaDoc {
187         System.out.println("write with spaces //" + "GEN-FIRST_HEADEREND_LAST:");
188         
189         String JavaDoc writeStr = "\nclass A" + "\n{" + "\n statement;\n}" + "\n";
190         String JavaDoc expStr = "\nclass A//" + "GEN-FIRST:hu\n{//" + "GEN-HEADEREND:hu\n statement;\n}//" + "GEN-LAST:hu\n";
191         char[] writeBuff = writeStr.toCharArray();
192         
193         JavaGuardedWriter instance = new JavaGuardedWriter();
194         JavaGuardedSectionsProvider provider = new JavaGuardedSectionsProvider(new Editor());
195         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>();
196         sections.add(provider.createInteriorSection("hu",
197                 1, writeStr.indexOf("\n statement;"),
198                 writeStr.indexOf("}"), writeStr.length() - 1));
199         
200         instance.setGuardedSection(sections);
201         char[] result = instance.translate(writeBuff);
202         assertEquals(expStr, String.valueOf(result));
203     }
204     
205 }
206
Popular Tags