1 19 20 package org.netbeans.modules.java.guards; 21 22 import java.util.List ; 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 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 testName) { 41 super(testName); 42 } 43 44 protected void setUp() throws Exception { 45 editor = new Editor(); 46 provider = new JavaGuardedSectionsProvider(editor); 47 48 instance = new JavaGuardedReader(provider); 49 } 50 51 protected void tearDown() throws Exception { 52 provider = null; 53 instance = null; 54 } 55 56 59 public void testTranslatePlain() { 60 System.out.println("read plain"); 61 62 String expStr = "\nclass A {\n}\n"; 63 char[] readBuff = expStr.toCharArray(); 64 65 char[] result = instance.translateToCharBuff(readBuff); 66 List <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 readStr = "\nclass A {//" + "GEN-LINE:hu\n}\n"; 75 editor.setStringContent(readStr); 76 String expStr = "\nclass A { " + " \n}\n"; 77 char[] readBuff = readStr.toCharArray(); 78 79 char[] result = instance.translateToCharBuff(readBuff); 80 List <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 readStr = "\nclass A {//" + "GEN-BEGIN:hu\n\n}//" + "GEN-END:hu\n"; 97 editor.setStringContent(readStr); 98 String expStr = "\nclass A { " + " \n\n} " + " \n"; 99 char[] readBuff = readStr.toCharArray(); 100 101 char[] result = instance.translateToCharBuff(readBuff); 102 List <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 readStr = "\nclass A {//" + "GEN-FIRST:hu\n statement;\n}//" + "GEN-LAST:hu\n"; 119 editor.setStringContent(readStr); 120 String expStr = "\nclass A { " + " \n statement;\n} " + " \n"; 121 char[] readBuff = readStr.toCharArray(); 122 123 char[] result = instance.translateToCharBuff(readBuff); 124 List <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 readStr = "\nclass A //" + "GEN-FIRST:hu\n{//" + "GEN-HEADEREND:hu\n statement;\n}//" + "GEN-LAST:hu\n"; 144 editor.setStringContent(readStr); 145 String expStr = "\nclass A " + " \n{ " + " \n statement;\n} " + " \n"; 146 char[] readBuff = readStr.toCharArray(); 147 148 char[] result = instance.translateToCharBuff(readBuff); 149 List <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 |