/*
 * 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.jena.tdb2.sys;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.apache.commons.io.FileUtils;
import org.apache.jena.atlas.lib.FileOps;
import org.apache.jena.dboe.base.file.Location;
import org.apache.jena.dboe.sys.IO_DB;
import org.apache.jena.graph.Triple;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.sse.SSE;
import org.apache.jena.system.Txn;
import org.apache.jena.tdb2.ConfigTest;
import org.apache.jena.tdb2.DatabaseMgr;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/** Test DatabaseOp - the compaction tests are in {@link TestDatabaseCompact}. */
public class TestDatabaseOps
{
    private Location dir = null;

    static Quad quad1 = SSE.parseQuad("(_ <s> <p> 1)");
    static Quad quad2 = SSE.parseQuad("(_ _:a <p> 2)");
    static Triple triple1 = quad1.asTriple();
    static Triple triple2 = quad2.asTriple();
    static Triple triple3 = SSE.parseTriple("(<s> <q> 3)");

    @Before
    public void before() {
        String DIR = ConfigTest.getCleanDir();
        FileOps.ensureDir(DIR);
        FileOps.clearAll(DIR);
        dir = Location.create(DIR);
    }

    @After
    public void after() {
        TDBInternal.reset();
        FileUtils.deleteQuietly(IO_DB.asFile(dir));
    }

    @Test public void backup_1() {
        DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir);
        Txn.executeWrite(dsg, ()-> {
            dsg.add(quad2);
            dsg.add(quad1);
        });
        String file1 = DatabaseMgr.backup(dsg);
        DatasetGraph dsg2 = RDFDataMgr.loadDatasetGraph(file1);
        Txn.executeRead(dsg, ()-> {
            assertTrue(dsg.contains(quad1));
            assertEquals(2, dsg.getDefaultGraph().size());
            assertTrue(dsg2.getDefaultGraph().isIsomorphicWith(dsg.getDefaultGraph()));
        });
        String file2 = DatabaseMgr.backup(dsg);
        assertNotEquals(file1, file2);
    }

}

@@ -972,16 +972,38 @@
 tTrue;%0A%0A
+import java.io.File;%0A%0A
 import o
@@ -1128,47 +1128,8 @@
 on;%0A
-import org.apache.jena.dboe.sys.IO_DB;%0A
 impo
@@ -1369,48 +1369,8 @@
 xn;%0A
-import org.apache.jena.tdb2.ConfigTest;%0A
 impo
@@ -1451,37 +1451,75 @@
 nit.
-Before;%0Aimport org.junit.Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder
 ;%0A%0A/
@@ -1634,42 +1634,8 @@
 s%0A%7B%0A
-    private Location dir = null;%0A%0A
     
@@ -1903,98 +1903,186 @@
     
-@Before%0A    public void before() %7B%0A        String DIR = ConfigTest.getCleanDir();%0A  
+private static final String testingDirBackup = %22target/tdb-testing/Backup%22;%0A    private static final File testingDirBackupFile = new File(testingDirBackup);%0A    static %7B%0A
       
+  
 File
@@ -2095,19 +2095,24 @@
 sureDir(
-DIR
+%22target%22
 );%0A     
@@ -2126,56 +2126,83 @@
 Ops.
-clearAll(DIR);%0A        dir = Location.create(DIR
+ensureDir(%22target/tdb-testing%22);%0A        FileOps.ensureDir(testingDirBackup
 );%0A 
@@ -2309,69 +2309,230 @@
 tly(
-IO_DB.asFile(dir));%0A    %7D%0A%0A    @Test public void backup_1() %7B
+testingDirBackupFile);%0A    %7D%0A%0A    @Rule%0A    public TemporaryFolder folder = new TemporaryFolder(testingDirBackupFile);%0A%0A    @Test public void backup_1() %7B%0A        Location dir = Location.create(folder.getRoot().getPath());
 %0A   
@@ -3143,11 +3143,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Use JUnit TemproraryFolder in backup test