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

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

/*******************************************************************************
 * 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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

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.IFileCacheResource;
import at.spardat.xma.boot.logger.Logger;


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

    private static FileCache   fc ;
    private static BootRuntime rt ;

    public FCResourceTest(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();

            FCResourceTest t = new FCResourceTest("main");

            t.test_isCached();
            t.test_BMode();
            t.test_Interface();

        } 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( rt );

        }// init

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

        URL url1 = TestUtils.getSTDURL();

        // delete if exist
          fc.invalidateResource(url1);
        Assert.assertTrue( !fc.isCached(url1) );
        Assert.assertTrue( !fc.isCached(url1) );

          IFileCacheResource fcr = fc.openResource( url1 );  // loads it from server, and store to disk
        Assert.assertTrue( fc.isCached(url1) );

        fc.invalidateResource(fcr);
        Assert.assertTrue( !fc.isCached(url1) );

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

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

        URL url1 = TestUtils.getSTDURL();

        // delete if exist
          fc.invalidateResource(url1);
        Assert.assertTrue( !fc.isCached(url1) );
        IFileCacheResource fcr = fc.openResource(url1, false );  // loads it from server, and store to disk
        Assert.assertTrue( fc.isCached(url1) );

        // trigger buffer loading
        InputStream is = fcr.getInputStream();
        is.close();

        fc.invalidateResource(fcr);
        Assert.assertTrue( !fc.isCached(url1) );
        System.out.println("test_BMode ok");
    }


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

        URL url1 = TestUtils.getSTDURL();

        // delete if exist
          fc.invalidateResource(url1);
        Assert.assertTrue( !fc.isCached(url1) );

        long start = System.currentTimeMillis();

        IFileCacheResource fcr = fc.openResource(url1, false );  // loads it from server, and store to disk
        Assert.assertTrue( fc.isCached(url1) );

        /* start interface */
        long expires = fcr.getExpiration();
        Assert.assertEquals(0, expires);

        long lastm = fcr.getLastModified();
//        Assert.assertEquals(0, lastm); // gub: why null?

        long lastupd = fcr.getLastUpdated();
        Assert.assertTrue( lastupd >= start && lastupd <= System.currentTimeMillis() );

        URL localres = fcr.getLocalRes();
        Assert.assertNotNull(localres);
        Assert.assertTrue( localres.toExternalForm().length() > 0 );

        URL location = fcr.getLocation();
        Assert.assertTrue( location.equals(url1) );

        String strlastm = (String)fcr.getProperty( "last-modified");
//        Assert.assertTrue( strlastm.equals("0") );  // gub: why null?

        fcr.getProperty("last-modified", "1");
//        Assert.assertTrue( strlastm.equals("0") );  // gub: why null?

        // if 0 it is not expired
        Assert.assertTrue( !fcr.isExpired() );

        fcr.setProperty("testkey", "testval");
        String testkey = fcr.getProperty("testkey");
        Assert.assertTrue( testkey.equals("testval") );

        InputStream is = fcr.getInputStream();
        File osf       = new File ( "c://temp/test.out");
        OutputStream os = new FileOutputStream( osf );

        try {
          byte bBuf[] = new byte[1024];
          while (true) {
            int cRead = is.read(bBuf, 0, bBuf.length);
            if (cRead == -1)
              break;
            os.write(bBuf, 0, cRead);
          }
        }
        finally { // close the streams
          is.close();
          os.close();
          osf.delete();
        }

        fc.invalidateResource(fcr);
        Assert.assertTrue( !fc.isCached(url1) );
        System.out.println("test_BMode ok");

    }




}// class












© 2015 - 2024 Weber Informatics LLC | Privacy Policy