/*
 * Copyright 2012-2014 the original author or authors.
 *
 * Licensed 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.springframework.boot.loader;

import java.io.File;
import java.net.URL;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.springframework.boot.loader.jar.JarFile;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * Tests for {@link LaunchedURLClassLoader}.
 *
 * @author Dave Syer
 * @author Phillip Webb
 */
@SuppressWarnings("resource")
public class LaunchedURLClassLoaderTests {

	@Rule
	public TemporaryFolder temporaryFolder = new TemporaryFolder();

	@Test
	public void resolveResourceFromWindowsFilesystem() throws Exception {
		// This path is invalid - it should return null even on Windows.
		// A regular URLClassLoader will deal with it gracefully.
		assertNull(getClass().getClassLoader()
				.getResource("c:\\Users\\user\\bar.properties"));
		LaunchedURLClassLoader loader = new LaunchedURLClassLoader(
				new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") },
				getClass().getClassLoader());
		// So we should too...
		assertNull(loader.getResource("c:\\Users\\user\\bar.properties"));
	}

	@Test
	public void resolveResourceFromArchive() throws Exception {
		LaunchedURLClassLoader loader = new LaunchedURLClassLoader(
				new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") },
				getClass().getClassLoader());
		assertNotNull(loader.getResource("demo/Application.java"));
	}

	@Test
	public void resolveResourcesFromArchive() throws Exception {
		LaunchedURLClassLoader loader = new LaunchedURLClassLoader(
				new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") },
				getClass().getClassLoader());
		assertTrue(loader.getResources("demo/Application.java").hasMoreElements());
	}

	@Test
	public void resolveRootPathFromArchive() throws Exception {
		LaunchedURLClassLoader loader = new LaunchedURLClassLoader(
				new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") },
				getClass().getClassLoader());
		assertNotNull(loader.getResource(""));
	}

	@Test
	public void resolveRootResourcesFromArchive() throws Exception {
		LaunchedURLClassLoader loader = new LaunchedURLClassLoader(
				new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") },
				getClass().getClassLoader());
		assertTrue(loader.getResources("").hasMoreElements());
	}

	@Test
	public void resolveFromNested() throws Exception {
		File file = this.temporaryFolder.newFile();
		TestJarCreator.createTestJar(file);
		JarFile jarFile = new JarFile(file);
		URL url = jarFile.getUrl();
		LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { url },
				null);
		URL resource = loader.getResource("nested.jar!/3.dat");
		assertThat(resource.toString(), equalTo(url + "nested.jar!/3.dat"));
		assertThat(resource.openConnection().getInputStream().read(), equalTo(3));
	}

}

@@ -21,9 +21,9 @@
 -201
-4
+6
  the
@@ -1158,16 +1158,42 @@
 ip Webb%0A
+ * @author Andy Wilkinson%0A
  */%0A@Sup
@@ -3623,10 +3623,642 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void resolveFromNestedWhileThreadIsInterrupted() throws Exception %7B%0A%09%09File file = this.temporaryFolder.newFile();%0A%09%09TestJarCreator.createTestJar(file);%0A%09%09JarFile jarFile = new JarFile(file);%0A%09%09URL url = jarFile.getUrl();%0A%09%09LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL%5B%5D %7B url %7D,%0A%09%09%09%09null);%0A%09%09try %7B%0A%09%09%09Thread.currentThread().interrupt();%0A%09%09%09URL resource = loader.getResource(%22nested.jar!/3.dat%22);%0A%09%09%09assertThat(resource.toString(), equalTo(url + %22nested.jar!/3.dat%22));%0A%09%09%09assertThat(resource.openConnection().getInputStream().read(), equalTo(3));%0A%09%09%7D%0A%09%09finally %7B%0A%09%09%09Thread.interrupted();%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

Test that LaunchedURLClassLoader works when thread is interrupted