KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
23 import junit.framework.TestCase;
24 import org.netbeans.api.editor.guards.GuardedSection;
25 import org.netbeans.api.editor.guards.InteriorSection;
26 import org.netbeans.api.editor.guards.SimpleSection;
27
28 /**
29  *
30  * @author Jan Pokorsky
31  */

32 public class JavaGuardedReaderTest extends TestCase {
33
34     private JavaGuardedSectionsProvider provider;
35
36     private JavaGuardedReader instance;
37
38     private Editor editor;
39     
40     public JavaGuardedReaderTest(String JavaDoc testName) {
41         super(testName);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45         editor = new Editor();
46         provider = new JavaGuardedSectionsProvider(editor);
47         
48         instance = new JavaGuardedReader(provider);
49     }
50
51     protected void tearDown() throws Exception JavaDoc {
52         provider = null;
53         instance = null;
54     }
55
56     /**
57      * Test of translateToCharBuff method, of class org.netbeans.modules.java.guards.JavaGuardedReader.
58      */

59     public void testTranslatePlain() {
60         System.out.println("read plain");
61         
62         String JavaDoc expStr = "\nclass A {\n}\n";
63         char[] readBuff = expStr.toCharArray();
64         
65         char[] result = instance.translateToCharBuff(readBuff);
66         List JavaDoc<GuardedSection> sections = instance.getGuardedSections();
67         assertEquals(expStr, String.valueOf(result));
68         assertTrue("sections not empty", sections.isEmpty());
69     }
70     
71     public void testTranslateLINE() {
72         System.out.println("read //" + "GEN-LINE:");
73         
74         String JavaDoc readStr = "\nclass A {//" + "GEN-LINE:hu\n}\n";
75         editor.setStringContent(readStr);
76         String JavaDoc expStr = "\nclass A { " + " \n}\n";
77         char[] readBuff = readStr.toCharArray();
78         
79         char[] result = instance.translateToCharBuff(readBuff);
80         List JavaDoc<GuardedSection> sections = instance.getGuardedSections();
81         
82         assertEquals(expStr, String.valueOf(result));
83         assertEquals("sections", 1, sections.size());
84         
85         GuardedSection expSection = sections.get(0);
86         assertEquals(SimpleSection.class, expSection.getClass());
87         assertEquals("section valid", true, expSection.isValid());
88         assertEquals("section name", "hu", expSection.getName());
89         assertEquals("begin", 1, expSection.getStartPosition().getOffset());
90         assertEquals("end", expStr.indexOf("}") - 1, expSection.getEndPosition().getOffset());
91     }
92     
93     public void testTranslateBEGIN_END() {
94         System.out.println("read //" + "GEN-BEGIN_END:");
95         
96         String JavaDoc readStr = "\nclass A {//" + "GEN-BEGIN:hu\n\n}//" + "GEN-END:hu\n";
97         editor.setStringContent(readStr);
98         String JavaDoc expStr = "\nclass A { " + " \n\n} " + " \n";
99         char[] readBuff = readStr.toCharArray();
100         
101         char[] result = instance.translateToCharBuff(readBuff);
102         List JavaDoc<GuardedSection> sections = instance.getGuardedSections();
103         
104         assertEquals(expStr, String.valueOf(result));
105         assertEquals("sections", 1, sections.size());
106         
107         GuardedSection expSection = sections.get(0);
108         assertEquals(SimpleSection.class, expSection.getClass());
109         assertEquals("section valid", true, expSection.isValid());
110         assertEquals("section name", "hu", expSection.getName());
111         assertEquals("begin", 1, expSection.getStartPosition().getOffset());
112         assertEquals("end", expStr.length() - 1, expSection.getEndPosition().getOffset());
113     }
114     
115     public void testTranslateFIRST_LAST() {
116         System.out.println("read //" + "GEN-FIRST_LAST:");
117         
118         String JavaDoc readStr = "\nclass A {//" + "GEN-FIRST:hu\n statement;\n}//" + "GEN-LAST:hu\n";
119         editor.setStringContent(readStr);
120         String JavaDoc expStr = "\nclass A { " + " \n statement;\n} " + " \n";
121         char[] readBuff = readStr.toCharArray();
122         
123         char[] result = instance.translateToCharBuff(readBuff);
124         List JavaDoc<GuardedSection> sections = instance.getGuardedSections();
125         
126         assertEquals(expStr, String.valueOf(result));
127         assertEquals("sections", 1, sections.size());
128         
129         GuardedSection expSection = sections.get(0);
130         assertEquals(InteriorSection.class, expSection.getClass());
131         assertEquals("section valid", true, expSection.isValid());
132         assertEquals("section name", "hu", expSection.getName());
133         assertEquals("begin", 1, expSection.getStartPosition().getOffset());
134         assertEquals("end", expStr.length() - 1, expSection.getEndPosition().getOffset());
135         InteriorSection iSection = (InteriorSection) expSection;
136         assertEquals("body.begin", expStr.indexOf(" statement;"), iSection.getBodyStartPosition().getOffset());
137         assertEquals("body.end", expStr.indexOf("\n}"), iSection.getBodyEndPosition().getOffset());
138     }
139     
140     public void testTranslateFIRST_HEADEREND_LAST() {
141         System.out.println("read //" + "GEN-FIRST_HEADEREND_LAST:");
142         
143         String JavaDoc readStr = "\nclass A //" + "GEN-FIRST:hu\n{//" + "GEN-HEADEREND:hu\n statement;\n}//" + "GEN-LAST:hu\n";
144         editor.setStringContent(readStr);
145         String JavaDoc expStr = "\nclass A " + " \n{ " + " \n statement;\n} " + " \n";
146         char[] readBuff = readStr.toCharArray();
147         
148         char[] result = instance.translateToCharBuff(readBuff);
149         List JavaDoc<GuardedSection> sections = instance.getGuardedSections();
150         
151         assertEquals(expStr, String.valueOf(result));
152         assertEquals("sections", 1, sections.size());
153         
154         GuardedSection expSection = sections.get(0);
155         assertEquals(InteriorSection.class, expSection.getClass());
156         assertEquals("section valid", true, expSection.isValid());
157         assertEquals("section name", "hu", expSection.getName());
158         assertEquals("begin", 1, expSection.getStartPosition().getOffset());
159         assertEquals("end", expStr.length() - 1, expSection.getEndPosition().getOffset());
160         InteriorSection iSection = (InteriorSection) expSection;
161         assertEquals("body.begin", expStr.indexOf(" statement;"), iSection.getBodyStartPosition().getOffset());
162         assertEquals("body.end", expStr.indexOf("\n}"), iSection.getBodyEndPosition().getOffset());
163     }
164     
165 }
166
Popular Tags