Example usage for org.springframework.mock.env MockEnvironment MockEnvironment

List of usage examples for org.springframework.mock.env MockEnvironment MockEnvironment

Introduction

In this page you can find the example usage for org.springframework.mock.env MockEnvironment MockEnvironment.

Prototype

public MockEnvironment() 

Source Link

Document

Create a new MockEnvironment with a single MockPropertySource .

Usage

From source file:com.github.nblair.web.ProfileConditionalDelegatingFilterProxyTest.java

/**
 * Verify behavior when multiple required profiles are set and all but 1 are active in the environment (delegate should NOT be invoked).
 * /*from  w  w  w. j a v  a  2s. com*/
 * @throws ServletException
 * @throws IOException
 */
@Test
public void doFilter_required_multiple_profiles_one_missing() throws ServletException, IOException {
    Filter delegate = mock(Filter.class);
    ProfileConditionalDelegatingFilterProxy filterProxy = new ProfileConditionalDelegatingFilterProxy(delegate);
    filterProxy.setRequiredProfiles("foo", "bar", "baz");
    MockEnvironment environment = new MockEnvironment();
    environment.setActiveProfiles("foo", "bar");
    ServletContext servletContext = mockServletContextWithEnvironment(environment);

    filterProxy.setServletContext(servletContext);
    ServletRequest request = mock(ServletRequest.class);
    ServletResponse response = mock(ServletResponse.class);
    FilterChain filterChain = mock(FilterChain.class);
    filterProxy.doFilter(request, response, filterChain);

    verify(delegate, never()).doFilter(request, response, filterChain);
    verify(filterChain).doFilter(request, response);
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaControllerMockMvcTests.java

@Test
public void testCustomSignupLink() throws Exception {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("links.signup", "http://www.example.com/signup");

    RemoteUaaController controller = new RemoteUaaController(environment, new RestTemplate());
    Prompt first = new Prompt("how", "text", "How did I get here?");
    Prompt second = new Prompt("where", "password", "Where does that highway go to?");
    controller.setPrompts(Arrays.asList(first, second));

    MockMvc mockMvc = getMockMvc(controller);

    mockMvc.perform(get("/login").accept(TEXT_HTML)).andExpect(status().isOk())
            .andExpect(model().attribute("createAccountLink", "http://www.example.com/signup"));
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaControllerMockMvcTests.java

@Test
public void testLocalSignupDisabled() throws Exception {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("login.selfServiceLinksEnabled", "false");

    RemoteUaaController controller = new RemoteUaaController(environment, new RestTemplate());
    Prompt first = new Prompt("how", "text", "How did I get here?");
    Prompt second = new Prompt("where", "password", "Where does that highway go to?");
    controller.setPrompts(Arrays.asList(first, second));

    MockMvc mockMvc = getMockMvc(controller);

    mockMvc.perform(get("/login").accept(TEXT_HTML)).andExpect(status().isOk())
            .andExpect(model().attribute("createAccountLink", nullValue()));
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaControllerMockMvcTests.java

@Test
public void testCustomSignupLinkWithLocalSignupDisabled() throws Exception {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("login.selfServiceLinksEnabled", "false");
    environment.setProperty("links.signup", "http://www.example.com/signup");
    environment.setProperty("links.passwd", "http://www.example.com/passwd");

    RemoteUaaController controller = new RemoteUaaController(environment, new RestTemplate());
    Prompt first = new Prompt("how", "text", "How did I get here?");
    Prompt second = new Prompt("where", "password", "Where does that highway go to?");
    controller.setPrompts(Arrays.asList(first, second));

    MockMvc mockMvc = getMockMvc(controller);

    mockMvc.perform(get("/login").accept(TEXT_HTML)).andExpect(status().isOk())
            .andExpect(model().attribute("createAccountLink", nullValue()))
            .andExpect(model().attribute("forgotPasswordLink", nullValue()));
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

private void verifyRelativeUriHandling(TestRestTemplateCallback callback) throws IOException {
    ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
    MockClientHttpRequest request = new MockClientHttpRequest();
    request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
    URI absoluteUri = URI.create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D");
    given(requestFactory.createRequest(eq(absoluteUri), (HttpMethod) any())).willReturn(request);
    RestTemplate delegate = new RestTemplate();
    TestRestTemplate template = new TestRestTemplate(delegate);
    delegate.setRequestFactory(requestFactory);
    LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(new MockEnvironment());
    template.setUriTemplateHandler(uriTemplateHandler);
    callback.doWithTestRestTemplate(template, URI.create("/a/b/c.txt?param=%7Bsomething%7D"));
    verify(requestFactory).createRequest(eq(absoluteUri), (HttpMethod) any());
}

From source file:org.cloudfoundry.identity.uaa.mock.audit.AuditCheckMvcMockTests.java

@Before
public void setUp() throws Exception {
    MockEnvironment mockEnvironment = new MockEnvironment();

    webApplicationContext = new XmlWebApplicationContext();
    webApplicationContext.setServletContext(new MockServletContext());
    webApplicationContext.setEnvironment(mockEnvironment);
    new YamlServletProfileInitializerContextInitializer().initializeContext(webApplicationContext,
            "uaa.yml,login.yml");
    webApplicationContext.setConfigLocation("file:./src/main/webapp/WEB-INF/spring-servlet.xml");
    webApplicationContext.refresh();/*from w  ww . j  a  v  a2s .c  o  m*/
    FilterChainProxy springSecurityFilterChain = webApplicationContext.getBean("springSecurityFilterChain",
            FilterChainProxy.class);

    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).addFilter(springSecurityFilterChain)
            .build();

    clientRegistrationService = (ClientRegistrationService) webApplicationContext
            .getBean("clientRegistrationService");
    listener = mock(new DefaultApplicationListener<AbstractUaaEvent>() {
    }.getClass());
    authSuccessListener = mock(new DefaultApplicationListener<UserAuthenticationSuccessEvent>() {
    }.getClass());
    webApplicationContext.addApplicationListener(listener);
    webApplicationContext.addApplicationListener(authSuccessListener);
    testListener = TestApplicationEventListener.forEventClass(AbstractUaaEvent.class);
    webApplicationContext.addApplicationListener(testListener);

    testClient = new TestClient(mockMvc);
    testAccounts = UaaTestAccounts.standard(null);
}

From source file:org.springframework.boot.logging.logback.LogbackLoggingSystemTests.java

@Before
public void setup() {
    this.logger = new SLF4JLogFactory().getInstance(getClass().getName());
    this.initializationContext = new LoggingInitializationContext(new MockEnvironment());
}

From source file:org.springframework.boot.logging.logback.LogbackLoggingSystemTests.java

@Test
public void testConsolePatternProperty() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("logging.pattern.console", "%logger %msg");
    LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(environment);
    this.loggingSystem.initialize(loggingInitializationContext, null, null);
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    assertFalse("Wrong output pattern:\n" + output, getLineWithText(output, "Hello world").contains("INFO"));
}

From source file:org.springframework.boot.logging.logback.LogbackLoggingSystemTests.java

@Test
public void testLevelPatternProperty() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("logging.pattern.level", "X%clr(%p)X");
    LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(environment);
    this.loggingSystem.initialize(loggingInitializationContext, null, null);
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    assertTrue("Wrong output pattern:\n" + output, getLineWithText(output, "Hello world").contains("XINFOX"));
}

From source file:org.springframework.boot.logging.logback.LogbackLoggingSystemTests.java

@Test
public void testFilePatternProperty() throws Exception {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("logging.pattern.file", "%logger %msg");
    LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(environment);
    File file = new File(tmpDir(), "logback-test.log");
    LogFile logFile = getLogFile(file.getPath(), null);
    this.loggingSystem.initialize(loggingInitializationContext, null, logFile);
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    assertTrue("Wrong console output pattern:\n" + output,
            getLineWithText(output, "Hello world").contains("INFO"));
    assertFalse("Wrong file output pattern:\n" + output, getLineWithText(file, "Hello world").contains("INFO"));
}