Skip to content

Commit

Permalink
Fix some generics warnings
Browse files Browse the repository at this point in the history
This commit fixes some generics usage warnings,
and also auto-closes some resources in the
test suite.

Signed-off-by: Luke Inman-Semerau <luke.semerau@gmail.com>
  • Loading branch information
asashour authored and lukeis committed Jul 6, 2015
1 parent 71fb31f commit bb7e418
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void setUp(String url, String browserString) throws Exception {

protected int getDefaultPort() {
try {
Class c = Class.forName("org.openqa.selenium.server.RemoteControlConfiguration");
Class<?> c = Class.forName("org.openqa.selenium.server.RemoteControlConfiguration");
Method getDefaultPort = c.getMethod("getDefaultPort", new Class[0]);
Integer portNumber = (Integer) getDefaultPort.invoke(null);
return portNumber.intValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti

if (toConvert instanceof Map) {
JsonObject converted = new JsonObject();
for (Object objectEntry : ((Map) toConvert).entrySet()) {
Map.Entry<String, Object> entry = (Map.Entry) objectEntry;
for (Map.Entry<String, Object> entry : ((Map<String, Object>) toConvert).entrySet()) {
converted.add(entry.getKey(), convertObject(entry.getValue(), maxDepth - 1));
}
return converted;
Expand All @@ -147,7 +146,7 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti

if (toConvert instanceof Collection) {
JsonArray array = new JsonArray();
for (Object o : (Collection) toConvert) {
for (Object o : (Collection<?>) toConvert) {
array.add(convertObject(o, maxDepth - 1));
}
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private Throwable rebuildServerError(Map<String, Object> rawErrorData, int respo

Throwable toReturn = null;
String message = (String) rawErrorData.get(MESSAGE);
Class clazz = null;
Class<?> clazz = null;

// First: allow Remote Driver to specify the Selenium Server internal exception
if (rawErrorData.containsKey(CLASS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected <X> X create(RemoteWebDriver driver, Map<String, AugmenterProvider> au
return objectToAugment;
}

InvocationHandler proxyHandler = new JdkHandler((RemoteWebDriver) driver,
InvocationHandler proxyHandler = new JdkHandler<>((RemoteWebDriver) driver,
objectToAugment, augmentationHandlers);
X augmentedProxy = (X) Proxy.newProxyInstance(
getClass().getClassLoader(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,8 @@ private WebDriverException propertyWriteException(
cause);
}

@SuppressWarnings("unchecked")
private Map convertMap(JsonObject toConvert, int depth) {
Map map = new HashMap();
private Map<String, Object> convertMap(JsonObject toConvert, int depth) {
Map<String, Object> map = new HashMap<>();

for (Map.Entry<String, JsonElement> entry : toConvert.entrySet()) {
map.put(entry.getKey(), convert(Object.class, entry.getValue(), depth + 1));
Expand All @@ -337,9 +336,8 @@ private Map convertMap(JsonObject toConvert, int depth) {
return map;
}

@SuppressWarnings("unchecked")
private List convertList(JsonArray toConvert, int depth) {
ArrayList list = new ArrayList(toConvert.size());
private List<?> convertList(JsonArray toConvert, int depth) {
List<Object> list = new ArrayList<>(toConvert.size());
for (int i = 0; i < toConvert.size(); i++) {
list.add(convert(Object.class, toConvert.get(i), depth + 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ public JsonObject toJson() throws IOException {
* @throws IOException If an error occurred while writing the safari extensions to a
* temporary directory.
*/
@SuppressWarnings("unchecked")
private static SafariOptions fromJsonMap(Map options) throws IOException {
private static SafariOptions fromJsonMap(Map<?, ?> options) throws IOException {
SafariOptions safariOptions = new SafariOptions();

Number port = (Number) options.get(Option.PORT);
Expand Down
6 changes: 3 additions & 3 deletions java/client/src/org/openqa/selenium/support/ThreadGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public static WebDriver protect(WebDriver actualWebDriver) {
invocationHandler);
}

private static Class[] getInterfaces(Object target) {
Class base = target.getClass();
Set<Class> interfaces = new HashSet<>();
private static Class<?>[] getInterfaces(Object target) {
Class<?> base = target.getClass();
Set<Class<?>> interfaces = new HashSet<>();
if (base.isInterface()) {
interfaces.add(base);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

public class InternalSelenseTestBase extends SeleneseTestBase {
private static final Logger log = Logger.getLogger(InternalSelenseTestBase.class.getName());
private static final ThreadLocal<Selenium> instance = new ThreadLocal<Selenium>();
private static final ThreadLocal<Selenium> instance = new ThreadLocal<>();
private static String seleniumServerUrl;

private static final AtomicBoolean mustBuild = new AtomicBoolean(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public void testShouldReturnTheEntireWrittenContentIfSmallerThanTheBufferSize()
String expected = "foo";
int maxSize = expected.getBytes().length;

CircularOutputStream os = new CircularOutputStream(maxSize);
os.write(expected.getBytes());
try (CircularOutputStream os = new CircularOutputStream(maxSize)) {
os.write(expected.getBytes());

String seen = os.toString();
String seen = os.toString();

assertEquals(expected, seen);
assertEquals(expected, seen);
}
}

@Test
Expand All @@ -49,38 +50,41 @@ public void testShouldReturnJustTheWrittenOutputIfBufferIsTooLarge() throws Exce
// Note, this makes the buffer larger than what we write to it
int maxSize = expected.getBytes().length + 1;

CircularOutputStream os = new CircularOutputStream(maxSize);
os.write(expected.getBytes());
try (CircularOutputStream os = new CircularOutputStream(maxSize)) {
os.write(expected.getBytes());

String seen = os.toString();
String seen = os.toString();

assertEquals(expected, seen);
assertEquals(expected, seen);
}
}

@Test
public void testShouldTruncateOutputToMatchTheSizeOfTheBuffer() throws Exception {
String expected = "oo";
int maxSize = expected.getBytes().length;

CircularOutputStream os = new CircularOutputStream(maxSize);
os.write("foo".getBytes());
try (CircularOutputStream os = new CircularOutputStream(maxSize)) {
os.write("foo".getBytes());

String seen = os.toString();
String seen = os.toString();

assertEquals(expected, seen);
assertEquals(expected, seen);
}
}

@Test
public void testShouldReturnContentInTheCorrectOrder() throws Exception {
String expected = "234";
int maxSize = expected.getBytes().length;

CircularOutputStream os = new CircularOutputStream(maxSize);
os.write("1234".getBytes());
try (CircularOutputStream os = new CircularOutputStream(maxSize)) {
os.write("1234".getBytes());

String seen = os.toString();
String seen = os.toString();

assertEquals(expected, seen);
assertEquals(expected, seen);
}
}

@Test
Expand All @@ -107,18 +111,19 @@ public void testLongerMultiLineOutputPreservesJustTheEnd() throws Exception {
@Test
public void testCircularness() {
CircularOutputStream os = new CircularOutputStream(5);
PrintWriter pw = new PrintWriter(os, true);
try (PrintWriter pw = new PrintWriter(os, true)) {

pw.write("12345");
pw.flush();
assertEquals("12345", os.toString());
pw.write("12345");
pw.flush();
assertEquals("12345", os.toString());

pw.write("6");
pw.flush();
assertEquals("23456", os.toString());
pw.write("6");
pw.flush();
assertEquals("23456", os.toString());

pw.write("789");
pw.flush();
assertEquals("56789", os.toString());
pw.write("789");
pw.flush();
assertEquals("56789", os.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,20 @@ public void doubleNegatives_conditionThatReturnsNullTimesOut() throws Interrupte

@Test
public void waitingForVisibilityOfAllElementsLocatedByReturnsListOfElements() {
List webElements = Lists.newArrayList(mockElement);
List<WebElement> webElements = Lists.newArrayList(mockElement);
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);
when(mockElement.isDisplayed()).thenReturn(true);

List returnedElements =
List<WebElement> returnedElements =
wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector)));
assertEquals(webElements, returnedElements);
}

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenElementNotDisplayed() {
List webElements = Lists.newArrayList(mockElement);
List<WebElement> webElements = Lists.newArrayList(mockElement);
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);
Expand All @@ -292,7 +292,7 @@ public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhen

@Test(expected = StaleElementReferenceException.class)
public void waitingForVisibilityOfAllElementsLocatedByThrowsStaleExceptionWhenElementIsStale() {
List webElements = Lists.newArrayList(mockElement);
List<WebElement> webElements = Lists.newArrayList(mockElement);
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);
Expand All @@ -303,7 +303,7 @@ public void waitingForVisibilityOfAllElementsLocatedByThrowsStaleExceptionWhenEl

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenNoElementsFound() {
List webElements = Lists.newArrayList();
List<WebElement> webElements = Lists.newArrayList();
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);
Expand All @@ -322,15 +322,15 @@ public void waitingForVisibilityOfAllElementsReturnsListOfElements() {

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenElementNotDisplayed() {
List webElements = Lists.newArrayList(mockElement);
List<WebElement> webElements = Lists.newArrayList(mockElement);
when(mockElement.isDisplayed()).thenReturn(false);

wait.until(visibilityOfAllElements(webElements));
}

@Test(expected = StaleElementReferenceException.class)
public void waitingForVisibilityOfAllElementsThrowsStaleElementReferenceExceptionWhenElementIsStale() {
List webElements = Lists.newArrayList(mockElement);
List<WebElement> webElements = Lists.newArrayList(mockElement);

when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element"));

Expand All @@ -339,7 +339,7 @@ public void waitingForVisibilityOfAllElementsThrowsStaleElementReferenceExceptio

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenNoElementsFound() {
List webElements = Lists.newArrayList();
List<WebElement> webElements = Lists.newArrayList();

wait.until(visibilityOfAllElements(webElements));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public abstract class JUnit4TestBase implements WrapsDriver {
protected TestEnvironment environment;
protected AppServer appServer;
protected Pages pages;
private static ThreadLocal<WebDriver> storedDriver = new ThreadLocal<WebDriver>();
private static ThreadLocal<WebDriver> storedDriver = new ThreadLocal<>();
protected WebDriver driver;
protected Wait<WebDriver> wait;
protected Wait<WebDriver> shortWait;
Expand Down

0 comments on commit bb7e418

Please sign in to comment.