All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.boot.test.FCTest Maven / Gradle / Ivy

There is a newer version: 1.16.0
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

package at.spardat.xma.boot.test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Random;

import junit.framework.Assert;
import junit.framework.TestCase;
import at.spardat.xma.boot.BootRuntime;
import at.spardat.xma.boot.cache.FileCache;
import at.spardat.xma.boot.cache.IFileCache;
import at.spardat.xma.boot.cache.IFileCacheResource;
import at.spardat.xma.boot.logger.Logger;
import at.spardat.xma.boot.transport.ConnectException;


/**
 * class: Tester
 *
 * @author s3595 Chris Sch?fer (CGS)
 * @version 0.1.0
 *
 */
public class FCTest  extends TestCase  {

    private static IFileCache   fc ;
    private static BootRuntime rt ;

    public FCTest(String name) throws IOException {
        super(name);

    }

    protected void setUp() throws IOException {
      if( fc == null) init();
    }


    /**
     * @param args
     * @return void
     */
	public static void main(String[] args) {
        try {
            init();

            FCTest t = new FCTest("");

            t.test_01();
            t.test_02();
            t.test_03();
            t.test_04();
            t.test_05();
            t.test_06();
            t.test_07();


        } catch (Exception e1) {
            e1.printStackTrace();
        }

	}// main

    public static void init ( ) throws IOException{

        File f = new File( TestUtils.strTestbaseDir );
        if( !f.exists() )
          f.mkdirs();

        rt = BootRuntime.initialize(f, Logger.getLogger( "tester"));
        // fc = FileCache.initialize( f = rt.getBaseDir() );

        fc = FileCache.getInstance();

        }// init


    public void test_01( ) throws IOException, Exception{

        URL url1 = TestUtils.getSTDURL();
        // URL url2 = new URL( "http://localhost:5555/imctestapp/testpages/test.gif" );

        fc.invalidateResource(url1);
        //fc.invalidateResource(url2);

        // get a clean copy
        fc.openResource( url1 );
        //fc.openResource( url2 );

        // cache hit
        fc.openResource( url1 );
        // fc.openResource( url2 );

        fc.invalidateResource(url1);
        fc.invalidateResource(url1);

        System.out.println("test 01 ok");
    }

    public void test_02( ) throws IOException, Exception{

        URL url1 = new URL( TestUtils.strTestServer + "/demo/notfound.psj" );

        fc.invalidateResource(url1);

        try {
            fc.openResource( url1 );
        }catch( IOException ex ) {
            System.out.println("resource not found. ok");
        }
        fc.invalidateResource(url1);

        System.out.println("test_02 ok");
    }

    public void test_03( ) throws IOException, Exception {

        URL url1 = new URL( TestUtils.getSTDURL() + "?param1=test&p??m2=?sterreich¶m3=drei" );
        fc.invalidateResource(url1);
        fc.openResource( url1 );

        System.out.println("test_03 ok");
    }

    public void test_04( ) throws IOException, Exception {
        System.out.println("test_04: ");

        URL url1 = new URL(TestUtils.getSTDURL() +"?param=???????&p1234567890=0987654321" );
        fc.invalidateResource(url1);

        fc.openResource( url1 );  // loads it from server, and store to disk

        // hit the file cache
        IFileCacheResource res = fc.openResource( url1 );


        URL url = res.getLocation();
        System.out.println("url:" + url.toExternalForm());

        Assert.assertNotNull(url);
        Assert.assertTrue( url1.equals(url1) );

        System.out.println("test_04 ok");
    }


    public void test_05( ) throws IOException, Exception {
        System.out.println("test_05: ");

        URL url1 = new URL( TestUtils.getSTDURL().toExternalForm() );
        // + "?param1=!\"?$%&/()='~*+_-.:;,|^? space??[]{}\\//" );
        fc.invalidateResource(url1);

        fc.openResource( url1 );  // loads it from server, and store to disk

        // hit the file cache
        IFileCacheResource res = fc.openResource( url1 );

        URL url = res.getLocation();
        System.out.println("url:" + url.toExternalForm());

        Assert.assertNotNull(url);
        Assert.assertTrue( url1.equals(url1) );
        Assert.assertTrue( url.toExternalForm().equals( url1.toExternalForm()));

        System.out.println("test_05 ok");
    }


    public void test_06( ) throws IOException, Exception {
        System.out.println("test_06: ");

        URL url1 = new URL( TestUtils.getSTDURL().toExternalForm() + "?param=" + getRandomStrings(1000) );
        fc.invalidateResource(url1);

        fc.openResource( url1 );  // loads it from server, and store to disk

        // hit the file cache
        IFileCacheResource res = fc.openResource( url1 );

        URL url = res.getLocation();
        System.out.println("url:" + url.toExternalForm());

        Assert.assertNotNull(url);
        Assert.assertTrue( url1.equals(url1) );
        Assert.assertTrue( url.toExternalForm().equals( url1.toExternalForm()));

        System.out.println("test_06 ok");
    }

    public void test_07( ) throws IOException, Exception {
          System.out.println("test_07: ");

          URL url1 = new URL( "http://invalid_hostname/" );
          fc.invalidateResource(url1);

          try {
             fc.openResource( url1 );
          }catch( ConnectException e ) {
              System.out.println( "connect exc. ok: " + e.getMessage() );
          }

          System.out.println("test_07 ok");
      }



    private static String getRandomStrings( int count) {
        StringBuffer buf = new StringBuffer(count);
        Random rand = new Random();

        for(int i = 0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy