PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/org/jclouds/config/BindRestContextWithWildcardExtendsExplicitAndRawTypeTest.java

https://github.com/andreisavu/jclouds
Java | 128 lines | 83 code | 22 blank | 23 comment | 0 complexity | 9f2c5390d88938e3edbf4a3ec5b9ab15 MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.config;
  20. import static org.easymock.EasyMock.createMock;
  21. import static org.testng.Assert.assertEquals;
  22. import javax.inject.Inject;
  23. import org.jclouds.domain.Credentials;
  24. import org.jclouds.http.IntegrationTestAsyncClient;
  25. import org.jclouds.http.IntegrationTestClient;
  26. import org.jclouds.providers.AnonymousProviderMetadata;
  27. import org.jclouds.providers.ProviderMetadata;
  28. import org.jclouds.providers.config.BindProviderMetadataContextAndCredentials;
  29. import org.jclouds.rest.RestApiMetadata;
  30. import org.jclouds.rest.RestContext;
  31. import org.jclouds.rest.Utils;
  32. import org.jclouds.rest.internal.BaseRestApiTest.MockModule;
  33. import org.testng.annotations.Test;
  34. import com.google.common.reflect.TypeToken;
  35. import com.google.inject.AbstractModule;
  36. import com.google.inject.Guice;
  37. import com.google.inject.Injector;
  38. /**
  39. *
  40. * @author Adrian Cole
  41. */
  42. @Test(groups = "unit", testName = "BindRestContextWithWildcardExtendsExplicitAndRawTypeTest")
  43. public class BindRestContextWithWildcardExtendsExplicitAndRawTypeTest {
  44. @SuppressWarnings( { "unused", "unchecked" })
  45. private static class ExpectedBindings {
  46. private final RestContext raw;
  47. private final RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit;
  48. @Inject
  49. public ExpectedBindings(RestContext raw,
  50. RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit) {
  51. this.raw = raw;
  52. this.explicit = explicit;
  53. }
  54. }
  55. @Test
  56. public void testRawAndExplicit() {
  57. ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
  58. IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
  59. ExpectedBindings bindings = injectorFor(md).getInstance(ExpectedBindings.class);
  60. assertEquals(bindings.raw, bindings.explicit);
  61. }
  62. private Injector injectorFor(ProviderMetadata md) {
  63. return Guice.createInjector(
  64. new BindNameToContext("test"),
  65. new BindProviderMetadataContextAndCredentials(md, new Credentials("user", "pass")),
  66. new BindRestContextWithWildcardExtendsExplicitAndRawType(RestApiMetadata.class.cast(md
  67. .getApiMetadata())),
  68. // stuff needed for RestContextImpl
  69. new MockModule(),
  70. new AbstractModule() {
  71. @Override
  72. protected void configure() {
  73. bind(Utils.class).toInstance(createMock(Utils.class));
  74. bind(IntegrationTestClient.class).toInstance(createMock(IntegrationTestClient.class));
  75. bind(IntegrationTestAsyncClient.class).toInstance(createMock(IntegrationTestAsyncClient.class));
  76. }
  77. });
  78. }
  79. @SuppressWarnings( { "unused", "unchecked" })
  80. private static class ExpectedBindingsWithWildCardExtends {
  81. private final RestContext raw;
  82. private final RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit;
  83. private final RestContext<? extends IntegrationTestClient, ? extends IntegrationTestAsyncClient> wildcardExtends;
  84. @Inject
  85. public ExpectedBindingsWithWildCardExtends(RestContext raw,
  86. RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit,
  87. RestContext<? extends IntegrationTestClient, ? extends IntegrationTestAsyncClient> wildcardExtends) {
  88. this.raw = raw;
  89. this.explicit = explicit;
  90. this.wildcardExtends = wildcardExtends;
  91. }
  92. }
  93. @SuppressWarnings("unchecked")
  94. @Test
  95. public void testRawExplicitAndWildCardExtends() {
  96. ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
  97. IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
  98. TypeToken wildCardExtendsType = new TypeToken<RestContext<? extends IntegrationTestClient, ? extends IntegrationTestAsyncClient>>() {
  99. private static final long serialVersionUID = -8170268554700397860L;
  100. };
  101. md = md.toBuilder().apiMetadata(md.getApiMetadata().toBuilder().context(wildCardExtendsType).build()).build();
  102. ExpectedBindingsWithWildCardExtends bindings = injectorFor(md).getInstance(ExpectedBindingsWithWildCardExtends.class);
  103. assertEquals(bindings.raw, bindings.explicit);
  104. assertEquals(bindings.explicit, bindings.wildcardExtends);
  105. }
  106. }