001: package de.java2html.javasource.test;
002:
003: import java.io.IOException;
004: import java.util.NoSuchElementException;
005:
006: import de.java2html.javasource.JavaSource;
007: import de.java2html.javasource.JavaSourceIterator;
008: import de.java2html.javasource.JavaSourceRun;
009: import de.java2html.javasource.JavaSourceType;
010: import junit.framework.TestCase;
011:
012: public class JavaSourceIteratorTest extends TestCase {
013: public void testEmpty() throws IOException {
014: JavaSource source = JavaSourceParserTestCase.doParse(""); //$NON-NLS-1$
015: JavaSourceIterator iterator = source.getIterator();
016: assertFalse(iterator.hasNext());
017: }
018:
019: public void testFirstIsNewLine() throws IOException {
020: JavaSource source = JavaSourceParserTestCase.doParse("\na"); //$NON-NLS-1$
021: JavaSourceIterator iterator = source.getIterator();
022:
023: assertTrue(iterator.hasNext());
024: JavaSourceRun run = iterator.getNext();
025: //run.dump();
026: assertEquals("", run.getCode()); //$NON-NLS-1$
027: assertTrue(run.isAtStartOfLine());
028: assertTrue(run.isAtEndOfLine());
029: assertEquals(JavaSourceType.BACKGROUND, run.getType());
030:
031: assertTrue(iterator.hasNext());
032: run = iterator.getNext();
033: //run.dump();
034: assertEquals("a", run.getCode()); //$NON-NLS-1$
035: assertTrue(run.isAtStartOfLine());
036: assertTrue(run.isAtEndOfLine());
037: assertEquals(JavaSourceType.CODE, run.getType());
038: assertFalse(iterator.hasNext());
039: }
040:
041: public void testSingle() throws IOException {
042: JavaSource source = JavaSourceParserTestCase.doParse("a"); //$NON-NLS-1$
043: JavaSourceIterator iterator = source.getIterator();
044:
045: assertTrue(iterator.hasNext());
046: JavaSourceRun run = iterator.getNext();
047:
048: assertEquals("a", run.getCode()); //$NON-NLS-1$
049: assertTrue(run.isAtStartOfLine());
050: assertTrue(run.isAtEndOfLine());
051: assertEquals(JavaSourceType.CODE, run.getType());
052:
053: assertFalse(iterator.hasNext());
054: try {
055: iterator.next();
056: fail();
057: } catch (NoSuchElementException expected) {
058: //expected
059: }
060: }
061:
062: // public void testSingleLineBreak() throws IOException{
063: // JavaSource source = JavaSourceParserTest.doParse("a\n");
064: // assertEquals("a\r\n", source.getCode());
065: //
066: // JavaSourceIterator iterator = source.getIterator();
067: //
068: // assertTrue(iterator.hasNext());
069: // JavaSourceRun run = iterator.getNext();
070: //run.dump();
071: // assertEquals("a", run.getCode());
072: // assertTrue(run.isAtStartOfLine());
073: // assertTrue(run.isAtEndOfLine());
074: // assertEquals(JavaSourceType.CODE, run.getType());
075: //
076: // assertTrue(iterator.hasNext());
077: // run = iterator.getNext();
078: // run.dump();
079: //
080: // assertEquals("", run.getCode());
081: // assertTrue(run.isAtStartOfLine());
082: // assertTrue(run.isAtEndOfLine());
083: // assertEquals(JavaSourceType.CODE, run.getType());
084: //
085: // assertTrue(iterator.hasNext());
086: // run = iterator.getNext();
087: // run.dump();
088: //
089: // assertEquals("", run.getCode());
090: // assertTrue(run.isAtStartOfLine());
091: // assertTrue(run.isAtEndOfLine());
092: // assertEquals(JavaSourceType.CODE, run.getType());
093: //
094: // assertFalse(iterator.hasNext());
095: // }
096:
097: public void testTwoLines() throws IOException {
098: JavaSource source = JavaSourceParserTestCase.doParse("a\nb"); //$NON-NLS-1$
099: JavaSourceIterator iterator = source.getIterator();
100:
101: JavaSourceRun run = iterator.getNext();
102:
103: //run.dump();
104: assertEquals("a", run.getCode()); //$NON-NLS-1$
105: assertTrue(run.isAtStartOfLine());
106: assertTrue(run.isAtEndOfLine());
107: assertEquals(JavaSourceType.CODE, run.getType());
108:
109: assertTrue(iterator.hasNext());
110: run = iterator.getNext();
111:
112: //run.dump();
113: assertEquals("b", run.getCode()); //$NON-NLS-1$
114: assertTrue(run.isAtStartOfLine());
115: assertTrue(run.isAtEndOfLine());
116: assertEquals(JavaSourceType.CODE, run.getType());
117:
118: assertFalse(iterator.hasNext());
119: }
120:
121: public void test1() throws IOException {
122: JavaSource source = JavaSourceParserTestCase
123: .doParse("public String text =\"test\";"); //$NON-NLS-1$
124:
125: JavaSourceIterator iterator = source.getIterator();
126:
127: assertTrue(iterator.hasNext());
128: JavaSourceRun run = iterator.getNext();
129:
130: // run.dump();
131: assertEquals("public ", run.getCode()); //$NON-NLS-1$
132: assertTrue(run.isAtStartOfLine());
133: assertFalse(run.isAtEndOfLine());
134: assertEquals(JavaSourceType.KEYWORD, run.getType());
135:
136: assertTrue(iterator.hasNext());
137: run = iterator.getNext();
138: // run.dump();
139: assertEquals("String text =", run.getCode()); //$NON-NLS-1$
140: assertFalse(run.isAtStartOfLine());
141: assertFalse(run.isAtEndOfLine());
142: assertEquals(JavaSourceType.CODE, run.getType());
143:
144: assertTrue(iterator.hasNext());
145: run = iterator.getNext();
146:
147: // run.dump();
148: assertEquals("\"test\"", run.getCode()); //$NON-NLS-1$
149: assertFalse(run.isAtStartOfLine());
150: assertFalse(run.isAtEndOfLine());
151: assertEquals(JavaSourceType.STRING, run.getType());
152:
153: assertTrue(iterator.hasNext());
154: run = iterator.getNext();
155:
156: // run.dump();
157: assertEquals(";", run.getCode()); //$NON-NLS-1$
158: assertFalse(run.isAtStartOfLine());
159: assertTrue(run.isAtEndOfLine());
160: assertEquals(JavaSourceType.CODE, run.getType());
161:
162: assertFalse(iterator.hasNext());
163: }
164:
165: public void testEmptyLinesAreEmptyRuns() throws IOException {
166: JavaSource source = JavaSourceParserTestCase
167: .doParse("public\n\ntest"); //$NON-NLS-1$
168: JavaSourceIterator iterator = source.getIterator();
169:
170: JavaSourceRun run = iterator.getNext();
171: //run.dump();
172: assertEquals("public", run.getCode()); //$NON-NLS-1$
173:
174: run = iterator.getNext();
175: //run.dump();
176: assertEquals("", run.getCode()); //$NON-NLS-1$
177:
178: run = iterator.getNext();
179: assertEquals("test", run.getCode()); //$NON-NLS-1$
180:
181: assertFalse(iterator.hasNext());
182: }
183: }
|