### Question:
UrlUtil { public static String replaceParameterInUrl( @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } String parameter = StringUtil.isBlank(parameterValue) ? parameterName : parameterName + '=' + parameterValue; int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url + '?' + parameter; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url.substring(0, sharpPos) + '?' + parameter + url.substring(sharpPos); } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos + 1)).append(parameter); if (url.length() > questionSignPos + 1) { String query = sharpPos == -1 ? url.substring(questionSignPos + 1) : url.substring(questionSignPos + 1, sharpPos); query = removeParameterFromQuery(query, parameterName); if (!StringUtil.isBlank(query)) { resultUrl.append('&').append(query); } if (sharpPos != -1) { resultUrl.append(url.substring(sharpPos)); } } return resultUrl.toString(); } } private  UrlUtil(); @SuppressWarnings("unused") static Set<String> getParameterNames(@Nonnull String url); @SuppressWarnings("IfStatementWithIdenticalBranches") static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName); static String replaceParameterInUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); static String appendParameterToUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); @SuppressWarnings("OverlyComplexMethod") static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts); @SuppressWarnings("unused") static String appendRelativePathToUrl(@Nonnull String url, @Nullable String relativePath); @SuppressWarnings("BooleanMethodIsAlwaysInverted") @Contract("null -> false") static boolean isValidUrl(@Nullable String url); @Contract("null, _ -> false") static boolean isValidUrl(@Nullable String url, String[] allowedSchemes); @SuppressWarnings({"ResultOfMethodCallIgnored", "BooleanMethodIsAlwaysInverted"}) @Contract("null -> false") static boolean isValidUri(@Nullable String uri); @SuppressWarnings("unused") @Contract("null -> null") @Nullable static String extractFileName(@Nullable String url);  }### Answer:
@Test public void testReplaceParameterInUrl() { assertEquals( "http: UrlUtil.replaceParameterInUrl("http: ); assertEquals( "http: UrlUtil.replaceParameterInUrl("http: ); assertEquals( "http: UrlUtil.replaceParameterInUrl("http: ); assertEquals("http: assertEquals( "http: UrlUtil.replaceParameterInUrl( "http: ) ); assertEquals( "http: UrlUtil.replaceParameterInUrl( "http: ) ); }