Package scenic3

Source Code of scenic3.ScenicControllerTest

package scenic3;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.concurrent.atomic.AtomicBoolean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Errors;
import org.slim3.tester.MockHttpServletRequest;
import org.slim3.tester.MockHttpServletResponse;
import org.slim3.tester.MockServletContext;

/**
* Test for {@link ScenicController}
* @author shuji.w6e
* @since 0.1.0
*/
public class ScenicControllerTest {

    @Test
    public void setupPage() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page);
        target.setupPage(page);
        assertThat(page.controller, is(sameInstance((ScenicController) target)));
        assertThat(page.basePath, is(sameInstance(target.getBasePath())));
        assertThat(page.request, is(sameInstance(target.getRequest())));
        assertThat(page.response, is(sameInstance(target.getResponse())));
        assertThat(page.errors, is(sameInstance(target.getErrors())));
        assertThat(page.response, is(sameInstance(target.getResponse())));
    }

    @Test
    public void setup() throws Throwable {
        final AtomicBoolean pathSetup = new AtomicBoolean(false);
        ScenicPage page = new ScenicPage() {
            @Override
            protected Navigation setUp() {
                pathSetup.set(true);
                assertThat(request, is(notNullValue()));
                assertThat(response, is(notNullValue()));
                assertThat(servletContext, is(notNullValue()));
                return super.setUp();
            }
        };
        TestScenicController target = new TestScenicController(page) {;
            protected Navigation run() throws Exception {
                // setUpが呼ばれている事を確認
                assertThat(pathSetup.get(), is(true));
                return null;
            }
        };
        target.runBare();
    }

    @Test
    public void paramAsString() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "aaa");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsString("foo", null), is("aaa"));
        assertThat(target.paramAsString("foo", ""), is("aaa"));
        assertThat(target.paramAsString("foo", "default"), is("aaa"));
        assertThat(target.paramAsString("bar", null), is(""));
        assertThat(target.paramAsString("bar", ""), is(""));
        assertThat(target.paramAsString("bar", "default"), is(""));
        assertThat(target.paramAsString("poo", null), is(nullValue()));
        assertThat(target.paramAsString("poo", ""), is(""));
        assertThat(target.paramAsString("poo", "default"), is("default"));
    }

    @Test
    public void paramsAsString() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", new String[]{"aaa", "bbb"});
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };

        String[] empty = new String[0];
        String[] dflt = new String[]{"default"};
        assertThat(target.paramsAsString("foo", null), arrayContaining("aaa", "bbb"));
        assertThat(target.paramsAsString("foo", empty), arrayContaining("aaa", "bbb"));
        assertThat(target.paramsAsString("foo", dflt), arrayContaining("aaa", "bbb"));
        assertThat(target.paramsAsString("bar", null), arrayContaining(""));
        assertThat(target.paramsAsString("bar", empty), arrayContaining(""));
        assertThat(target.paramsAsString("bar", dflt), arrayContaining(""));
        assertThat(target.paramsAsString("poo", null), is(nullValue()));
        assertThat(target.paramsAsString("poo", empty), is(emptyArray()));
        assertThat(target.paramsAsString("poo", dflt), arrayContaining("default"));
    }

    @Test
    public void paramAsInteger() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "1000");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsInteger("foo", null), is(1000));
        assertThat(target.paramAsInteger("foo", ""), is(1000));
        assertThat(target.paramAsInteger("foo", "1"), is(1000));
        assertThat(target.paramAsInteger("bar", null), is(nullValue()));
        assertThat(target.paramAsInteger("bar", ""), is(nullValue()));
        assertThat(target.paramAsInteger("bar", "1"), is(nullValue()));
        assertThat(target.paramAsInteger("poo", null), is(nullValue()));
        assertThat(target.paramAsInteger("poo", ""), is(nullValue()));
        assertThat(target.paramAsInteger("poo", "1"), is(1));
    }
   
    @Test
    public void paramAsPrimitiveInt() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "1000");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsPrimitiveInt("foo", null), is(1000));
        assertThat(target.paramAsPrimitiveInt("foo", ""), is(1000));
        assertThat(target.paramAsPrimitiveInt("foo", "1"), is(1000));
        assertThat(target.paramAsPrimitiveInt("bar", null), is(0));
        assertThat(target.paramAsPrimitiveInt("bar", ""), is(0));
        assertThat(target.paramAsPrimitiveInt("bar", "1"), is(0));
        assertThat(target.paramAsPrimitiveInt("poo", null), is(0));
        assertThat(target.paramAsPrimitiveInt("poo", ""), is(0));
        assertThat(target.paramAsPrimitiveInt("poo", "1"), is(1));
    }

    @Test
    public void paramAsLong() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "1000");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsLong("foo", null), is(1000L));
        assertThat(target.paramAsLong("foo", ""), is(1000L));
        assertThat(target.paramAsLong("foo", "1"), is(1000L));
        assertThat(target.paramAsLong("bar", null), is(nullValue()));
        assertThat(target.paramAsLong("bar", ""), is(nullValue()));
        assertThat(target.paramAsLong("bar", "1"), is(nullValue()));
        assertThat(target.paramAsLong("poo", null), is(nullValue()));
        assertThat(target.paramAsLong("poo", ""), is(nullValue()));
        assertThat(target.paramAsLong("poo", "1"), is(1L));
    }

    @Test
    public void paramAsPrimitiveLong() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "1000");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsPrimitiveLong("foo", null), is(1000L));
        assertThat(target.paramAsPrimitiveLong("foo", ""), is(1000L));
        assertThat(target.paramAsPrimitiveLong("foo", "1"), is(1000L));
        assertThat(target.paramAsPrimitiveLong("bar", null), is(0L));
        assertThat(target.paramAsPrimitiveLong("bar", ""), is(0L));
        assertThat(target.paramAsPrimitiveLong("bar", "1"), is(0L));
        assertThat(target.paramAsPrimitiveLong("poo", null), is(0L));
        assertThat(target.paramAsPrimitiveLong("poo", ""), is(0L));
        assertThat(target.paramAsPrimitiveLong("poo", "1"), is(1L));
    }

    @Test
    public void paramAsBoolean() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "true");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsBoolean("foo", null), is(true));
        assertThat(target.paramAsBoolean("foo", ""), is(true));
        assertThat(target.paramAsBoolean("foo", "false"), is(true));
        assertThat(target.paramAsBoolean("bar", null), is(false));    //BooleanUtil.toBoolean("") returns false
        assertThat(target.paramAsBoolean("bar", ""), is(false));      //BooleanUtil.toBoolean("") returns false
        assertThat(target.paramAsBoolean("bar", "true"), is(false))//BooleanUtil.toBoolean("") returns false
        assertThat(target.paramAsBoolean("poo", null), is(nullValue()));
        assertThat(target.paramAsBoolean("poo", ""), is(false));      //BooleanUtil.toBoolean("") returns false
        assertThat(target.paramAsBoolean("poo", "true"), is(true));
    }

    @Test
    public void paramAsPrimitiveBoolean() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "true");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsPrimitiveBoolean("foo", null), is(true));
        assertThat(target.paramAsPrimitiveBoolean("foo", ""), is(true));
        assertThat(target.paramAsPrimitiveBoolean("foo", "false"), is(true));
        assertThat(target.paramAsPrimitiveBoolean("bar", null), is(false));
        assertThat(target.paramAsPrimitiveBoolean("bar", ""), is(false));
        assertThat(target.paramAsPrimitiveBoolean("bar", "true"), is(false));
        assertThat(target.paramAsPrimitiveBoolean("poo", null), is(false));
        assertThat(target.paramAsPrimitiveBoolean("poo", ""), is(false));
        assertThat(target.paramAsPrimitiveBoolean("poo", "true"), is(true));
    }

    @Test
    public void paramAsDouble() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "1234.5");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsDouble("foo", null), is(1234.5));
        assertThat(target.paramAsDouble("foo", ""), is(1234.5));
        assertThat(target.paramAsDouble("foo", "1.2"), is(1234.5));
        assertThat(target.paramAsDouble("bar", null), is(nullValue()));
        assertThat(target.paramAsDouble("bar", ""), is(nullValue()));
        assertThat(target.paramAsDouble("bar", "1.2"), is(nullValue()));
        assertThat(target.paramAsDouble("poo", null), is(nullValue()));
        assertThat(target.paramAsDouble("poo", ""), is(nullValue()));
        assertThat(target.paramAsDouble("poo", "1.2"), is(1.2));
    }

    @Test
    public void paramAsPrimitiveDouble() throws Exception {
        ScenicPage page = new ScenicPage() {
        };
        TestScenicController target = new TestScenicController(page) {
            {
                ((MockHttpServletRequest)request).setParameter("foo", "1234.5");
                ((MockHttpServletRequest)request).setParameter("bar", "");
            }
        };
        assertThat(target.paramAsPrimitiveDouble("foo", null), is(1234.5));
        assertThat(target.paramAsPrimitiveDouble("foo", ""), is(1234.5));
        assertThat(target.paramAsPrimitiveDouble("foo", "1.2"), is(1234.5));
        assertThat(target.paramAsPrimitiveDouble("bar", null), is(0.0));
        assertThat(target.paramAsPrimitiveDouble("bar", ""), is(0.0));
        assertThat(target.paramAsPrimitiveDouble("bar", "1.2"), is(0.0));
        assertThat(target.paramAsPrimitiveDouble("poo", null), is(0.0));
        assertThat(target.paramAsPrimitiveDouble("poo", ""), is(0.0));
        assertThat(target.paramAsPrimitiveDouble("poo", "1.2"), is(1.2));
    }

    static class TestScenicController extends ScenicController {
        private ScenicPage page;
        TestScenicController(ScenicPage page) {
            this.page = page;
            this.basePath = "aaa";
            this.servletContext = new MockServletContext();
            this.request = new MockHttpServletRequest(servletContext);
            this.response = new MockHttpServletResponse();
            this.errors = new Errors();
        }
       
        @Override
        protected Navigation setUp() {
            this.setupPage(page);
            return super.setUp();
        }

        @Override
        protected Navigation run() throws Exception {
            return null;
        }

        @Override
        public ScenicPage getPage() {
            return page;
        }

        @Override
        public String getActionMethodName() {
            return null;
        }

        String getBasePath() {
            return basePath;
        }

        HttpServletRequest getRequest() {
            return request;
        }

        HttpServletResponse getResponse() {
            return response;
        }

        Errors getErrors() {
            return errors;
        }
    }
}
TOP

Related Classes of scenic3.ScenicControllerTest

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.