Code Search for Developers
 
 
  

HtmlUnitSessionTagTest.java from Jameleon at Krugle


Show HtmlUnitSessionTagTest.java syntax highlighted

/*
    Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
    Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
*/
package net.sf.jameleon.plugin.htmlunit;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sf.jameleon.MockTestCaseTag;
import net.sf.jameleon.plugin.htmlunit.util.MockHtmlUnitHelper;
import net.sf.jameleon.plugin.htmlunit.util.TrustEverythingSSLProtocolSocketFactory;
import net.sf.jameleon.util.Configurator;
import net.sf.jameleon.util.StateStorer;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.MissingAttributeException;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlUnitSessionTagTest extends TestCase {

    private HtmlUnitSessionTag sessionTag;
    private JellyContext context;

    public HtmlUnitSessionTagTest( String name ) {
        super( name );
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run( suite() );
    }

    public static Test suite() {
        return new TestSuite( HtmlUnitSessionTagTest.class );
    }

    public void setUp() throws JellyTagException {
        sessionTag = new HtmlUnitSessionTag();
        sessionTag.setParent(new MockTestCaseTag());
        context = new JellyContext();
        sessionTag.setContext(context);
        Configurator.getInstance().setConfigName("SomeNonExsistentfile");
        sessionTag.getAttributeBroker().transferAttributes(context);
    }

    public void tearDown(){
        Configurator.clearInstance();
    }

    public void testGetEnableSslCertCheckPort(){
        assertEquals("port default", 443, sessionTag.getEnableSslCertCheckPort());
        int port = 555;
        Configurator.getInstance().setValue("htmlUnitEnableSslCertCheckPort", ""+port);
        assertEquals("port", port, sessionTag.getEnableSslCertCheckPort());

        port = 443;
        sessionTag.getContext().setVariable("htmlUnitEnableSslCertCheckPort", ""+port);
        assertEquals("port", port, sessionTag.getEnableSslCertCheckPort());
    }

    public void testGetSession(){
        assertNull("sessionTag's session should be null", sessionTag.session);
        WebClient session = new WebClient();
        sessionTag.session = session;
        assertEquals("sessionTag's session should be the same", session, sessionTag.getSession());
    }

    public void testGetWebClient(){
        sessionTag.setUpSession();
        WebClient client = sessionTag.getWebClient();
        assertEquals("WebClient", sessionTag.session, client);
    }

    public void testSetUpSession(){
        assertNull("sessionTag's session should be null", sessionTag.session);
        assertNull("sessionTag's HtmlUnitHelper should be null", sessionTag.htmlHelper);
        sessionTag.setUpSession();
        assertNotNull("sessionTag's session should not be null", sessionTag.session);
        assertNotNull("sessionTag's HtmlUnitHelper instance", sessionTag.htmlHelper);
    }

    public void testTearDownSession(){
        sessionTag.setUpSession();
        assertNotNull("sessionTag's session should not be null", sessionTag.session);
        sessionTag.tearDownSession();
        assertNull("sessionTag's session should be null", sessionTag.session);
    }

    public void testGetRequestUrl(){
        sessionTag.baseUrl = "http://jameleon.sf.net";
        sessionTag.beginAt = "/index.html";
        sessionTag.setUpSession();
        String requestUrl = sessionTag.getRequestUrl();
        assertEquals("requestUrl", "http://jameleon.sf.net/index.html", requestUrl);
        sessionTag.beginAt = null;
        sessionTag.setUpSession();
        requestUrl = sessionTag.getRequestUrl();
        assertEquals("requestUrl", "http://jameleon.sf.net", requestUrl);
    }

    public void testStartApplication(){
        sessionTag.baseUrl = "file:";
        sessionTag.beginAt = "./tst/html/sessionTag.html";
        sessionTag.setUpSession();
        sessionTag.startApplication();
        HtmlPage page = (HtmlPage)sessionTag.getSession().getCurrentWindow().getEnclosedPage();
        assertEquals("title", "session page", page.getTitleText());
    }

    public void testStartApplicationStorable() throws JellyTagException {
        MockHtmlUnitSessionTag sessionTag = new MockHtmlUnitSessionTag();
        sessionTag.setParent(new MockTestCaseTag());
        sessionTag.setContext(new JellyContext());

        sessionTag.baseUrl = "file:";
        sessionTag.beginAt = "./tst/html/sessionTag.html";
        sessionTag.setUpSession();
        sessionTag.startApplication();
        assertTrue("registerStorable should have been called.", sessionTag.registerStorableCalled);
        assertTrue("deregisterStorableCalled should have been called", sessionTag.deregisterStorableCalled);
    }

    public void testIsEnableSsslCertCheck(){
        assertTrue("Default of enableSslCertCheck", sessionTag.isEnableSslCertCheck());
        Configurator config = Configurator.getInstance();
        sessionTag.enableSslCertCheck = null;
        config.setValue("htmlUnitEnableSslCertCheck", "true");
        assertTrue("With htmlUnitenableSslCertCheck set to 'true' in jameleon.conf", sessionTag.isEnableSslCertCheck());
        sessionTag.enableSslCertCheck = null;
        config.setValue("htmlUnitEnableSslCertCheck", "false");
        assertFalse("With htmlUnitEnableSslCertCheck set to 'false' in jameleon.conf", sessionTag.isEnableSslCertCheck());
        config.setValue("htmlUnitEnableSslCertCheck", null);

        sessionTag.enableSslCertCheck = new Boolean(true);
        assertTrue("With enableSslCertCheck set to 'true' via xml attribute", sessionTag.isEnableSslCertCheck());
        sessionTag.enableSslCertCheck = new Boolean(false);
        assertFalse("With enableSslCertCheck set to 'false' via xml attribute", sessionTag.isEnableSslCertCheck());
        sessionTag.enableSslCertCheck = null;
        config.setValue("htmlUnitEnableSslCertCheck", "false");
        assertFalse("With enableSslCertCheck set to 'false' via xml attribute and Environment.propertie", sessionTag.isEnableSslCertCheck());
        config.setValue("htmlUnitEnableSslCertCheck", "true");
        assertFalse("With enableSslCertCheck set to 'false' via xml attribute and to 'true' in Environment.propertie", sessionTag.isEnableSslCertCheck());
    }

    public void testSslCertCheckDefaultsToEnabled() {
        assertEquals("enableSslCertCheck", true, sessionTag.isEnableSslCertCheck());
        Protocol p = Protocol.getProtocol("https");
        assertFalse("EasySSLProtocolSocketFactory should not be the factory", p.getSocketFactory() instanceof TrustEverythingSSLProtocolSocketFactory);
    }

    public void testDisableSslCertCheck() throws MissingAttributeException {
        sessionTag.enableSslCertCheck = new Boolean(false);
        sessionTag.setUpSession();
        Protocol p = Protocol.getProtocol("https");
        assertTrue("EasySSLProtocolSocketFactory should be the factory", p.getSocketFactory() instanceof TrustEverythingSSLProtocolSocketFactory);
    }

    public void testRegisterStorable(){
        StateStorer.getInstance().getStorables().clear();
        assertEquals("# of storables registered before call", 0, StateStorer.getInstance().getStorables().size());
        sessionTag.registerStorable();
        assertEquals("# of storables registered after call", 1, StateStorer.getInstance().getStorables().size());
        StateStorer.getInstance().reset();
    }

    public void testDegegisterStorable(){
        assertEquals("# of storables registered before registered", 0, StateStorer.getInstance().getStorables().size());
        sessionTag.registerStorable();
        assertEquals("# of storables registered after registered", 1, StateStorer.getInstance().getStorables().size());
        sessionTag.deregisterStorable();
        assertEquals("# of storables registered after deregisterStorable", 0, StateStorer.getInstance().getStorables().size());
    }

    public void testStore() throws Exception{
        MockHtmlUnitSessionTag sessionTag = new MockHtmlUnitSessionTag();
        sessionTag.setParent(new MockTestCaseTag());
        sessionTag.setContext(new JellyContext());

        sessionTag.baseUrl = "file:";
        sessionTag.beginAt = "./tst/html/sessionTag.html";
        sessionTag.setUpSession();
        sessionTag.startApplication();

        MockHtmlUnitHelper helper = new MockHtmlUnitHelper(sessionTag);
        sessionTag.htmlHelper = helper;

        sessionTag.register();
        StateStorer.getInstance().eventOccured(StateStorer.ON_ERROR_EVENT);
        sessionTag.deregister();

        StateStorer.getInstance().eventOccured(StateStorer.ON_ERROR_EVENT);
        assertNotNull("pathToFile", sessionTag.pathToFile);
        assertNotNull("pathToFile", helper.storeFileName);
        assertEquals("paths", sessionTag.pathToFile, helper.storeFileName);
    }

}





See more files for this project here

Jameleon

Jameleon is a data-driven automated testing tool that is easily extensible via plug-ins. Features of applications are automated in Java and tied together independently in XML, creating self-documenting automated test cases.

Project homepage: http://sourceforge.net/projects/jameleon
Programming language(s): Java,XML
License: other

  acceptance/
    FormTestTag.java
  util/
    HtmlUnitHelperTest.java
    MockHtmlUnitHelper.java
  HtmlUnitFunctionTagTest.java
  HtmlUnitSessionTagTest.java
  MockHtmlUnitFunctionTag.java
  MockHtmlUnitSessionTag.java