EditorActionTest.java :  » Content-Management-System » TransferCM » com » methodhead » shim » Java Open Source

Java Open Source » Content Management System » TransferCM 
TransferCM » com » methodhead » shim » EditorActionTest.java
/* 
 * Copyright (C) 2006 Methodhead Software LLC.  All rights reserved.
 * 
 * This file is part of TransferCM.
 * 
 * TransferCM is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * TransferCM 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 General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA  02110-1301  USA
 */

package com.methodhead.shim;

import java.util.*;
import java.sql.*;
import java.io.*;
import junit.framework.*;
import org.apache.log4j.*;
import com.methodhead.persistable.*;
import com.methodhead.test.*;
import servletunit.struts.*;
import org.apache.struts.action.*;
import org.apache.cactus.*;
import com.methodhead.auth.*;
import com.methodhead.sitecontext.*;
import org.apache.struts.util.*;
import com.methodhead.tree.*;
import com.methodhead.*;

public class EditorActionTest extends CactusStrutsTestCase {

  Thumbnail thumb = null;
  FileInfo info = null;
  List list = null;
  DynaActionForm dynaForm = null;
  LabelValueBean option = null;
  FoldingTreeNode node = null;
  FoldingTreeNode node2 = null;

  private Link link1_ = null;
  private Link link2_ = null;
  private Link link3_ = null;
  private Link link4_ = null;
  private Link link5_ = null;

  private SiteMap siteMap1_ = null;

  private SiteMapTree siteMapTree1_ = null;

  private TestSite site = null;

  private void createData() {
    site = new TestSite();
    site.createPages();
    site.createLinks();
    site.createSiteMap();
    site.createSiteMapTree();

    // Node1
    //   Node2
    //     Node3
    //   Node4
    //   Node5
/*
    link1_ = new Link();
    link1_.setPageId( 1 );
    link1_.setTitle( "Page1" );
    link2_ = new Link();
    link2_.setPageId( 2 );
    link2_.setTitle( "Page2" );
    link3_ = new Link();
    link3_.setPageId( 3 );
    link3_.setTitle( "Page3" );
    link4_ = new Link();
    link4_.setPageId( 4 );
    link4_.setTitle( "Page4" );
    link5_ = new Link();
    link5_.setPageId( 5 );
    link5_.setTitle( "Page5" );

    link1_.add( link2_ );
    link2_.add( link3_ );
    link1_.add( link4_ );
    link1_.add( link5_ );

    siteMap1_ = new SiteMap();
    siteMap1_.setRoot( link1_ );
    siteMap1_.setSiteContext( SiteContext.getDefaultContext() );
    siteMap1_.save();

    siteMapTree1_ = new SiteMapTree();
    siteMapTree1_.build( siteMap1_ );

    node = siteMapTree1_.find( 1 );
    node.setOpened( true );
    node = siteMapTree1_.find( 2 );
    node.setOpened( false );
    node = siteMapTree1_.find( 3 );
    node.setOpened( false );
    node = siteMapTree1_.find( 4 );
    node.setOpened( false );
    node = siteMapTree1_.find( 5 );
    node.setOpened( false );
*/
    node = site.siteMapTree_.find( 1 );
    node.setOpened( true );

    session.getServletContext().removeAttribute( ShimGlobals.SITEMAPMAP_KEY );
    session.removeAttribute( ShimGlobals.SITEMAPTREE_KEY );
    session.removeAttribute( ShimGlobals.MODE_KEY );

    request.getSession().setAttribute( ShimGlobals.SITEMAPTREE_KEY, site.siteMapTree_ );
    SiteContext.setContext( request, SiteContext.getDefaultContext() );
  }

  static {
    TestUtils.initLogger();
    TestUtils.initDb();
  }

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

  public void setUp() {
    //setLogLevel( Level.DEBUG );
    try {
      super.setUp();
      ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );

      TestData.createUsers();
      AuthUtil.setUser( request, TestData.user1 );
      SiteContext.setContext( request, SiteContext.getDefaultContext() );
    }
    catch ( Exception e ) {
      fail( e.getMessage() );
    }
  }

  public void tearDown() 
  throws
    Exception {
    super.tearDown();
  }

  public void testListImagesBlankPath() {
    //
    // no or blank path
    //
    setRequestPathInfo( "/listImages" );
    addRequestParameter( "path", "" );
    actionPerform();

    verifyForward( "form" );

    dynaForm = ( DynaActionForm )getActionForm();

    list = ( List )dynaForm.get( "images" );
    assertNotNull( list );
    assertEquals( 0, list.size() );
  }

  public void testListImagesInvalidPath() {
    //
    // invalid path
    //
    setRequestPathInfo( "/listImages" );
    addRequestParameter( "path", "../etc" );
    actionPerform();

    verifyForward( "form" );

    dynaForm = ( DynaActionForm )getActionForm();

    list = ( List )dynaForm.get( "images" );
    assertNotNull( list );
    assertEquals( 0, list.size() );
  }

  public void testListImagesImagesDirectory() {
    //
    // images directory
    //
    setRequestPathInfo( "/listImages" );
    addRequestParameter( "path", "images" );
    actionPerform();

    verifyForward( "form" );

    dynaForm = ( DynaActionForm )getActionForm();

    list = ( List )dynaForm.get( "images" );
    assertNotNull( list );
    assertEquals( 5, list.size() );

    thumb = ( Thumbnail )list.get( 0 );
    assertEquals( "badimage.jpg", thumb.getName() );
    assertNull( thumb.getThumbPath() );
    assertEquals( "images/badimage.jpg", thumb.getImagePath() );
    assertEquals( 21, thumb.getSize() );

    thumb = ( Thumbnail )list.get( 1 );
    assertEquals( "test200x300.jpg", thumb.getName() );
    assertEquals( "thumbs/images/test200x300.jpg.jpg", thumb.getThumbPath() );
    assertEquals( "images/test200x300.jpg", thumb.getImagePath() );
    assertEquals( 4306, thumb.getSize() );

    thumb = ( Thumbnail )list.get( 2 );
    assertEquals( "test300x200.gif", thumb.getName() );
    assertEquals( "thumbs/images/test300x200.gif.jpg", thumb.getThumbPath() );
    assertEquals( "images/test300x200.gif", thumb.getImagePath() );
    assertEquals( 1637, thumb.getSize() );

    thumb = ( Thumbnail )list.get( 3 );
    assertEquals( "test300x200.jpg", thumb.getName() );
    assertEquals( "thumbs/images/test300x200.jpg.jpg", thumb.getThumbPath() );
    assertEquals( "images/test300x200.jpg", thumb.getImagePath() );
    assertEquals( 4443, thumb.getSize() );

    thumb = ( Thumbnail )list.get( 4 );
    assertEquals( "test300x200.png", thumb.getName() );
    assertEquals( "thumbs/images/test300x200.png.jpg", thumb.getThumbPath() );
    assertEquals( "images/test300x200.png", thumb.getImagePath() );
    assertEquals( 3489, thumb.getSize() );
  }

  public void testPickImage() {
    try {
      setRequestPathInfo( "/pickImage" );
      actionPerform();

      verifyForward( "form" );

      dynaForm = ( DynaActionForm )getActionForm();

      assertEquals( "", dynaForm.get( "path" ) );

      list = ( List )dynaForm.get( "paths" );
      assertNotNull( list );
      assertEquals( 3, list.size() );

      option = ( LabelValueBean )list.get( 0 );
      assertEquals( "/", option.getLabel() );
      assertEquals( "", option.getValue() );

      option = ( LabelValueBean )list.get( 1 );
      assertEquals( "/images", option.getLabel() );
      assertEquals( "images", option.getValue() );

      option = ( LabelValueBean )list.get( 2 );
      assertEquals( "/images/moreimages", option.getLabel() );
      assertEquals( "images/moreimages", option.getValue() );
    }
    catch ( Exception e ) {
      e.printStackTrace();
      fail();
    }
  }

  public void testDisplayFiles() {
    try {
      //
      // no or blank path
      //
      setRequestPathInfo( "/displayFiles" );
      addRequestParameter( "path", "" );
      actionPerform();

      verifyForward( "form" );

      dynaForm = ( DynaActionForm )getActionForm();

      list = ( List )dynaForm.get( "files" );
      assertNotNull( list );
      assertEquals( 0, list.size() );

      //
      // invalid path
      //
      setRequestPathInfo( "/displayFiles" );
      addRequestParameter( "path", "../etc" );
      actionPerform();

      verifyForward( "form" );

      dynaForm = ( DynaActionForm )getActionForm();

      list = ( List )dynaForm.get( "files" );
      assertNotNull( list );
      assertEquals( 0, list.size() );

      //
      // images directory
      //
      setRequestPathInfo( "/displayFiles" );
      addRequestParameter( "path", "images" );
      actionPerform();

      verifyForward( "form" );

      dynaForm = ( DynaActionForm )getActionForm();

      list = ( List )dynaForm.get( "files" );
      assertNotNull( list );
      assertEquals( 5, list.size() );

      info = ( FileInfo )list.get( 0 );
      assertEquals( "badimage.jpg", info.getName() );
      assertEquals( "images/badimage.jpg", info.getPath() );
      assertEquals( "IMG", info.getIconHint() );
      assertEquals( 21, info.getSize() );

      info = ( FileInfo )list.get( 1 );
      assertEquals( "test200x300.jpg", info.getName() );
      assertEquals( "images/test200x300.jpg", info.getPath() );
      assertEquals( "IMG", info.getIconHint() );
      assertEquals( 4306, info.getSize() );

      info = ( FileInfo )list.get( 2 );
      assertEquals( "test300x200.gif", info.getName() );
      assertEquals( "images/test300x200.gif", info.getPath() );
      assertEquals( "IMG", info.getIconHint() );
      assertEquals( 1637, info.getSize() );

      info = ( FileInfo )list.get( 3 );
      assertEquals( "test300x200.jpg", info.getName() );
      assertEquals( "images/test300x200.jpg", info.getPath() );
      assertEquals( "IMG", info.getIconHint() );
      assertEquals( 4443, info.getSize() );

      info = ( FileInfo )list.get( 4 );
      assertEquals( "test300x200.png", info.getName() );
      assertEquals( "images/test300x200.png", info.getPath() );
      assertEquals( "IMG", info.getIconHint() );
      assertEquals( 3489, info.getSize() );
    }
    catch ( Exception e ) {
      e.printStackTrace();
      fail();
    }
  }

  public void testPickFile() {
    try {
      setRequestPathInfo( "/pickFile" );
      actionPerform();

      verifyForward( "form" );

      dynaForm = ( DynaActionForm )getActionForm();

      assertEquals( "", dynaForm.get( "path" ) );
    }
    catch ( Exception e ) {
      e.printStackTrace();
      fail();
    }
  }

  public void testListPages() {
    try {
      createData();
      setRequestPathInfo( "/listPages" );
      actionPerform();

      verifyForward( "form" );

      dynaForm = ( DynaActionForm )getActionForm();
      node = ( FoldingTreeNode )dynaForm.get( "node" );

      assertNotNull( node );
      assertEquals( "Page1", node.getLabel() );
      assertEquals( "javascript:onPageClick(1,'page1.shtml')", node.getUrl() );
      assertEquals( true, node.getOpened() );
      assertEquals( 3, node.getChildCount() );

      node2 = ( FoldingTreeNode )node.getChildAt( 0 );
      assertNotNull( node2 );
      assertEquals( "Page2", node2.getLabel() );
      assertEquals( "javascript:onPageClick(2,'page2.shtml')", node2.getUrl() );
      assertEquals( false, node2.getOpened() );
      assertEquals( 1, node2.getChildCount() );

      node2 = ( FoldingTreeNode )node.getChildAt( 1 );
      assertNotNull( node2 );
      assertEquals( "Page4", node2.getLabel() );
      assertEquals( "javascript:onPageClick(4,'page4.shtml')", node2.getUrl() );
      assertEquals( false, node2.getOpened() );
      assertEquals( 0, node2.getChildCount() );

      node2 = ( FoldingTreeNode )node.getChildAt( 2 );
      assertNotNull( node2 );
      assertEquals( "Page5", node2.getLabel() );
      assertEquals( "javascript:onPageClick(5,'page5.shtml')", node2.getUrl() );
      assertEquals( false, node2.getOpened() );
      assertEquals( 1, node2.getChildCount() );
    }
    catch ( Exception e ) {
      e.printStackTrace();
      fail();
    }
  }

  public void testPickPage() {
    try {
      createData();
      setRequestPathInfo( "/pickPage" );
      actionPerform();

      verifyForward( "form" );
    }
    catch ( Exception e ) {
      e.printStackTrace();
      fail();
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.