Example usage for io.netty.util AttributeKey valueOf

List of usage examples for io.netty.util AttributeKey valueOf

Introduction

In this page you can find the example usage for io.netty.util AttributeKey valueOf.

Prototype

@SuppressWarnings("unchecked")
    public static <T> AttributeKey<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) 

Source Link

Usage

From source file:com.linecorp.armeria.client.DefaultClientRequestContextTest.java

License:Apache License

@Test
public void deriveContext() {
    final DefaultClientRequestContext originalCtx = new DefaultClientRequestContext(mock(EventLoop.class),
            NoopMeterRegistry.get(), SessionProtocol.H2C, Endpoint.of("example.com", 8080), HttpMethod.POST,
            "/foo", null, null, ClientOptions.DEFAULT, mock(Request.class));

    final AttributeKey<String> foo = AttributeKey.valueOf(DefaultClientRequestContextTest.class, "foo");
    originalCtx.attr(foo).set("foo");

    Request newRequest = mock(Request.class);
    final ClientRequestContext derivedCtx = originalCtx.newDerivedContext(newRequest);
    assertThat(derivedCtx.endpoint()).isSameAs(originalCtx.endpoint());
    assertThat(derivedCtx.sessionProtocol()).isSameAs(originalCtx.sessionProtocol());
    assertThat(derivedCtx.method()).isSameAs(originalCtx.method());
    assertThat(derivedCtx.options()).isSameAs(originalCtx.options());
    assertThat(derivedCtx.<Request>request()).isSameAs(newRequest);

    assertThat(derivedCtx.path()).isEqualTo(originalCtx.path());
    assertThat(derivedCtx.maxResponseLength()).isEqualTo(originalCtx.maxResponseLength());
    assertThat(derivedCtx.responseTimeoutMillis()).isEqualTo(originalCtx.responseTimeoutMillis());
    assertThat(derivedCtx.writeTimeoutMillis()).isEqualTo(originalCtx.writeTimeoutMillis());
    // the attribute is derived as well
    assertThat(derivedCtx.attr(foo).get()).isEqualTo("foo");

    // log is different
    assertThat(derivedCtx.log()).isNotSameAs(originalCtx.log());

    final AttributeKey<String> bar = AttributeKey.valueOf(DefaultClientRequestContextTest.class, "bar");
    originalCtx.attr(bar).set("bar");

    // the Attribute added to the original context after creation is not propagated to the derived context
    assertThat(derivedCtx.attr(bar).get()).isEqualTo(null);
}

From source file:com.linecorp.armeria.common.logback.RequestContextExportingAppenderTest.java

License:Apache License

@Test
public void testMutabilityAndImmutability() {
    final AttributeKey<Object> someAttr = AttributeKey.valueOf(RequestContextExportingAppenderTest.class,
            "SOME_ATTR");
    final RequestContextExportingAppender a = new RequestContextExportingAppender();

    // Ensure mutability before start.
    a.addBuiltIn(BuiltInProperty.ELAPSED_NANOS);
    assertThat(a.getBuiltIns()).containsExactly(BuiltInProperty.ELAPSED_NANOS);

    a.addAttribute("some-attr", someAttr);
    assertThat(a.getAttributes()).containsOnlyKeys("some-attr").containsValue(someAttr);

    a.addHttpRequestHeader(HttpHeaderNames.USER_AGENT);
    assertThat(a.getHttpRequestHeaders()).containsExactly(HttpHeaderNames.USER_AGENT);

    a.addHttpResponseHeader(HttpHeaderNames.SET_COOKIE);
    assertThat(a.getHttpResponseHeaders()).containsExactly(HttpHeaderNames.SET_COOKIE);

    final ListAppender<ILoggingEvent> la = new ListAppender<>();
    a.addAppender(la);//  w w  w .  ja  v a 2s  .  co m
    a.start();
    la.start();

    // Ensure immutability after start.
    assertThatThrownBy(() -> a.addBuiltIn(BuiltInProperty.REQ_PATH))
            .isExactlyInstanceOf(IllegalStateException.class);

    assertThatThrownBy(() -> a.addAttribute("my-attr", MY_ATTR))
            .isExactlyInstanceOf(IllegalStateException.class);

    assertThatThrownBy(() -> a.addHttpRequestHeader(HttpHeaderNames.ACCEPT))
            .isExactlyInstanceOf(IllegalStateException.class);

    assertThatThrownBy(() -> a.addHttpResponseHeader(HttpHeaderNames.DATE))
            .isExactlyInstanceOf(IllegalStateException.class);
}

From source file:com.linecorp.armeria.internal.DefaultAttributeMapTest.java

License:Apache License

@Test
public void testIteratorWithSparseMap() {
    final AttributeKey<Integer> key = AttributeKey.valueOf(DefaultAttributeMap.class, "KEY");
    map.attr(key).set(42);/*from w  w  w  .  j  av  a 2s.  c o m*/

    final List<Attribute<?>> attrs = Lists.newArrayList(map.attrs());
    assertEquals(Collections.singletonList(map.attr(key)), attrs);

    map.attr(key).remove();
    assertFalse(map.attrs().hasNext());
}

From source file:com.linecorp.armeria.internal.DefaultAttributeMapTest.java

License:Apache License

@Test
public void testIteratorWithFullMap() {
    final List<AttributeKey<Integer>> expectedKeys = new ArrayList<>();
    for (int i = 0; i < 1024; i++) {
        final AttributeKey<Integer> key = AttributeKey.valueOf(DefaultAttributeMapTest.class,
                String.valueOf(i));
        expectedKeys.add(key);//from w w w.  jav a 2s .  c  om
        map.attr(key).set(i);
    }

    // Make sure all buckets are filled.
    for (int i = 0; i < map.attributes.length(); i++) {
        assertNotNull(map.attributes.get(i));
    }

    // Make sure the Iterator yields all attributes.
    assertEquals(expectedKeys, actualKeys());

    // Make sure the Iterator does not yield the attributes whose 'removed' property is 'true'.
    for (int i = 0; i < map.attributes.length(); i++) {
        Attribute<?> a = map.attributes.get(i);
        a.remove();

        // A head attribute is never removed from the linked list.
        assertSame(a, map.attributes.get(i));

        // Remove the removed key from the list of expected expectedKeys.
        expectedKeys.remove(a.key());
    }

    assertEquals(expectedKeys, actualKeys());
}

From source file:com.linecorp.armeria.server.DefaultServiceRequestContextTest.java

License:Apache License

@Test
public void deriveContext() {
    final VirtualHost virtualHost = virtualHost();
    final DefaultPathMappingContext mappingCtx = new DefaultPathMappingContext(virtualHost, "example.com",
            HttpMethod.GET, "/hello", null, MediaType.JSON_UTF_8,
            ImmutableList.of(MediaType.JSON_UTF_8, MediaType.XML_UTF_8));

    final ServiceRequestContext originalCtx = new DefaultServiceRequestContext(
            virtualHost.serviceConfigs().get(0), mock(Channel.class), NoopMeterRegistry.get(),
            SessionProtocol.H2, mappingCtx, PathMappingResult.of("/foo", null, ImmutableMap.of()),
            mock(Request.class), null, null);

    final AttributeKey<String> foo = AttributeKey.valueOf(DefaultServiceRequestContextTest.class, "foo");
    originalCtx.attr(foo).set("foo");

    Request newRequest = mock(Request.class);
    final ServiceRequestContext derivedCtx = originalCtx.newDerivedContext(newRequest);
    assertThat(derivedCtx.server()).isSameAs(originalCtx.server());
    assertThat(derivedCtx.sessionProtocol()).isSameAs(originalCtx.sessionProtocol());
    assertThat(derivedCtx.<Service<HttpRequest, HttpResponse>>service()).isSameAs(originalCtx.service());
    assertThat(derivedCtx.pathMapping()).isSameAs(originalCtx.pathMapping());
    assertThat(derivedCtx.<Request>request()).isSameAs(newRequest);

    assertThat(derivedCtx.path()).isEqualTo(originalCtx.path());
    assertThat(derivedCtx.maxRequestLength()).isEqualTo(originalCtx.maxRequestLength());
    assertThat(derivedCtx.requestTimeoutMillis()).isEqualTo(originalCtx.requestTimeoutMillis());
    // the attribute is derived as well
    assertThat(derivedCtx.attr(foo).get()).isEqualTo("foo");

    // log is different
    assertThat(derivedCtx.log()).isNotSameAs(originalCtx.log());

    final AttributeKey<String> bar = AttributeKey.valueOf(DefaultServiceRequestContextTest.class, "bar");
    originalCtx.attr(bar).set("bar");

    // the Attribute added to the original context after creation is not propagated to the derived context
    assertThat(derivedCtx.attr(bar).get()).isEqualTo(null);
}

From source file:com.turo.pushy.apns.AugmentingReflectiveChannelFactoryTest.java

License:Open Source License

@Test
public void testNewChannel() {
    final AttributeKey<String> attributeKey = AttributeKey.valueOf(getClass(), "attributeKey");

    final AugmentingReflectiveChannelFactory<LocalChannel, String> factory = new AugmentingReflectiveChannelFactory<>(
            LocalChannel.class, attributeKey, "Test!");

    final LocalChannel channel = factory.newChannel();

    assertTrue("Newly-created channels should have attribute provided to factory.",
            channel.hasAttr(attributeKey));
}