001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.servlet.result;
018
019import org.hamcrest.Matcher;
020
021import org.springframework.test.web.servlet.MvcResult;
022import org.springframework.test.web.servlet.ResultMatcher;
023
024import static org.hamcrest.MatcherAssert.*;
025import static org.springframework.test.util.AssertionErrors.*;
026
027/**
028 * Factory for "output" flash attribute assertions.
029 *
030 * <p>An instance of this class is typically accessed via
031 * {@link MockMvcResultMatchers#flash}.
032 *
033 * @author Rossen Stoyanchev
034 * @since 3.2
035 */
036public class FlashAttributeResultMatchers {
037
038        /**
039         * Protected constructor.
040         * Use {@link MockMvcResultMatchers#flash()}.
041         */
042        protected FlashAttributeResultMatchers() {
043        }
044
045
046        /**
047         * Assert a flash attribute's value with the given Hamcrest {@link Matcher}.
048         */
049        public <T> ResultMatcher attribute(final String name, final Matcher<T> matcher) {
050                return new ResultMatcher() {
051                        @Override
052                        @SuppressWarnings("unchecked")
053                        public void match(MvcResult result) throws Exception {
054                                assertThat("Flash attribute", (T) result.getFlashMap().get(name), matcher);
055                        }
056                };
057        }
058
059        /**
060         * Assert a flash attribute's value.
061         */
062        public <T> ResultMatcher attribute(final String name, final Object value) {
063                return new ResultMatcher() {
064                        @Override
065                        public void match(MvcResult result) throws Exception {
066                                assertEquals("Flash attribute", value, result.getFlashMap().get(name));
067                        }
068                };
069        }
070
071        /**
072         * Assert the existence of the given flash attributes.
073         */
074        public <T> ResultMatcher attributeExists(final String... names) {
075                return new ResultMatcher() {
076                        @Override
077                        public void match(MvcResult result) throws Exception {
078                                for (String name : names) {
079                                        assertTrue("Flash attribute [" + name + "] does not exist", result.getFlashMap().get(name) != null);
080                                }
081                        }
082                };
083        }
084
085        /**
086         * Assert the number of flash attributes.
087         */
088        public <T> ResultMatcher attributeCount(final int count) {
089                return new ResultMatcher() {
090                        @Override
091                        public void match(MvcResult result) throws Exception {
092                                assertEquals("FlashMap size", count, result.getFlashMap().size());
093                        }
094                };
095        }
096
097}