View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.tests;
20  
21  import static net.sourceforge.jwebunit.junit.JWebUnit.assertTableEquals;
22  import static net.sourceforge.jwebunit.junit.JWebUnit.assertTableMatch;
23  import static net.sourceforge.jwebunit.junit.JWebUnit.assertTablePresent;
24  import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
25  import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
26  
27  import org.junit.Test;
28  
29  public class TableAssertionsTest extends JWebUnitAPITestCase {
30  
31  	public void setUp() throws Exception {
32  		super.setUp();
33  		setBaseUrl(HOST_PATH + "/TableAssertionsTest");
34  		beginAt("/TableAssertionsTestPage.html");
35  	}
36  
37  	@Test public void testAssertTablePresent() throws Throwable {
38  		assertPassFail("assertTablePresent", "testTable", "noTable");
39  	}
40  
41  	@Test public void testAssertTableNotPresent() throws Throwable {
42  		assertPassFail("assertTableNotPresent", "noTable", "testTable");
43  	}
44  
45  	@Test public void testAssertTextInTable() throws Throwable {
46  		assertPassFail("assertTextInTable", new Object[] { "testTable",
47  				"table text" }, new Object[] { "testTable", "no such text" });
48  	}
49  
50  	@Test public void testAssertTextNotInTable() throws Throwable {
51  		assertPassFail("assertTextNotInTable", new Object[] { "testTable",
52  				"no such text" }, new Object[] { "testTable", "table text" });
53  	}
54  
55  	@Test public void testAssertTextArrayInTable() throws Throwable {
56  		assertPassFail("assertTextInTable", new Object[] { "testTable",
57  				new String[] { "table text", "table text row 2" } },
58  				new Object[] { "testTable",
59  						new String[] { "table text", "no such row 2" } });
60  	}
61  
62  	@Test public void testAssertTextArrayNotInTable() throws Throwable {
63  		assertPassFail("assertTextNotInTable", new Object[] { "testTable",
64  				new String[] { "no such row 1", "no such row 2" } },
65  				new Object[] { "testTable",
66  						new String[] { "no such row 1", "table text row 2" } });
67  	}
68  
69      @Test public void testAssertMatchInTable() throws Throwable {
70          assertPassFail("assertMatchInTable",
71                         new Object[]{"testTable", "table [Tt]ext"},
72                         new Object[]{"testTable", "no.*text"});
73      }
74  
75      @Test public void testAssertNoMatchInTable() throws Throwable {
76          assertPassFail("assertNoMatchInTable",
77                         new Object[]{"testTable", "no.*text"},
78                         new Object[]{"testTable", "table [Tt]ext"});
79      }
80  
81      @Test public void testAssertMatchArrayInTable() throws Throwable {
82          assertPassFail("assertMatchInTable",
83                         new Object[]{"testTable", new String[]{"table [Tt]ext", "table [Tt]ext row 2"}},
84                         new Object[]{"testTable", new String[]{"table [Tt]ext", "no.*row 2"}});
85      }
86  
87      @Test public void testAssertNoMatchArrayInTable() throws Throwable {
88          assertPassFail("assertNoMatchInTable",
89                         new Object[]{"testTable", new String[]{"no.*row 1", "no.*row 2"}},
90                         new Object[]{"testTable", new String[]{"no.*row 1", "table [Tt]ext row 2"}});
91      }
92  
93  	@Test public void testAssertTableEquals() throws Throwable {
94  		assertPass("assertTableEquals", new Object[] {
95  				"testTable",
96  				new String[][] { { "table text" },
97  						{ "table text row 2" },
98  						{ "table text row 3", "row 3 col 1" } } });
99  	}
100 
101     @Test public void testAssertTableRowCountEquals() throws Throwable {
102         assertPassFail("assertTableRowCountEquals", new Object[] {
103                 "tree", new Integer(3)}, new Object[] {
104                         "tree", Integer.valueOf(4)});
105     }
106 
107     @Test public void testAssertTableEqualsExtraColumn() throws Throwable {
108 		assertFail("assertTableEquals", new Object[] {
109 				"testTable",
110 				new String[][] { { "table text", "extra column" },
111 						{ "table text row 2" },
112 						{ "table text row 3", "row 3 col 1" } } });
113 	}
114 
115 	@Test public void testAssertTableEqualsExtraRow() throws Throwable {
116 		assertFail("assertTableEquals",
117 				new Object[] {
118 						"testTable",
119 						new String[][] { { "table text" },
120 								{ "table text row 2" },
121 								{ "table text row 3", "row 3 col 1" },
122 								{ "no row 4" } } });
123 	}
124 
125 	@Test public void testAssertTableEqualsInvalidColumnText() throws Throwable {
126 		assertFail("assertTableEquals", new Object[] {
127 				"testTable",
128 				new String[][] { { "table text" },
129 						{ "no such text in row 2" },
130 						{ "table text row 3", "row 3 col 1" } } });
131 	}
132 
133 	@Test public void testAssertTableEqualsMissingText() throws Throwable {
134 		assertFail("assertTableEquals",
135 				new Object[] {
136 						"testTable",
137 						new String[][] { { "table text" },
138 								{ "table text row 2" },
139 								{ "table text row 3", "" } } });
140 	}
141 
142 	@Test public void testAssertTableRowsEquals() throws Throwable {
143 		assertPass("assertTableRowsEqual", new Object[] {
144 				"testTable",
145 				Integer.valueOf(1),
146 				new String[][] { { "table text row 2" },
147 						{ "table text row 3", "row 3 col 1" } } });
148 	}
149 
150 	@Test public void testAssertTableRowsEqualsTooManyExpected() throws Throwable {
151 		assertFail("assertTableRowsEqual", new Object[] {
152 				"testTable",
153 				Integer.valueOf(2),
154 				new String[][] { { "table text row 3", "row 3 col 1" },
155 						{ "unexpected" } } });
156 	}
157 
158 	@Test public void testTableWithSpaces() throws Throwable {
159 		assertTablePresent("tree");
160 		String[][] table = { { "root", " ", "" },
161 				{ "child1 ;semicolon", " ", "child2", " " },
162 				{ "child1.1", "", "child2.1", "child2.2" } };
163 		assertTableEquals("tree", table);
164 	}
165 
166     @Test public void testAssertTableMatch() throws Throwable {
167         assertPass("assertTableMatch",
168                    new Object[]{"testTable", new String[][]{{"table [Tt]ext"},
169                                                             {"table [Tt]ext row 2"},
170                                                             {"table [Tt]ext row 3", "row [0-9] col 1"}}});
171     }
172 
173     @Test public void testAssertTableMatchExtraColumn() throws Throwable {
174         assertFail("assertTableMatch",
175                    new Object[]{"testTable", new String[][]{{"table text", "", "extra column"},
176                                                             {"table text row 2", ""},
177                                                             {"table text row 3", "row 3 col 1"}}});
178     }
179 
180     @Test public void testAssertTableMatchExtraRow() throws Throwable {
181         assertFail("assertTableMatch",
182                    new Object[]{"testTable", new String[][]{{"table text", ""},
183                                                             {"table text row 2", ""},
184                                                             {"table text row 3", "row 3 col 1"},
185                                                             {"no row 4"}}});
186     }
187 
188     @Test public void testAssertTableMatchInvalidColumnText() throws Throwable {
189         assertFail("assertTableMatch",
190                    new Object[]{"testTable", new String[][]{{"table [Tt]ext", ""},
191                                                             {"no such [Tt]ext in row 2", ""},
192                                                             {"table text row 3", "row 3 col 1"}}});
193     }
194 
195     @Test public void testAssertTableMatchMissingText() throws Throwable {
196         assertFail("assertTableMatch",
197                    new Object[]{"testTable", new String[][]{{"table text", ""},
198                                                             {"table text row 2", ""},
199                                                             {"table text row 3", "^$"}}});
200     }
201 
202     @Test public void testAssertTableRowsMatch() throws Throwable {
203         assertPass("assertTableRowsMatch",
204                    new Object[]{"testTable",
205                                 Integer.valueOf(1),
206                                 new String[][]{{"table text row 2"},
207                                                {"table text row 3", "row 3 col 1"}}});
208     }
209 
210     @Test public void testAssertTableRowsMatchTooManyExpected() throws Throwable {
211         assertFail("assertTableRowsMatch",
212                    new Object[]{"testTable",
213                                 Integer.valueOf(2),
214                                 new String[][]{{"table text row 3", "row 3 col 1"},
215                                                {"unexpected", ""}}});
216     }
217     
218     @Test public void testTableWithSpacesMatch() throws Throwable {
219         assertTablePresent("tree");
220         String[][] table = {{"root", "", ""},
221         {"child1 ;semicolon", "", "child2", ""},
222         {"child1.1", "", "child2.1", "child2.2"}};
223         assertTableMatch("tree", table);
224     }
225 
226 }