AttributeNormalizer.normalize() now supports URI

This commit is contained in:
Joakim Erdfelt 2016-02-12 09:29:49 -07:00
parent bc59c0853d
commit 30b5fe1c96
3 changed files with 279 additions and 211 deletions

View File

@ -123,6 +123,11 @@ public class AttributeNormalizer
return 1;
}
if( (o1.path == null) && (o2.path == null) )
{
return 0;
}
// Different lengths?
int diff = o2.path.getNameCount() - o1.path.getNameCount();
if(diff != 0)
@ -200,7 +205,13 @@ public class AttributeNormalizer
{
return "file:" + normalizePath(new File(uri).toPath());
}
else
{
if(uri.isAbsolute())
{
return normalizeUri(uri);
}
}
}
catch (Exception e)
{
@ -209,6 +220,17 @@ public class AttributeNormalizer
return String.valueOf(o);
}
public String normalizeUri(URI uri)
{
String uriStr = uri.toASCIIString();
String warStr = warURI.toASCIIString();
if (uriStr.startsWith(warStr))
{
return "${WAR}" + uriStr.substring(warStr.length());
}
return uriStr;
}
public String normalizePath(Path path)
{
for (PathAttribute attr : attributes)

View File

@ -0,0 +1,244 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.quickstart;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class AttributeNormalizerPathTest
{
@Parameters(name="{0} = {1}")
public static List<String[]> data()
{
String[][] tests = {
// Can't test 'WAR' property, as its not a Path (which this testcase works with)
// { "WAR", toSystemPath("http://localhost/resources/webapps/root") },
{ "jetty.home", toSystemPath("/opt/jetty-distro") },
{ "jetty.base", toSystemPath("/opt/jetty-distro/demo.base") },
{ "user.home", toSystemPath("/home/user") },
{ "user.dir", toSystemPath("/etc/init.d") },
};
return Arrays.asList(tests);
}
/**
* As the declared paths in this testcase might be actual paths on the system
* running these tests, the expected paths should be cleaned up to represent
* the actual system paths.
* <p>
* Eg: on fedora /etc/init.d is a symlink to /etc/rc.d/init.d
*/
private static String toSystemPath(String rawpath)
{
Path path = FileSystems.getDefault().getPath(rawpath);
if (Files.exists(path))
{
// It exists, resolve it to the real path
try
{
path = path.toRealPath();
}
catch (IOException e)
{
// something prevented us from resolving to real path, fallback to
// absolute path resolution (not as accurate)
path = path.toAbsolutePath();
e.printStackTrace();
}
}
else
{
// File doesn't exist, resolve to absolute path
// We can't rely on File.toCanonicalPath() here
path = path.toAbsolutePath();
}
return path.toString();
}
private static String origJettyBase;
private static String origJettyHome;
private static String origUserHome;
private static String origUserDir;
@BeforeClass
public static void initProperties()
{
origJettyBase = System.getProperty("jetty.base");
origJettyHome = System.getProperty("jetty.home");
origUserHome = System.getProperty("user.home");
origUserDir = System.getProperty("user.dir");
System.setProperty("jetty.home","/opt/jetty-distro");
System.setProperty("jetty.base","/opt/jetty-distro/demo.base");
System.setProperty("user.home","/home/user");
System.setProperty("user.dir","/etc/init.d");
}
@AfterClass
public static void restoreProperties()
{
if(origJettyBase != null) System.setProperty("jetty.base",origJettyBase);
if(origJettyHome != null) System.setProperty("jetty.home",origJettyHome);
if(origUserHome != null) System.setProperty("user.home",origUserHome);
if(origUserDir != null) System.setProperty("user.dir",origUserDir);
}
@Parameter(0)
public String key;
@Parameter(1)
public String path;
private AttributeNormalizer normalizer;
public AttributeNormalizerPathTest() throws MalformedURLException
{
normalizer = new AttributeNormalizer(Resource.newResource("/opt/jetty-distro/demo.base/webapps/root"));
}
@Test
public void testEqual()
{
assertThat(normalizer.normalize("file:" + path), is("file:${" + key + "}"));
}
@Test
public void testEqualsSlash()
{
assertThat(normalizer.normalize("file:" + path + "/"), is("file:${" + key + "}"));
}
@Test
public void testEqualsSlashFile()
{
assertThat(normalizer.normalize("file:" + path + "/file"), is("file:${" + key + "}/file"));
}
@Test
public void testURIEquals() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("file:" + path)), is("file:${" + key + "}"));
}
@Test
public void testURIEqualsSlash() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("file:" + path + "/")), is("file:${" + key + "}"));
}
@Test
public void testURIEqualsSlashFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("file:" + path + "/file")), is("file:${" + key + "}/file"));
}
@Test
public void testURLEquals() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("file:" + path)), is("file:${" + key + "}"));
}
@Test
public void testURLEqualsSlash() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("file:" + path + "/")), is("file:${" + key + "}"));
}
@Test
public void testURLEqualsSlashFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("file:" + path + "/file")), is("file:${" + key + "}/file"));
}
@Test
public void testJarFileEquals_BangFile()
{
assertThat(normalizer.normalize("jar:file:" + path + "!/file"), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_SlashBangFile()
{
assertThat(normalizer.normalize("jar:file:" + path + "/!/file"), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_FileBangFile()
{
assertThat(normalizer.normalize("jar:file:" + path + "/file!/file"), is("jar:file:${" + key + "}/file!/file"));
}
@Test
public void testJarFileEquals_URIBangFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("jar:file:" + path + "!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URISlashBangFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("jar:file:" + path + "/!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URIFileBangFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("jar:file:" + path + "/file!/file")), is("jar:file:${" + key + "}/file!/file"));
}
@Test
public void testJarFileEquals_URLBangFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("jar:file:" + path + "!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URLSlashBangFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("jar:file:" + path + "/!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URLFileBangFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("jar:file:" + path + "/file!/file")), is("jar:file:${" + key + "}/file!/file"));
}
}

View File

@ -1,4 +1,4 @@
//
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
@ -21,224 +21,26 @@ package org.eclipse.jetty.quickstart;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class AttributeNormalizerTest
{
@Parameters(name="{0} = {1}")
public static List<String[]> data()
{
String[][] tests = {
// Can't test 'WAR' property, as its not a Path (which this testcase works with)
// { "WAR", toSystemPath("http://localhost/resources/webapps/root") },
{ "jetty.home", toSystemPath("/opt/jetty-distro") },
{ "jetty.base", toSystemPath("/opt/jetty-distro/demo.base") },
{ "user.home", toSystemPath("/home/user") },
{ "user.dir", toSystemPath("/etc/init.d") },
};
return Arrays.asList(tests);
}
/**
* As the declared paths in this testcase might be actual paths on the system
* running these tests, the expected paths should be cleaned up to represent
* the actual system paths.
* <p>
* Eg: on fedora /etc/init.d is a symlink to /etc/rc.d/init.d
*/
private static String toSystemPath(String rawpath)
{
Path path = FileSystems.getDefault().getPath(rawpath);
if (Files.exists(path))
{
// It exists, resolve it to the real path
try
{
path = path.toRealPath();
}
catch (IOException e)
{
// something prevented us from resolving to real path, fallback to
// absolute path resolution (not as accurate)
path = path.toAbsolutePath();
e.printStackTrace();
}
}
else
{
// File doesn't exist, resolve to absolute path
// We can't rely on File.toCanonicalPath() here
path = path.toAbsolutePath();
}
return path.toString();
}
private static String origJettyBase;
private static String origJettyHome;
private static String origUserHome;
private static String origUserDir;
@BeforeClass
public static void initProperties()
{
origJettyBase = System.getProperty("jetty.base");
origJettyHome = System.getProperty("jetty.home");
origUserHome = System.getProperty("user.home");
origUserDir = System.getProperty("user.dir");
System.setProperty("jetty.home","/opt/jetty-distro");
System.setProperty("jetty.base","/opt/jetty-distro/demo.base");
System.setProperty("user.home","/home/user");
System.setProperty("user.dir","/etc/init.d");
}
@AfterClass
public static void restoreProperties()
{
if(origJettyBase != null) System.setProperty("jetty.base",origJettyBase);
if(origJettyHome != null) System.setProperty("jetty.home",origJettyHome);
if(origUserHome != null) System.setProperty("user.home",origUserHome);
if(origUserDir != null) System.setProperty("user.dir",origUserDir);
}
@Parameter(0)
public String key;
@Parameter(1)
public String path;
private AttributeNormalizer normalizer;
public AttributeNormalizerTest() throws MalformedURLException
{
normalizer = new AttributeNormalizer(Resource.newResource("/opt/jetty-distro/demo.base/webapps/root"));
}
@Test
public void testEqual()
public void testNormalizeWAR() throws MalformedURLException
{
assertThat(normalizer.normalize("file:" + path), is("file:${" + key + "}"));
}
String webref = "http://localhost/resource/webapps/root";
Resource webresource = Resource.newResource(webref);
AttributeNormalizer normalizer = new AttributeNormalizer(webresource);
String result = null;
@Test
public void testEqualsSlash()
{
assertThat(normalizer.normalize("file:" + path + "/"), is("file:${" + key + "}"));
}
result = normalizer.normalize(URI.create(webref));
assertThat(result, is("${WAR}"));
@Test
public void testEqualsSlashFile()
{
assertThat(normalizer.normalize("file:" + path + "/file"), is("file:${" + key + "}/file"));
}
@Test
public void testURIEquals() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("file:" + path)), is("file:${" + key + "}"));
}
@Test
public void testURIEqualsSlash() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("file:" + path + "/")), is("file:${" + key + "}"));
}
@Test
public void testURIEqualsSlashFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("file:" + path + "/file")), is("file:${" + key + "}/file"));
}
@Test
public void testURLEquals() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("file:" + path)), is("file:${" + key + "}"));
}
@Test
public void testURLEqualsSlash() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("file:" + path + "/")), is("file:${" + key + "}"));
}
@Test
public void testURLEqualsSlashFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("file:" + path + "/file")), is("file:${" + key + "}/file"));
}
@Test
public void testJarFileEquals_BangFile()
{
assertThat(normalizer.normalize("jar:file:" + path + "!/file"), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_SlashBangFile()
{
assertThat(normalizer.normalize("jar:file:" + path + "/!/file"), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_FileBangFile()
{
assertThat(normalizer.normalize("jar:file:" + path + "/file!/file"), is("jar:file:${" + key + "}/file!/file"));
}
@Test
public void testJarFileEquals_URIBangFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("jar:file:" + path + "!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URISlashBangFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("jar:file:" + path + "/!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URIFileBangFile() throws URISyntaxException
{
assertThat(normalizer.normalize(new URI("jar:file:" + path + "/file!/file")), is("jar:file:${" + key + "}/file!/file"));
}
@Test
public void testJarFileEquals_URLBangFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("jar:file:" + path + "!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URLSlashBangFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("jar:file:" + path + "/!/file")), is("jar:file:${" + key + "}!/file"));
}
@Test
public void testJarFileEquals_URLFileBangFile() throws MalformedURLException
{
assertThat(normalizer.normalize(new URL("jar:file:" + path + "/file!/file")), is("jar:file:${" + key + "}/file!/file"));
result = normalizer.normalize(URI.create(webref + "/deep/ref"));
assertThat(result, is("${WAR}/deep/ref"));
}
}