package org.apache.clerezza.rdf.jena.tdb.storage;
/*
 *
 * 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.
 *
*/


import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import junit.framework.Assert;

import org.apache.clerezza.rdf.core.BNode;
import org.apache.clerezza.rdf.core.Literal;
import org.apache.clerezza.rdf.core.MGraph;
import org.apache.clerezza.rdf.core.Resource;
import org.apache.clerezza.rdf.core.Triple;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
import org.apache.felix.scr.annotations.Activate;
import org.junit.Before;
import org.junit.Test;
import org.wymiwyg.commons.util.Util;


public class MultiThreadedTest {
    
    
    
    /** 
     * how many threads to start
     */
    private static final int THREAD_COUNT = 100;
    /**
     * how many seconds to let them run
     */
    private static final int DELAY = 120;
    
    
    private MGraph mGraph;
    private Set<Triple> testTriples = Collections.synchronizedSet(new HashSet<Triple>()); 

    class TestThread extends Thread {

        private final int id;
        private boolean stopRequested;
        private int addedTripleCount = 0;

        public TestThread(final int id) {
            this.id = id;
            start();
        }

        public void requestStop() {
            stopRequested = true;
        }

        @Override
        public void run() {
            while (!stopRequested) {
                Literal randomLiteral = new PlainLiteralImpl(Util.createRandomString(22));
                Triple triple = new TripleImpl(new BNode(), new UriRef("http://example.com/property"), randomLiteral);
                mGraph.add(triple);
                addedTripleCount++;
                if ((addedTripleCount % 100) == 0) {
                    testTriples.add(triple);
                }
            }
        }

        public int getAddedTripleCount() {
            return addedTripleCount;
        }

    }

    
    
    @Before
    public void setUp() throws IOException {
        File tempFile;
        UriRef MGRAPHNAME = new UriRef("http://text.example.org/");
        TdbTcProvider tdbTcProvider;
        tempFile = File.createTempFile("tdbtest", null);
        tempFile.delete();
        tempFile.mkdirs();
        tdbTcProvider = new TdbTcProvider(tempFile);
        tdbTcProvider.activate(null);
        mGraph = tdbTcProvider.createMGraph(MGRAPHNAME);
    }

    @Test
    public void perform() throws InterruptedException {
        TestThread[] threads =  new TestThread[THREAD_COUNT];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new TestThread(i);
        }
        Thread.sleep(DELAY*1000);
        for (TestThread testThread : threads) {
            testThread.requestStop();
        }
        for (TestThread testThread : threads) {
            testThread.join();
        }
        int addedTriples = 0;
        for (TestThread testThread : threads) {
            addedTriples += testThread.getAddedTripleCount();
        }
        Assert.assertEquals(addedTriples, mGraph.size());
        for (Triple testTriple : testTriples) {
            Assert.assertTrue(mGraph.contains(testTriple));
        }
    }

}

@@ -990,39 +990,8 @@
 t;%0A%0A
-import junit.framework.Assert;%0A
 %0Aimp
@@ -1415,16 +1415,41 @@
 tivate;%0A
+import org.junit.Assert;%0A
 import o
@@ -1670,17 +1670,17 @@
 COUNT = 
-1
+2
 00;%0A    
@@ -2051,16 +2051,50 @@
 unt = 0;
+%0A        private Object exception;
 %0A%0A      
@@ -2347,24 +2347,50 @@
 equested) %7B%0A
+                try %7B%0A    
             
@@ -2464,24 +2464,28 @@
 tring(22));%0A
+    
             
@@ -2603,24 +2603,28 @@
             
+    
 mGraph.add(t
@@ -2639,32 +2639,36 @@
                 
+  
+  
 addedTripleCount
@@ -2671,16 +2671,20 @@
 ount++;%0A
+    
         
@@ -2744,24 +2744,28 @@
             
+    
 testTriples.
@@ -2769,32 +2769,129 @@
 es.add(triple);%0A
+                    %7D%0A                %7D catch (Exception e) %7B%0A                    exception = e;%0A
                 
@@ -4180,24 +4180,24 @@
 tTriples) %7B%0A
-
             
@@ -4250,21 +4250,132 @@
 ;%0A        %7D%0A
+        for (TestThread testThread : threads) %7B%0A            Assert.assertNull(testThread.exception);%0A        %7D%0A
     %7D%0A%0A%7D%0A

make the test fail whenever there's an exception