jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dpfis...@apache.org
Subject svn commit: r1577127 [3/3] - in /jackrabbit/trunk: jackrabbit-aws-ext/ jackrabbit-aws-ext/src/main/java/org/apache/jackrabbit/aws/ext/ jackrabbit-aws-ext/src/main/java/org/apache/jackrabbit/aws/ext/ds/ jackrabbit-aws-ext/src/test/java/org/apache/jackra...
Date Thu, 13 Mar 2014 12:12:34 GMT
Added: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestCaseBase.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestCaseBase.java?rev=1577127&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestCaseBase.java
(added)
+++ jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestCaseBase.java
Thu Mar 13 12:12:33 2014
@@ -0,0 +1,666 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.core.data;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Random;
+
+import javax.jcr.RepositoryException;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.core.data.CachingDataStore;
+import org.apache.jackrabbit.core.data.DataIdentifier;
+import org.apache.jackrabbit.core.data.DataRecord;
+import org.apache.jackrabbit.core.data.DataStore;
+import org.apache.jackrabbit.core.data.DataStoreException;
+import org.apache.jackrabbit.core.data.LocalCache;
+import org.apache.jackrabbit.core.data.MultiDataStoreAware;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test base class which covers all scenarios.
+ */
+public abstract class TestCaseBase extends TestCase {
+
+    /**
+     * Logger
+     */
+    protected static final Logger LOG = LoggerFactory.getLogger(TestCaseBase.class);
+
+    /**
+     * temp directory
+     */
+    private static final String TEST_DIR = "target/temp";
+
+    /**
+     * Constant describing aws properties file path.
+     */
+    public static final String CONFIG = "config";
+
+    /**
+     * File path of aws properties.
+     */
+    protected String config;
+
+    /**
+     * Parameter to use in-memory backend. If false {@link S3Backend}
+     */
+    protected boolean memoryBackend = true;
+
+    /**
+     * Parameter to use local cache. If true local cache {@link LocalCache} is
+     * not used.
+     */
+    protected boolean noCache;
+
+    /**
+     * length of record to be added
+     */
+    private int dataLength = 123456;
+
+    /**
+     * datastore directory path
+     */
+    protected String dataStoreDir;
+
+    protected CachingDataStore ds;
+
+    /**
+     * Random number generator to populate data
+     */
+    protected Random randomGen = new Random();
+
+    /**
+     * Delete temporary directory.
+     */
+    @Override
+    protected void setUp() throws Exception {
+        dataStoreDir = TEST_DIR + "-"
+            + String.valueOf(randomGen.nextInt(dataLength));
+    }
+
+    /**
+     * Delete temporary directory.
+     */
+    @Override
+    protected void tearDown() throws Exception {
+        boolean delSuccessFul = FileUtils.deleteQuietly(new File(dataStoreDir));
+        int retry = 2, count = 0;
+        while (!delSuccessFul && count <= retry) {
+            // try once more
+            delSuccessFul = FileUtils.deleteQuietly(new File(dataStoreDir));
+            count++;
+        }
+        LOG.info("tearDown : directory [" + dataStoreDir + "] deleted ["
+            + delSuccessFul + "]");
+    }
+
+    /**
+     * Testcase to validate {@link DataStore#addRecord(InputStream)} API.
+     */
+    public void testAddRecord() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#addRecord, testDir=" + dataStoreDir);
+            doAddRecordTest();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#addRecord finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Testcase to validate {@link DataStore#getRecord(DataIdentifier)} API.
+     */
+    public void testGetRecord() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testGetRecord, testDir=" + dataStoreDir);
+            doGetRecordTest();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testGetRecord finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+        }
+    }
+
+    /**
+     * Testcase to validate {@link DataStore#getAllIdentifiers()} API.
+     */
+    public void testGetAllIdentifiers() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testGetAllIdentifiers, testDir=" + dataStoreDir);
+            doGetAllIdentifiersTest();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testGetAllIdentifiers finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Testcase to validate {@link DataStore#updateModifiedDateOnAccess(long)}
+     * API.
+     */
+    public void testUpdateLastModifiedOnAccess() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testUpdateLastModifiedOnAccess, testDir=" + dataStoreDir);
+            doUpdateLastModifiedOnAccessTest();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testUpdateLastModifiedOnAccess finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+        }
+    }
+
+    /**
+     * Testcase to validate
+     * {@link MultiDataStoreAware#deleteRecord(DataIdentifier)}.API.
+     */
+    public void testDeleteRecord() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testDeleteRecord, testDir=" + dataStoreDir);
+            doDeleteRecordTest();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testDeleteRecord finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Testcase to validate {@link DataStore#deleteAllOlderThan(long)} API.
+     */
+    public void testDeleteAllOlderThan() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testDeleteAllOlderThan, testDir=" + dataStoreDir);
+            doDeleteAllOlderThan();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testDeleteAllOlderThan finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Testcase to validate {@link DataStore#getRecordFromReference(String)}
+     */
+    public void testReference() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testReference, testDir=" + dataStoreDir);
+            doReferenceTest();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testReference finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Testcase to validate mixed scenario use of {@link DataStore}.
+     */
+    public void testSingleThread() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#test, testDir=" + dataStoreDir);
+            doTestSingleThread();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#test finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Testcase to validate mixed scenario use of {@link DataStore} in
+     * multi-threaded concurrent environment.
+     */
+    public void testMultiThreaded() {
+        try {
+            long start = System.currentTimeMillis();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testMultiThreaded, testDir=" + dataStoreDir);
+            doTestMultiThreaded();
+            LOG.info("Testcase: " + this.getClass().getName()
+                + "#testMultiThreaded finished, time taken = ["
+                + (System.currentTimeMillis() - start) + "]ms");
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail(e.getMessage());
+        }
+
+    }
+
+    protected CachingDataStore createDataStore() throws RepositoryException {
+        ds = new InMemoryDataStore();
+        ds.setConfig(config);
+        if (noCache) {
+            ds.setCacheSize(0);
+        }
+        ds.init(dataStoreDir);
+        return ds;
+    }
+
+    /**
+     * Test {@link DataStore#addRecord(InputStream)} and assert length of added
+     * record.
+     */
+    protected void doAddRecordTest() throws Exception {
+        ds = createDataStore();
+        byte[] data = new byte[dataLength];
+        randomGen.nextBytes(data);
+        DataRecord rec = ds.addRecord(new ByteArrayInputStream(data));
+        assertEquals(data.length, rec.getLength());
+        assertRecord(data, rec);
+        ds.close();
+    }
+
+    /**
+     * Test {@link DataStore#getRecord(DataIdentifier)} and assert length and
+     * inputstream.
+     */
+    protected void doGetRecordTest() throws Exception {
+        ds = createDataStore();
+        byte[] data = new byte[dataLength];
+        randomGen.nextBytes(data);
+        DataRecord rec = ds.addRecord(new ByteArrayInputStream(data));
+        rec = ds.getRecord(rec.getIdentifier());
+        assertEquals(data.length, rec.getLength());
+        assertRecord(data, rec);
+        ds.close();
+    }
+
+    /**
+     * Test {@link MultiDataStoreAware#deleteRecord(DataIdentifier)}.
+     */
+    protected void doDeleteRecordTest() throws Exception {
+        ds = createDataStore();
+        Random random = randomGen;
+        byte[] data1 = new byte[dataLength];
+        random.nextBytes(data1);
+        DataRecord rec1 = ds.addRecord(new ByteArrayInputStream(data1));
+
+        byte[] data2 = new byte[dataLength];
+        random.nextBytes(data2);
+        DataRecord rec2 = ds.addRecord(new ByteArrayInputStream(data2));
+
+        byte[] data3 = new byte[dataLength];
+        random.nextBytes(data3);
+        DataRecord rec3 = ds.addRecord(new ByteArrayInputStream(data3));
+
+        ds.deleteRecord(rec2.getIdentifier());
+
+        assertNull("rec2 should be null",
+            ds.getRecordIfStored(rec2.getIdentifier()));
+        assertEquals(new ByteArrayInputStream(data1),
+            ds.getRecord(rec1.getIdentifier()).getStream());
+        assertEquals(new ByteArrayInputStream(data3),
+            ds.getRecord(rec3.getIdentifier()).getStream());
+        ds.close();
+    }
+
+    /**
+     * Test {@link DataStore#getAllIdentifiers()} and asserts all identifiers
+     * are returned.
+     */
+    protected void doGetAllIdentifiersTest() throws Exception {
+        ds = createDataStore();
+        List<DataIdentifier> list = new ArrayList<DataIdentifier>();
+        Random random = randomGen;
+        byte[] data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec = ds.addRecord(new ByteArrayInputStream(data));
+        list.add(rec.getIdentifier());
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        rec = ds.addRecord(new ByteArrayInputStream(data));
+        list.add(rec.getIdentifier());
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        rec = ds.addRecord(new ByteArrayInputStream(data));
+        list.add(rec.getIdentifier());
+
+        Iterator<DataIdentifier> itr = ds.getAllIdentifiers();
+        while (itr.hasNext()) {
+            assertTrue("record found on list", list.remove(itr.next()));
+        }
+        assertEquals(0, list.size());
+        ds.close();
+    }
+
+    /**
+     * Asserts that timestamp of all records accessed after
+     * {@link DataStore#updateModifiedDateOnAccess(long)} invocation.
+     */
+    protected void doUpdateLastModifiedOnAccessTest() throws Exception {
+        ds = createDataStore();
+        Random random = randomGen;
+        byte[] data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec1 = ds.addRecord(new ByteArrayInputStream(data));
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec2 = ds.addRecord(new ByteArrayInputStream(data));
+        LOG.debug("rec2 timestamp=" + rec2.getLastModified());
+
+        Thread.sleep(2000);
+        long updateTime = System.currentTimeMillis();
+        LOG.debug("updateTime=" + updateTime);
+        ds.updateModifiedDateOnAccess(updateTime);
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec3 = ds.addRecord(new ByteArrayInputStream(data));
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec4 = ds.addRecord(new ByteArrayInputStream(data));
+
+        rec1 = ds.getRecord(rec1.getIdentifier());
+
+        assertEquals("rec1 touched", true,
+            ds.getLastModified(rec1.getIdentifier()) > updateTime);
+        LOG.debug("rec2 timestamp=" + rec2.getLastModified());
+        assertEquals("rec2 not touched", true,
+            ds.getLastModified(rec2.getIdentifier()) < updateTime);
+        assertEquals("rec3 touched", true,
+            ds.getLastModified(rec3.getIdentifier()) > updateTime);
+        assertEquals("rec4 touched", true,
+            ds.getLastModified(rec4.getIdentifier()) > updateTime);
+        ds.close();
+
+    }
+
+    /**
+     * Asserts that {@link DataStore#deleteAllOlderThan(long)} only deleted
+     * records older than argument passed.
+     */
+    protected void doDeleteAllOlderThan() throws Exception {
+        ds = createDataStore();
+        Random random = randomGen;
+        byte[] data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec1 = ds.addRecord(new ByteArrayInputStream(data));
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec2 = ds.addRecord(new ByteArrayInputStream(data));
+
+        Thread.sleep(2000);
+        long updateTime = System.currentTimeMillis();
+        ds.updateModifiedDateOnAccess(updateTime);
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec3 = ds.addRecord(new ByteArrayInputStream(data));
+
+        data = new byte[dataLength];
+        random.nextBytes(data);
+        DataRecord rec4 = ds.addRecord(new ByteArrayInputStream(data));
+
+        rec1 = ds.getRecord(rec1.getIdentifier());
+        ds.clearInUse();
+        assertEquals("only rec2 should be deleted", 1,
+            ds.deleteAllOlderThan(updateTime));
+        assertNull("rec2 should be null",
+            ds.getRecordIfStored(rec2.getIdentifier()));
+
+        Iterator<DataIdentifier> itr = ds.getAllIdentifiers();
+        List<DataIdentifier> list = new ArrayList<DataIdentifier>();
+        list.add(rec1.getIdentifier());
+        list.add(rec3.getIdentifier());
+        list.add(rec4.getIdentifier());
+        while (itr.hasNext()) {
+            assertTrue("record found on list", list.remove(itr.next()));
+        }
+
+        assertEquals("touched records found", 0, list.size());
+        assertEquals("rec1 touched", true,
+            ds.getLastModified(rec1.getIdentifier()) > updateTime);
+        assertEquals("rec3 touched", true,
+            ds.getLastModified(rec3.getIdentifier()) > updateTime);
+        assertEquals("rec4 touched", true,
+            ds.getLastModified(rec4.getIdentifier()) > updateTime);
+        ds.close();
+    }
+
+    /**
+     * Test if record can be accessed via
+     * {@link DataStore#getRecordFromReference(String)}
+     */
+    public void doReferenceTest() throws Exception {
+        ds = createDataStore();
+        ds.setSecret("12345");
+        byte[] data = new byte[dataLength];
+        randomGen.nextBytes(data);
+        String reference;
+        DataRecord record = ds.addRecord(new ByteArrayInputStream(data));
+        reference = record.getReference();
+        assertReference(data, reference, ds);
+        ds.close();
+    }
+
+    /**
+     * Method to validate mixed scenario use of {@link DataStore}.
+     */
+    protected void doTestSingleThread() throws Exception {
+        ds = createDataStore();
+        doTestMultiThreaded(ds, 1);
+        ds.close();
+    }
+
+    /**
+     * Method to validate mixed scenario use of {@link DataStore} in
+     * multi-threaded concurrent environment.
+     */
+    protected void doTestMultiThreaded() throws Exception {
+        ds = createDataStore();
+        doTestMultiThreaded(ds, 4);
+        ds.close();
+    }
+
+    /**
+     * Method to assert record with byte array.
+     */
+    protected void assertRecord(byte[] expected, DataRecord record)
+            throws DataStoreException, IOException {
+        InputStream stream = record.getStream();
+        try {
+            for (int i = 0; i < expected.length; i++) {
+                assertEquals(expected[i] & 0xff, stream.read());
+            }
+            assertEquals(-1, stream.read());
+        } finally {
+            stream.close();
+        }
+    }
+
+    /**
+     * Method to run {@link TestCaseBase#doTest(DataStore, int)} in multiple
+     * concurrent threads.
+     */
+    protected void doTestMultiThreaded(final DataStore ds, int threadCount)
+            throws Exception {
+        final Exception[] exception = new Exception[1];
+        Thread[] threads = new Thread[threadCount];
+        for (int i = 0; i < threadCount; i++) {
+            final int x = i;
+            Thread t = new Thread() {
+                public void run() {
+                    try {
+                        doTest(ds, x);
+                    } catch (Exception e) {
+                        exception[0] = e;
+                    }
+                }
+            };
+            threads[i] = t;
+            t.start();
+        }
+        for (int i = 0; i < threadCount; i++) {
+            threads[i].join();
+        }
+        if (exception[0] != null) {
+            throw exception[0];
+        }
+    }
+
+    /**
+     * Assert randomly read stream from record.
+     */
+    void doTest(DataStore ds, int offset) throws Exception {
+        ArrayList<DataRecord> list = new ArrayList<DataRecord>();
+        HashMap<DataRecord, Integer> map = new HashMap<DataRecord, Integer>();
+        for (int i = 0; i < 10; i++) {
+            int size = 1000000 - (i * 100);
+            RandomInputStream in = new RandomInputStream(size + offset, size);
+            DataRecord rec = ds.addRecord(in);
+            list.add(rec);
+            map.put(rec, new Integer(size));
+        }
+        Random random = new Random(1);
+        for (int i = 0; i < list.size(); i++) {
+            int pos = random.nextInt(list.size());
+            DataRecord rec = list.get(pos);
+            int size = map.get(rec);
+            rec = ds.getRecord(rec.getIdentifier());
+            assertEquals(size, rec.getLength());
+            InputStream in = rec.getStream();
+            RandomInputStream expected = new RandomInputStream(size + offset,
+                size);
+            if (random.nextBoolean()) {
+                in = readInputStreamRandomly(in, random);
+            }
+            assertEquals(expected, in);
+        }
+    }
+
+    InputStream readInputStreamRandomly(InputStream in, Random random)
+            throws IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        byte[] buffer = new byte[8000];
+        while (true) {
+            if (random.nextBoolean()) {
+                int x = in.read();
+                if (x < 0) {
+                    break;
+                }
+                out.write(x);
+            } else {
+                if (random.nextBoolean()) {
+                    int l = in.read(buffer);
+                    if (l < 0) {
+                        break;
+                    }
+                    out.write(buffer, 0, l);
+                } else {
+                    int offset = random.nextInt(buffer.length / 2);
+                    int len = random.nextInt(buffer.length / 2);
+                    int l = in.read(buffer, offset, len);
+                    if (l < 0) {
+                        break;
+                    }
+                    out.write(buffer, offset, l);
+                }
+            }
+        }
+        in.close();
+        return new ByteArrayInputStream(out.toByteArray());
+    }
+
+    /**
+     * Assert two inputstream
+     */
+    protected void assertEquals(InputStream a, InputStream b)
+            throws IOException {
+        try {
+            assertTrue("binary not equal",
+                org.apache.commons.io.IOUtils.contentEquals(a, b));
+        } finally {
+            try {
+                a.close();
+            } catch (Exception ignore) {
+            }
+            try {
+                b.close();
+            } catch (Exception ignore) {
+            }
+        }
+    }
+
+    /**
+     * Assert inputstream read from reference.
+     */
+    protected void assertReference(byte[] expected, String reference,
+            DataStore store) throws Exception {
+        DataRecord record = store.getRecordFromReference(reference);
+        assertNotNull(record);
+        assertEquals(expected.length, record.getLength());
+
+        InputStream stream = record.getStream();
+        try {
+            assertTrue("binary not equal",
+                org.apache.commons.io.IOUtils.contentEquals(
+                    new ByteArrayInputStream(expected), stream));
+        } finally {
+            stream.close();
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestCaseBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDs.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDs.java?rev=1577127&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDs.java
(added)
+++ jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDs.java
Thu Mar 13 12:12:33 2014
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.core.data;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test {@link CachingDataStore} with InMemoryBackend and local cache on.
+ */
+public class TestInMemDs extends TestCaseBase {
+
+    protected static final Logger LOG = LoggerFactory.getLogger(TestInMemDs.class);
+
+    public TestInMemDs() {
+        config = null;
+        memoryBackend = true;
+        noCache = false;
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDs.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDsCacheOff.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDsCacheOff.java?rev=1577127&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDsCacheOff.java
(added)
+++ jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDsCacheOff.java
Thu Mar 13 12:12:33 2014
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.core.data;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test {@link CachingDataStore} with InMemoryBackend and local cache off.
+ */
+public class TestInMemDsCacheOff extends TestCaseBase {
+
+    protected static final Logger LOG = LoggerFactory.getLogger(TestInMemDsCacheOff.class);
+
+    public TestInMemDsCacheOff() {
+        config = null;
+        memoryBackend = true;
+        noCache = true;
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestInMemDsCacheOff.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestLocalCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestLocalCache.java?rev=1577127&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestLocalCache.java
(added)
+++ jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestLocalCache.java
Thu Mar 13 12:12:33 2014
@@ -0,0 +1,279 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.core.data;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.apache.jackrabbit.core.data.AsyncUploadCache;
+import org.apache.jackrabbit.core.data.AsyncUploadCacheResult;
+import org.apache.jackrabbit.core.data.LocalCache;
+import org.apache.jackrabbit.core.fs.local.FileUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Testcase to test local cache.
+ */
+public class TestLocalCache extends TestCase {
+
+    private static final String CACHE_DIR = "target/cache";
+
+    private static final String TEMP_DIR = "target/temp";
+
+    private static final String TARGET_DIR = "target";
+
+    private static final Logger LOG = LoggerFactory.getLogger(TestLocalCache.class);
+
+    @Override
+    protected void setUp() {
+        try {
+            File cachedir = new File(CACHE_DIR);
+            if (cachedir.exists()) FileUtil.delete(cachedir);
+            cachedir.mkdirs();
+
+            File tempdir = new File(TEMP_DIR);
+            if (tempdir.exists()) FileUtil.delete(tempdir);
+            tempdir.mkdirs();
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail();
+        }
+    }
+
+    @Override
+    protected void tearDown() throws IOException {
+        File cachedir = new File(CACHE_DIR);
+        if (cachedir.exists()) FileUtil.delete(cachedir);
+
+        File tempdir = new File(TEMP_DIR);
+        if (tempdir.exists()) FileUtil.delete(tempdir);
+    }
+
+    /**
+     * Test to validate store retrieve in cache.
+     */
+    public void testStoreRetrieve() {
+        try {
+            AsyncUploadCache pendingFiles = new AsyncUploadCache();
+            pendingFiles.init(TARGET_DIR, CACHE_DIR, 100);
+            pendingFiles.reset();
+            LocalCache cache = new LocalCache(CACHE_DIR, TEMP_DIR, 400, 0.95,
+                0.70, pendingFiles);
+            Random random = new Random(12345);
+            byte[] data = new byte[100];
+            Map<String, byte[]> byteMap = new HashMap<String, byte[]>();
+            random.nextBytes(data);
+            byteMap.put("a1", data);
+
+            data = new byte[100];
+            random.nextBytes(data);
+            byteMap.put("a2", data);
+
+            data = new byte[100];
+            random.nextBytes(data);
+            byteMap.put("a3", data);
+
+            cache.store("a1", new ByteArrayInputStream(byteMap.get("a1")));
+            cache.store("a2", new ByteArrayInputStream(byteMap.get("a2")));
+            cache.store("a3", new ByteArrayInputStream(byteMap.get("a3")));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a1")),
+                cache.getIfStored("a1"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a2")),
+                cache.getIfStored("a2"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a3")),
+                cache.getIfStored("a3"));
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail();
+        }
+    }
+
+    /**
+     * Test to verify cache's purging if cache current size exceeds
+     * cachePurgeTrigFactor * size.
+     */
+    public void testAutoPurge() {
+        try {
+            AsyncUploadCache pendingFiles = new AsyncUploadCache();
+            pendingFiles.init(TARGET_DIR, CACHE_DIR, 100);
+            pendingFiles.reset();
+            LocalCache cache = new LocalCache(CACHE_DIR, TEMP_DIR, 400, 0.95,
+                0.70, pendingFiles);
+            Random random = new Random(12345);
+            byte[] data = new byte[100];
+            Map<String, byte[]> byteMap = new HashMap<String, byte[]>();
+            random.nextBytes(data);
+            byteMap.put("a1", data);
+
+            data = new byte[100];
+            random.nextBytes(data);
+            byteMap.put("a2", data);
+
+            data = new byte[100];
+            random.nextBytes(data);
+            byteMap.put("a3", data);
+
+            data = new byte[100];
+            random.nextBytes(data);
+            byteMap.put("a4", data);
+
+            cache.store("a1", new ByteArrayInputStream(byteMap.get("a1")));
+            cache.store("a2", new ByteArrayInputStream(byteMap.get("a2")));
+            cache.store("a3", new ByteArrayInputStream(byteMap.get("a3")));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a1")),
+                cache.getIfStored("a1"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a2")),
+                cache.getIfStored("a2"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a3")),
+                cache.getIfStored("a3"));
+
+            data = new byte[90];
+            random.nextBytes(data);
+            byteMap.put("a4", data);
+            // storing a4 should purge cache
+            cache.store("a4", new ByteArrayInputStream(byteMap.get("a4")));
+            Thread.sleep(1000);
+            assertNull("a1 should be null", cache.getIfStored("a1"));
+            assertNull("a2 should be null", cache.getIfStored("a2"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a3")),
+                cache.getIfStored("a3"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a4")),
+                cache.getIfStored("a4"));
+            data = new byte[100];
+            random.nextBytes(data);
+            byteMap.put("a5", data);
+            cache.store("a5", new ByteArrayInputStream(byteMap.get("a5")));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a3")),
+                cache.getIfStored("a3"));
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail();
+        }
+    }
+
+    /**
+     * Test to verify cache's purging if cache current size exceeds
+     * cachePurgeTrigFactor * size.
+     */
+    public void testAutoPurgeWithPendingUpload() {
+        try {
+            AsyncUploadCache pendingFiles = new AsyncUploadCache();
+            pendingFiles.init(TARGET_DIR, CACHE_DIR, 100);
+            pendingFiles.reset();
+            LocalCache cache = new LocalCache(CACHE_DIR, TEMP_DIR, 400, 0.95,
+                0.70, pendingFiles);
+            Random random = new Random(12345);
+            byte[] data = new byte[125];
+            Map<String, byte[]> byteMap = new HashMap<String, byte[]>();
+            random.nextBytes(data);
+            byteMap.put("a1", data);
+
+            data = new byte[125];
+            random.nextBytes(data);
+            byteMap.put("a2", data);
+
+            data = new byte[125];
+            random.nextBytes(data);
+            byteMap.put("a3", data);
+
+            data = new byte[100];
+            random.nextBytes(data);
+            byteMap.put("a4", data);
+            File tempDir = new File(TEMP_DIR);
+            File f = File.createTempFile("test", "tmp", tempDir);
+            FileOutputStream fos = new FileOutputStream(f);
+            fos.write(byteMap.get("a1"));
+            fos.close();
+            AsyncUploadCacheResult result = cache.store("a1", f, true);
+            assertTrue("should be able to add to pending upload",
+                result.canAsyncUpload());
+
+            f = File.createTempFile("test", "tmp", tempDir);
+            fos = new FileOutputStream(f);
+            fos.write(byteMap.get("a2"));
+            fos.close();
+            result = cache.store("a2", f, true);
+            assertTrue("should be able to add to pending upload",
+                result.canAsyncUpload());
+
+            f = File.createTempFile("test", "tmp", tempDir);
+            fos = new FileOutputStream(f);
+            fos.write(byteMap.get("a3"));
+            fos.close();
+            result = cache.store("a3", f, true);
+            assertTrue("should be able to add to pending upload",
+                result.canAsyncUpload());
+
+            assertEquals(new ByteArrayInputStream(byteMap.get("a1")),
+                cache.getIfStored("a1"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a2")),
+                cache.getIfStored("a2"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a3")),
+                cache.getIfStored("a3"));
+
+            data = new byte[90];
+            random.nextBytes(data);
+            byteMap.put("a4", data);
+
+            f = File.createTempFile("test", "tmp", tempDir);
+            fos = new FileOutputStream(f);
+            fos.write(byteMap.get("a4"));
+            fos.close();
+
+            result = cache.store("a4", f, true);
+            assertFalse("should not be able to add to pending upload",
+                result.canAsyncUpload());
+            Thread.sleep(1000);
+
+            assertEquals(new ByteArrayInputStream(byteMap.get("a1")),
+                cache.getIfStored("a1"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a2")),
+                cache.getIfStored("a2"));
+            assertEquals(new ByteArrayInputStream(byteMap.get("a3")),
+                cache.getIfStored("a3"));
+            assertNull("a4 should be null", cache.getIfStored("a4"));
+        } catch (Exception e) {
+            LOG.error("error:", e);
+            fail();
+        }
+    }
+
+    /**
+     * Assert two inputstream
+     */
+    protected void assertEquals(InputStream a, InputStream b)
+            throws IOException {
+        while (true) {
+            int ai = a.read();
+            int bi = b.read();
+            assertEquals(ai, bi);
+            if (ai < 0) {
+                break;
+            }
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-data/src/test/java/org/apache/jackrabbit/core/data/TestLocalCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-data/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-data/src/test/resources/log4j.properties?rev=1577127&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-data/src/test/resources/log4j.properties (added)
+++ jackrabbit/trunk/jackrabbit-data/src/test/resources/log4j.properties Thu Mar 13 12:12:33
2014
@@ -0,0 +1,29 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+# this is the log4j configuration for the JCR API tests
+log4j.rootLogger=INFO, file
+
+#log4j.logger.org.apache.jackrabbit.test=DEBUG
+
+# 'file' is set to be a FileAppender.
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.File=target/debug.log
+
+# 'file' uses PatternLayout.
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss} *%-5p* [%t] %c{1}: %m
(%F, line %L)\n

Propchange: jackrabbit/trunk/jackrabbit-data/src/test/resources/log4j.properties
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message