001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.jci.stores;
019    
020    import org.apache.commons.jci.AbstractTestCase;
021    import org.apache.commons.lang3.ArrayUtils;
022    
023    /**
024     * 
025     * @author tcurdt
026     */
027    public final class ResourceStoreTestCase extends AbstractTestCase {
028    
029        public void testMemoryResourceStore() {
030            checkReadWrite(new MemoryResourceStore());
031            checkRemove(new MemoryResourceStore());
032        }
033    
034        public void testFileResourceStore() {
035            checkReadWrite(new FileResourceStore(directory));
036            checkRemove(new FileResourceStore(directory));
037        }
038    
039        public void testTransactionalFileResourceStore() {          
040            checkReadWrite(new TransactionalResourceStore(new FileResourceStore(directory)));
041            checkRemove(new TransactionalResourceStore(new FileResourceStore(directory)));
042            
043            final ResourceStore rs = new FileResourceStore(directory);
044            final TransactionalResourceStore trs = new TransactionalResourceStore(rs);
045            assertEquals(rs.toString(), trs.toString());
046        }
047    
048        private void checkReadWrite( final ResourceStore pStore ) {
049            final byte[] data = { 1, 2, 3 };
050            pStore.write("key", data);
051            
052            final byte[] read = pStore.read("key");
053            
054            assertTrue(read != null);
055            assertTrue(ArrayUtils.isEquals(data, read));
056        }
057    
058        private void checkRemove( final ResourceStore pStore ) {
059            final byte[] data = { 1, 2, 3 };
060            pStore.write("key", data);
061            
062            final byte[] read = pStore.read("key");
063            
064            assertTrue(read != null);
065            assertTrue(ArrayUtils.isEquals(data, read));
066    
067            pStore.remove("key");
068    
069            final byte[] empty = pStore.read("key");
070            
071            assertTrue(empty == null);
072        }    
073    }