Introduce constants GitHubProjectURL+ProjectName in SongodaCoreConstants

This commit is contained in:
Christian Koop 2023-05-06 22:24:01 +02:00
parent 3722ebb46a
commit 17780fffdc
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
5 changed files with 41 additions and 7 deletions

View File

@ -44,7 +44,7 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class SongodaCore { public class SongodaCore {
private static final Logger logger = Logger.getLogger("SongodaCore"); private static final Logger logger = Logger.getLogger(SongodaCoreConstants.getProjectName());
/** /**
* Whenever we make a major change to the core GUI, updater, * Whenever we make a major change to the core GUI, updater,
@ -64,6 +64,7 @@ public class SongodaCore {
/** /**
* This is specific to the website api * This is specific to the website api
*
* @deprecated Seems useless and will probably be removed in the near future * @deprecated Seems useless and will probably be removed in the near future
*/ */
@Deprecated @Deprecated
@ -326,7 +327,7 @@ public class SongodaCore {
} }
public static String getPrefix() { public static String getPrefix() {
return "[SongodaCore] "; return "[" + SongodaCoreConstants.getProjectName() + "] ";
} }
public static Logger getLogger() { public static Logger getLogger() {

View File

@ -1,9 +1,11 @@
package com.songoda.core; package com.songoda.core;
/* /**
* Return values in this class are automatically replaced by a maven plugin after the project has been compiled. * Some return values in this class are automatically replaced by a maven plugin after the project has been compiled.
* This allows for properties to be defined at one place without relying on a text file * This allows for properties to be defined at one place without relying on a text file
* that needs to be inside the final jar (might get lost when this lib is shaded into other projects). * that needs to be inside the final jar (might get lost when this lib is shaded into other projects).
* <p>
* <b>!! Manually changing the values in this class has to be considered a breaking change. !!</b>
*/ */
public class SongodaCoreConstants { public class SongodaCoreConstants {
private SongodaCoreConstants() { private SongodaCoreConstants() {
@ -13,4 +15,12 @@ public class SongodaCoreConstants {
public static String getCoreVersion() { public static String getCoreVersion() {
return "UNKNOWN_VESION"; return "UNKNOWN_VESION";
} }
public static String getProjectName() {
return "SongodaCore";
}
public static String getGitHubProjectUrl() {
return "https://github.com/craftaro/SongodaCore";
}
} }

View File

@ -1,6 +1,6 @@
package com.songoda.core.http; package com.songoda.core.http;
import com.songoda.core.SongodaCore; import com.songoda.core.SongodaCoreConstants;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.IOException; import java.io.IOException;
@ -8,14 +8,24 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
public class SimpleHttpClient implements HttpClient { public class SimpleHttpClient implements HttpClient {
private static final String USER_AGENT = generateUserAgent();
public @NotNull HttpResponse get(String url) throws IOException { public @NotNull HttpResponse get(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setInstanceFollowRedirects(true); connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.setReadTimeout(5000); connection.setReadTimeout(5000);
connection.setRequestProperty("User-Agent", "SongodaCore/" + SongodaCore.getVersion() + " (+https://github.com/songoda/SongodaCore)"); connection.setRequestProperty("User-Agent", USER_AGENT);
return new HttpResponseImpl(connection); return new HttpResponseImpl(connection);
} }
private static String generateUserAgent() {
String projectName = SongodaCoreConstants.getProjectName();
String version = SongodaCoreConstants.getCoreVersion();
String projectUrl = SongodaCoreConstants.getGitHubProjectUrl();
return projectName + "/" + version + " (+" + projectUrl + ")";
}
} }

View File

@ -1,5 +1,7 @@
package com.songoda.core.math; package com.songoda.core.math;
import com.songoda.core.SongodaCoreConstants;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -7,7 +9,7 @@ public class MathUtils {
private static final Map<String, Double> cache = new HashMap<>(); private static final Map<String, Double> cache = new HashMap<>();
public static double eval(String toParse) { public static double eval(String toParse) {
return eval(toParse, "SongodaCore Eval Engine"); return eval(toParse, SongodaCoreConstants.getProjectName() + " Eval Engine");
} }
public static double eval(String toParse, String warningMessage) { public static double eval(String toParse, String warningMessage) {

View File

@ -6,6 +6,7 @@ import org.opentest4j.TestSkippedException;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
class SongodaCoreConstantsTest { class SongodaCoreConstantsTest {
@ -22,4 +23,14 @@ class SongodaCoreConstantsTest {
assertTrue(VERSION_PATTERN.matcher(coreVersion).matches(), "Version string is not a valid semver string: " + coreVersion); assertTrue(VERSION_PATTERN.matcher(coreVersion).matches(), "Version string is not a valid semver string: " + coreVersion);
} }
@Test
void getProjectName() {
assertFalse(SongodaCoreConstants.getProjectName().isEmpty());
}
@Test
void getGitHubProjectUrl() {
assertFalse(SongodaCoreConstants.getGitHubProjectUrl().isEmpty());
}
} }