PageRenderTime 31ms CodeModel.GetById 24ms app.highlight 4ms 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 */
 19package org.jclouds.config;
 20
 21import static org.easymock.EasyMock.createMock;
 22import static org.testng.Assert.assertEquals;
 23
 24import javax.inject.Inject;
 25
 26import org.jclouds.domain.Credentials;
 27import org.jclouds.http.IntegrationTestAsyncClient;
 28import org.jclouds.http.IntegrationTestClient;
 29import org.jclouds.providers.AnonymousProviderMetadata;
 30import org.jclouds.providers.ProviderMetadata;
 31import org.jclouds.providers.config.BindProviderMetadataContextAndCredentials;
 32import org.jclouds.rest.RestApiMetadata;
 33import org.jclouds.rest.RestContext;
 34import org.jclouds.rest.Utils;
 35import org.jclouds.rest.internal.BaseRestApiTest.MockModule;
 36import org.testng.annotations.Test;
 37
 38import com.google.common.reflect.TypeToken;
 39import com.google.inject.AbstractModule;
 40import com.google.inject.Guice;
 41import com.google.inject.Injector;
 42
 43/**
 44 * 
 45 * @author Adrian Cole
 46 */
 47@Test(groups = "unit", testName = "BindRestContextWithWildcardExtendsExplicitAndRawTypeTest")
 48public class BindRestContextWithWildcardExtendsExplicitAndRawTypeTest {
 49
 50   @SuppressWarnings( { "unused", "unchecked" })
 51   private static class ExpectedBindings {
 52
 53      private final RestContext raw;
 54      private final RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit;
 55
 56      @Inject
 57      public ExpectedBindings(RestContext raw,
 58               RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit) {
 59         this.raw = raw;
 60         this.explicit = explicit;
 61      }
 62
 63   }
 64
 65   @Test
 66   public void testRawAndExplicit() {
 67      ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
 68               IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
 69
 70      ExpectedBindings bindings = injectorFor(md).getInstance(ExpectedBindings.class);
 71      assertEquals(bindings.raw, bindings.explicit);
 72   }
 73
 74   private Injector injectorFor(ProviderMetadata md) {
 75      return Guice.createInjector(
 76               new BindNameToContext("test"),
 77               new BindProviderMetadataContextAndCredentials(md, new Credentials("user", "pass")),
 78               new BindRestContextWithWildcardExtendsExplicitAndRawType(RestApiMetadata.class.cast(md
 79                                 .getApiMetadata())),
 80                                 
 81               // stuff needed for RestContextImpl
 82               new MockModule(),
 83               new AbstractModule() {
 84                  
 85                  @Override
 86                  protected void configure() {
 87                     bind(Utils.class).toInstance(createMock(Utils.class));
 88                     bind(IntegrationTestClient.class).toInstance(createMock(IntegrationTestClient.class));
 89                     bind(IntegrationTestAsyncClient.class).toInstance(createMock(IntegrationTestAsyncClient.class));
 90                  }
 91               });
 92   }
 93
 94   @SuppressWarnings( { "unused", "unchecked" })
 95   private static class ExpectedBindingsWithWildCardExtends {
 96
 97      private final RestContext raw;
 98      private final RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit;
 99      private final RestContext<? extends IntegrationTestClient, ? extends IntegrationTestAsyncClient> wildcardExtends;
100
101      @Inject
102      public ExpectedBindingsWithWildCardExtends(RestContext raw,
103               RestContext<IntegrationTestClient, IntegrationTestAsyncClient> explicit,
104               RestContext<? extends IntegrationTestClient, ? extends IntegrationTestAsyncClient> wildcardExtends) {
105         this.raw = raw;
106         this.explicit = explicit;
107         this.wildcardExtends = wildcardExtends;
108      }
109
110   }
111
112   @SuppressWarnings("unchecked")
113   @Test
114   public void testRawExplicitAndWildCardExtends() {
115      ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(
116               IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost");
117
118      TypeToken wildCardExtendsType = new TypeToken<RestContext<? extends IntegrationTestClient, ? extends IntegrationTestAsyncClient>>() {
119         private static final long serialVersionUID = -8170268554700397860L;
120      };
121      
122      md = md.toBuilder().apiMetadata(md.getApiMetadata().toBuilder().context(wildCardExtendsType).build()).build();
123
124      ExpectedBindingsWithWildCardExtends bindings = injectorFor(md).getInstance(ExpectedBindingsWithWildCardExtends.class);
125      assertEquals(bindings.raw, bindings.explicit);
126      assertEquals(bindings.explicit, bindings.wildcardExtends);
127   }
128}