Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions androidcommon_lib/src/test/java/org/opendatakit/test/UrlUtilsTest.java
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) {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.
I could add a public helper method in the UrlUtils class specifically for testing or use reflection. Please let me know if you'd like me to try a different approach for accessing the package-private method

Copy link
Member

Choose a reason for hiding this comment

The 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:
org.opendatakit.webkitserver.utilities; vs org.opendatakit.test.webkitserver.utilities;

It needs to be exactly the same: org.opendatakit.webkitserver.utilities;

As stated in the review.

Copy link
Author

Choose a reason for hiding this comment

The 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);
}
}