package org.purl.wf4ever.robundle.fs;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.spi.FileSystemProvider;
import java.util.Collections;

import org.junit.Test;

public class TestFileSystemProvider {

	@Test
	public void installedProviders() throws Exception {
		for (FileSystemProvider provider : FileSystemProvider
				.installedProviders()) {
			if (provider instanceof BundleFileSystemProvider) {
				assertSame(provider, BundleFileSystemProvider.getInstance());
				return;
			}
		}
		fail("Could not find BundleFileSystemProvider as installed provider");
	}

	@Test
	public void newByURI() throws Exception {

		Path path = Files.createTempFile("test", "zip");
		BundleFileSystemProvider.createBundleAsZip(path, null);

		// HACK: Use a opaque version of widget: with the file URI as scheme
		// specific part
		URI w = new URI("widget", path.toUri().toASCIIString(), null);
		FileSystem fs = FileSystems.newFileSystem(w,
				Collections.<String, Object> emptyMap());
		assertTrue(fs instanceof BundleFileSystem);
	}

	@Test
	public void createFSfromZip() throws Exception {
		Path path = Files.createTempFile("test", null);
		Files.delete(path);
		BundleFileSystemProvider.createBundleAsZip(path, null);
		assertTrue(Files.exists(path));
	}

}

@@ -28,24 +28,58 @@
 bundle.fs;%0A%0A
+import static org.junit.Assert.*;%0A
 import stati
@@ -109,16 +109,16 @@
 rtSame;%0A
-
 import s
@@ -211,16 +211,49 @@
 et.URI;%0A
+import java.nio.charset.Charset;%0A
 import j
@@ -1352,23 +1352,33 @@
 oid 
-createFSfromZip
+newFileSystemFromExisting
 () t
@@ -1522,45 +1522,1122 @@
 th, 
-null);%0A%09%09assertTrue(Files.exists(path
+%22application/x-test%22);%0A%09%09assertTrue(Files.exists(path));%0A%09%09BundleFileSystem f = BundleFileSystemProvider.newFileSystemFromExisting(path);%0A%09%09assertEquals(%22application/x-test%22, Files.readAllLines(%0A%09%09%09%09f.getRootDirectory().resolve(%22mimetype%22), %0A%09%09%09%09Charset.forName(%22ASCII%22)).get(0));%0A%09%7D%0A%09%0A%09@Test%0A%09public void newFileSystemFromNewDefaultMime() throws Exception %7B%0A%09%09Path path = Files.createTempFile(%22test%22, null);%0A%09%09Files.delete(path);%0A%09%09BundleFileSystem f = BundleFileSystemProvider.newFileSystemFromNew(path);%0A%09%09assertTrue(Files.exists(path));%0A%09%09assertEquals(%22application/vnd.wf4ever.robundle+zip%22, Files.readAllLines(%0A%09%09%09%09f.getRootDirectory().resolve(%22mimetype%22), %0A%09%09%09%09Charset.forName(%22ASCII%22)).get(0));%0A%09%7D%0A%09%0A%09@Test%0A%09public void newFileSystemFromNew() throws Exception %7B%0A%09%09Path path = Files.createTempFile(%22test%22, null);%0A%09%09Files.delete(path);%0A%09%09BundleFileSystem f = BundleFileSystemProvider.newFileSystemFromNew(path, %22application/x-test2%22);%0A%09%09assertTrue(Files.exists(path));%0A%09%09assertEquals(%22application/x-test2%22, Files.readAllLines(%0A%09%09%09%09f.getRootDirectory().resolve(%22mimetype%22), %0A%09%09%09%09Charset.forName(%22ASCII%22)).get(0
 ));%0A

Test newFileSystemFromNew and newFileSystemFromExisting