View Javadoc

    1   package net.sourceforge.pmd.lang.jsp.ast;
    2
    3   import static org.junit.Assert.assertFalse;
    4   import static org.junit.Assert.assertTrue;
    5
    6   import org.junit.Before;
    7   import org.junit.Test;
    8
    9   public class OpenTagRegisterTest {
    10
    11  	private OpenTagRegister tagList;
    12
    13  	private int elmId = 0;
    14
    15  	@Before
    16  	public void newRegister() {
    17  		tagList = new OpenTagRegister();
    18  	}
    19
    20  	/**
    21  	 * <a> <b> </a>
    22  	 */
    23  	@Test
    24  	public void testSimpleNesting() {
    25  		ASTElement elm = element("a");
    26  		ASTElement elm2 = element("b");
    27
    28  		tagList.openTag(elm);
    29  		tagList.openTag(elm2);
    30  		tagList.closeTag(elm);
    31
    32  		assertFalse(elm.isUnclosed());
    33  		assertTrue(elm2.isUnclosed());
    34  	}
    35
    36  	/**
    37  	 * <a> <b> <b> </a>
    38  	 */
    39  	@Test
    40  	public void doubleNesting() {
    41  		ASTElement elm = element("a");
    42  		ASTElement elm2 = element("b");
    43  		ASTElement elm3 = element("b");
    44
    45  		tagList.openTag(elm);
    46  		tagList.openTag(elm2);
    47  		tagList.openTag(elm3);
    48  		tagList.closeTag(elm);
    49
    50  		assertFalse(elm.isUnclosed());
    51  		assertTrue(elm2.isUnclosed());
    52  		assertTrue(elm3.isUnclosed());
    53  	}
    54
    55  	/**
    56  	 * <x> <a> <b> <b> </x> </a> </x>
    57  	 */
    58  	@Test
    59  	public void unopenedTags() {
    60  		ASTElement elm = element("x");
    61  		ASTElement elm2 = element("a");
    62  		ASTElement elm3 = element("b");
    63  		ASTElement elm4 = element("b");
    64
    65  		tagList.openTag(elm);
    66  		tagList.openTag(elm2);
    67  		tagList.openTag(elm3);
    68  		tagList.openTag(elm4);
    69  		tagList.closeTag(elm);
    70  		tagList.closeTag(elm2);
    71  		tagList.closeTag(elm3);
    72  		tagList.closeTag(elm);
    73
    74  		assertFalse(elm.isUnclosed());
    75  		assertTrue(elm2.isUnclosed());
    76  		assertTrue(elm3.isUnclosed());
    77  		assertTrue(elm4.isUnclosed());
    78  	}
    79
    80  	/**
    81  	 * <x> <a> <b> <b> </z> </a> </x>
    82  	 *
    83  	 */
    84  	@Test
    85  	public void interleavedTags() {
    86  		ASTElement elm = element("x");
    87  		ASTElement elm2 = element("a");
    88  		ASTElement elm3 = element("b");
    89  		ASTElement elm4 = element("b");
    90  		ASTElement elm5 = element("z");
    91
    92  		tagList.openTag(elm);
    93  		tagList.openTag(elm2);
    94  		tagList.openTag(elm3);
    95  		tagList.openTag(elm4);// open b
    96  		tagList.closeTag(elm5);// close z
    97  		tagList.closeTag(elm2);// close a
    98  		tagList.closeTag(elm);// close x
    99
    100 		assertFalse(elm.isUnclosed()); // x is closed
    101 		assertFalse(elm2.isUnclosed()); // a is closed
    102 		assertTrue(elm3.isUnclosed());
    103 		assertTrue(elm4.isUnclosed());
    104 		// elm5 ???
    105 	}
    106
    107 	/**
    108 	 * <a> <x> <a> <b> <b> </z> </a> </x>
    109 	 */
    110 	@Test
    111 	public void openedIsolatedTag() {
    112 		ASTElement a = element("a");
    113 		ASTElement x = element("x");
    114 		ASTElement a2 = element("a");
    115 		ASTElement b = element("b");
    116 		ASTElement b2 = element("b");
    117 		ASTElement z = element("z");
    118
    119 		tagList.openTag(a);
    120 		tagList.openTag(x);
    121 		tagList.openTag(a2);
    122 		tagList.openTag(b);
    123 		tagList.openTag(b2);
    124 		tagList.closeTag(z);// close z
    125 		tagList.closeTag(a2);// close second a
    126 		tagList.closeTag(x);// close x
    127
    128 		assertTrue(a.isUnclosed());   // first a is unclosed
    129 		assertFalse(x.isUnclosed()); // x is closed
    130 		assertFalse(a2.isUnclosed()); // a is closed
    131
    132 		assertTrue(b.isUnclosed());
    133 		assertTrue(b2.isUnclosed());
    134 	}
    135
    136 	private ASTElement element(String name) {
    137 		ASTElement elm = new ASTElement(elmId++);
    138 		elm.setName(name);
    139 		return elm;
    140 	}
    141
    142 }