What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
package org.netbeans.modules.xml.text.completion;
import java.util.*;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.text.*;
import org.w3c.dom.*;
import org.netbeans.editor.*;
import org.netbeans.editor.ext.*;
import org.netbeans.modules.xml.text.syntax.*;
import org.netbeans.modules.xml.text.syntax.dom.*;
import org.netbeans.modules.xml.spi.model.*;
import junit.framework.*;
import javax.swing.JEditorPane;
import java.io.StringWriter;
import java.io.PrintWriter;
import javax.swing.SwingUtilities;
/**
* Test context passed to grammars
* REquires interanl execution.
*
* @author Petr Kuzel
*/
public class XMLCompletionQueryTest extends TestCase {
// |<-- this location simplifies offset location (add +20 and look at the column)
String data = "dataa&ref;now";
// E - element, A - attribute name, V - value, R - entity reference, . - nothing, N - notation
String mask = ".EEEEEVEEEEAAAAAVVVVV.VVVVVEEEEEE.VVRRRRVVVVEEEEAAAVVVVRRRRV..V.....V......V";
public XMLCompletionQueryTest(java.lang.String testName) {
super(testName);
}
public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite(XMLCompletionQueryTest.class);
return suite;
}
/** Test of query method, of class org.netbeans.modules.xml.text.completion.XMLCompletionQuery. */
public void testQuery() throws Exception {
System.out.println("Testing query invocation contexts...");
SwingUtilities.invokeAndWait( new Runnable() {
public void run() {
try {
BaseDocument doc = new BaseDocument(XMLKit.class, false);
try {
doc.insertString(0, data, null);
} catch (BadLocationException ex) {
fail( ex.toString());
}
TestQuery q = new TestQuery(XMLCompletionQueryTest.this);
XMLSyntaxSupport sup = new XMLSyntaxSupport(doc);
JTextComponent comp = new JEditorPane();
comp.setDocument(doc);
for (int i = 0; i < data.length(); i++) {
q.reset();
q.query(comp, i, sup);
String op = q.operation();
if (op.charAt(0) != mask.charAt(i)) {
// dump doc to provide info to fail
System.out.println("\nDiagnostics document dump:");
TokenItem token = sup.getTokenChain(0, doc.getLength());
while (token != null) {
System.out.println(">" + token);
token = token.getNext();
}
if (q.performerUsed() == false) fail("UnitTestQuery not used!");
fail( "Expected query was not called at " + i + ".\nGot '" + op + "' instead of '" + mask.charAt(i) + "'.\nContext: " + q.context() + "\nStack trace:\n" + q.trace());
} else {
System.out.println("Context[" + i + "]: " + q.context() + " OK!");
}
}
} catch (Exception ex) {
StringWriter swr = new StringWriter();
PrintWriter out = new PrintWriter(swr);
ex.printStackTrace(out);
out.flush();
fail (swr.getBuffer().toString());
}
}
});
}
/**
* Use our logging query performer.
*/
private static class TestQuery extends XMLCompletionQuery {
private UnitTestQuery q;
private boolean bind = false;
TestQuery(TestCase test) {
q = new UnitTestQuery(test);
}
/**
* XMLCompletionQuery method
*/
protected GrammarQuery getPerformer() {
bind = true;
return q;
}
void reset() {
bind = false;
q.reset();
}
boolean performerUsed() {
return bind;
}
String operation() {
return q.operation();
}
String context() {
return q.context();
}
String trace() {
return q.stackTrace();
}
}
}
|