1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.compiler;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditorSupport;
import java.io.File;
import java.io.IOException;
import java.nio.charset.CodingErrorAction;
import java.util.Date;
import java.util.Scanner;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.DynamicAttributes;
import javax.servlet.jsp.tagext.JspIdConsumer;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.TryCatchFinally;
import javax.servlet.jsp.tagext.VariableInfo;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.jasper.servlet.JasperInitializer;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.compat.JreCompat;
public class TestGenerator extends TomcatBaseTest {
private static final String NEW_LINE = System.lineSeparator();
@Test
public void testBug45015a() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45015a.jsp");
String result = res.toString();
// Beware of the differences between escaping in JSP attributes and
// in Java Strings
assertEcho(result, "00-hello 'world'");
assertEcho(result, "01-hello 'world");
assertEcho(result, "02-hello world'");
assertEcho(result, "03-hello world'");
assertEcho(result, "04-hello world\"");
assertEcho(result, "05-hello \"world\"");
assertEcho(result, "06-hello \"world");
assertEcho(result, "07-hello world\"");
assertEcho(result, "08-hello world'");
assertEcho(result, "09-hello world\"");
}
@Test
public void testBug45015b() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45015b.jsp", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug45015c() throws Exception {
getTomcatInstanceTestWebapp(false, true);
int rc = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45015c.jsp", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug48701Fail() throws Exception {
getTomcatInstanceTestWebapp(true, true);
int rc = getUrl("http://localhost:" + getPort() + "/test/bug48nnn/bug48701-fail.jsp", new ByteChunk(), null);
Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug48701UseBean() throws Exception {
testBug48701("bug48nnn/bug48701-UseBean.jsp");
}
@Test
public void testBug48701VariableInfo() throws Exception {
testBug48701("bug48nnn/bug48701-VI.jsp");
}
@Test
public void testBug48701TagVariableInfoNameGiven() throws Exception {
testBug48701("bug48nnn/bug48701-TVI-NG.jsp");
}
@Test
public void testBug48701TagVariableInfoNameFromAttribute() throws Exception {
testBug48701("bug48nnn/bug48701-TVI-NFA.jsp");
}
private void testBug48701(String jsp) throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/" + jsp);
String result = res.toString();
assertEcho(result, "00-PASS");
}
public static class Bug48701 extends TagSupport {
private static final long serialVersionUID = 1L;
private String beanName = null;
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
@Override
public int doStartTag() throws JspException {
Bean bean = new Bean();
bean.setTime((new Date()).toString());
pageContext.setAttribute("now", bean);
return super.doStartTag();
}
}
public static class Bug48701TEI extends TagExtraInfo {
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("now", Bean.class.getCanonicalName(), true, VariableInfo.AT_END) };
}
}
public static class Bean {
private String time;
public void setTime(String time) {
this.time = time;
}
public String getTime() {
return time;
}
}
@Test
public void testBug49799() throws Exception {
String[] expected =
{ "<p style=\"color:red\">00-Red</p>", "<p>01-Not Red</p>", "<p style=\"color:red\">02-Red</p>",
"<p>03-Not Red</p>", "<p style=\"color:red\">04-Red</p>", "<p>05-Not Red</p>" };
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49799.jsp", res, null);
// Check request completed
String result = res.toString();
String[] lines = result.split("\n|\r|\r\n");
int i = 0;
for (String line : lines) {
if (line.length() > 0) {
Assert.assertEquals(expected[i], line);
i++;
}
}
}
/** Assertion for text printed by tags:echo */
private static void assertEcho(String result, String expected) {
Assert.assertTrue(result.indexOf("<p>" + expected + "</p>") > 0);
}
@Test
public void testBug56529() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk bc = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56529.jsp", bc, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
String response = bc.toStringInternal(CodingErrorAction.REPORT, CodingErrorAction.REPORT);
Assert.assertTrue(response, response.contains("[1:attribute1: '', attribute2: '']"));
Assert.assertTrue(response, response.contains("[2:attribute1: '', attribute2: '']"));
}
public static class Bug56529 extends TagSupport {
private static final long serialVersionUID = 1L;
private String attribute1 = null;
private String attribute2 = null;
public void setAttribute1(String attribute1) {
this.attribute1 = attribute1;
}
public String getAttribute1() {
return attribute1;
}
public void setAttribute2(String attribute2) {
this.attribute2 = attribute2;
}
public String getAttribute2() {
return attribute2;
}
@Override
public int doEndTag() throws JspException {
try {
pageContext.getOut().print("attribute1: '" + attribute1 + "', " + "attribute2: '" + attribute2 + "'");
} catch (IOException ioe) {
throw new JspException(ioe);
}
return EVAL_PAGE;
}
}
@Test
public void testBug56581() throws LifecycleException {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = new ByteChunk();
try {
getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56581.jsp", res, null);
Assert.fail("An IOException was expected.");
} catch (IOException ignore) {
/*
* ErrorReportValve flushes and aborts the connection when an unexpected error is encountered and response
* has already been committed. It results in an exception here: java.io.IOException: Premature EOF
*/
}
String result = res.toString();
Assert.assertTrue(result.startsWith("0 Hello world!\n"));
Assert.assertTrue(result.endsWith("999 Hello world!\n"));
}
// https://bz.apache.org/bugzilla/show_bug.cgi?id=43400
@Test
public void testTagsWithEnums() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug43nnn/bug43400.jsp");
String result = res.toString();
System.out.println(result);
assertEcho(result, "ASYNC");
}
@Test
public void testTrimSpacesExtended01() throws Exception {
doTestTrimSpacesExtended(false);
}
@Test
public void testTrimSpacesExtended02() throws Exception {
doTestTrimSpacesExtended(true);
}
private void doTestTrimSpacesExtended(boolean removeBlankLines) throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
ctxt.addServletContainerInitializer(new JasperInitializer(), null);
Tomcat.initWebappDefaults(ctxt);
Wrapper w = (Wrapper) ctxt.findChild("jsp");
if (removeBlankLines) {
w.addInitParameter("trimSpaces", "extended");
}
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/jsp/trim-spaces-extended.jsp");
String result = res.toString();
Scanner scanner = new Scanner(result);
int blankLineCount = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.length() == 0) {
blankLineCount++;
}
}
if (!removeBlankLines && blankLineCount == 0) {
Assert.fail("TrimSpaceOptions.EXTENDED not configured but blank lines have been removed");
} else if (removeBlankLines && blankLineCount > 0) {
Assert.fail("TrimSpaceOptions.EXTENDED does not allow the line to be just a new line character");
}
scanner.close();
}
@Test
public void testEscape01() {
String result = Generator.escape("\"\\\n\r");
Assert.assertEquals("\\\"\\\\\\n\\r", result);
}
@Test
public void testEscape02() {
String result = Generator.escape("\\");
Assert.assertEquals("\\\\", result);
}
@Test
public void testEscape03() {
String result = Generator.escape("xxx\\");
Assert.assertEquals("xxx\\\\", result);
}
@Test
public void testEscape04() {
String result = Generator.escape("\\xxx");
Assert.assertEquals("\\\\xxx", result);
}
@Test
public void testQuote01() {
String result = Generator.quote('\'');
Assert.assertEquals("\'\\\'\'", result);
}
@Test
public void testQuote02() {
String result = Generator.quote('\\');
Assert.assertEquals("\'\\\\\'", result);
}
@Test
public void testQuote03() {
String result = Generator.quote('\n');
Assert.assertEquals("\'\\n\'", result);
}
@Test
public void testQuote04() {
String result = Generator.quote('\r');
Assert.assertEquals("\'\\r\'", result);
}
@Test
public void testQuote05() {
String result = Generator.quote('x');
Assert.assertEquals("\'x\'", result);
}
@Test
public void testJspId() throws Exception {
doTestJspId(false);
}
@Test
public void testJspIdDocument() throws Exception {
doTestJspId(true);
}
private void doTestJspId(boolean document) throws Exception {
getTomcatInstanceTestWebapp(false, true);
String uri = "http://localhost:" + getPort() + "/test/jsp/generator/jsp-id.jsp";
if (document) {
uri += "x";
}
ByteChunk res = getUrl(uri);
String result = res.toString();
// Two tags should have different IDs
String[] ids = new String[2];
int start = 0;
int end = 0;
for (int i = 0; i < ids.length; i++) {
start = result.indexOf("Jsp ID is [", start) + 11;
end = result.indexOf("]", start);
ids[i] = result.substring(start, end);
}
// Confirm the IDs are not the same
Assert.assertNotEquals(ids[0], ids[1]);
}
public static class JspIdTag extends TagSupport implements JspIdConsumer {
private static final long serialVersionUID = 1L;
private volatile String jspId;
@Override
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("<p>Jsp ID is [" + jspId + "]</p>");
} catch (IOException ioe) {
throw new JspException(ioe);
}
return super.doStartTag();
}
@Override
public void setJspId(String jspId) {
this.jspId = jspId;
}
}
public static class TryCatchFinallyBodyTag extends BodyTagSupport implements TryCatchFinally {
private static final long serialVersionUID = 1L;
@Override
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("<p>OK</p>");
} catch (IOException ioe) {
throw new JspException(ioe);
}
return super.doStartTag();
}
@Override
public void doCatch(Throwable t) throws Throwable {
// NO-OP
}
@Override
public void doFinally() {
// NO-OP
}
}
public static class TesterBodyTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
@Override
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("<p>OK</p>");
} catch (IOException ioe) {
throw new JspException(ioe);
}
return super.doStartTag();
}
}
public static class TesterTag implements Tag {
private Tag parent;
@Override
public void setPageContext(PageContext pc) {
}
@Override
public void setParent(Tag t) {
parent = t;
}
@Override
public Tag getParent() {
return parent;
}
@Override
public int doStartTag() throws JspException {
return 0;
}
@Override
public int doEndTag() throws JspException {
return 0;
}
@Override
public void release() {
}
}
public static class TesterTagA extends TesterTag {
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
private static boolean tagTesterTagReleaseReleased = false;
public static class TesterTagRelease extends TesterTag {
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
@Override
public void release() {
tagTesterTagReleaseReleased = true;
}
}
public static class DataPropertyEditor extends PropertyEditorSupport {
}
public static class TesterScriptingTag extends TagSupport {
private static final long serialVersionUID = 1L;
private String attribute02;
private String attribute03;
public String getAttribute02() {
return attribute02;
}
public void setAttribute02(String attribute02) {
this.attribute02 = attribute02;
}
public String getAttribute03() {
return attribute03;
}
public void setAttribute03(String attribute03) {
this.attribute03 = attribute03;
}
}
public static class TesterScriptingTagB extends TagSupport {
private static final long serialVersionUID = 1L;
private String attribute02;
public String getAttribute02() {
return attribute02;
}
public void setAttribute02(String attribute02) {
this.attribute02 = attribute02;
}
}
public static class TesterScriptingTagBTEI extends TagExtraInfo {
@Override
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] { new VariableInfo("variable01", "java.lang.String", true, VariableInfo.NESTED),
new VariableInfo(data.getAttribute("attribute02").toString(), "java.lang.String", true,
VariableInfo.NESTED),
new VariableInfo("variable03", "java.lang.String", false, VariableInfo.NESTED) };
}
}
public static class TesterDynamicTag extends TagSupport implements DynamicAttributes {
private static final long serialVersionUID = 1L;
@Override
public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {
// NO-OP
}
}
public static class TesterAttributeTag extends TagSupport {
private static final long serialVersionUID = 1L;
private Object attribute01;
private Object attribute02;
private Object attribute03;
private Object attribute04;
private Object attribute05;
private Object attribute06;
public Object getAttribute01() {
return attribute01;
}
public void setAttribute01(Object attribute01) {
this.attribute01 = attribute01;
}
public Object getAttribute02() {
return attribute02;
}
public void setAttribute02(Object attribute02) {
this.attribute02 = attribute02;
}
public Object getAttribute03() {
return attribute03;
}
public void setAttribute03(Object attribute03) {
this.attribute03 = attribute03;
}
public Object getAttribute04() {
return attribute04;
}
public void setAttribute04(Object attribute04) {
this.attribute04 = attribute04;
}
public Object getAttribute05() {
return attribute05;
}
public void setAttribute05(Object attribute05) {
this.attribute05 = attribute05;
}
public Object getAttribute06() {
return attribute06;
}
public void setAttribute06(Object attribute06) {
this.attribute06 = attribute06;
}
}
@Test
public void testLambdaScriptlets() throws Exception {
doTestJsp("lambda.jsp");
}
@Test
public void testInfoConflictNone() throws Exception {
doTestJsp("info-conflict-none.jsp");
}
@Test
public void testInfoConflict() throws Exception {
doTestJsp("info-conflict.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testTagWithVariable() throws Exception {
doTestJsp("variable-tei-nested.jsp");
}
@Test
public void testTagWithVariableFromAttr() throws Exception {
doTestJsp("variable-from-attr-nested.jsp");
}
@Test
public void testTagFileWithVariable() throws Exception {
doTestJsp("variable-tagfile-nested.jsp");
}
@Test
public void testTagFileWithVariableFromAttr() throws Exception {
doTestJsp("variable-tagfile-from-attr-nested.jsp");
}
@Test
public void testSingleThreaded() throws Exception {
doTestJsp("single-threaded.jsp");
}
@Test
public void testXpoweredBy() throws Exception {
doTestJsp("x-powered-by.jsp");
}
@Test
public void testXmlProlog01() throws Exception {
doTestJsp("xml-prolog-01.jspx");
}
@Test
public void testXmlProlog02() throws Exception {
doTestJsp("xml-prolog-02.jspx");
}
@Test
public void testXmlPrologTag() throws Exception {
doTestJsp("xml-prolog-tag.jspx");
}
@Test
public void testXmlDoctype01() throws Exception {
doTestJsp("xml-doctype-01.jspx");
}
@Test
public void testXmlDoctype02() throws Exception {
doTestJsp("xml-doctype-02.jspx");
}
@Test
public void testPlugin01() throws Exception {
doTestJsp("plugin-01.jspx");
}
@Test
public void testForward01() throws Exception {
doTestJsp("forward-01.jsp");
}
@Test
public void testForward02() throws Exception {
doTestJsp("forward-02.jsp");
}
@Test
public void testForward03() throws Exception {
doTestJsp("forward-03.jsp");
}
@Test
public void testForward04() throws Exception {
doTestJsp("forward-04.jsp");
}
@Test
public void testElement01() throws Exception {
doTestJsp("element-01.jsp");
}
@Test
public void testInclude01() throws Exception {
doTestJsp("include-01.jsp");
}
@Test
public void testSetProperty01() throws Exception {
doTestJsp("setproperty-01.jsp");
}
@Test
public void testUseBean01() throws Exception {
doTestJsp("usebean-01.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testUseBean02() throws Exception {
doTestJsp("usebean-02.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testUseBean03() throws Exception {
doTestJsp("usebean-03.jsp");
}
@Test
public void testUseBean04() throws Exception {
doTestJsp("usebean-04.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testUseBean05() throws Exception {
// Whether this test passes or fails depends on the Java version used
// and the JRE settings.
// For the test to pass it requires --illegal-access=deny
// This is the default setting on Java 16 upwards
if (JreCompat.isJre16Available()) {
doTestJsp("usebean-05.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} else {
doTestJsp("usebean-05.jsp", HttpServletResponse.SC_OK);
}
}
@Test
public void testUseBean06() throws Exception {
doTestJsp("usebean-06.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testUseBean07() throws Exception {
doTestJsp("usebean-07.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testUseBean08() throws Exception {
doTestJsp("usebean-08.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testCustomTag01() throws Exception {
doTestJsp("try-catch-finally.jsp");
}
@Test
public void testCustomTag02() throws Exception {
doTestJsp("customtag-02.jsp");
}
@Test
public void testCustomTag03() throws Exception {
doTestJsp("customtag-03.jsp");
}
@Test
public void testCustomTag04() throws Exception {
doTestJsp("customtag-04.jsp", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
@Test
public void testTemplateText01() throws Exception {
doTestJsp("templatetext-01.jsp");
}
@Test
public void testTemplateText02() throws Exception {
doTestJsp("templatetext-02.jsp");
}
@Test
public void testInvoke01() throws Exception {
doTestJsp("invoke-01.jsp");
}
@Test
public void testDoBody01() throws Exception {
doTestJsp("dobody-01.jsp");
}
@Test
public void testScriptingVariables01() throws Exception {
doTestJsp("scriptingvariables-01.jsp");
}
@Test
public void testScriptingVariables02() throws Exception {
doTestJsp("scriptingvariables-02.jsp");
}
@Test
public void testAttribute01() throws Exception {
doTestJsp("attribute-01.jsp");
}
@Test
public void testAttribute02() throws Exception {
doTestJsp("attribute-02.jsp");
}
@Test
public void testAttribute03() throws Exception {
doTestJsp("attribute-03.jsp");
}
@Test
public void testAttribute04() throws Exception {
doTestJsp("attribute-04.jsp");
}
@Test
public void testSetters01() throws Exception {
doTestJsp("setters-01.jsp");
}
@Test
public void testCircular01() throws Exception {
doTestJsp("circular-01.jsp");
}
@Test
public void testDeferredMethod01() throws Exception {
doTestJsp("deferred-method-01.jsp");
}
@Test
public void testDeferredMethod02() throws Exception {
doTestJsp("deferred-method-02.jsp");
}
@Test
public void testBeanInfo01() throws Exception {
BeanInfo bi = Introspector.getBeanInfo(TesterTagA.class);
for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
if (pd.getName().equals("data")) {
pd.setPropertyEditorClass(DataPropertyEditor.class);
}
}
doTestJsp("beaninfo-01.jsp");
}
@Test
public void testBreakELInterpreter() throws Exception {
getTomcatInstanceTestWebapp(false, true);
// This should break all subsequent requests
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/jsp/generator/break-el-interpreter.jsp", body, null);
Assert.assertEquals(body.toString(), HttpServletResponse.SC_OK, rc);
body.recycle();
rc = getUrl("http://localhost:" + getPort() + "/test/jsp/generator/info.jsp", body, null);
Assert.assertEquals(body.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBreakStringInterpreter() throws Exception {
getTomcatInstanceTestWebapp(false, true);
// This should break all subsequent requests
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/jsp/generator/break-string-interpreter.jsp", body,
null);
Assert.assertEquals(body.toString(), HttpServletResponse.SC_OK, rc);
body.recycle();
rc = getUrl("http://localhost:" + getPort() + "/test/jsp/generator/info.jsp", body, null);
Assert.assertEquals(body.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
}
@Test
public void testBug65390() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug65390.jsp", body, null);
Assert.assertEquals(body.toString(), HttpServletResponse.SC_OK, rc);
}
@Test
public void testBug69508() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug69508.jsp?init=InitCommand", body, null);
String text = body.toString();
Assert.assertEquals(text, HttpServletResponse.SC_OK, rc);
// include page URL with param cmd
Assert.assertTrue(text, text.contains("<p>cmd - someCommand</p>"));
Assert.assertTrue(text, text.contains("<p>param1 - value1</p>"));
Assert.assertTrue(text, text.contains("<p>cmd - someCommandAbs</p>"));
Assert.assertTrue(text, text.contains("<p>param1 - value1Abs</p>"));
// include page URL without param
Assert.assertTrue(text, text.contains("<p>param2 - value2</p>"));
Assert.assertTrue(text, text.contains("<p>param2 - value2Abs</p>"));
Assert.assertTrue(text, text.contains("<p>param3 - InitCommand</p>"));
Assert.assertTrue(text, text.contains("<p>param3 - InitCommandAbs</p>"));
Assert.assertTrue(text, text.contains("<p>param4 - value4</p>"));
Assert.assertTrue(text, text.contains("<p>param4 - value4Abs</p>"));
Assert.assertTrue(text, text.contains("<p>param5 - InitCommand</p>"));
Assert.assertTrue(text, text.contains("<p>param5 - InitCommandAbs</p>"));
Assert.assertTrue(text, text.contains("<p>param6 - value6</p>"));
Assert.assertTrue(text, text.contains("<p>param6 - value6Abs</p>"));
}
@Test
public void testTagReleaseWithPooling() throws Exception {
doTestTagRelease(true);
}
@Test
public void testTagReleaseWithoutPooling() throws Exception {
doTestTagRelease(false);
}
public void doTestTagRelease(boolean enablePooling) throws Exception {
tagTesterTagReleaseReleased = false;
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
ctxt.addServletContainerInitializer(new JasperInitializer(), null);
Tomcat.initWebappDefaults(ctxt);
Wrapper w = (Wrapper) ctxt.findChild("jsp");
w.addInitParameter("enablePooling", String.valueOf(enablePooling));
tomcat.start();
getUrl("http://localhost:" + getPort() + "/jsp/generator/release.jsp");
if (enablePooling) {
Assert.assertFalse(tagTesterTagReleaseReleased);
} else {
Assert.assertTrue(tagTesterTagReleaseReleased);
}
}
@Test
public void testNonstandardSets() throws Exception {
getTomcatInstanceTestWebapp(true, true);
// This should break all subsequent requests
ByteChunk body = new ByteChunk();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/set-01.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=testValue" + NEW_LINE
+ "request value=null" + NEW_LINE
+ "session value=null" + NEW_LINE
+ "application value=null", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/set-02.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=testValue" + NEW_LINE
+ "request value=null" + NEW_LINE
+ "session value=null" + NEW_LINE
+ "application value=null", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/set-03.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=null" + NEW_LINE
+ "request value=testValue" + NEW_LINE
+ "session value=null" + NEW_LINE
+ "application value=null", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/set-04.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=null" + NEW_LINE
+ "request value=null" + NEW_LINE
+ "session value=testValue" + NEW_LINE
+ "application value=null", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/set-05.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=null" + NEW_LINE
+ "request value=null" + NEW_LINE
+ "session value=null" + NEW_LINE
+ "application value=testValue", body.toString());
body.recycle();
}
private void doTestJsp(String jspName) throws Exception {
doTestJsp(jspName, HttpServletResponse.SC_OK);
}
private void doTestJsp(String jspName, int expectedResponseCode) throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/jsp/generator/" + jspName, body, null);
Assert.assertEquals(body.toString(), expectedResponseCode, rc);
}
@Test
public void testNonstandardRemoves() throws Exception {
getTomcatInstanceTestWebapp(true, true);
// This should break all subsequent requests
ByteChunk body = new ByteChunk();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/remove-01.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=null" + NEW_LINE
+ "request value=testValue" + NEW_LINE
+ "session value=testValue" + NEW_LINE
+ "application value=testValue", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/remove-02.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=testValue" + NEW_LINE
+ "request value=null" + NEW_LINE
+ "session value=testValue" + NEW_LINE
+ "application value=testValue", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/remove-03.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=testValue" + NEW_LINE
+ "request value=testValue" + NEW_LINE
+ "session value=null" + NEW_LINE
+ "application value=testValue", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/remove-04.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=testValue" + NEW_LINE
+ "request value=testValue" + NEW_LINE
+ "session value=testValue" + NEW_LINE
+ "application value=null", body.toString());
body.recycle();
getUrl("http://localhost:" + getPort() + "/test/jsp/generator/nonstandard/remove-05.jsp", body, null);
Assert.assertEquals(NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE + NEW_LINE
+ "pageContext value=null" + NEW_LINE
+ "request value=null" + NEW_LINE
+ "session value=null" + NEW_LINE
+ "application value=null", body.toString());
body.recycle();
}
}
|