package dev;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.Iterator;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openjena.atlas.lib.FileOps;
import org.openjena.riot.Lang;
import org.openjena.riot.RiotLoader;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.ReadWrite;
import com.hp.hpl.jena.sparql.core.DatasetGraph;
import com.hp.hpl.jena.sparql.core.Quad;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.tdb.base.file.Location;

public class TestTDBCorruption {

    private static final String path = "target/tdb_corruption";
    private static final String str_literal = "Hello \\uDAE0 World";
    private static final String str_triple = "<s> <p> \"" + str_literal + "\" .";
   
    @Test public void test() {
        Location location = new Location ( path );
        Dataset dataset = TDBFactory.createDataset ( location );
        dataset.begin ( ReadWrite.WRITE );
        try {
            DatasetGraph dsg = dataset.asDatasetGraph();
            DatasetGraph dsg2 = RiotLoader.datasetFromString ( str_triple, Lang.TURTLE, null );
            Iterator<Quad> quads = dsg2.find();
            while ( quads.hasNext() ) {
                Quad quad = quads.next();
                dsg.add(quad);
            }
            dataset.commit();
        } catch ( Exception e ) {
            e.printStackTrace(System.err);
            if ( dataset.isInTransaction() ) dataset.abort();
        } finally {
            dataset.end();
        }
        assertEquals ( 1, dataset.getDefaultModel().size() );
    }
    
}

@@ -1,8 +1,816 @@
+/**%0A * Licensed to the Apache Software Foundation (ASF) under one%0A * or more contributor license agreements.  See the NOTICE file%0A * distributed with this work for additional information%0A * regarding copyright ownership.  The ASF licenses this file%0A * to you under the Apache License, Version 2.0 (the%0A * %22License%22); you may not use this file except in compliance%0A * with the License.  You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -865,29 +865,8 @@
 s;%0A%0A
-import java.io.File;%0A
 impo
@@ -910,81 +910,12 @@
 nit.
-BeforeClass;%0Aimport org.junit.Test;%0Aimport org.openjena.atlas.lib.FileOps
+Test
 ;%0Aim
@@ -1176,26 +1176,31 @@
 ena.tdb.
-TDBFactory
+StoreConnection
 ;%0Aimport
@@ -1580,24 +1580,174 @@
  );%0A        
+%0A        //%0A        // This is wrong: if transaction are needed, %0A        // StoreConnection must be used instead of TDBFactory%0A        //%0A        // 
 Dataset data
@@ -1794,24 +1794,27 @@
 n );%0A       
+ //
  dataset.beg
@@ -1833,24 +1833,177 @@
 te.WRITE );%0A
+        // %0A        %0A        StoreConnection sc = StoreConnection.make ( location );%0A        Dataset dataset = sc.begin ( ReadWrite.WRITE ).toDataset();%0A
         try 

Use StoreConnection not TDBFactory.