package com.gentics.mesh.core;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.gentics.mesh.cli.Mesh;
import com.gentics.mesh.error.MeshSchemaException;
import com.gentics.mesh.test.AbstractDBTest;

public class DatabaseTest extends AbstractDBTest {

	private File outputDirectory;

	@Before
	public void createOutputDirectory() throws JsonParseException, JsonMappingException, IOException, MeshSchemaException {
		outputDirectory = new File("target", "tmp_" + System.currentTimeMillis());
		outputDirectory.mkdirs();
		Mesh.mesh().getOptions().getStorageOptions().setDirectory(new File(outputDirectory, "graphdb").getAbsolutePath());
		db.reset();
		setupData();
	}

	@After
	public void cleanup() throws IOException {
		FileUtils.deleteDirectory(outputDirectory);
	}

	@Test
	public void testExport() throws IOException {
		db.exportGraph(outputDirectory.getAbsolutePath());
	}

	@Test
	public void testImport() throws IOException {
		db.exportGraph(outputDirectory.getAbsolutePath());
		db.importGraph(outputDirectory.listFiles()[0].getAbsolutePath());
	}

	@Test
	public void testBackup() throws IOException {
		db.backupGraph(outputDirectory.getAbsolutePath());
	}

	@Test
	public void testRestore() throws IOException {
		db.backupGraph(outputDirectory.getAbsolutePath());
		db.restoreGraph(outputDirectory.listFiles()[0].getAbsolutePath());
	}

}

@@ -25,16 +25,62 @@
 .core;%0A%0A
+import static org.junit.Assert.assertEquals;%0A%0A
 import j
@@ -425,32 +425,69 @@
 chemaException;%0A
+import com.gentics.mesh.graphdb.Trx;%0A
 import com.genti
@@ -1549,127 +1549,574 @@
 %7B%0A%09%09
-db.backupGraph(outputDirectory.getAbsolutePath());%0A%09%09db.restoreGraph(outputDirectory.listFiles()%5B0%5D.getAbsolutePath());
+String name = %22username%22;%0A%09%09try (Trx tx = new Trx(db)) %7B%0A%09%09%09user().setUsername(name);%0A%09%09%09tx.success();%0A%09%09%7D%0A%09%09try (Trx tx = new Trx(db)) %7B%0A%09%09%09assertEquals(name, user().getUsername());%0A%09%09%7D%0A%09%09db.backupGraph(outputDirectory.getAbsolutePath());%0A%09%09try (Trx tx = new Trx(db)) %7B%0A%09%09%09user().setUsername(%22changed%22);%0A%09%09%09tx.success();%0A%09%09%7D%0A%09%09try (Trx tx = new Trx(db)) %7B%0A%09%09%09assertEquals(%22changed%22, user().getUsername());%0A%09%09%7D%0A%09%09db.restoreGraph(outputDirectory.listFiles()%5B0%5D.getAbsolutePath());%0A%09%09try (Trx tx = new Trx(db)) %7B%0A%09%09%09assertEquals(%22username%22, user().getUsername());%0A%09%09%7D%0A
 %0A%09%7D%0A

Improve testcase