package org.analogweb.core;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.analogweb.InvocationMetadata;
import org.analogweb.RequestPath;
import org.junit.Before;
import org.junit.Test;

/**
 * @author snowgoose
 */
public class DefaultRouteRegistryTest {

    private DefaultRouteRegistry registry;

    @Before
    public void setUp() throws Exception {
        registry = new DefaultRouteRegistry();
    }

    @Test
    public void testGetActionMethodMetadata() {
        RequestPath requestPath1 = mock(RequestPath.class);
        RequestPath requestPath2 = mock(RequestPath.class);
        RequestPath requestPath3 = mock(RequestPath.class);
        InvocationMetadata metadata1 = mock(InvocationMetadata.class);
        InvocationMetadata metadata2 = mock(InvocationMetadata.class);
        InvocationMetadata metadata3 = mock(InvocationMetadata.class);
        when(requestPath1.match(requestPath1)).thenReturn(true);
        when(requestPath2.match(requestPath2)).thenReturn(true);
        when(requestPath3.match(requestPath3)).thenReturn(true);
        when(metadata1.getDefinedPath()).thenReturn(requestPath1);
        when(metadata2.getDefinedPath()).thenReturn(requestPath2);
        when(metadata3.getDefinedPath()).thenReturn(requestPath3);
        registry.register(metadata1);
        registry.register(metadata2);
        registry.register(metadata3);
        assertThat(registry.findInvocationMetadata(requestPath1), is(metadata1));
        assertThat(registry.findInvocationMetadata(requestPath2), is(metadata2));
        assertThat(registry.findInvocationMetadata(requestPath3), is(metadata3));
    }

    @Test
    public void testGetActionMethodMetadataWithNoMatch() {
        RequestPath requestPath1 = mock(RequestPath.class);
        RequestPath requestPath2 = mock(RequestPath.class);
        InvocationMetadata metadata1 = mock(InvocationMetadata.class);
        when(metadata1.getDefinedPath()).thenReturn(requestPath1);
        registry.register(metadata1);
        when(requestPath1.match(requestPath1)).thenReturn(true);
        when(metadata1.getDefinedPath()).thenReturn(requestPath1);
        assertThat(registry.findInvocationMetadata(requestPath1), is(metadata1));
        assertNull(registry.findInvocationMetadata(requestPath2));
    }

    @Test
    public void testDisposedGetActionMethodMetadataWithNoMatch() {
        RequestPath requestPath1 = mock(RequestPath.class);
        RequestPath requestPath2 = mock(RequestPath.class);
        InvocationMetadata metadata1 = mock(InvocationMetadata.class);
        when(metadata1.getDefinedPath()).thenReturn(requestPath1);
        when(requestPath1.match(requestPath1)).thenReturn(true);
        registry.register(metadata1);
        registry.dispose();
        assertNull(registry.findInvocationMetadata(requestPath1));
        assertNull(registry.findInvocationMetadata(requestPath2));
    }
}

@@ -228,16 +228,38 @@
 .when;%0A%0A
+import java.net.URI;%0A%0A
 import o
@@ -325,16 +325,58 @@
 stPath;%0A
+import org.analogweb.RequestPathMetadata;%0A
 import o
@@ -3111,11 +3111,1143 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testDuplicatePath() %7B%0A        InvocationMetadata meta1 = mock(InvocationMetadata.class);%0A        InvocationMetadata meta2 = mock(InvocationMetadata.class);%0A        RequestPathMetadata path1 = RequestPathDefinition.define(%22/%22, %22/path%22,%0A                new String%5B%5D %7B %22GET%22 %7D);%0A        when(meta1.getDefinedPath()).thenReturn(path1);%0A        RequestPathMetadata path2 = RequestPathDefinition.define(%22/%22, %22/path%22,%0A                new String%5B%5D %7B %22POST%22 %7D);%0A        when(meta2.getDefinedPath()).thenReturn(path2);%0A        registry.register(meta1);%0A        registry.register(meta2);%0A        RequestPath requestPath1 = new DefaultRequestPath(URI.create(%22/%22), URI.create(%22/path%22),%0A                %22GET%22);%0A        when(meta2.getDefinedPath()).thenReturn(path2);%0A        InvocationMetadata actual = registry.findInvocationMetadata(requestPath1);%0A        assertThat(actual, is(meta1));%0A        RequestPath requestPath2 = new DefaultRequestPath(URI.create(%22/%22), URI.create(%22/path%22),%0A                %22POST%22);%0A        actual = registry.findInvocationMetadata(requestPath2);%0A        assertThat(actual, is(meta2));%0A    %7D
 %0A%7D%0A

Add Test case