001: /*
002: * SQLTokenMarkerTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.editor;
013:
014: import javax.swing.text.BadLocationException;
015: import javax.swing.text.Element;
016: import javax.swing.text.Segment;
017: import junit.framework.TestCase;
018:
019: /**
020: * @author support@sql-workbench.net
021: */
022: public class SQLTokenMarkerTest extends TestCase {
023: public SQLTokenMarkerTest(String testName) {
024: super (testName);
025: }
026:
027: public void testMarkTokensImpl() {
028: try {
029: SyntaxDocument doc = new SyntaxDocument();
030:
031: SQLTokenMarker marker = new AnsiSQLTokenMarker();
032: doc.setTokenMarker(marker);
033:
034: String sql = "SELECT x FROM thetable\n"
035: + "WHERE desc = 'this is a \n" + " multiline \n"
036: + "string literal' \n" + "AND name = 'arthur'\n"
037: + "AND col = 5\n" + "AND filename IS NOT NULL \n"
038: + "AND http_status IN ('200')";
039:
040: // Expected token IDs
041: int[][] expectedTokens = new int[][] {
042: { Token.KEYWORD1, Token.NULL, Token.KEYWORD1,
043: Token.NULL }, // SELECT ...
044: { Token.KEYWORD1, Token.NULL, Token.KEYWORD2,
045: Token.NULL, Token.OPERATOR, Token.NULL,
046: Token.LITERAL1 }, // WHERE ...
047: { Token.LITERAL1 }, // multiline
048: { Token.LITERAL1, Token.NULL }, // string literal'
049: { Token.KEYWORD1, Token.NULL, Token.OPERATOR,
050: Token.NULL, Token.LITERAL1 }, // AND name = ...
051: { Token.KEYWORD1, Token.NULL, Token.OPERATOR,
052: Token.NULL, Token.LITERAL1 }, // AND col = ...
053: { Token.KEYWORD1, Token.NULL, Token.KEYWORD1,
054: Token.NULL, Token.KEYWORD1, Token.NULL,
055: Token.KEYWORD1, Token.NULL, Token.KEYWORD1,
056: Token.NULL }, // AND filename ...
057: { Token.KEYWORD1, Token.NULL, Token.KEYWORD1,
058: Token.NULL, Token.NULL, Token.LITERAL1,
059: Token.NULL } // AND htpp ...
060: };
061:
062: doc.insertString(0, sql, null);
063:
064: Segment lineContent = new Segment();
065: int lineCount = doc.getDefaultRootElement()
066: .getElementCount();
067: assertEquals(8, lineCount);
068: assertEquals(8, marker.getLineCount());
069:
070: for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) {
071: getLineText(doc, lineIndex, lineContent);
072: Token token = marker.markTokens(lineContent, lineIndex);
073: printTokenLine(lineContent, token);
074: verifyLine(expectedTokens, token, lineIndex);
075: }
076:
077: // Test access in the middle of the text
078: for (int lineIndex = 2; lineIndex < lineCount; lineIndex++) {
079: getLineText(doc, lineIndex, lineContent);
080: Token token = marker.markTokens(lineContent, lineIndex);
081: verifyLine(expectedTokens, token, lineIndex);
082: }
083: } catch (Exception ex) {
084: ex.printStackTrace();
085: fail(ex.getMessage());
086: }
087: }
088:
089: private void verifyLine(int[][] expectedLines, Token token,
090: int lineIndex) {
091: if (lineIndex < 0 || lineIndex > expectedLines.length)
092: fail("Wrong line index");
093: int[] expectedTokens = expectedLines[lineIndex];
094: int tokenIndex = 0;
095: while (token != null) {
096: if (tokenIndex >= expectedTokens.length) {
097: fail("Line " + lineIndex + " has " + (tokenIndex + 1)
098: + " tokens, but only " + expectedTokens.length
099: + " tokens defined for test.");
100: } else {
101: assertEquals("Wrong token at line: " + lineIndex
102: + ", token: " + tokenIndex + " expected: "
103: + Token.typeString(expectedTokens[tokenIndex])
104: + " got: " + Token.typeString(token.id) + "",
105: expectedTokens[tokenIndex], token.id);
106: }
107: token = token.next;
108: tokenIndex++;
109: }
110: }
111:
112: private void printTokenLine(Segment line, Token tokens) {
113: while (tokens != null) {
114: int length = tokens.length;
115: line.count = length;
116: System.out.print("[" + tokens.typeString() + "]"
117: + line.toString());
118: line.offset += length;
119: tokens = tokens.next;
120: }
121: System.out.println("");
122: }
123:
124: private void getLineText(SyntaxDocument doc, int lineIndex,
125: Segment lineContent) throws BadLocationException {
126: Element lineElement = doc.getDefaultRootElement().getElement(
127: lineIndex);
128: int start = lineElement.getStartOffset();
129: int end = lineElement.getEndOffset();
130: doc.getText(start, end - start - 1, lineContent);
131: }
132: }
|