From d5fd82500cd7e28e4a1d23db6430356be0807b25 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 19 Jan 2014 19:05:34 -0800 Subject: [PATCH] JCLOUDS-428: Address Java 8 compatibility nits Tested with JDK 1.6.0_45, 1.7.0_45, and 1.8.0-ea-b123. --- .../jclouds/http/functions/ParseSaxTest.java | 39 +++++++++++++-- .../test/java/org/jclouds/json/JsonTest.java | 13 ++--- .../org/jclouds/reflect/Reflection2Test.java | 49 ++++++++++++++++--- .../java/org/jclouds/utils/TestUtils.java | 10 ++++ 4 files changed, 93 insertions(+), 18 deletions(-) diff --git a/core/src/test/java/org/jclouds/http/functions/ParseSaxTest.java b/core/src/test/java/org/jclouds/http/functions/ParseSaxTest.java index c64b286f3c..53f4e2ee1f 100644 --- a/core/src/test/java/org/jclouds/http/functions/ParseSaxTest.java +++ b/core/src/test/java/org/jclouds/http/functions/ParseSaxTest.java @@ -54,6 +54,11 @@ public class ParseSaxTest extends BaseHandlerTest { ParseSax createParser() { return factory.create(injector.getInstance(TestHandler.class)); } + + @DataProvider + public Object[][] runUnderJava6() { + return TestUtils.isJava6() ? SINGLE_NO_ARG_INVOCATION : NO_INVOCATIONS; + } @DataProvider public Object[][] runUnderJava7() { @@ -61,8 +66,8 @@ public class ParseSaxTest extends BaseHandlerTest { } @DataProvider - public Object[][] ignoreUnderJava7() { - return TestUtils.isJava7() ? NO_INVOCATIONS : SINGLE_NO_ARG_INVOCATION; + public Object[][] runUnderJava8() { + return TestUtils.isJava8() ? SINGLE_NO_ARG_INVOCATION : NO_INVOCATIONS; } @Test @@ -129,7 +134,7 @@ public class ParseSaxTest extends BaseHandlerTest { } } - @Test(dataProvider = "ignoreUnderJava7", description = "see http://code.google.com/p/jclouds/issues/detail?id=795") + @Test(dataProvider = "runUnderJava6") public void testAddDetailsAndPropagateOkWithValidRequestResponseWithSAXParseException() throws ExecutionException, InterruptedException, TimeoutException, IOException { @@ -155,7 +160,7 @@ public class ParseSaxTest extends BaseHandlerTest { } } - @Test(dataProvider = "runUnderJava7", description = "see http://code.google.com/p/jclouds/issues/detail?id=795") + @Test(dataProvider = "runUnderJava7") public void testAddDetailsAndPropagateOkWithValidRequestResponseWithSAXParseException_java7() throws ExecutionException, InterruptedException, TimeoutException, IOException { @@ -180,4 +185,30 @@ public class ParseSaxTest extends BaseHandlerTest { assertEquals(e.getCause(), input); } } + + @Test(dataProvider = "runUnderJava8") + public void testAddDetailsAndPropagateOkWithValidRequestResponseWithSAXParseException_java8() throws ExecutionException, + InterruptedException, TimeoutException, IOException { + + ParseSax parser = createParser(); + HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://foohost").build(); + HttpResponse response = HttpResponse.builder().statusCode(304).message("Not Modified").build(); + Locator locator = createMock(Locator.class); + expect(locator.getColumnNumber()).andReturn(1); + expect(locator.getLineNumber()).andReturn(1); + expect(locator.getPublicId()).andReturn("publicId"); + expect(locator.getSystemId()).andReturn("systemId"); + replay(locator); + Exception input = new SAXParseException("foo", locator); + verify(locator); + + try { + parser.setContext(request); + parser.addDetailsAndPropagate(response, input); + } catch (RuntimeException e) { + assertEquals(e.getMessage(), + "request: GET http://foohost HTTP/1.1; response: HTTP/1.1 304 Not Modified; error at 1:1 in document systemId; cause: org.xml.sax.SAXParseExceptionpublicId: publicId; systemId: systemId; lineNumber: 1; columnNumber: 1; foo"); + assertEquals(e.getCause(), input); + } + } } diff --git a/core/src/test/java/org/jclouds/json/JsonTest.java b/core/src/test/java/org/jclouds/json/JsonTest.java index 235f6c1021..1ced96c088 100644 --- a/core/src/test/java/org/jclouds/json/JsonTest.java +++ b/core/src/test/java/org/jclouds/json/JsonTest.java @@ -139,12 +139,13 @@ public class JsonTest { } public void testMapStringObjectWithAllValidValuesOneDeep() { - Map map = Maps.newHashMap(); - map.put("string", "string"); - map.put("number", 1.0); - map.put("boolean", true); - map.put("map", ImmutableMap.of("key", "value")); - map.put("list", ImmutableList.of("key", "value")); + Map map = ImmutableMap.builder() + .put("string", "string") + .put("map", ImmutableMap.of("key", "value")) + .put("list", ImmutableList.of("key", "value")) + .put("boolean", true) + .put("number", 1.0) + .build(); assertEquals(json.toJson(map), "{\"string\":\"string\",\"map\":{\"key\":\"value\"},\"list\":[\"key\",\"value\"],\"boolean\":true,\"number\":1.0}"); Map map2 = json.fromJson(json.toJson(map), new TypeLiteral>() { diff --git a/core/src/test/java/org/jclouds/reflect/Reflection2Test.java b/core/src/test/java/org/jclouds/reflect/Reflection2Test.java index f5987cd417..3bed1b26e7 100644 --- a/core/src/test/java/org/jclouds/reflect/Reflection2Test.java +++ b/core/src/test/java/org/jclouds/reflect/Reflection2Test.java @@ -30,6 +30,7 @@ import java.util.SortedSet; import org.testng.annotations.Test; import com.google.common.base.Function; +import com.google.common.base.Predicates; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableSet; import com.google.common.reflect.Invokable; @@ -92,23 +93,55 @@ public class Reflection2Test { assertEquals(methodInSuper.getParameters().get(0).getType().getRawType(), Object.class); } - ImmutableSet setMethods = ImmutableSet.of("add", "equals", "hashCode", "clear", "isEmpty", "contains", - "addAll", "size", "toArray", "iterator", "remove", "removeAll", "containsAll", "retainAll"); + ImmutableSet SET_METHODS = ImmutableSet.of( + // Java 6 and 7 methods + "add", + "addAll", + "clear", + "contains", + "containsAll", + "equals", + "hashCode", + "isEmpty", + "iterator", + "remove", + "removeAll", + "retainAll", + "size", + "toArray", + // Java 8 methods + "forEach", + "parallelStream", + "removeIf", + "spliterator", + "stream"); + + ImmutableSet SORTED_SET_METHODS = ImmutableSet.builder() + .addAll(SET_METHODS) + .add("comparator") + .add("first") + .add("headSet") + .add("last") + .add("subSet") + .add("tailSet") + .build(); public void testMethods() { Set methodNames = FluentIterable.from(methods(Set.class)).transform(invokableToName) - .transform(toStringFunction()).toSet(); + .transform(toStringFunction()) + .filter(Predicates.not(Predicates.in(SET_METHODS))) + .toSet(); - assertEquals(methodNames, setMethods); + assertEquals(methodNames, ImmutableSet.of()); } public void testMethodsSubClass() { Set methodNames = FluentIterable.from(methods(SortedSet.class)).transform(invokableToName) - .transform(toStringFunction()).toSet(); + .transform(toStringFunction()) + .filter(Predicates.not(Predicates.in(SORTED_SET_METHODS))) + .toSet(); - assertEquals(methodNames, - ImmutableSet.builder().add("comparator", "last", "first", "subSet", "headSet", "tailSet") - .addAll(setMethods).build()); + assertEquals(methodNames, ImmutableSet.of()); } static final Function, String> invokableToName = new Function, String>() { diff --git a/core/src/test/java/org/jclouds/utils/TestUtils.java b/core/src/test/java/org/jclouds/utils/TestUtils.java index 19fe36effa..27b7a636b8 100644 --- a/core/src/test/java/org/jclouds/utils/TestUtils.java +++ b/core/src/test/java/org/jclouds/utils/TestUtils.java @@ -26,8 +26,18 @@ public class TestUtils { public static final Object[][] NO_INVOCATIONS = new Object[0][0]; public static final Object[][] SINGLE_NO_ARG_INVOCATION = { new Object[0] }; + public static boolean isJava6() { + System.out.println(System.getProperty("java.version", "None??")); + return System.getProperty("java.version", "").contains("1.6."); + } + public static boolean isJava7() { System.out.println(System.getProperty("java.version", "None??")); return System.getProperty("java.version", "").contains("1.7."); } + + public static boolean isJava8() { + System.out.println(System.getProperty("java.version", "None??")); + return System.getProperty("java.version", "").contains("1.8."); + } }