testjetspeedlink.java

来自「jetspeed源代码」· Java 代码 · 共 458 行 · 第 1/2 页

JAVA
458
字号
/*
 * Copyright 2000-2001,2004 The Apache Software Foundation.
 * 
 * Licensed 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.jetspeed.util.template;

// Java imports

// Junit imports
import junit.awtui.TestRunner;
import junit.framework.Test;
import junit.framework.TestSuite;

// Cactus imports
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;

// Jetspeed imports
import org.apache.jetspeed.om.profile.ProfileLocator;
import org.apache.jetspeed.services.Profiler;
import org.apache.jetspeed.services.resources.JetspeedResources;
import org.apache.jetspeed.util.template.JetspeedLink;
import org.apache.jetspeed.util.template.JetspeedLinkFactory;
import org.apache.jetspeed.test.TurbineTestUtilities;

// Turbine imports
import org.apache.turbine.services.pull.TurbinePull;
import org.apache.turbine.services.velocity.TurbineVelocity;
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.RunDataFactory;

// Velocity
import org.apache.velocity.context.Context;
/**
 * TestJespeedLink
 *
 * @author <a href="paulsp@apache.org">Paul Spencer</a>
 * @version $Id: TestJetspeedLink.java,v 1.1 2004/04/07 22:02:42 jford Exp $
 */
public class TestJetspeedLink extends ServletTestCase
{
    private static String TEST_CONTEXT = "/test";
    private static String TEST_HOST = "localhost";
    private static String TEST_SERVLET = "/portal";
    
    /**
     * Defines the testcase name for JUnit.
     *
     * @param name the testcase's name.
     */
    public TestJetspeedLink(String name)
    {
        super( name );
    }
    
    /**
     * Start the tests.
     *
     * @param args the arguments. Not used
     */
    public static void main(String args[])
    {
        TestRunner.main( new String[] { TestJetspeedLink.class.getName() } );
    }
    
    /**
     * Creates the test suite.
     *
     * @return a test suite (<code>TestSuite</code>) that includes all methods
     *         starting with "test"
     */
    public static Test suite()
    {
        // All methods starting with "test" will be executed in the test suite.
        return new TestSuite( TestJetspeedLink.class );
    }
    
    
    public void beginBaseLink(WebRequest theRequest)
    {
        System.out.println("URL = " + theRequest.getURL());
        theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET, "", null);
        System.out.println("post set URL = " + theRequest.getURL());
    }
    
    /**
     * Test the base link
     * @throws Exception
     */
    public void testBaseLink() throws Exception
    {
        ProfileLocator profileLocator = null;

        // Create the RunData object to be used during testing.
        RunData rundata = RunDataFactory.getRunData( request, response, config );
        assertNotNull( "Got rundata", rundata);
        TurbineTestUtilities.setupRunData(rundata);
        
        // Get and populate the context
        Context context = TurbineVelocity.getContext(rundata);
        assertNotNull( "Got context", context);
        TurbinePull.populateContext( context, rundata);
        
        // Verify tool are in the context
        JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
        assertNotNull( "Got jslink from context", jsLink);
                
        // Generate the link
        String link = jsLink.toString();
        System.out.println("Base link = " + link);
        assertTrue( "Verifing base Link", 
          link.startsWith("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET));

        // Generatate and output thre page
        TurbineTestUtilities.generatePage(rundata);
        TurbineTestUtilities.outputPage(rundata);
        
        // Return the used RunData to the factory for recycling.
        RunDataFactory.putRunData(rundata);
    }
    
    public void endBaseLink(org.apache.cactus.WebResponse theResponse)
    {
        System.out.println("text length = " + theResponse.getText().length());
    }
    
    public void beginGroupLink(WebRequest theRequest)
    {
        theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
        , "/" + Profiler.PARAM_GROUP + "/apache" , null);
        System.out.println("post set URL = " + theRequest.getURL());
    }
    
    /**
     * Test the Group link
     * @throws Exception
     */
    public void testGroupLink() throws Exception
    {
        // Create the RunData object to be used during testing.
        RunData rundata = RunDataFactory.getRunData( request, response, config );
        assertNotNull( "Got rundata", rundata);
        TurbineTestUtilities.setupRunData(rundata);
        
        // Get and populate the context
        Context context = TurbineVelocity.getContext(rundata);
        assertNotNull( "Got context", context);
        TurbinePull.populateContext( context, rundata);
        
        // Verify tool are in the context
        JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
        assertNotNull( "Got jslink from context", jsLink);
        
        // Generate the link
        String link = jsLink.toString();
        System.out.println("Group link = " + link);
        assertTrue( "Verifing base Link", 
          link.startsWith("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET));

        assertTrue( "Verify link contains /" + Profiler.PARAM_GROUP + "/apache", 
          (link.indexOf("/" + Profiler.PARAM_GROUP + "/apache") >0));

        assertEquals( "Verify link does NOT contain /" + Profiler.PARAM_USER, -1,
           link.indexOf("/" + Profiler.PARAM_USER ));

        // Generatate and output thre page
        TurbineTestUtilities.generatePage(rundata);
        TurbineTestUtilities.outputPage(rundata);
        
        // Return the used RunData to the factory for recycling.
        RunDataFactory.putRunData(rundata);
        
    }
    
    public void endGroupLink(org.apache.cactus.WebResponse theResponse)
    {
        System.out.println("text = " + theResponse.getText().length());
    }
    
    public void beginPageLink(WebRequest theRequest)
    {
        theRequest.setURL(TEST_HOST, TEST_CONTEXT, TEST_SERVLET
        , "/" + Profiler.PARAM_PAGE + "/news" , null);
        System.out.println("post set URL = " + theRequest.getURL());
    }
    
    /**
     * Test the User link
     * @throws Exception
     */
    public void testPageLink() throws Exception
    {
        // Create the RunData object to be used during testing.
        RunData rundata = RunDataFactory.getRunData( request, response, config );
        assertNotNull( "Got rundata", rundata);
        TurbineTestUtilities.setupRunData(rundata);
        
        // Get and populate the context
        Context context = TurbineVelocity.getContext(rundata);
        assertNotNull( "Got context", context);
        TurbinePull.populateContext( context, rundata);
        
        // Verify tool are in the context
        JetspeedLink jsLink = (JetspeedLink) context.get("jslink");
        assertNotNull( "Got jslink from context", jsLink);
        
        // Generate the link
        String link = jsLink.toString();
        System.out.println("Page Link = " + link);
        assertTrue( "Verifing base Link", 
          link.startsWith("http://" + TEST_HOST + TEST_CONTEXT + TEST_SERVLET));

        assertTrue( "Verify link contains /" + Profiler.PARAM_USER + "/anon", 
          (link.indexOf("/" + Profiler.PARAM_USER + "/anon") >0));

        assertTrue( "Verify link contains /" + Profiler.PARAM_PAGE + "/news", 
          (link.indexOf("/" + Profiler.PARAM_PAGE + "/news") >0));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?