    1   package test.net.sourceforge.pmd.jsp.rules;
    2
    3   import net.sourceforge.pmd.Rule;
    4   import net.sourceforge.pmd.RuleSetFactory;
    5   import net.sourceforge.pmd.RuleSetNotFoundException;
    6   import net.sourceforge.pmd.SourceType;
    7   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    8   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    9
    10  public class NoLongScriptsTest extends SimpleAggregatorTst {
    11
    12      public void testAll() throws RuleSetNotFoundException {
    13          Rule rule = new RuleSetFactory()
    14                  .createSingleRuleSet("rulesets/basic-jsp.xml").getRuleByName("NoLongScripts");
    15          runTests(new TestDescriptor[]{
    16              new TestDescriptor(VIOLATION1, "Too long javascript.", 1, rule),
    17              new TestDescriptor(NO_VIOLATION1, "Short javascript.", 0, rule),
    18          }, SourceType.JSP);
    19      }
    20
    21      private static final String VIOLATION1 = "<HTML>\n"
    22              + "<BODY>\n"
    23              + "<!--Java Script-->\n"
    24              + "<SCRIPT language='JavaScript' type='text/javascript'>\n"
    25              + "<!--\n"
    26              + "function calcDays(){\n"
    27              + "  var date1 = document.getElementById('d1').lastChild.data;\n"
    28              + "  var date2 = document.getElementById('d2').lastChild.data;\n"
    29              + "  date1 = date1.split(\"-\");\n"
    30              + "  date2 = date2.split(\"-\");\n"
    31              + "  var sDate = new Date(date1[0]+\"/\"+date1[1]+\"/\"+date1[2]);\n"
    32              + "  var eDate = new Date(date2[0]+\"/\"+date2[1]+\"/\"+date2[2]);\n"
    33              + "  var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));\n"
    34              + "  document.getElementById('diffDays').lastChild.data = daysApart;\n"
    35              + "}\n" + "\n" + "onload=calcDays;\n" + "//-->\n" + "</SCRIPT>\n"
    36              + "</BODY>\n" + "</HTML>;\n";
    37
    38      private static final String NO_VIOLATION1 = "<HTML>\n"
    39              + "<BODY>\n"
    40              + "<!--Java Script-->\n"
    41              + "<SCRIPT language='JavaScript' type='text/javascript'>\n"
    42              + "<!--\n"
    43              + "function calcDays(){\n"
    44              + "  document.getElementById('diffDays').lastChild.data = daysApart;\n"
    45              + "}\n" + "\n" + "onload=calcDays;\n" + "//-->\n" + "</SCRIPT>\n"
    46              + "</BODY>\n" + "</HTML>;\n";
    47  }