Skip to content

Commit

Permalink
Minor quality improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
essiembre committed May 7, 2023
1 parent 62e4ab5 commit 261be46
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ public static CachedInputStream cache(
if (is == null) {
return null;
}
if (is instanceof CachedInputStream) {
return (CachedInputStream) is;
if (is instanceof CachedInputStream cis) {
return cis;
}
return Optional.ofNullable(streamFactory)
.orElseGet(CachedStreamFactory::new).newInputStream(is);
Expand Down Expand Up @@ -539,13 +539,13 @@ public long getMemCacheSize() {
public int length() {
if (length == UNDEFINED_LENGTH) {
LOG.debug("""
Obtaining stream length before a stream\s\
of unknown lenght was fully read.\s\
This forces a full\s\
read just to get the length. To avoid this extra\s\
read cycle, consider calling\s\
the length() method after the stream has been\s\
fully read at least once through regular usage.""");
Obtaining stream length before a stream\s\
of unknown lenght was fully read.\s\
This forces a full\s\
read just to get the length. To avoid this extra\s\
read cycle, consider calling\s\
the length() method after the stream has been\s\
fully read at least once through regular usage.""");

// Reset marking
var savedPos = pos;
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/com/norconex/commons/lang/url/HttpURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public HttpURL(String url, String encoding) {
this.encoding = encoding;
}

String u = StringUtils.trimToEmpty(url);
var u = StringUtils.trimToEmpty(url);
if (u.matches("[a-zA-Z][a-zA-Z0-9\\+\\-\\.]*:.*")) {
URL urlwrap;
try {
Expand Down Expand Up @@ -267,7 +267,7 @@ public String getLastPathSegment() {
if (StringUtils.isBlank(path)) {
return StringUtils.EMPTY;
}
String segment = path;
var segment = path;
return StringUtils.substringAfterLast(segment, "/");
}
/**
Expand All @@ -277,7 +277,7 @@ public String getLastPathSegment() {
* @throws URLException when URL is malformed
*/
public URL toURL() {
String url = toString();
var url = toString();
try {
return new URL(url);
} catch (MalformedURLException e) {
Expand All @@ -286,10 +286,12 @@ public URL toURL() {
}

/**
* Gets the root of this HttpUrl. That is the left part of a URL up to
* and including the host name. If there are no root, <code>null</code>
* is returned.
* @return left part of a URL up to (and including the host name) or
* <p>Gets the root of a URL. That is the left part of a URL up to and
* including the host name. If a URL is only made of the scheme and
* host name, it is returned unchanged.
* A <code>null</code> or empty string returns
* a <code>null</code> document root.
* @return left part of a URL up to (and including) the host name or
* <code>null</code>
* @throws URLException when URL is malformed
* @since 1.8.0
Expand All @@ -306,7 +308,7 @@ public String getRoot() {
* @throws URLException when URL is malformed
*/
public URI toURI() {
String url = toString();
var url = toString();
try {
return new URI(url);
} catch (URISyntaxException e) {
Expand Down Expand Up @@ -345,20 +347,22 @@ public static URI toURI(String url) {

/**
* <p>Gets the root of a URL. That is the left part of a URL up to and
* including the host name. A <code>null</code> or empty string returns
* including the host name. If a URL is only made of the scheme and
* host name, it is returned unchanged.
* A <code>null</code> or empty string returns
* a <code>null</code> document root.
* This method is a short form of:<br>
* <code>new HttpURL("http://example.com/path").getRoot();</code>
* </p>
* @param url a URL string
* @return left part of a URL up to (and including the host name
* @return left part of a URL up to (and including) the host name
* @since 1.8.0
*/
public static String getRoot(String url) {
if (StringUtils.isBlank(url)) {
return null;
}
return RegExUtils.replacePattern(url, "(.*?://.*?)([/?#].*)", "$1");
return RegExUtils.replacePattern(url, "(.*?://[^/?#]+).*", "$1");
}

/**
Expand All @@ -367,7 +371,7 @@ public static String getRoot(String url) {
*/
@Override
public String toString() {
StringBuilder b = new StringBuilder();
var b = new StringBuilder();
if (StringUtils.isNotBlank(protocol)) {
b.append(protocol);
b.append("://");
Expand Down Expand Up @@ -440,7 +444,7 @@ public static String encodePath(String path) {
if (StringUtils.isBlank(path)) {
return path;
}
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
for (char ch : path.toCharArray()) {
// Space to plus sign
if (ch == ' ') {
Expand All @@ -455,10 +459,10 @@ public static String encodePath(String path) {
bytes = Character.toString(ch).getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
sb.append('%');
int upper = ((b) >> 4) & 0xf;
var upper = ((b) >> 4) & 0xf;
sb.append(Integer.toHexString(
upper).toUpperCase(Locale.US));
int lower = (b) & 0xf;
var lower = (b) & 0xf;
sb.append(Integer.toHexString(
lower).toUpperCase(Locale.US));
}
Expand Down Expand Up @@ -494,7 +498,7 @@ public static String toAbsolute(String baseURL, String relativeURL) {
if (StringUtils.isBlank(relativeURL)) {
return baseURL;
}
String relURL = relativeURL.trim();
var relURL = relativeURL.trim();

// Relative is in fact absolute
if (relURL.matches("^[A-Za-z][A-Za-z0-9\\+\\-\\.]*:.*$")) {
Expand All @@ -516,7 +520,7 @@ public static String toAbsolute(String baseURL, String relativeURL) {
}

// Relative to last directory/segment
String base = baseURL.replaceFirst("(.*?)([\\?\\#])(.*)", "$1");
var base = baseURL.replaceFirst("(.*?)([\\?\\#])(.*)", "$1");
if (StringUtils.countMatches(base, '/') > 2) {
base = base.replaceFirst("(.*/)(.*)", "$1");
}
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/com/norconex/commons/lang/time/DateUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Deprecated(since = "3.0.0")
class DateUtilTest {

private static final ZonedDateTime utcZonedDateTime = ZonedDateTime.of(
Expand Down Expand Up @@ -79,9 +80,9 @@ class DateUtilTest {

@Test
void testDateToFromLocalDateTime() {
Date utcDateTime = new Date(utcDateTimeAsEpoch);
Date sysDateTime = new Date(sysDateTimeAsEpoch);
Date vanDateTime = new Date(vanDateTimeAsEpoch);
var utcDateTime = new Date(utcDateTimeAsEpoch);
var sysDateTime = new Date(sysDateTimeAsEpoch);
var vanDateTime = new Date(vanDateTimeAsEpoch);

// Date to LocalDateTime
assertEquals(toLocalDateTimeUTC(utcDateTime), utcLocalDateTime());
Expand Down Expand Up @@ -127,9 +128,9 @@ void testDateToFromLocalDateTime() {

@Test
void testDateToFromLocalDate() {
Date utcDate = new Date(utcDateAsEpoch);
Date sysDate = new Date(sysDateAsEpoch);
Date vanDate = new Date(vanDateAsEpoch);
var utcDate = new Date(utcDateAsEpoch);
var sysDate = new Date(sysDateAsEpoch);
var vanDate = new Date(vanDateAsEpoch);

// Date to LocalDateTime
assertEquals(toLocalDateUTC(utcDate), utcLocalDate());
Expand Down
157 changes: 97 additions & 60 deletions src/test/java/com/norconex/commons/lang/url/HttpURLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,50 @@ void tearDown() throws Exception {
@ParameterizedTest
@CsvSource(
value = {
"Keep protocol character case,"
+ "HTTP://www.example.com,"
+ "HTTP://www.example.com",
"'http' protocol without port,"
+ "http://www.example.com/blah,"
+ "http://www.example.com/blah",
"'http' protocol with default port,"
+ "http://www.example.com:80/blah,"
+ "http://www.example.com/blah",
"'http' protocol with non-default port,"
+ "http://www.example.com:81/blah,"
+ "http://www.example.com:81/blah",
"'https' protocol without port,"
+ "https://www.example.com/blah,"
+ "https://www.example.com/blah",
"'https' protocol with default port,"
+ "https://www.example.com:443/blah,"
+ "https://www.example.com/blah",
"'https' protocol with non-default port,"
+ "https://www.example.com:444/blah,"
+ "https://www.example.com:444/blah",
"Non 'http(s)' protocol without port,"
+ "ftp://ftp.example.com/dir,"
+ "ftp://ftp.example.com/dir",
"Non 'http(s)' protocol with port,"
+ "ftp://ftp.example.com:20/dir,"
+ "ftp://ftp.example.com:20/dir",
"Invalid URL,"
+ "http://www.example.com/\"path\","
+ "http://www.example.com/%22path%22",
"URL with leading or trailing spaces,"
+ " http://www.example.com/path ,"
+ "http://www.example.com/path",
"""
Keep protocol character case,\
HTTP://www.example.com,\
HTTP://www.example.com""",
"""
'http' protocol without port,\
http://www.example.com/blah,\
http://www.example.com/blah""",
"""
'http' protocol with default port,\
http://www.example.com:80/blah,\
http://www.example.com/blah""",
"""
'http' protocol with non-default port,\
http://www.example.com:81/blah,\
http://www.example.com:81/blah""",
"""
'https' protocol without port,\
https://www.example.com/blah,\
https://www.example.com/blah""",
"""
'https' protocol with default port,\
https://www.example.com:443/blah,\
https://www.example.com/blah""",
"""
'https' protocol with non-default port,\
https://www.example.com:444/blah,\
https://www.example.com:444/blah""",
"""
Non 'http(s)' protocol without port,\
ftp://ftp.example.com/dir,\
ftp://ftp.example.com/dir""",
"""
Non 'http(s)' protocol with port,\
ftp://ftp.example.com:20/dir,\
ftp://ftp.example.com:20/dir""",
"""
Invalid URL,\
http://www.example.com/"path",\
http://www.example.com/%22path%22""",
"""
URL with leading or trailing spaces,\
http://www.example.com/path ,\
http://www.example.com/path""",
},
ignoreLeadingAndTrailingWhitespace = false
)
Expand All @@ -90,31 +101,38 @@ void testURLToStringEquals(
@ParameterizedTest
@CsvSource(
value = {
"Relative to protocol,"
+ "//www.relative.com/e/f.html,"
+ "https://www.relative.com/e/f.html",
"Relative to domain name,"
+ "/e/f.html,"
+ "https://www.example.com/e/f.html",
"Relative to full page URL,"
+ "?name=john,"
+ "https://www.example.com/a/b/c.html?name=john",
"Relative to last directory,"
+ "g.html,"
+ "https://www.example.com/a/b/g.html",
"Absolute URL,"
+ "http://www.sample.com/xyz.html,"
+ "http://www.sample.com/xyz.html",
//Test for https://github.com/Norconex/collector-http/issues/788
"Relative with colon in parameter,"
+ "h/i.html?param=1:2,"
+ "https://www.example.com/a/b/h/i.html?param=1:2",
"Starts with valid odd scheme,"
+ "x1+2-3.4:yz,"
+ "x1+2-3.4:yz",
"Starts with invalid odd scheme,"
+ "1+2-3.4x:yz,"
+ "https://www.example.com/a/b/1+2-3.4x:yz",
"""
Relative to protocol,\
//www.relative.com/e/f.html,\
https://www.relative.com/e/f.html""",
"""
Relative to domain name,\
/e/f.html,\
https://www.example.com/e/f.html""",
"""
Relative to full page URL,\
?name=john,\
https://www.example.com/a/b/c.html?name=john""",
"""
Relative to last directory,\
g.html,\
https://www.example.com/a/b/g.html""",
"""
Absolute URL,\
http://www.sample.com/xyz.html,\
http://www.sample.com/xyz.html""",
"""
Relative with colon in parameter,\
h/i.html?param=1:2,\
https://www.example.com/a/b/h/i.html?param=1:2""",
"""
Starts with valid odd scheme,\
x1+2-3.4:yz,\
x1+2-3.4:yz""",
"""
Starts with invalid odd scheme,\
1+2-3.4x:yz,\
https://www.example.com/a/b/1+2-3.4x:yz""",
},
ignoreLeadingAndTrailingWhitespace = false
)
Expand Down Expand Up @@ -187,14 +205,14 @@ void testFileProtocol() {
@Test
void testMisc() throws MalformedURLException, URISyntaxException {
assertThat(new HttpURL().getHost()).isNull();
URL url = new URL("http://example.com/some/path.html?param1=value1#A1");
var url = new URL("http://example.com/some/path.html?param1=value1#A1");
assertThat(new HttpURL(url).toURL()).isEqualTo(url);
assertThat(new HttpURL(url, "UTF-8").toURL()).isEqualTo(url);

assertThatExceptionOfType(URLException.class).isThrownBy(
() -> new HttpURL("blah:I am invalid"));

HttpURL httpUrl = new HttpURL(url, "UTF-8");
var httpUrl = new HttpURL(url, "UTF-8");
assertThat(httpUrl)
.returns("UTF-8", HttpURL::getEncoding)
.returns("/some/path.html", HttpURL::getPath)
Expand Down Expand Up @@ -224,4 +242,23 @@ void testMisc() throws MalformedURLException, URISyntaxException {

assertThat(HttpURL.getRoot(null)).isNull();
}

@Test
void testGetRoot() throws MalformedURLException, URISyntaxException {
assertThat(HttpURL.getRoot("http://acme.com"))
.isEqualTo("http://acme.com");
assertThat(HttpURL.getRoot("http://acme.com/"))
.isEqualTo("http://acme.com");
assertThat(HttpURL.getRoot("http://acme.com/asdf"))
.isEqualTo("http://acme.com");
assertThat(HttpURL.getRoot("http://acme.com/asdf/"))
.isEqualTo("http://acme.com");
assertThat(HttpURL.getRoot("http://acme.com?adsf=asdf"))
.isEqualTo("http://acme.com");
assertThat(HttpURL.getRoot("http://acme.com#asdf"))
.isEqualTo("http://acme.com");
assertThat(HttpURL.getRoot("http://acme.com%123"))
.isEqualTo("http://acme.com%123");
}

}

0 comments on commit 261be46

Please sign in to comment.