/*
 * Copyright 2012-2016 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.jar;

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

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

import org.springframework.boot.loader.TestJarCreator;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link JarURLConnection}.
 *
 * @author Andy Wilkinson
 */
public class JarURLConnectionTests {

	@Rule
	public TemporaryFolder temporaryFolder = new TemporaryFolder(new File("target"));

	private File rootJarFile;

	private JarFile jarFile;

	@Before
	public void setup() throws Exception {
		this.rootJarFile = this.temporaryFolder.newFile();
		TestJarCreator.createTestJar(this.rootJarFile);
		this.jarFile = new JarFile(this.rootJarFile);
	}

	@Test
	public void connectionToRootUsingAbsoluteUrl() throws Exception {
		URL absoluteUrl = new URL(
				"jar:file:" + this.rootJarFile.getAbsoluteFile() + "!/");
		assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getContent())
				.isSameAs(this.jarFile);
	}

	@Test
	public void connectionToRootUsingRelativeUrl() throws Exception {
		URL relativeUrl = new URL("jar:file:" + this.rootJarFile + "!/");
		assertThat(new JarURLConnection(relativeUrl, this.jarFile).getContent())
				.isSameAs(this.jarFile);
	}

	@Test
	public void connectionToEntryUsingAbsoluteUrl() throws Exception {
		URL absoluteUrl = new URL(
				"jar:file:" + this.rootJarFile.getAbsoluteFile() + "!/1.dat");
		assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getInputStream())
				.hasSameContentAs(new ByteArrayInputStream(new byte[] { 1 }));
	}

	@Test
	public void connectionToEntryUsingRelativeUrl() throws Exception {
		URL relativeUrl = new URL("jar:file:" + this.rootJarFile + "!/1.dat");
		assertThat(new JarURLConnection(relativeUrl, this.jarFile).getInputStream())
				.hasSameContentAs(new ByteArrayInputStream(new byte[] { 1 }));
	}

	@Test
	public void connectionToEntryUsingAbsoluteUrlWithFileColonSlashSlashPrefix()
			throws Exception {
		URL absoluteUrl = new URL(
				"jar:file:/" + this.rootJarFile.getAbsoluteFile() + "!/1.dat");
		assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getInputStream())
				.hasSameContentAs(new ByteArrayInputStream(new byte[] { 1 }));
	}

	@Test
	public void connectionToEntryUsingAbsoluteUrlForNestedEntry() throws Exception {
		URL absoluteUrl = new URL(
				"jar:file:" + this.rootJarFile.getAbsoluteFile() + "!/nested.jar!/3.dat");
		assertThat(new JarURLConnection(absoluteUrl, this.jarFile).getInputStream())
				.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
	}

	@Test
	public void connectionToEntryUsingRelativeUrlForNestedEntry() throws Exception {
		URL relativeUrl = new URL("jar:file:" + this.rootJarFile + "!/nested.jar!/3.dat");
		assertThat(new JarURLConnection(relativeUrl, this.jarFile).getInputStream())
				.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
	}

}

@@ -1526,37 +1526,32 @@
 teUrl = new URL(
-%0A%09%09%09%09
 %22jar:file:%22 + th
@@ -1548,33 +1548,16 @@
 ile:%22 + 
-this.rootJarFile.
 getAbsol
@@ -1551,36 +1551,36 @@
 :%22 + getAbsolute
-File
+Path
 () + %22!/%22);%0A%09%09as
@@ -1799,32 +1799,33 @@
 ile:%22 + 
-this.rootJarFile
+getRelativePath()
  + %22!/%22)
@@ -2029,37 +2029,32 @@
 teUrl = new URL(
-%0A%09%09%09%09
 %22jar:file:%22 + th
@@ -2051,33 +2051,16 @@
 ile:%22 + 
-this.rootJarFile.
 getAbsol
@@ -2054,36 +2054,36 @@
 :%22 + getAbsolute
-File
+Path
 () + %22!/1.dat%22);
@@ -2350,32 +2350,33 @@
 ile:%22 + 
-this.rootJarFile
+getRelativePath()
  + %22!/1.
@@ -2659,37 +2659,32 @@
 teUrl = new URL(
-%0A%09%09%09%09
 %22jar:file:/%22 + t
@@ -2682,33 +2682,16 @@
 le:/%22 + 
-this.rootJarFile.
 getAbsol
@@ -2685,36 +2685,36 @@
 /%22 + getAbsolute
-File
+Path
 () + %22!/1.dat%22);
@@ -3004,25 +3004,8 @@
 %22 + 
-this.rootJarFile.
 getA
@@ -3011,20 +3011,20 @@
 Absolute
-File
+Path
 () + %22!/
@@ -3303,32 +3303,37 @@
 veUrl = new URL(
+%0A%09%09%09%09
 %22jar:file:%22 + th
@@ -3330,32 +3330,33 @@
 ile:%22 + 
-this.rootJarFile
+getRelativePath()
  + %22!/ne
@@ -3524,10 +3524,210 @@
 ));%0A%09%7D%0A%0A
+%09private String getAbsolutePath() %7B%0A%09%09return this.rootJarFile.getAbsolutePath().replace('%5C%5C', '/');%0A%09%7D%0A%0A%09private String getRelativePath() %7B%0A%09%09return this.rootJarFile.getPath().replace('%5C%5C', '/');%0A%09%7D%0A%0A
 %7D%0A

Fix JarURLConnectionTests on Windows