1 19 20 package org.netbeans.modules.java.guards; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import javax.swing.text.BadLocationException ; 26 import junit.framework.TestCase; 27 import org.netbeans.api.editor.guards.GuardedSection; 28 29 33 public class JavaGuardedWriterTest extends TestCase { 34 35 public JavaGuardedWriterTest(String testName) { 36 super(testName); 37 } 38 39 protected void setUp() throws Exception { 40 } 41 42 protected void tearDown() throws Exception { 43 } 44 45 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 <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 { 62 System.out.println("write //" + "GEN-LINE:"); 63 64 String writeStr = "\nclass A {" + "\n}\n"; 65 String 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 <GuardedSection> sections = new ArrayList <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 { 79 System.out.println("write with spaces //" + "GEN-LINE:"); 80 81 String writeStr = "\nclass A { " + " \n}\n"; 82 String 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 <GuardedSection> sections = new ArrayList <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 { 96 System.out.println("write //" + "GEN-BEGIN_END:"); 97 98 String writeStr = "\nclass A {" + "\n\n}" + "\n"; 99 String 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 <GuardedSection> sections = new ArrayList <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 { 113 System.out.println("write with spaces //" + "GEN-BEGIN_END:"); 114 115 String writeStr = "\nclass A { " + " \n\n} " + " \n"; 116 String 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 <GuardedSection> sections = new ArrayList <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 { 130 System.out.println("write //" + "GEN-FIRST_LAST:"); 131 132 String writeStr = "\nclass A { " + "\n statement;\n}" + "\n"; 133 String 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 <GuardedSection> sections = new ArrayList <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 { 149 System.out.println("write with spaces //" + "GEN-FIRST_LAST:"); 150 151 String writeStr = "\nclass A { " + " \n statement;\n} " + " \n"; 152 String 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 <GuardedSection> sections = new ArrayList <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 { 168 System.out.println("write //" + "GEN-FIRST_HEADEREND_LAST:"); 169 170 String writeStr = "\nclass A " + " \n{ " + " \n statement;\n} " + " \n"; 171 String 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 <GuardedSection> sections = new ArrayList <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 { 187 System.out.println("write with spaces //" + "GEN-FIRST_HEADEREND_LAST:"); 188 189 String writeStr = "\nclass A" + "\n{" + "\n statement;\n}" + "\n"; 190 String 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 <GuardedSection> sections = new ArrayList <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 |