You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2010/04/04 15:08:02 UTC

svn commit: r930684 - in /myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src: main/java/org/apache/myfaces/scripting/components/ test/java/org/apache/myfaces/scripting/components/

Author: werpu
Date: Sun Apr  4 13:08:02 2010
New Revision: 930684

URL: http://svn.apache.org/viewvc?rev=930684&view=rev
Log:
http://issues.apache.org/jira/browse/EXTSCRIPT-109

adding 100% coverage for the components

Added:
    myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/CompilerComponentTest.java   (with props)
    myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryTest.java   (with props)
Removed:
    myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryRendererTest.java
Modified:
    myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/components/TaintHistory.java

Modified: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/components/TaintHistory.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/components/TaintHistory.java?rev=930684&r1=930683&r2=930684&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/components/TaintHistory.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/components/TaintHistory.java Sun Apr  4 13:08:02 2010
@@ -33,7 +33,7 @@ import javax.faces.context.FacesContext;
 
 public class TaintHistory extends UIOutput {
 
-    static final int DEFAULT_NO_ENTRIES = 10;
+    public static final int DEFAULT_NO_ENTRIES = 10;
 
     Integer _noEntries;
     String _filter;

Added: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/CompilerComponentTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/CompilerComponentTest.java?rev=930684&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/CompilerComponentTest.java (added)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/CompilerComponentTest.java Sun Apr  4 13:08:02 2010
@@ -0,0 +1,235 @@
+/*
+ * 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.myfaces.scripting.components;
+
+import org.apache.myfaces.renderkit.html.HtmlFormRenderer;
+import org.apache.myfaces.scripting.api.CompilationResult;
+import org.apache.myfaces.scripting.api.ScriptingConst;
+import org.apache.myfaces.scripting.core.support.MockServletContext;
+import org.apache.myfaces.scripting.core.util.WeavingContext;
+import org.apache.myfaces.scripting.core.util.WeavingContextInitializer;
+import org.apache.myfaces.test.base.AbstractJsfTestCase;
+import org.apache.myfaces.test.mock.MockRenderKitFactory;
+import org.apache.myfaces.test.mock.MockResponseWriter;
+
+import javax.faces.component.html.HtmlForm;
+import javax.servlet.ServletContext;
+import java.io.StringWriter;
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+
+public class CompilerComponentTest extends AbstractJsfTestCase {
+
+    ServletContext context;
+    private CompilerComponent _compilerComponent;
+    private MockResponseWriter _writer;
+    private static final String ERROR_LABEL = "Error:";
+    private static final String JAVA = "java";
+    private static final String WARNINGS_LABEL = "Warnings:";
+    private static final String GROOVY = "groovy";
+    private static final String BLANK = "";
+    private static final String ERROR_1 = "1_error";
+    private static final String ERROR_2 = "2_error";
+    private static final String WARNING_1 = "1_warning";
+    private static final String WARNING_2 = "2_warning";
+
+    public CompilerComponentTest() {
+        super(CompilerComponentTest.class.getName());
+    }
+
+    public void setUp() throws Exception {
+        super.setUp();
+        context = new MockServletContext();
+        WeavingContextInitializer.initWeavingContext(context);
+
+        _writer = new MockResponseWriter(new StringWriter(), null, null);
+
+        facesContext.setResponseWriter(_writer);
+        _compilerComponent = new CompilerComponent();
+        HtmlForm form = new HtmlForm();
+        _compilerComponent.setParent(form);
+
+        facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
+        facesContext.getRenderKit().addRenderer(
+                _compilerComponent.getFamily(),
+                _compilerComponent.getRendererType(),
+                new CompilerComponentRenderer());
+        facesContext.getRenderKit().addRenderer(
+                form.getFamily(),
+                form.getRendererType(),
+                new HtmlFormRenderer());
+
+        _compilerComponent.setErrorsLabel(ERROR_LABEL);
+        _compilerComponent.setScriptingLanguage(JAVA);
+        _compilerComponent.setWarningsLabel(WARNINGS_LABEL);
+
+    }
+
+    public void testIsTransient() throws Exception {
+        assertTrue(_compilerComponent.isTransient());
+    }
+
+    public void testSaveRestoreState() throws Exception {
+        Object state = _compilerComponent.saveState(facesContext);
+        _compilerComponent.setErrorsLabel(BLANK);
+        _compilerComponent.setScriptingLanguage(BLANK);
+        _compilerComponent.setWarningsLabel(BLANK);
+        _compilerComponent.restoreState(facesContext, state);
+
+        assertDefaultTestingValues();
+    }
+
+    private void assertDefaultTestingValues() {
+        assertTrue(_compilerComponent.getErrorsLabel().equals(ERROR_LABEL));
+        assertTrue(_compilerComponent.getScriptingLanguage().equals(JAVA));
+        assertTrue(_compilerComponent.getWarningsLabel().equals(WARNINGS_LABEL));
+        assertTrue(_compilerComponent.getScriptingLanguageAsInt().equals(ScriptingConst.ENGINE_TYPE_JSF_JAVA));
+    }
+
+    public void testScriptingLanguageAsInt() {
+        assertTrue(_compilerComponent.getScriptingLanguageAsInt().equals(ScriptingConst.ENGINE_TYPE_JSF_JAVA));
+        _compilerComponent.setScriptingLanguage(GROOVY);
+        assertTrue(_compilerComponent.getScriptingLanguageAsInt().equals(ScriptingConst.ENGINE_TYPE_JSF_GROOVY));
+        _compilerComponent.setScriptingLanguage(BLANK);
+        assertTrue(_compilerComponent.getScriptingLanguageAsInt().equals(ScriptingConst.ENGINE_TYPE_JSF_ALL));
+        _compilerComponent.setScriptingLanguage("booga");
+        assertTrue(_compilerComponent.getScriptingLanguageAsInt().equals(ScriptingConst.ENGINE_TYPE_JSF_NO_ENGINE));
+    }
+
+    public void testElPart() {
+        _compilerComponent.setWarningsLabel(null);
+        _compilerComponent.setErrorsLabel(null);
+        _compilerComponent.setScriptingLanguage(null);
+
+        _compilerComponent.getAttributes().put("warningsLabel", WARNINGS_LABEL);
+        _compilerComponent.getAttributes().put("errorsLabel", ERROR_LABEL);
+        _compilerComponent.getAttributes().put("scriptingLanguage", JAVA);
+
+        assertDefaultTestingValues();
+    }
+
+    public void testRendererNoErrorsAndWarnings() throws Exception {
+        _compilerComponent.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertTrue("no compile errors found", _writer.getWriter().toString().contains(RendererConst.NO_COMPILE_ERRORS));
+    }
+
+    public void testRendererNoErrorsAndWarnings2() throws Exception {
+        _compilerComponent.setScriptingLanguage(GROOVY);
+        _compilerComponent.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertTrue("no compile errors found", _writer.getWriter().toString().contains(RendererConst.NO_COMPILE_ERRORS));
+    }
+
+    public void testRendererNoErrorsAndWarnings3() throws Exception {
+        _compilerComponent.setScriptingLanguage(BLANK);
+        _compilerComponent.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertTrue("no compile errors found", _writer.getWriter().toString().contains(RendererConst.NO_COMPILE_ERRORS));
+    }
+
+    public void testRendererNoErrorsAndWarnings4() throws Exception {
+        _compilerComponent.setScriptingLanguage("booga");
+        _compilerComponent.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertTrue("no compile errors found", _writer.getWriter().toString().contains(RendererConst.NO_COMPILE_ERRORS));
+    }
+
+    public void testCompilationResultJava() throws Exception {
+        CompilationResult result = getCompilationResult(JAVA);
+
+        WeavingContext.setCompilationResult(ScriptingConst.ENGINE_TYPE_JSF_JAVA, result);
+
+        _compilerComponent.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertStandardResponse(JAVA);
+
+    }
+
+    public void testEmptyLabels() throws Exception {
+        _compilerComponent.setWarningsLabel("");
+        _compilerComponent.setErrorsLabel("");
+        CompilationResult result = getCompilationResult(JAVA);
+
+        WeavingContext.setCompilationResult(ScriptingConst.ENGINE_TYPE_JSF_JAVA, result);
+
+        _compilerComponent.encodeAll(facesContext);
+        facesContext.renderResponse();
+        String response = _writer.getWriter().toString();
+
+        assertFalse(response.contains(WARNINGS_LABEL));
+        assertFalse(response.contains(ERROR_LABEL));
+
+    }
+
+    private void assertStandardResponse(String prefix) {
+        assertFalse("Compile errors found", _writer.getWriter().toString().contains(RendererConst.NO_COMPILE_ERRORS));
+        assertTrue("Compile errors found", _writer.getWriter().toString().contains(ERROR_LABEL));
+
+        String response = _writer.getWriter().toString();
+        assertTrue(response.contains(WARNINGS_LABEL));
+        assertTrue(response.contains(prefix + ERROR_1));
+        assertTrue(response.contains(prefix + ERROR_2));
+        assertTrue(response.contains(prefix + WARNING_1));
+        assertTrue(response.contains(prefix + WARNING_2));
+    }
+
+    public void testCompilationResultGroovy() throws Exception {
+        CompilationResult result = getCompilationResult(GROOVY);
+
+        WeavingContext.setCompilationResult(ScriptingConst.ENGINE_TYPE_JSF_JAVA, result);
+
+        _compilerComponent.encodeAll(facesContext);
+        _compilerComponent.setScriptingLanguage(GROOVY);
+        facesContext.renderResponse();
+        assertStandardResponse(GROOVY);
+
+    }
+
+    public void testCompilationResultAll() throws Exception {
+        CompilationResult result = getCompilationResult(JAVA);
+
+        WeavingContext.setCompilationResult(ScriptingConst.ENGINE_TYPE_JSF_JAVA, result);
+        result = getCompilationResult(GROOVY);
+        WeavingContext.setCompilationResult(ScriptingConst.ENGINE_TYPE_JSF_GROOVY, result);
+
+        _compilerComponent.setScriptingLanguage("");
+        _compilerComponent.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertStandardResponse(JAVA);
+        assertStandardResponse(GROOVY);
+
+    }
+
+    private CompilationResult getCompilationResult(String prefix) {
+        CompilationResult result = new CompilationResult("output");
+
+        result.getErrors().add(new CompilationResult.CompilationMessage(1, prefix + ERROR_1));
+        result.getErrors().add(new CompilationResult.CompilationMessage(2, prefix + ERROR_2));
+
+        result.getWarnings().add(new CompilationResult.CompilationMessage(1, prefix + WARNING_1));
+        result.getWarnings().add(new CompilationResult.CompilationMessage(2, prefix + WARNING_2));
+        return result;
+    }
+
+}

Propchange: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/CompilerComponentTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/CompilerComponentTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryTest.java?rev=930684&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryTest.java (added)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryTest.java Sun Apr  4 13:08:02 2010
@@ -0,0 +1,158 @@
+/*
+ * 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.myfaces.scripting.components;
+
+import org.apache.myfaces.renderkit.html.HtmlFormRenderer;
+import org.apache.myfaces.scripting.api.ScriptingConst;
+import org.apache.myfaces.scripting.core.support.MockServletContext;
+import org.apache.myfaces.scripting.core.util.WeavingContext;
+import org.apache.myfaces.scripting.core.util.WeavingContextInitializer;
+import org.apache.myfaces.scripting.refresh.ReloadingMetadata;
+import org.apache.myfaces.test.base.AbstractJsfTestCase;
+import org.apache.myfaces.test.mock.MockRenderKitFactory;
+import org.apache.myfaces.test.mock.MockResponseWriter;
+
+import javax.faces.component.html.HtmlForm;
+import javax.servlet.ServletContext;
+import java.io.StringWriter;
+
+/**
+ * Test cases for the taint history component and renderer
+ * (note the filter attribute currently is not yet
+ * tested for semantic usage because we dont have it implemented yet)
+ * 
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+
+public class TaintHistoryTest extends AbstractJsfTestCase {
+
+    ServletContext context;
+    private TaintHistory _taintHistory;
+    private TaintHistoryRenderer _taintHistoryRenderer;
+    private HtmlForm _form;
+    private MockResponseWriter _writer;
+    private static final String VAL_FILTER = "bla";
+
+    public TaintHistoryTest() {
+        super(TaintHistoryTest.class.getName());
+    }
+
+    public void setUp() throws Exception {
+        super.setUp();
+        context = new MockServletContext();
+        WeavingContextInitializer.initWeavingContext(context);
+
+        _writer = new MockResponseWriter(new StringWriter(), null, null);
+
+        facesContext.setResponseWriter(_writer);
+        _taintHistory = new TaintHistory();
+        _form = new HtmlForm();
+        _taintHistory.setParent(_form);
+
+        facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
+        facesContext.getRenderKit().addRenderer(
+                _taintHistory.getFamily(),
+                _taintHistory.getRendererType(),
+                new TaintHistoryRenderer());
+        facesContext.getRenderKit().addRenderer(
+                _form.getFamily(),
+                _form.getRendererType(),
+                new HtmlFormRenderer());
+
+    }
+
+    public void testNoTaintHistory() throws Exception {
+        _taintHistory.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertTrue("no taint history found", _writer.getWriter().toString().contains(RendererConst.NO_TAINT_HISTORY_FOUND));
+    }
+
+    public void testTaintHistory() throws Exception {
+        ReloadingMetadata historyEntry = new ReloadingMetadata();
+        historyEntry.setAClass(this.getClass());
+        historyEntry.setTimestamp(System.currentTimeMillis());
+        historyEntry.setScriptingEngine(ScriptingConst.ENGINE_TYPE_JSF_JAVA);
+        historyEntry.setFileName("booga.java");
+        historyEntry.setTainted(true);
+        historyEntry.setTaintedOnce(true);
+        WeavingContext.getRefreshContext().addTaintLogEntry(historyEntry);
+
+        _taintHistory.encodeAll(facesContext);
+        facesContext.renderResponse();
+        assertFalse("taint history found", _writer.getWriter().toString().contains(RendererConst.NO_TAINT_HISTORY_FOUND));
+
+        assertTrue(_writer.getWriter().toString().contains("booga.java"));
+    }
+
+    public void testSaveRestore() {
+        _taintHistory.setFilter(VAL_FILTER);
+        _taintHistory.setNoEntries(10);
+        Object state = _taintHistory.saveState(facesContext);
+        _taintHistory.setFilter("");
+        _taintHistory.setNoEntries(0);
+        _taintHistory.restoreState(facesContext, state);
+
+        assertTrue(_taintHistory.getFilter().equals(VAL_FILTER));
+
+        assertTrue(_taintHistory.getNoEntries().equals(10));
+
+    }
+
+    public void testNoEntries() throws Exception {
+        int noEntries = 10;
+        for (int cnt = 0; cnt < 100; cnt++) {
+            ReloadingMetadata historyEntry = new ReloadingMetadata();
+            historyEntry.setAClass(this.getClass());
+            historyEntry.setTimestamp(System.currentTimeMillis());
+            historyEntry.setScriptingEngine(ScriptingConst.ENGINE_TYPE_JSF_JAVA);
+            if(cnt < 10)
+                historyEntry.setFileName("0"+cnt + "_booga.java");
+            else
+                historyEntry.setFileName(cnt + "_booga.java");
+            historyEntry.setTainted(true);
+            historyEntry.setTaintedOnce(true);
+            WeavingContext.getRefreshContext().addTaintLogEntry(historyEntry);
+        }
+
+        _taintHistory.setNoEntries(10);
+        _taintHistory.encodeAll(facesContext);
+        facesContext.renderResponse();
+
+        assertTrue(_writer.getWriter().toString().contains("99_booga.java"));
+        assertFalse(_writer.getWriter().toString().contains("89_booga.java"));
+        assertFalse(_writer.getWriter().toString().contains("00_booga.java"));
+    }
+
+    public void testElAttributes() {
+       assertTrue(_taintHistory.getFilter() == null);
+       assertTrue(_taintHistory.getNoEntries().equals(TaintHistory.DEFAULT_NO_ENTRIES));
+       _taintHistory.setNoEntries(null); 
+
+       _taintHistory.getAttributes().put("noEntries", 20);
+       _taintHistory.getAttributes().put("filter", VAL_FILTER);
+
+       assertTrue(_taintHistory.getNoEntries() == 20);
+       assertTrue(_taintHistory.getFilter().equals(VAL_FILTER));
+    }
+
+}
+

Propchange: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/components/TaintHistoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL