You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by hc...@apache.org on 2005/01/24 11:30:39 UTC

cvs commit: jakarta-turbine-jcs/sandbox/yajcache/test/org/apache/jcs/yajcache/file CacheFileDAOTest.java FileContentTypeTest.java

hchar       2005/01/24 02:30:39

  Added:       sandbox/yajcache/test/org/apache/jcs/yajcache/file
                        CacheFileDAOTest.java FileContentTypeTest.java
  Log:
  junit tests for cache file system
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-jcs/sandbox/yajcache/test/org/apache/jcs/yajcache/file/CacheFileDAOTest.java
  
  Index: CacheFileDAOTest.java
  ===================================================================
  
  /*
   * Copyright 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.jcs.yajcache.file;
  
  import java.io.File;
  import java.io.RandomAccessFile;
  import java.util.Arrays;
  import junit.framework.*;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.jcs.yajcache.annotate.*;
  
  /**
   *
   * @author Hanson Char
   */
  @CopyRightApache
  public class CacheFileDAOTest extends TestCase {
      private Log log = LogFactory.getLog(this.getClass());
      
      public void test() {
          log.debug("testing cache directory " 
                  + CacheFileUtils.inst.getCacheDir("testCache").getAbsolutePath());
          log.debug("remove Cache directory");
          assertTrue(CacheFileUtils.inst.rmCacheDir("testCache"));
          log.debug("create Cache directory");
          assertTrue(CacheFileUtils.inst.mkCacheDirs("testCache"));
          
          log.debug("test writeCacheItem");
          byte[] ba1 = {1, 2, 3, 4};
          CacheFileDAO.inst.writeCacheItem("testCache", 
                  CacheFileContentType.JAVA_SERIALIZATION, "key1", ba1);
          byte[] ba2 = {'a', 'b', 'c', 'd'};
          CacheFileDAO.inst.writeCacheItem("testCache", 
                  CacheFileContentType.XML_ENCODER, "key2", ba2);
          
          log.debug("test readCacheItem");
          byte[] ba1r = CacheFileDAO.inst.readCacheItem("testCache", "key1");
          assertTrue(Arrays.equals(ba1, ba1r));
          byte[] ba2r = CacheFileDAO.inst.readCacheItem("testCache", "key2");
          assertTrue(Arrays.equals(ba2, ba2r));
          
          log.debug("test removeCacheItem");
          assertTrue(CacheFileDAO.inst.removeCacheItem("testCache", "key1"));
          assertTrue(CacheFileDAO.inst.removeCacheItem("testCache", "key2"));
          assertFalse(CacheFileDAO.inst.removeCacheItem("testCache", "key3"));
      }
      public void testCorruptedFile() throws Exception {
          log.debug("create testCacheCorrupt Cache directory");
          CacheFileUtils.inst.mkCacheDirs("testCacheCorrupt");
          
          log.debug("test readCacheItem missing content");
          byte[] ba = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyx");
          assertTrue(ba == null);
  
          log.debug("test readCacheItem with corrupted hash code");
          File file = CacheFileUtils.inst.getCacheFile("testCacheCorrupt", "keyy");
          RandomAccessFile raf = new RandomAccessFile(file, "rw");
          byte[] ba2 = {1, 2, 3, 4};
          CacheFileContent cfc = CacheFileContent.getInstance(CacheFileContentType.JAVA_SERIALIZATION, ba2);
          cfc.setContentHashCode(0);
          cfc.write(raf);
          raf.close();
  
          byte[] ba2i = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy");
          assertTrue(ba2i == null);
  
          log.debug("test readCacheItem with corrupted length");
          file.delete();
          raf = new RandomAccessFile(file, "rw");
          cfc = CacheFileContent.getInstance(CacheFileContentType.JAVA_SERIALIZATION, ba2);
          cfc.setContentLength(100);
          cfc.write(raf);
          raf.close();
  
          ba2i = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy");
          assertTrue(ba2i == null);
  
          log.debug("test readCacheItem with corrupted content");
          file.delete();
          raf = new RandomAccessFile(file, "rw");
          cfc = CacheFileContent.getInstance(CacheFileContentType.JAVA_SERIALIZATION, ba2);
          cfc.setContent(new byte[] {'a', 'b', 'c', 'd', 'e'});
          cfc.write(raf);
          raf.close();
          ba2i = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy");
          assertTrue(ba2i == null);
          
          log.debug("test readCacheItem with appended content");
          file.delete();
          raf = new RandomAccessFile(file, "rw");
          cfc = CacheFileContent.getInstance(CacheFileContentType.JAVA_SERIALIZATION, ba2);
          cfc.setContent(new byte[] {1, 2, 3, 4, 5});
          cfc.write(raf);
          raf.close();
          ba2i = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy");
          assertTrue(ba2i == null);
          
          log.debug("test readCacheItem with content less than min length");
          file.delete();
          raf = new RandomAccessFile(file, "rw");
          cfc = CacheFileContent.getInstance(CacheFileContentType.JAVA_SERIALIZATION, ba2);
          cfc.setContent(null);
          cfc.write(raf);
          raf.close();
          ba2i = CacheFileDAO.inst.readCacheItem("testCacheCorrupt", "keyy");
          assertTrue(ba2i == null);
          
          log.debug(CacheFileDAO.inst.toString());
          
      }
  }
  
  
  
  1.1                  jakarta-turbine-jcs/sandbox/yajcache/test/org/apache/jcs/yajcache/file/FileContentTypeTest.java
  
  Index: FileContentTypeTest.java
  ===================================================================
  /*
   * Copyright 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.jcs.yajcache.file;
  
  import junit.framework.*;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import org.apache.jcs.yajcache.annotate.*;
  
  /**
   *
   * @author Hanson Char
   */
  @CopyRightApache
  public class FileContentTypeTest extends TestCase {
      private Log log = LogFactory.getLog(this.getClass());
      /**
       * Test of toByte method, of class org.apache.jcs.yajcache.config.FileContentType.
       */
      public void test() {
          log.debug("test toByte");
          Byte bJavaSerialization = CacheFileContentType.JAVA_SERIALIZATION.toByte();
          Byte bXmlEncoder = CacheFileContentType.XML_ENCODER.toByte();
          assertFalse(bJavaSerialization == bXmlEncoder);
          log.debug("test fromByte");
          assertTrue(CacheFileContentType.JAVA_SERIALIZATION == CacheFileContentType.fromByte(bJavaSerialization));
          assertTrue(CacheFileContentType.XML_ENCODER == CacheFileContentType.fromByte(bXmlEncoder));
          log.debug("test fromByte with unknown type");
          try {
              CacheFileContentType.fromByte((byte)99);
              fail("Should never get here for unknown fileContentType");
          } catch(IllegalArgumentException ex) {
          }
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org