001    // Copyright May 9, 2006 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry.util;
015    
016    import org.apache.oro.text.perl.Perl5Util;
017    import org.apache.oro.text.regex.MatchResult;
018    import org.apache.tapestry.BaseComponentTestCase;
019    import org.testng.annotations.Test;
020    
021    
022    /**
023     * Tests functionality of {@link ScriptUtils}.
024     * 
025     */
026    @Test
027    public class ScriptUtilsTest extends BaseComponentTestCase
028    {
029        protected static final String JAVASCRIPT_NOCOMMENT = 
030            "<script type=\"text/javascript\">"
031            + "if (document.updateObject) {"
032            + " document.updateObject.progressFinished('updateId');"
033            + "}"
034            + "</script>";
035        
036        protected static final String MULTI_JAVASCRIPT_NOCOMMENT = 
037            "<script type=\"text/javascript\">"
038            + "if (document.updateObject) {"
039            + " document.updateObject.progressFinished('updateId');"
040            + "}"
041            + "if (document.updateObject) {"
042            + " document.updateObject.progressFinished('updateId');"
043            + "}"
044            + "</script>"
045            + JAVASCRIPT_NOCOMMENT
046            + JAVASCRIPT_NOCOMMENT;
047        
048        protected static final String TEST_INPUT1 =
049            JAVASCRIPT_NOCOMMENT 
050            + "some text is here";
051        
052        protected static final String TEST_INPUT2 =
053            "beginning text"
054            + JAVASCRIPT_NOCOMMENT 
055            + "some text is here";
056        
057        protected static final String TEST_INPUT3 =
058            JAVASCRIPT_NOCOMMENT 
059            + "Here yee"
060            + JAVASCRIPT_NOCOMMENT
061            + "some text is here";
062        
063        protected static final String TEST_INPUT4 =
064            TEST_INPUT3
065            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
066            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
067            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
068            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
069            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
070            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
071            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
072            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
073            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
074            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
075            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
076            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
077            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
078            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
079            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
080            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
081            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
082            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
083            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
084            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
085            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
086            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
087            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
088            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
089            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
090            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
091            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
092            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
093            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
094            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
095            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
096            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
097            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
098            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3
099            + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3 + TEST_INPUT3;
100        
101        protected static final String TEST_INPUT_INCLUDE =
102            JAVASCRIPT_NOCOMMENT 
103            + "some text is here"
104            + "<script type=\"text/javascript\" src=\"http://yourmom.js\"></script>";
105        
106        public static final String BEGIN_COMMENT = "//<![CDATA[";
107        public static final String END_COMMENT = "//]]>";
108        
109        /**
110         * Tests finding {@link #JAVASCRIPT_NOCOMMENT} with 
111         * regular expressions.
112         */
113        public void test_Find_Script()
114        {
115            Perl5Util util = new Perl5Util();
116            String expr = "/(?:<script.*?>)((\\n|.)*?)(?:<\\/script>)/";
117            
118            assertTrue(util.match(expr, JAVASCRIPT_NOCOMMENT));
119            
120            MatchResult result = util.getMatch();
121            assertNotNull(result);
122            assertEquals(3, result.groups());
123            assertEquals("if (document.updateObject) { document.updateObject.progressFinished('updateId');}",
124                    result.group(1));
125        }
126        
127        /**
128         * Tests finding {@link #JAVASCRIPT_NOCOMMENT} with 
129         * regular expressions.
130         */
131        public void test_Find_Multiple_Scripts()
132        {
133            Perl5Util util = new Perl5Util();
134            String expr = "/(?:<script.*?>)((\\n|.)*?)(?:<\\/script>)/";
135            
136            assertTrue(util.match(expr, MULTI_JAVASCRIPT_NOCOMMENT));
137            
138            MatchResult result = util.getMatch();
139            assertNotNull(result);
140            assertEquals(3, result.groups());
141            assertEquals("if (document.updateObject) { document.updateObject.progressFinished('updateId');}"
142                    + "if (document.updateObject) { document.updateObject.progressFinished('updateId');}",
143                    result.group(1));
144        }
145        
146        /**
147         * Calls {@link ScriptUtils#ensureValidScriptTags(String)} with 
148         * {@link #JAVASCRIPT_NOCOMMENT} and tests that it returns a validly
149         * marked up content block capable of being embedded in an xml document.
150         */
151        public void test_Ensure_Valid_Script_Tags()
152        {
153            assertEquals(ScriptUtils.ensureValidScriptTags(JAVASCRIPT_NOCOMMENT),
154                    ScriptUtils.BEGIN_COMMENT
155                    + "if (document.updateObject) { document.updateObject.progressFinished('updateId');}"
156                    + ScriptUtils.END_COMMENT);
157            
158            //Test other various non jscript text
159            assertNull(null, ScriptUtils.ensureValidScriptTags(null));
160            assertEquals("", ScriptUtils.ensureValidScriptTags(""));
161            assertEquals(ScriptUtils.ensureValidScriptTags("<html>This is html</html>"),
162                    "<html>This is html</html>");
163        }
164    
165        public void test_Ensure_Valid_Script_Tags_With_Html_Comments()
166        {  
167            String data = "<!-- some comments1 -->" + TEST_INPUT1 + "<b>test</b><!-- some comments2 -->";
168            data += " <!-- some comments3 -->" + TEST_INPUT1 + "<b>test</b><!-- some comments4 -->";
169            
170            String result = ScriptUtils.ensureValidScriptTags(data);
171            
172            assertTrue(result.indexOf("<!-- some comments1 -->") >= 0);
173            assertTrue(result.indexOf("<!-- some comments2 -->") >= 0);
174            assertTrue(result.indexOf("<!-- some comments3 -->") >= 0);
175            assertTrue(result.indexOf("<!-- some comments4 -->") >= 0);
176        }
177        
178        /**
179         * Tests that the complete string is returned, with 
180         * any js in it "fixed".
181         */
182        public void test_Complete_Return()
183        {
184            assertEquals(ScriptUtils.ensureValidScriptTags(TEST_INPUT1),
185                    ScriptUtils.BEGIN_COMMENT
186                    + "if (document.updateObject) { document.updateObject.progressFinished('updateId');}"
187                    + ScriptUtils.END_COMMENT + "some text is here");
188    
189            assertEquals(ScriptUtils.ensureValidScriptTags(TEST_INPUT2),
190                    "beginning text" 
191                    + ScriptUtils.BEGIN_COMMENT
192                    + "if (document.updateObject) { document.updateObject.progressFinished('updateId');}"
193                    + ScriptUtils.END_COMMENT + "some text is here");
194            
195            assertEquals(ScriptUtils.ensureValidScriptTags(TEST_INPUT3),
196                    ScriptUtils.BEGIN_COMMENT
197                    + "if (document.updateObject) { document.updateObject.progressFinished('updateId');}"
198                    + ScriptUtils.END_COMMENT
199                    + "Here yee" 
200                    +  ScriptUtils.BEGIN_COMMENT
201                    + "if (document.updateObject) { document.updateObject.progressFinished('updateId');}"
202                    + ScriptUtils.END_COMMENT
203                    + "some text is here");
204        }
205    }