001package ch.qos.logback.access.spi;
002
003import static org.junit.Assert.*;
004
005import java.util.Map;
006
007import org.junit.After;
008import org.junit.Before;
009import org.junit.Test;
010
011import ch.qos.logback.access.dummy.DummyAccessEventBuilder;
012import ch.qos.logback.access.dummy.DummyRequest;
013import ch.qos.logback.core.testUtil.RandomUtil;
014
015public class AccessEventTest {
016
017    int diff = RandomUtil.getPositiveInt();
018    
019    @Before
020    public void setUp() throws Exception {
021    }
022
023    @After
024    public void tearDown() throws Exception {
025    }
026
027    // See LOGBACK-1189
028    @Test
029    public void callingPrepareForDeferredProcessingShouldBeIdempotent() {
030        String key = "key-"+diff;
031        String val = "val-"+diff;
032        
033        IAccessEvent ae = DummyAccessEventBuilder.buildNewAccessEvent();
034        DummyRequest request = (DummyRequest) ae.getRequest();
035        Map<String, String> headersMap = request.getHeaders();
036        Map<String, String[]> parametersMap = request.getParameterMap();
037        
038        headersMap.put(key, val);
039        request.setAttribute(key, val);
040        parametersMap.put(key, new String[] {val});
041        ae.prepareForDeferredProcessing();
042        assertEquals(val, ae.getAttribute(key));
043        assertEquals(val, ae.getRequestHeader(key));
044        assertEquals(val, ae.getRequestParameter(key)[0]);
045        
046        
047        request.setAttribute(key, "change");
048        headersMap.put(key, "change");
049        parametersMap.put(key, new String[] {"change"});
050        ae.prepareForDeferredProcessing();
051        assertEquals(val, ae.getAttribute(key));
052        assertEquals(val, ae.getRequestHeader(key));
053        assertEquals(val, ae.getRequestParameter(key)[0]);
054        
055    }
056
057}