Package se.dannej.fakehttpserver.expect

Source Code of se.dannej.fakehttpserver.expect.ExpectationTest

/*
* Copyright 2011 Daniel Josefsson
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package se.dannej.fakehttpserver.expect;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static se.dannej.fakehttpserver.expect.DummyFakeRequest.newRequest;
import static se.dannej.fakehttpserver.expect.DummyFakeResponse.newResponse;
import static se.dannej.fakehttpserver.expect.cardinality.Cardinalities.allowing;
import static se.dannej.fakehttpserver.expect.cardinality.Cardinalities.oneOf;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Test;
import org.junit.runner.RunWith;

import se.dannej.fakehttpserver.FakeHttpServletRequest;
import se.dannej.fakehttpserver.FakeHttpServletResponse;
import se.dannej.fakehttpserver.expect.matcher.RequestMatcher;

@RunWith(JMock.class)
public class ExpectationTest {
  private static final FakeHttpServletRequest VALID_REQUEST = newRequest();
  private static final FakeHttpServletResponse VALID_RESPONSE = newResponse();

  private final Mockery context = new JUnit4Mockery();
 
  private final Expectation expectation = new Expectation();
  private final ResponseAction action1 = context.mock(ResponseAction.class, "action-1");
  private final ResponseAction action2 = context.mock(ResponseAction.class, "action-2");
 
  public ExpectationTest() {
    expectation.setcardinality(allowing());
  }
 
  @Test
  public void doesNotdelegateHandleToActionWhenExpectationNotMet() {
    expectation.addAction(action1);

    allowingRequestMatcherReturns(false);
    context.checking(new Expectations() {{
      never(action1);
    }});
   
    expectation.handle(VALID_REQUEST, VALID_RESPONSE);
  }
 
  @Test
  public void returnsFalseWhenExpectationNotMet() {
    allowingRequestMatcherReturns(false);
   
    assertThat(expectation.handle(VALID_REQUEST, VALID_RESPONSE), is(false));
  }
 
  @Test
  public void returnsTrueWhenExpectationMet() {
    allowingRequestMatcherReturns(true);
   
    assertThat(expectation.handle(VALID_REQUEST, VALID_RESPONSE), is(true));
  }
 
  @Test
  public void delegatesHandleToActionsWhenExpectationMet() {
    expectation.addAction(action1);
    expectation.addAction(action2);
   
    allowingRequestMatcherReturns(true);
    context.checking(new Expectations() {{
      allowing(action1).applyTo(VALID_REQUEST, VALID_RESPONSE);
      allowing(action2).applyTo(VALID_REQUEST, VALID_RESPONSE);
    }});
   
    expectation.handle(VALID_REQUEST, VALID_RESPONSE);
  }
 
  @Test
  public void isSatisfiedReturnsFalseWhenNotSatisfied() {
    expectation.setcardinality(oneOf());
   
    assertThat(expectation.isSatisfied(new StringDescription()), is(false));
  }

  @Test
  public void isSatisfiedReturnsTrueWhenSatisfied() {
    expectation.setcardinality(oneOf());
   
    allowingRequestMatcherReturns(true);

    expectation.handle(VALID_REQUEST, VALID_RESPONSE);

    assertThat(expectation.isSatisfied(new StringDescription()), is(true));
  }

  private void allowingRequestMatcherReturns(final boolean returnValue) {
    expectation.addRequestMatcher(requestMatcherReturning(returnValue));
  }
 
  private Matcher<FakeHttpServletRequest> requestMatcherReturning(final boolean returnValue) {
    return new RequestMatcher() {
     
      @Override
      public void describeTo(Description paramDescription) {
        paramDescription.appendText("request matcher returning " + returnValue);
      }
     
      @Override
      public boolean matchesSafely(FakeHttpServletRequest request) {
        return returnValue;
      }
    };
  }
}
TOP

Related Classes of se.dannej.fakehttpserver.expect.ExpectationTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.