Java Code Examples for com.google.common.net.InetAddresses#fromInteger()

The following examples show how to use com.google.common.net.InetAddresses#fromInteger() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DnsNameResolverTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void resolveAll_nullResourceResolver() throws Exception {
  final String hostname = "addr.fake";
  final Inet4Address backendAddr = InetAddresses.fromInteger(0x7f000001);

  AddressResolver mockResolver = mock(AddressResolver.class);
  when(mockResolver.resolveAddress(Matchers.anyString()))
      .thenReturn(Collections.<InetAddress>singletonList(backendAddr));
  ResourceResolver resourceResolver = null;
  boolean resovleSrv = true;
  boolean resolveTxt = true;

  ResolutionResults res = DnsNameResolver.resolveAll(
      mockResolver, resourceResolver, resovleSrv, resolveTxt, hostname);
  assertThat(res.addresses).containsExactly(backendAddr);
  assertThat(res.balancerAddresses).isEmpty();
  assertThat(res.txtRecords).isEmpty();
  verify(mockResolver).resolveAddress(hostname);
}
 
Example 2
Source File: GeolocationProcessor.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
InetAddress toAddress(Field field) throws UnknownHostException, OnRecordErrorException {
  switch (field.getType()) {
    case LONG:
    case INTEGER:
      return InetAddresses.fromInteger(field.getValueAsInteger());
    case STRING:
      String ip = field.getValueAsString();
      if (ip == null) {
        throw new OnRecordErrorException(Errors.GEOIP_13);
      }

      ip = ip.trim();
      if (!ip.contains(".") && !ip.contains(":")) {
        return InetAddresses.fromInteger(Integer.parseInt(ip));
      }

      if (!InetAddresses.isInetAddress(ip)) {
        throw new OnRecordErrorException(Errors.GEOIP_06, ip);
      }
      return InetAddresses.forString(ip);
    default:
      throw new IllegalStateException(Utils.format("Unknown field type: ", field.getType()));
  }
}
 
Example 3
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void resolve_nullResourceResolver() throws Exception {
  DnsNameResolver.enableTxt = true;
  InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
  AddressResolver mockAddressResolver = mock(AddressResolver.class);
  when(mockAddressResolver.resolveAddress(anyString()))
      .thenReturn(Collections.singletonList(backendAddr));
  String name = "foo.googleapis.com";

  DnsNameResolver resolver = newResolver(name, 81);
  resolver.setAddressResolver(mockAddressResolver);
  resolver.setResourceResolver(null);
  resolver.start(mockListener);
  assertEquals(1, fakeExecutor.runDueTasks());
  verify(mockListener).onResult(resultCaptor.capture());
  ResolutionResult result = resultCaptor.getValue();
  InetSocketAddress resolvedBackendAddr =
      (InetSocketAddress) Iterables.getOnlyElement(
          Iterables.getOnlyElement(result.getAddresses()).getAddresses());
  assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
  verify(mockAddressResolver).resolveAddress(name);
  assertThat(result.getAttributes()).isEqualTo(Attributes.EMPTY);
  assertThat(result.getServiceConfig()).isNull();
}
 
Example 4
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void resolve_serviceConfigLookupFails_nullServiceConfig() throws Exception {
  DnsNameResolver.enableTxt = true;
  InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
  AddressResolver mockAddressResolver = mock(AddressResolver.class);
  when(mockAddressResolver.resolveAddress(anyString()))
      .thenReturn(Collections.singletonList(backendAddr));
  String name = "foo.googleapis.com";
  ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
  when(mockResourceResolver.resolveTxt(anyString()))
      .thenThrow(new Exception("something like javax.naming.NamingException"));

  DnsNameResolver resolver = newResolver(name, 81);
  resolver.setAddressResolver(mockAddressResolver);
  resolver.setResourceResolver(mockResourceResolver);
  resolver.start(mockListener);
  assertEquals(1, fakeExecutor.runDueTasks());
  verify(mockListener).onResult(resultCaptor.capture());
  ResolutionResult result = resultCaptor.getValue();
  InetSocketAddress resolvedBackendAddr =
      (InetSocketAddress) Iterables.getOnlyElement(
          Iterables.getOnlyElement(result.getAddresses()).getAddresses());
  assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
  verify(mockAddressResolver).resolveAddress(name);
  assertThat(result.getAttributes()).isEqualTo(Attributes.EMPTY);
  assertThat(result.getServiceConfig()).isNull();
  verify(mockResourceResolver).resolveTxt(anyString());
}
 
Example 5
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void resolve_serviceConfigMalformed_serviceConfigError() throws Exception {
  DnsNameResolver.enableTxt = true;
  InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
  AddressResolver mockAddressResolver = mock(AddressResolver.class);
  when(mockAddressResolver.resolveAddress(anyString()))
      .thenReturn(Collections.singletonList(backendAddr));
  String name = "foo.googleapis.com";
  ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
  when(mockResourceResolver.resolveTxt(anyString()))
      .thenReturn(Collections.singletonList("grpc_config=something invalid"));

  DnsNameResolver resolver = newResolver(name, 81);
  resolver.setAddressResolver(mockAddressResolver);
  resolver.setResourceResolver(mockResourceResolver);
  resolver.start(mockListener);
  assertEquals(1, fakeExecutor.runDueTasks());
  verify(mockListener).onResult(resultCaptor.capture());
  ResolutionResult result = resultCaptor.getValue();
  InetSocketAddress resolvedBackendAddr =
      (InetSocketAddress) Iterables.getOnlyElement(
          Iterables.getOnlyElement(result.getAddresses()).getAddresses());
  assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
  verify(mockAddressResolver).resolveAddress(name);
  assertThat(result.getAttributes()).isEqualTo(Attributes.EMPTY);
  assertThat(result.getServiceConfig()).isNotNull();
  assertThat(result.getServiceConfig().getError()).isNotNull();
  verify(mockResourceResolver).resolveTxt(anyString());
}
 
Example 6
Source File: IPUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 从int转换为Inet4Address(仅支持IPV4)
 */
public static Inet4Address fromInt(int address) {
	return InetAddresses.fromInteger(address);
}
 
Example 7
Source File: IPUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 从int转换为Inet4Address(仅支持IPV4)
 */
public static Inet4Address fromInt(int address) {
	return InetAddresses.fromInteger(address);
}
 
Example 8
Source File: IPUtil.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
/**
 * 从int转换为Inet4Address(仅支持IPV4)
 */
public static Inet4Address fromInt(int address) {
	return InetAddresses.fromInteger(address);
}
 
Example 9
Source File: DnsNameResolverTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void resolve_presentResourceResolver() throws Exception {
  DnsNameResolver.enableTxt = true;
  InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
  AddressResolver mockAddressResolver = mock(AddressResolver.class);
  when(mockAddressResolver.resolveAddress(anyString()))
      .thenReturn(Collections.singletonList(backendAddr));
  ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
  when(mockResourceResolver.resolveTxt(anyString()))
      .thenReturn(
          Collections.singletonList(
              "grpc_config=[{\"clientLanguage\": [\"java\"], \"serviceConfig\": {}}]"));
  ServiceConfigParser serviceConfigParser = new ServiceConfigParser() {
    @Override
    public ConfigOrError parseServiceConfig(Map<String, ?> rawServiceConfig) {
      return ConfigOrError.fromConfig(rawServiceConfig);
    }
  };
  NameResolver.Args args =
      NameResolver.Args.newBuilder()
          .setDefaultPort(DEFAULT_PORT)
          .setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR)
          .setSynchronizationContext(syncContext)
          .setServiceConfigParser(serviceConfigParser)
          .build();

  String name = "foo.googleapis.com";
  DnsNameResolver resolver = newResolver(name, Stopwatch.createUnstarted(), false, args);
  resolver.setAddressResolver(mockAddressResolver);
  resolver.setResourceResolver(mockResourceResolver);

  resolver.start(mockListener);
  assertEquals(1, fakeExecutor.runDueTasks());
  verify(mockListener).onResult(resultCaptor.capture());
  ResolutionResult result = resultCaptor.getValue();
  InetSocketAddress resolvedBackendAddr =
      (InetSocketAddress) Iterables.getOnlyElement(
          Iterables.getOnlyElement(result.getAddresses()).getAddresses());
  assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
  assertThat(result.getServiceConfig().getConfig()).isNotNull();
  verify(mockAddressResolver).resolveAddress(name);
  verify(mockResourceResolver).resolveTxt("_grpc_config." + name);
}
 
Example 10
Source File: IPAddress.java    From arctic-sea with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@code IPAddress} from an 32-Bit integer.
 *
 * @param address the address
 */
public IPAddress(int address) {
    this.address = InetAddresses.fromInteger(address);
}