Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 700b2af

Browse files
authored
fix: correct minor issues with tests (#226)
* Remove `public` modifier * Remove exceptions which are not thrown * Print test description if there is an unexpected failure
1 parent 1aafeae commit 700b2af

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed

src/test/java/com/github/packageurl/PackageURLBuilderTest.java

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void packageURLBuilder(
7272
builder.withSubpath(subpath);
7373
}
7474
if (invalid) {
75-
assertThrows(MalformedPackageURLException.class, builder::build);
75+
assertThrows(MalformedPackageURLException.class, builder::build, "Build should fail due to " + description);
7676
} else {
7777
assertEquals(parameters.getType(), builder.getType(), "type");
7878
assertEquals(parameters.getNamespace(), builder.getNamespace(), "namespace");
@@ -109,68 +109,58 @@ void packageURLBuilderException1Null() throws MalformedPackageURLException {
109109
void packageURLBuilderException2() {
110110
assertThrowsExactly(
111111
MalformedPackageURLException.class,
112-
() -> {
113-
PackageURLBuilder.aPackageURL()
114-
.withType("type")
115-
.withNamespace("invalid//namespace")
116-
.withName("name")
117-
.build();
118-
},
112+
() -> PackageURLBuilder.aPackageURL()
113+
.withType("type")
114+
.withNamespace("invalid//namespace")
115+
.withName("name")
116+
.build(),
119117
"Build should fail due to invalid namespace");
120118
}
121119

122120
@Test
123121
void packageURLBuilderException3() {
124122
assertThrowsExactly(
125123
MalformedPackageURLException.class,
126-
() -> {
127-
PackageURLBuilder.aPackageURL()
128-
.withType("typ^e")
129-
.withSubpath("invalid/name%2Fspace")
130-
.withName("name")
131-
.build();
132-
},
124+
() -> PackageURLBuilder.aPackageURL()
125+
.withType("typ^e")
126+
.withSubpath("invalid/name%2Fspace")
127+
.withName("name")
128+
.build(),
133129
"Build should fail due to invalid subpath");
134130
}
135131

136132
@Test
137133
void packageURLBuilderException4() {
138134
assertThrowsExactly(
139135
MalformedPackageURLException.class,
140-
() -> {
141-
PackageURLBuilder.aPackageURL()
142-
.withType("0_type")
143-
.withName("name")
144-
.build();
145-
},
136+
() -> PackageURLBuilder.aPackageURL()
137+
.withType("0_type")
138+
.withName("name")
139+
.build(),
146140
"Build should fail due to invalid type");
147141
}
148142

149143
@Test
150144
void packageURLBuilderException5() {
151145
assertThrowsExactly(
152146
MalformedPackageURLException.class,
153-
() -> {
154-
PackageURLBuilder.aPackageURL()
155-
.withType("ype")
156-
.withName("name")
157-
.withQualifier("0_key", "value")
158-
.build();
159-
},
147+
() -> PackageURLBuilder.aPackageURL()
148+
.withType("ype")
149+
.withName("name")
150+
.withQualifier("0_key", "value")
151+
.build(),
160152
"Build should fail due to invalid qualifier key");
161153
}
162154

163155
@Test
164156
void packageURLBuilderException6() {
165157
assertThrowsExactly(
166158
MalformedPackageURLException.class,
167-
() -> {
168-
PackageURLBuilder.aPackageURL()
169-
.withType("ype")
170-
.withName("name")
171-
.withQualifier("", "value")
172-
.build();
173-
},
159+
() -> PackageURLBuilder.aPackageURL()
160+
.withType("ype")
161+
.withName("name")
162+
.withQualifier("", "value")
163+
.build(),
174164
"Build should fail due to invalid qualifier key");
175165
}
176166

src/test/java/com/github/packageurl/PackageURLTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import java.io.IOException;
3131
import java.util.Locale;
32-
import java.util.Map;
3332
import java.util.stream.Stream;
3433
import org.jspecify.annotations.Nullable;
3534
import org.junit.jupiter.api.AfterAll;
@@ -63,8 +62,7 @@ static void resetLocale() {
6362

6463
@Test
6564
void validPercentEncoding() throws MalformedPackageURLException {
66-
PackageURL purl =
67-
new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", (Map<String, String>) null, null);
65+
PackageURL purl = new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", null, null);
6866
assertEquals("pkg:maven/com.google.summit/[email protected]%0A", purl.toString());
6967
PackageURL purl2 =
7068
new PackageURL("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5");
@@ -74,7 +72,7 @@ void validPercentEncoding() throws MalformedPackageURLException {
7472
}
7573

7674
@Test
77-
void invalidPercentEncoding() throws MalformedPackageURLException {
75+
void invalidPercentEncoding() {
7876
assertThrowsExactly(
7977
MalformedPackageURLException.class,
8078
() -> new PackageURL("pkg:maven/com.google.summit/[email protected]%"));
@@ -99,7 +97,10 @@ void constructorParsing(
9997
boolean invalid)
10098
throws Exception {
10199
if (invalid) {
102-
assertThrows(getExpectedException(purlString), () -> new PackageURL(purlString));
100+
assertThrows(
101+
getExpectedException(purlString),
102+
() -> new PackageURL(purlString),
103+
"Build should fail due to " + description);
103104
} else {
104105
PackageURL purl = new PackageURL(purlString);
105106
assertPurlEquals(parameters, purl);
@@ -131,7 +132,8 @@ void constructorParameters(
131132
parameters.getName(),
132133
parameters.getVersion(),
133134
parameters.getQualifiers(),
134-
parameters.getSubpath()));
135+
parameters.getSubpath()),
136+
"Build should fail due to " + description);
135137
} else {
136138
PackageURL purl = new PackageURL(
137139
parameters.getType(),

src/test/java/com/github/packageurl/internal/StringUtilTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424
import static org.junit.jupiter.api.Assertions.assertEquals;
2525
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
2626

27-
import com.github.packageurl.MalformedPackageURLException;
2827
import com.github.packageurl.ValidationException;
2928
import org.junit.jupiter.api.Test;
3029

31-
public class StringUtilTest {
32-
30+
class StringUtilTest {
3331
@Test
34-
void invalidPercentEncoding() throws MalformedPackageURLException {
32+
void invalidPercentEncoding() {
3533
Throwable t1 = assertThrowsExactly(ValidationException.class, () -> StringUtil.percentDecode("a%0"));
3634
assertEquals("Incomplete percent encoding at offset 1 with value '%0'", t1.getMessage());
3735
Throwable t2 = assertThrowsExactly(ValidationException.class, () -> StringUtil.percentDecode("aaaa%%0A"));

0 commit comments

Comments
 (0)