-
Notifications
You must be signed in to change notification settings - Fork 51
Added Test for UrlUtils #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: upgrade-jdk11
Are you sure you want to change the base?
Changes from 4 commits
bc88cb4
73e8c85
5bf50e8
fcaccb8
45a7159
0885ac9
91b3acf
1364638
c208c70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package org.opendatakit.test; | ||
|
|
||
| import org.junit.Test; | ||
| import org.opendatakit.webkitserver.utilities.UrlUtils; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| public class UrlUtilsTest { | ||
|
|
||
| private static final String EMPTY_STRING = ""; | ||
| private static final String URL_SEGMENT1 = "test/"; | ||
| private static final String FILE_PATH_PREFIX = "this/test/file/path/"; | ||
| private static final String HTML_EXTENSION = ".html"; | ||
| private static final String QUERY_PARAM_PREFIX = "?"; | ||
| private static final String HASH_PREFIX = "#"; | ||
|
|
||
| // File paths | ||
| private static final String FILE_PATH_WITH_HTML = FILE_PATH_PREFIX + HTML_EXTENSION; | ||
|
|
||
| // Query and hash parameter values | ||
| private static final String HASH_VALUE_FOO = "foo"; | ||
| private static final String QUERY_VALUE_FOO_AND_BAR = "foo&bar=3"; | ||
| private static final String QUERY_VALUE_BAR_AND_BAZ = "bar=3&baz=55"; | ||
| private static final String QUERY_VALUE_FOO_BAR = "foo=bar"; | ||
| private static final String QUERY_VALUE_BAR_BAZ = "bar=baz"; | ||
|
|
||
| // Complete URL fragments with parameters | ||
| private static final String FILE_WITH_HASH = URL_SEGMENT1 + "file" + HASH_PREFIX + HASH_VALUE_FOO; | ||
| private static final String FILE_WITH_QUERY_PARAMS = "pretty/little/liar" + QUERY_PARAM_PREFIX + QUERY_VALUE_FOO_AND_BAR; | ||
| private static final String FILE_WITH_HASH_AND_QUERY = URL_SEGMENT1 + "test/test" + HTML_EXTENSION + | ||
| HASH_PREFIX + HASH_VALUE_FOO + | ||
| QUERY_PARAM_PREFIX + QUERY_VALUE_BAR_AND_BAZ; | ||
| private static final String FILE_WITHOUT_PARAMS = URL_SEGMENT1 + "test/test" + HTML_EXTENSION; | ||
| private static final String FILE_WITH_HASH_ONLY = URL_SEGMENT1 + "test" + HTML_EXTENSION + | ||
| HASH_PREFIX + HASH_VALUE_FOO; | ||
| private static final String FILE_WITH_QUERY = "this/is/a/file/that/i/like" + HTML_EXTENSION + | ||
| QUERY_PARAM_PREFIX + QUERY_VALUE_FOO_BAR; | ||
| private static final String FILE_WITH_BOTH = "foo/bar" + HTML_EXTENSION + | ||
| HASH_PREFIX + HASH_VALUE_FOO + | ||
| QUERY_PARAM_PREFIX + QUERY_VALUE_BAR_BAZ; | ||
|
|
||
| @Test | ||
| public void testNoHashOrParameters() { | ||
| assertRetrieveFileNameHelper(FILE_PATH_WITH_HTML, FILE_PATH_WITH_HTML); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyString() { | ||
| assertRetrieveFileNameHelper(EMPTY_STRING, EMPTY_STRING); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOnlyHash() { | ||
| assertRetrieveFileNameHelper(URL_SEGMENT1 + "file", FILE_WITH_HASH); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOnlyQueryParams() { | ||
| assertRetrieveFileNameHelper("pretty/little/liar", FILE_WITH_QUERY_PARAMS); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHashAndQueryParams() { | ||
| assertRetrieveFileNameHelper(URL_SEGMENT1 + "test/test" + HTML_EXTENSION, FILE_WITH_HASH_AND_QUERY); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetIndexOfParamsNoParams() { | ||
| assertGetIndexHelper(FILE_WITHOUT_PARAMS, -1); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetIndexOfParamsHash() { | ||
| assertGetIndexHelper(FILE_WITH_HASH_ONLY, 14); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetIndexOfQueryHash() { | ||
| assertGetIndexHelper(FILE_WITH_QUERY, 31); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetIndexOfBoth() { | ||
| assertGetIndexHelper(FILE_WITH_BOTH, 12); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetParamsNone() { | ||
| assertGetParamsHelper(FILE_PATH_WITH_HTML, EMPTY_STRING); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetParamsHash() { | ||
| assertGetParamsHelper(FILE_WITH_HASH, HASH_PREFIX + HASH_VALUE_FOO); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetParamsQuery() { | ||
| assertGetParamsHelper(FILE_WITH_QUERY_PARAMS, QUERY_PARAM_PREFIX + QUERY_VALUE_FOO_AND_BAR); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetParamsBoth() { | ||
| assertGetParamsHelper(FILE_WITH_HASH_AND_QUERY, HASH_PREFIX + HASH_VALUE_FOO + QUERY_PARAM_PREFIX + QUERY_VALUE_BAR_AND_BAZ); | ||
| } | ||
|
|
||
| /** | ||
| * Take start, retrieve the file name, and assert that the result is equal to | ||
| * expected. | ||
| * @param expected Expected file path | ||
| * @param start Input URL segment | ||
| */ | ||
| protected void assertRetrieveFileNameHelper(String expected, String start) { | ||
| String result = UrlUtils.getPathFromUriFragment(start); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| /** | ||
| * Since getIndexOfParameters is package-private, we'll use reflection to access it | ||
| * @param segment URL segment to test | ||
| * @param expected Expected index of parameters | ||
| */ | ||
| protected void assertGetIndexHelper(String segment, int expected) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you change the package to org.opendatakit.webkitserver.utilities you should not need to use reflection.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this approach but encountered access errors. Even with the test in the same package name, I got the error: 'getIndexOfParameters(java.lang.String)' is not public in 'org.opendatakit.webkitserver.utilities.UrlUtils'. It cannot be accessed from outside the package.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This conversation should not be resolved because it was not resolved. Also, generally, the person doing the review resolves when it's complete. The package you changed it to is NOT the same: It needs to be exactly the same: org.opendatakit.webkitserver.utilities; As stated in the review.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relocated UrlUtilsTest to match the package structure of the class under test. To allows access to package-private methods for more thorough testing. |
||
| try { | ||
| java.lang.reflect.Method method = UrlUtils.class.getDeclaredMethod("getIndexOfParameters", String.class); | ||
| method.setAccessible(true); | ||
| int actual = (int) method.invoke(null, segment); | ||
| assertEquals(expected, actual); | ||
| } catch (Exception e) { | ||
| fail("Could not access getIndexOfParameters method: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Assert the parameters extracted from the URL segment | ||
| * @param segment URL segment to test | ||
| * @param expected Expected parameters string | ||
| */ | ||
| protected void assertGetParamsHelper(String segment, String expected) { | ||
| String actual = UrlUtils.getParametersFromUriFragment(segment); | ||
| assertEquals(expected, actual); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.