PageRenderTime 90ms CodeModel.GetById 47ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/test/xml/parser/TagParserTest.java

#
Java | 613 lines | 457 code | 114 blank | 42 comment | 0 complexity | 82c8c0f54910eabf2bfbdca922482c35 MD5 | raw file
  1/*
  2 * TagParserTest.java
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * Copyright (C) 2010 Eric Le Lay
  6 *
  7 * The XML plugin is licensed under the GNU General Public License, with
  8 * the following exception:
  9 *
 10 * "Permission is granted to link this code with software released under
 11 * the Apache license version 1.1, for example used by the Xerces XML
 12 * parser package."
 13 */
 14package xml.parser;
 15
 16// {{{ jUnit imports 
 17import java.util.concurrent.TimeUnit;
 18
 19import org.junit.*;
 20import static org.junit.Assert.*;
 21import static org.junit.Assume.*;
 22
 23import org.fest.swing.fixture.*;
 24import org.fest.swing.core.*;
 25import org.fest.swing.data.TableCell;
 26import org.fest.swing.finder.*;
 27import org.fest.swing.edt.*;
 28import org.fest.swing.timing.*;
 29import org.fest.swing.core.matcher.JButtonMatcher;
 30
 31import static org.fest.assertions.Assertions.*;
 32
 33import org.gjt.sp.jedit.testframework.Log;
 34
 35import static org.gjt.sp.jedit.testframework.TestUtils.*;
 36import static org.gjt.sp.jedit.testframework.EBFixture.*;
 37import org.gjt.sp.jedit.testframework.PluginOptionsFixture;
 38import org.gjt.sp.jedit.testframework.TestUtils;
 39import static xml.XMLTestUtils.*;
 40
 41// }}}
 42
 43import org.gjt.sp.jedit.jEdit;
 44import org.gjt.sp.jedit.EBMessage;
 45import org.gjt.sp.jedit.textarea.JEditTextArea;
 46import org.gjt.sp.jedit.Buffer;
 47
 48import java.io.*;
 49import java.util.regex.Pattern;
 50import javax.swing.text.*;
 51import javax.swing.*;
 52import java.util.List;
 53
 54import java.awt.event.KeyEvent;
 55import java.awt.event.InputEvent;
 56import org.gjt.sp.jedit.gui.CompletionPopup;
 57
 58import static xml.parser.TagParser.*;
 59import xml.XmlParsedData;
 60import sidekick.SideKickParsedData;
 61
 62/**
 63 * unit tests for TagParser
 64 * $Id: TagParserTest.java 21248 2012-03-05 19:37:01Z kerik-sf $
 65 */
 66public class TagParserTest{
 67	private static File testData;
 68	
 69    @BeforeClass
 70    public static void setUpjEdit() throws IOException{
 71        TestUtils.beforeClass();
 72        testData = new File(System.getProperty("test_data")).getCanonicalFile();
 73        assertTrue(testData.exists());
 74    }
 75    
 76    @AfterClass
 77    public static void tearDownjEdit() {
 78        TestUtils.afterClass();
 79    }
 80    
 81    @Test
 82    public void testIsInsideTagNoStart(){
 83    	String text = "1 > 0";
 84    	assertFalse(TagParser.isInsideTag(text,0)); // |1 > 0
 85    	assertFalse(TagParser.isInsideTag(text,2)); // 1 |> 0
 86    	assertFalse(TagParser.isInsideTag(text,3)); // 1 >| 0
 87    	assertFalse(TagParser.isInsideTag(text,4)); // 1 > |0
 88    	assertFalse(TagParser.isInsideTag(text,5)); // 1 > 0|
 89    }
 90
 91    @Test
 92    public void testIsInsideTagSimple(){
 93    	String text = "<a>b <c>";
 94    	assertFalse(TagParser.isInsideTag(text,0)); // |<a>b <c>
 95    	assertTrue( TagParser.isInsideTag(text,1)); // <|a>b <c>
 96    	assertTrue( TagParser.isInsideTag(text,2)); // <a|>b <c>
 97    	assertFalse(TagParser.isInsideTag(text,3)); // <a>|b <c>
 98    	assertFalse(TagParser.isInsideTag(text,4)); // <a>b| <c>
 99    	assertTrue(TagParser.isInsideTag(text,6)); // <a>b <|c>
100    }
101
102    @Test
103    public void testIsInsideTagGtInAttr(){
104    	String text = "<a b='>'>";
105    	assertTrue( TagParser.isInsideTag(text,1)); // <|a b='>'>
106    	assertTrue( TagParser.isInsideTag(text,6)); // <a b='|>'>
107    	assertTrue(TagParser.isInsideTag(text,7)); // <a b='>|'>
108    	assertTrue(TagParser.isInsideTag(text,8)); // <a b='>'|>
109    	assertFalse(TagParser.isInsideTag(text,9)); // <a b='>'>|
110    }
111
112    /* currently, TagParser does not distinguish between end of tag and greater than,
113     * so the test fails
114     */
115    @Test
116    public void testGetTagAtOffsetGtInAttr(){
117    	String text = "<a b='>'> ";
118    	assertNotNull( TagParser.getTagAtOffset(text,1)); // <|a b='>'>
119    	assertNotNull( TagParser.getTagAtOffset(text,6)); // <a b='|>'>
120    	assertNotNull( TagParser.getTagAtOffset(text,7)); // <a b='>|'>
121    	assertNotNull( TagParser.getTagAtOffset(text,8)); // <a b='>'|>
122    	assertNotNull( TagParser.getTagAtOffset(text,9)); // <a b='>'>|
123    	assertNull( TagParser.getTagAtOffset(text,10)); // <a b='>'> |
124    }
125    
126    /* success */
127    @Test
128    public void testGetTagAtOffsetSimple(){
129    	String text = "<a> </a> <b c='d'/><br/>";
130    	Tag t;
131    	assertNull(TagParser.getTagAtOffset(text,-1));
132    	assertNull(TagParser.getTagAtOffset(text,text.length()+1));
133    	assertNull(TagParser.getTagAtOffset("short <",7));
134
135    	t = TagParser.getTagAtOffset(text,1);             // <|a> </a> <b c='d'/><br/>
136    	assertNotNull(t);
137    	assertEquals(0,t.start);
138    	assertEquals(3,t.end);
139    	assertEquals("a",t.tag);
140    	assertEquals(T_START_TAG,t.type);
141
142    	t = TagParser.getTagAtOffset(text,2);             // <a|> </a> <b c='d'/><br/>
143    	assertNotNull(t);
144    	assertEquals(0,t.start);
145    	assertEquals(3,t.end);
146    	assertEquals("a",t.tag);
147    	assertEquals(T_START_TAG,t.type);
148
149    	t = TagParser.getTagAtOffset(text,2);             // <a>| </a> <b c='d'/><br/>
150    	assertNotNull(t);
151    	assertEquals(0,t.start);
152    	assertEquals(3,t.end);
153    	assertEquals("a",t.tag);
154    	assertEquals(T_START_TAG,t.type);
155
156    	t = TagParser.getTagAtOffset(text,4);             // <a> |</a> <b c='d'/><br/>
157    	assertNull(t);
158
159    	t = TagParser.getTagAtOffset(text,5);             // <a> <|/a> <b c='d'/><br/>
160    	assertNotNull(t);
161    	assertEquals(4,t.start);
162    	assertEquals(8,t.end);
163    	assertEquals("a",t.tag);
164    	assertEquals(T_END_TAG,t.type);
165
166    	t = TagParser.getTagAtOffset(text,8);             // <a> </a>| <b c='d'/><br/>
167    	assertNotNull(t);
168    	assertEquals(4,t.start);
169    	assertEquals(8,t.end);
170    	assertEquals("a",t.tag);
171    	assertEquals(T_END_TAG,t.type);
172
173    	t = TagParser.getTagAtOffset(text,12);             // <a> </a> <b |c='d'/><br/>
174    	assertNotNull(t);
175    	assertEquals(9,t.start);
176    	assertEquals(19,t.end);
177    	assertEquals("b",t.tag);
178    	assertEquals(T_STANDALONE_TAG,t.type);
179
180    	t = TagParser.getTagAtOffset(text,19);             // <a> </a> <b c='d'/>|<br/>
181    	assertNotNull(t);
182    	assertEquals(9,t.start);
183    	assertEquals(19,t.end);
184    	assertEquals("b",t.tag);
185    	assertEquals(T_STANDALONE_TAG,t.type);
186
187    	t = TagParser.getTagAtOffset(text,22);             // <a> </a> <b c='d'/><br|/>
188    	assertNotNull(t);
189    	assertEquals(19,t.start);
190    	assertEquals(24,t.end);
191    	assertEquals("br",t.tag);
192    	assertEquals(T_STANDALONE_TAG,t.type);
193    }
194
195    /* success */
196    @Test
197    public void testGetTagAtOffsetComment(){
198    	String text = "<a> <!-- comment --> </a>";
199    	Tag t;
200
201    	t = TagParser.getTagAtOffset(text,1);             // <|a> <!-- comment --> </a>";
202    	assertNotNull(t);
203    	
204    	assertNull(TagParser.getTagAtOffset(text,5));             // <a> <|!-- comment --> </a>";
205    	assertNull(TagParser.getTagAtOffset(text,6));             // <a> <!|-- comment --> </a>";
206    	assertNull(TagParser.getTagAtOffset(text,10));             // <a> <!-- c|omment --> </a>";
207    	assertNull(TagParser.getTagAtOffset(text,19));             // <a> <!-- comment --> </a>";
208    	assertNull(TagParser.getTagAtOffset(text,20));             // <a> <!-- comment --> </a>";
209    }
210
211    /* success */
212    @Test
213    public void testGetTagAtOffsetNotClosed(){
214    	String text = "<a <b> <c d='>";
215    	Tag t;
216
217    	t = TagParser.getTagAtOffset(text,1);             // <|a <b> <c d='>
218    	assertNull(t);
219    	
220    	t = TagParser.getTagAtOffset(text,4);             // <a <|b> <c d='>
221    	assertNotNull(t);
222    	assertEquals(3,t.start);
223    	assertEquals(6,t.end);
224    	assertEquals("b",t.tag);
225    	assertEquals(T_START_TAG,t.type);
226
227    	t = TagParser.getTagAtOffset(text,8);             // <a <b> <|c d='>
228    	assertNull(t);
229    }
230    
231    @Test
232    public void testGetMatchingTagSimple(){
233    	String text = "<a> <b/> <c d='toto'> titi </c> </a>";
234    	
235    	Tag f;
236    	Tag t;
237    	
238    	// find closing tag for <a>
239    	f = new Tag(0,3);
240    	f.tag = "a";
241    	f.type = T_START_TAG;
242    	
243    	t = TagParser.getMatchingTag(text, f);			  // <|a> <b/> <c d='toto'> titi </c> </a>
244    	assertNotNull(t);
245    	assertEquals(32,t.start);
246    	assertEquals(36,t.end);
247    	assertEquals("a",t.tag);
248    	assertEquals(T_END_TAG,t.type);
249
250    	// find opening tag for </a>
251    	t = TagParser.getMatchingTag(text, t);			  // <a> <b/> <c d='toto'> titi </c> </|a>
252    	assertNotNull(t);
253    	assertEquals(0,t.start);
254    	assertEquals(3,t.end);
255    	assertEquals("a",t.tag);
256    	assertEquals(T_START_TAG,t.type);
257    	
258    	// no matching tag for standalone
259    	f = new Tag(4,8);
260    	f.tag = "b";
261    	f.type = T_STANDALONE_TAG;
262    	t = TagParser.getMatchingTag(text, f);			  // <a> <|b/> <c d='toto'> titi </c> </a>
263    	assertNull(t);
264    }
265    
266    @Test
267    public void testGetMatchingTagComment(){
268    	String text = "<a> <!-- </a> <a> --> </a>";
269    	
270    	Tag f;
271    	Tag t;
272    	
273    	// find closing tag for <a>, ignoring the commented one
274    	f = new Tag(0,3);
275    	f.tag = "a";
276    	f.type = T_START_TAG;
277    	
278    	t = TagParser.getMatchingTag(text, f);
279    	assertNotNull(t);
280    	assertEquals(22,t.start);
281    	assertEquals(26,t.end);
282    	assertEquals("a",t.tag);
283    	assertEquals(T_END_TAG,t.type);
284
285    	// find opening tag for </a>, ignoring the commented one
286    	t = TagParser.getMatchingTag(text, t);
287    	assertNotNull(t);
288    	assertEquals(0,t.start);
289    	assertEquals(3,t.end);
290    	assertEquals("a",t.tag);
291    	assertEquals(T_START_TAG,t.type);
292    }
293    
294    @Test
295    public void testGetMatchingTagRecurse(){
296    	String text = "<a> <a d='toto'> </a> </a>";
297    	
298    	Tag f;
299    	Tag t;
300    	
301    	// find closing tag for <a>
302    	f = new Tag(0,3);
303    	f.tag = "a";
304    	f.type = T_START_TAG;
305    	
306    	t = TagParser.getMatchingTag(text, f);
307    	assertNotNull(t);
308    	assertEquals(22,t.start);
309    	assertEquals(26,t.end);
310    	assertEquals("a",t.tag);
311    	assertEquals(T_END_TAG,t.type);
312
313    	t = TagParser.getMatchingTag(text, t);
314    	assertNotNull(t);
315    	assertEquals(0,t.start);
316    	assertEquals(3,t.end);
317    	assertEquals("a",t.tag);
318    	assertEquals(T_START_TAG,t.type);
319    }
320    
321    @Test
322    public void testGetMatchingTagNoClose(){
323    	String text = "<a>  </b>";
324    	
325    	Tag f;
326    	Tag t;
327    	
328    	// find closing tag for <a> (not there)
329    	f = new Tag(0,3);
330    	f.tag = "a";
331    	f.type = T_START_TAG;
332    	
333    	t = TagParser.getMatchingTag(text, f);
334    	assertNull(t);
335
336    	// find opening tag for </b> (not there)
337    	f = new Tag(5,9);
338    	f.tag = "b";
339    	f.type = T_END_TAG;
340    	
341    	t = TagParser.getMatchingTag(text, f);
342    	assertNull(t);
343    }
344    
345    @Test                  
346    public void testGetMatchingTagGt(){
347    	String text = "<a> a>b </a>";
348    	
349    	Tag f;
350    	Tag t;
351    	
352    	// find closing tag for <a> (ignoring a>b)
353    	f = new Tag(0,3);
354    	f.tag = "a";
355    	f.type = T_START_TAG;
356    	
357    	t = TagParser.getMatchingTag(text, f);
358    	assertNotNull(t);
359    	assertEquals(8,t.start);
360    	assertEquals(12,t.end);
361    	assertEquals("a",t.tag);
362    	assertEquals(T_END_TAG,t.type);
363
364    	// find opening tag for </a> (ignoring a>b)
365    	t = TagParser.getMatchingTag(text, t);
366    	assertNotNull(t);
367    	assertEquals(0,t.start);
368    	assertEquals(3,t.end);
369    	assertEquals("a",t.tag);
370    	assertEquals(T_START_TAG,t.type);
371    }
372    
373    @Test                  
374    public void testFindLastOpenTagXML(){
375    	final File in = new File(testData,"tagparser/test.xml");
376    	
377    	Buffer b = openFile(in.getPath());
378    	
379    	// wait for end of parsing
380    	doInBetween(new Runnable(){
381    			public void run(){
382    				action("sidekick-parse");
383    		}}, 
384    		messageOfClassCondition(sidekick.SideKickUpdate.class),
385    		10000);
386		
387		XmlParsedData data = getXmlParsedData();
388		String text = b.getText(0, b.getLength());
389    	Tag t;
390    	
391    	t = TagParser.findLastOpenTag(text, 27 , data);    //line 2:   <p:p| xmlns:p="urn:p:">
392    	assertNull(t);
393    	
394    	t = TagParser.findLastOpenTag(text, 45 , data);    //line 2:   <p:p xmlns:p="urn:p:">|
395    	assertNotNull(t);
396    	assertEquals(23,t.start);
397    	assertEquals(45,t.end);
398    	assertEquals("p:p",t.tag);
399    	assertEquals(T_START_TAG,t.type);
400
401		t = TagParser.findLastOpenTag(text, 49 , data);    //line 3:   <p:|eqn>a>b</p:eqn>
402    	assertNotNull(t);
403    	assertEquals(23,t.start);
404    	assertEquals(45,t.end);
405    	assertEquals("p:p",t.tag);
406    	assertEquals(T_START_TAG,t.type);
407
408		t = TagParser.findLastOpenTag(text, 55 , data);    //line 3:   <p:eqn>a>|b</p:eqn>
409    	assertNotNull(t);
410    	assertEquals(46,t.start);
411    	assertEquals(53,t.end);
412    	assertEquals("p:eqn",t.tag);
413    	assertEquals(T_START_TAG,t.type);
414
415		t = TagParser.findLastOpenTag(text, 84 , null);    //line 4:   <!-- some commented| <open>tag -->
416    	assertNotNull(t);
417    	assertEquals(23,t.start);
418    	assertEquals(45,t.end);
419    	assertEquals("p:p",t.tag);
420    	assertEquals(T_START_TAG,t.type);
421
422    	// inside comments, matching still works but not after the comment
423		t = TagParser.findLastOpenTag(text, 94 , data);    //line 4:   <!-- some commented <open>tag| -->
424    	assertNotNull(t);
425    	assertEquals(85,t.start);
426    	assertEquals(91,t.end);
427    	assertEquals("open",t.tag);
428    	assertEquals(T_START_TAG,t.type);
429
430		t = TagParser.findLastOpenTag(text, 114 , null);    //line 6:   |<attr c="a>d"/>
431    	assertNotNull(t);
432    	assertEquals(23,t.start);
433    	assertEquals(45,t.end);
434    	assertEquals("p:p",t.tag);
435    	assertEquals(T_START_TAG,t.type);
436
437    	
438    	// FAILS because of the GT !
439		t = TagParser.findLastOpenTag(text, 125 , data);    //line 6:   <attr c="a>|d"/>
440    	assertNotNull(t);
441    	assertEquals(23,t.start);
442    	assertEquals(45,t.end);
443    	assertEquals("p:p",t.tag);
444    	assertEquals(T_START_TAG,t.type);
445
446 		close(view(),b);
447   }
448
449   
450    @Test                  
451    public void testFindLastOpenTagHTML(){
452    	final File in = new File(testData,"tagparser/test.html");
453    	
454    	Buffer b = openParseAndWait(in.getPath());
455    	
456		XmlParsedData data = getXmlParsedData();
457		String text = b.getText(0, b.getLength());
458    	Tag t;
459    	
460    	// after the img (standalone)
461    	t = TagParser.findLastOpenTag(text, 56 , data);    //line 5:   na|vig
462    	assertNotNull(t);
463    	assertEquals(22,t.start);
464    	assertEquals(37,t.end);
465    	assertEquals("DIV",t.tag);
466    	assertEquals(T_START_TAG,t.type);
467
468    	// after the embedded div (recursion)
469		t = TagParser.findLastOpenTag(text, 67 , data);    //line 7:   |
470    	assertNotNull(t);
471    	assertEquals(7,t.start);
472    	assertEquals(21,t.end);
473    	assertEquals("div",t.tag);
474    	assertEquals(T_START_TAG,t.type);
475    	
476    	// in HTML mode, tags are lower-cased for comparison, but not when returned
477		t = TagParser.findLastOpenTag(text, 85 , data);    //line 9:   <p>hello <br>|</br>
478    	assertNotNull(t);
479    	assertEquals(68,t.start);
480    	assertEquals(71,t.end);
481    	assertEquals("P",t.tag);
482    	assertEquals(T_START_TAG,t.type);
483    	
484    	close(view(),b);
485    }
486    
487    @Test                  
488    public void testFindLastOpenTagMalformed(){
489		String text = "<html><p> not closed</html> ";
490    	Tag t;
491    	
492    	// after the closing html, to trigger the if(unwindIndex == -1) return tag; line 190 of TagParser
493    	t = TagParser.findLastOpenTag(text, 28 , null);    //  <html><p> not closed</html>| 
494    	assertNotNull(t);
495    	assertEquals(6,t.start);
496    	assertEquals(9,t.end);
497    	assertEquals("p",t.tag);
498    	assertEquals(T_START_TAG,t.type);
499
500    }
501
502    @Test
503    public void testGetAttrsContents()
504    {
505		String text = "<a title= 'a \"nice\" title' a:href=\"../test\" xmlns:a\n= 'urn:a&lt;b'>";
506    	Tag t;
507    	t = new Tag(0,text.length());
508    	t.tag="a";
509    	t.type=T_START_TAG;
510    	
511    	List<Attr> attrs = TagParser.getAttrs(text,t);
512    	assertEquals(3,attrs.size());
513    	
514    	Attr a;
515    	
516    	a = attrs.get(0);
517    	assertEquals("title",a.name);
518    	assertEquals("'a \"nice\" title'",a.val);
519
520    	a = attrs.get(1);
521    	assertEquals("a:href",a.name);
522    	assertEquals("\"../test\"",a.val);
523
524    	a = attrs.get(2);
525    	assertEquals("xmlns:a",a.name);
526    	assertEquals("'urn:a&lt;b'",a.val);
527    }
528
529    @Test
530    public void testGetAttrsStandaloneAndEmpty()
531    {
532		String text = "<a><img src='i.png'/></a>";
533    	Tag t;
534    	t = new Tag(0,3);
535    	t.tag="a";
536    	t.type=T_START_TAG;
537    	
538    	List<Attr> attrs;
539    	Attr a;
540    	
541    	attrs = TagParser.getAttrs(text,t);
542    	assertEquals(0,attrs.size());
543    	
544    	t = new Tag(3,21);
545    	t.tag="img";
546    	t.type=T_STANDALONE_TAG;
547    	
548    	attrs = TagParser.getAttrs(text,t);
549    	assertEquals(1,attrs.size());
550
551    	a = attrs.get(0);
552    	assertEquals("src",a.name);
553    	assertEquals("'i.png'",a.val);
554
555    	t = new Tag(21,25);
556    	t.tag="a";
557    	t.type=T_END_TAG;
558    	
559    	attrs = TagParser.getAttrs(text,t);
560    	assertEquals(0,attrs.size());
561
562    }
563
564    @Test
565    public void testGetAttrsMalformed()
566    {
567		String text = "<i a=='b'>";
568    	Tag t;
569    	t = new Tag(0,10);
570    	t.tag="i";
571    	t.type=T_START_TAG;
572    	
573    	List<Attr> attrs;
574    	Attr a;
575    	
576    	attrs = TagParser.getAttrs(text,t);
577    	assertEquals(1,attrs.size());
578
579    	a = attrs.get(0);
580    	assertEquals("a",a.name);
581    	assertEquals("'b'",a.val);
582
583    	text = "<j 'no name'>";
584    	t = new Tag(0,13);
585    	t.tag="j";
586    	t.type=T_START_TAG;
587    	
588    	attrs = TagParser.getAttrs(text,t);
589    	System.err.println(attrs);
590    	assertEquals(0,attrs.size());
591
592    	text= "<k l='1' 'not closed>";
593    	t = new Tag(0,21);
594    	t.tag="k";
595    	t.type=T_START_TAG;
596    	
597    	attrs = TagParser.getAttrs(text,t);
598    	assertEquals(1,attrs.size());
599
600    	a = attrs.get(0);
601    	assertEquals("l",a.name);
602    	assertEquals("'1'",a.val);
603
604    	text = "<l &attrRef;>";
605    	t = new Tag(0,13);
606    	t.tag="l";
607    	t.type=T_START_TAG;
608    	
609    	attrs = TagParser.getAttrs(text,t);
610    	assertEquals(0,attrs.size());
611    }
612
613}