Skip to content

Commit 5dc2b3e

Browse files
committed
feat: enhance enums to have from*(String) methods
1 parent 3768a15 commit 5dc2b3e

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

src/main/java/org/cyclonedx/Format.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@ public String getExtension() {
3030
public String getMediaType() {
3131
return mediaType;
3232
}
33+
34+
/**
35+
* Returns the format based on the given file extension.
36+
*
37+
* @param extension The file extension.
38+
* @return The format or null if not found.
39+
*/
40+
public static Format fromExtension(String extension) {
41+
if (extension != null) {
42+
switch (extension) {
43+
case "xml": return XML;
44+
case "json": return JSON;
45+
}
46+
}
47+
return null;
48+
}
3349
}

src/main/java/org/cyclonedx/Version.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,25 @@ public EnumSet<Format> getFormats() {
4444
this.version = version;
4545
this.formats = formats;
4646
}
47+
48+
/**
49+
* Returns the version based on the given version string.
50+
*
51+
* @param versionString The version string.
52+
* @return The version or null if not found.
53+
*/
54+
public static Version fromVersionString(String versionString) {
55+
if (versionString != null) {
56+
switch (versionString) {
57+
case "1.0": return VERSION_10;
58+
case "1.1": return VERSION_11;
59+
case "1.2": return VERSION_12;
60+
case "1.3": return VERSION_13;
61+
case "1.4": return VERSION_14;
62+
case "1.5": return VERSION_15;
63+
case "1.6": return VERSION_16;
64+
}
65+
}
66+
return null;
67+
}
4768
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of CycloneDX Core (Java).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
package org.cyclonedx;
20+
21+
import org.junit.jupiter.api.Test;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
24+
25+
class FormatTest {
26+
27+
@Test
28+
void testFromExtension() {
29+
for (Format format : Format.values()) {
30+
String extension = format.getExtension();
31+
Format result = Format.fromExtension(extension);
32+
assertEquals(format, result, () -> "Format should match for extension " + extension);
33+
}
34+
}
35+
36+
@Test
37+
void testFromExtensionInvalid() {
38+
Format result = Format.fromExtension("invalid-extension");
39+
assertNull(result);
40+
}
41+
42+
@Test
43+
void testFromExtensionNull() {
44+
Format result = Format.fromExtension(null);
45+
assertNull(result);
46+
}
47+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of CycloneDX Core (Java).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
package org.cyclonedx;
20+
21+
import org.junit.jupiter.api.Test;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
24+
25+
class VersionTest {
26+
27+
/**
28+
* Test the getVersionString method - this is to ensure the method is future-proof.
29+
* If a new enum value is added this test will fail unless fromVersionString is updated.
30+
*/
31+
@Test
32+
void testFromVersionString() {
33+
for (Version version : Version.values()) {
34+
String versionString = version.getVersionString();
35+
Version result = Version.fromVersionString(versionString);
36+
assertEquals(version, result, () -> "Version should match for " + versionString);
37+
}
38+
}
39+
40+
@Test
41+
void testFromVersionStringInvalid() {
42+
Version result = Version.fromVersionString("invalid-version");
43+
assertNull(result);
44+
}
45+
@Test
46+
void testFromVersionStringNull() {
47+
Version result = Version.fromVersionString(null);
48+
assertNull(result);
49+
}
50+
}

0 commit comments

Comments
 (0)