-
-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite Owner address parsing to Java
- Loading branch information
Showing
6 changed files
with
170 additions
and
63 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
allure-generator/src/main/java/io/qameta/allure/owner/OwnerAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2016-2024 Qameta Software Inc | ||
* | ||
* 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 io.qameta.allure.owner; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class OwnerAddress { | ||
private final String displayName; | ||
private final String url; | ||
|
||
public OwnerAddress(final String displayName, final String url) { | ||
this.displayName = displayName; | ||
this.url = url; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
allure-generator/src/main/java/io/qameta/allure/owner/OwnerAddressParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2016-2024 Qameta Software Inc | ||
* | ||
* 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 io.qameta.allure.owner; | ||
|
||
import java.net.MalformedURLException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public final class OwnerAddressParser { | ||
private static final Pattern RFC2822_ADDRESS = Pattern.compile("^(.*) <(.*)>$"); | ||
private static final Pattern LOOKS_LIKE_EMAIL = Pattern.compile("^[^@]+@[^@]+$"); | ||
|
||
private OwnerAddressParser() { | ||
} | ||
|
||
public static OwnerAddress parseAddress(final String maybeAddress) { | ||
if (maybeAddress == null || maybeAddress.isEmpty()) { | ||
return null; | ||
} | ||
|
||
final Matcher matcher = RFC2822_ADDRESS.matcher(maybeAddress); | ||
if (matcher.matches()) { | ||
final String displayName = matcher.group(1); | ||
final String url = toHref(matcher.group(2)); | ||
return new OwnerAddress(displayName, url); | ||
} | ||
|
||
return new OwnerAddress(maybeAddress, toHref(maybeAddress)); | ||
} | ||
|
||
private static String toHref(final String address) { | ||
if (isValidURL(address)) { | ||
return address; | ||
} | ||
|
||
if (LOOKS_LIKE_EMAIL.matcher(address).matches()) { | ||
return "mailto:" + address; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static boolean isValidURL(final String maybeURL) { | ||
try { | ||
new java.net.URL(maybeURL); | ||
return true; | ||
} catch (MalformedURLException e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
allure-generator/src/main/javascript/utils/parseAddress.js
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
allure-generator/src/test/java/io/qameta/allure/owner/OwnerAddressParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2016-2024 Qameta Software Inc | ||
* | ||
* 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 io.qameta.allure.owner; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class OwnerAddressParserTest { | ||
@Test | ||
void shouldReturnNullForNullInput() { | ||
assertNull(OwnerAddressParser.parseAddress(null)); | ||
} | ||
|
||
@Test | ||
void shouldReturnNullForEmptyInput() { | ||
assertNull(OwnerAddressParser.parseAddress("")); | ||
} | ||
|
||
@Test | ||
void shouldParseRFC2822FormattedStringWithEmail() { | ||
String input = "John Doe <[email protected]>"; | ||
OwnerAddress expected = new OwnerAddress("John Doe", "mailto:[email protected]"); | ||
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(input).getDisplayName()); | ||
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(input).getUrl()); | ||
} | ||
|
||
@Test | ||
void shouldParseRFC2822FormattedStringWithURL() { | ||
String input = "John Doe <https://github.com/john.doe>"; | ||
OwnerAddress expected = new OwnerAddress("John Doe", "https://github.com/john.doe"); | ||
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(input).getDisplayName()); | ||
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(input).getUrl()); | ||
} | ||
|
||
@Test | ||
void shouldReturnDisplayNameForPlainTextInput() { | ||
String displayName = "John Doe"; | ||
OwnerAddress expected = new OwnerAddress(displayName, null); | ||
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(displayName).getDisplayName()); | ||
assertNull(OwnerAddressParser.parseAddress(displayName).getUrl()); | ||
} | ||
|
||
@Test | ||
void shouldReturnDisplayNameAndUrlForEmailAddress() { | ||
String email = "[email protected]"; | ||
OwnerAddress expected = new OwnerAddress(email, "mailto:" + email); | ||
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(email).getDisplayName()); | ||
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(email).getUrl()); | ||
} | ||
|
||
@Test | ||
void shouldReturnDisplayNameAndUrlForValidURL() { | ||
String validUrl = "https://github.com/john.doe"; | ||
OwnerAddress expected = new OwnerAddress(validUrl, validUrl); | ||
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(validUrl).getDisplayName()); | ||
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(validUrl).getUrl()); | ||
} | ||
} |