Skip to content

Commit 8b5d1cd

Browse files
Migrate javaconfig-helloworld groovy->java
See spring-projects#4939.
1 parent 0970f64 commit 8b5d1cd

File tree

7 files changed

+233
-126
lines changed

7 files changed

+233
-126
lines changed

samples/javaconfig/helloworld/spring-security-samples-javaconfig-helloworld.gradle

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
apply plugin: 'io.spring.convention.spring-sample-war'
218

319
dependencies {
@@ -9,5 +25,5 @@ dependencies {
925
providedCompile 'javax.servlet:javax.servlet-api'
1026
providedCompile 'javax.servlet.jsp:javax.servlet.jsp-api'
1127

12-
integrationTestCompile gebDependencies
28+
integrationTestCompile seleniumDependencies
1329
}

samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/HelloWorldJcTests.groovy

Lines changed: 0 additions & 56 deletions
This file was deleted.

samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy

Lines changed: 0 additions & 32 deletions
This file was deleted.

samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy

Lines changed: 0 additions & 37 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.samples;
17+
18+
import org.junit.After;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.openqa.selenium.WebDriver;
22+
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
23+
import org.springframework.security.samples.pages.HomePage;
24+
import org.springframework.security.samples.pages.LoginPage;
25+
26+
/**
27+
* @author Michael Simons
28+
*/
29+
public class HelloWorldJcTests {
30+
31+
private WebDriver driver;
32+
33+
private int port;
34+
35+
@Before
36+
public void setup() {
37+
this.port = Integer.parseInt(System.getProperty("app.httpPort"));
38+
this.driver = new HtmlUnitDriver();
39+
}
40+
41+
@After
42+
public void tearDown() {
43+
this.driver.quit();
44+
}
45+
46+
@Test
47+
public void accessHomePageWithUnauthenticatedUserSendsToLoginPage() {
48+
final LoginPage loginPage = HomePage.to(this.driver, this.port);
49+
loginPage.assertAt();
50+
}
51+
52+
@Test
53+
public void authenticatedUserIsSentToOriginalPage() {
54+
final HomePage homePage = HomePage.to(this.driver, this.port)
55+
.loginForm()
56+
.username("user")
57+
.password("password")
58+
.submit();
59+
homePage
60+
.assertAt()
61+
.andTheUserNameIsDisplayed();
62+
}
63+
64+
@Test
65+
public void authenticatedUserLogsOut() {
66+
LoginPage loginPage = HomePage.to(this.driver, this.port)
67+
.loginForm()
68+
.username("user")
69+
.password("password")
70+
.submit()
71+
.logout();
72+
loginPage.assertAt();
73+
74+
loginPage = HomePage.to(this.driver, this.port);
75+
loginPage.assertAt();
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.samples.pages;
17+
18+
import org.openqa.selenium.WebDriver;
19+
import org.openqa.selenium.WebElement;
20+
import org.openqa.selenium.support.FindBy;
21+
import org.openqa.selenium.support.PageFactory;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* @author Michael Simons
27+
*/
28+
public class HomePage {
29+
private final WebDriver webDriver;
30+
31+
@FindBy(css = "p")
32+
private WebElement message;
33+
34+
@FindBy(css = "input[type=submit]")
35+
private WebElement logoutButton;
36+
37+
public static LoginPage to(WebDriver driver, int port) {
38+
driver.get("http://localhost:" + port +"/");
39+
return PageFactory.initElements(driver, LoginPage.class);
40+
}
41+
42+
public HomePage(WebDriver webDriver) {
43+
this.webDriver = webDriver;
44+
}
45+
46+
public Content assertAt() {
47+
assertThat(this.webDriver.getTitle()).isEqualTo("Hello Security");
48+
return PageFactory.initElements(this.webDriver, Content.class);
49+
}
50+
51+
public LoginPage logout() {
52+
this.logoutButton.submit();
53+
return PageFactory.initElements(this.webDriver, LoginPage.class);
54+
}
55+
56+
public static class Content {
57+
@FindBy(css = "p")
58+
private WebElement message;
59+
60+
public Content andTheUserNameIsDisplayed() {
61+
assertThat(message.getText()).isEqualTo("Hello user");
62+
return this;
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.samples.pages;
17+
18+
import org.openqa.selenium.WebDriver;
19+
import org.openqa.selenium.WebElement;
20+
import org.openqa.selenium.support.FindBy;
21+
import org.openqa.selenium.support.PageFactory;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* @author Michael Simons
27+
*/
28+
public class LoginPage {
29+
30+
private final WebDriver webDriver;
31+
32+
private final LoginForm loginForm;
33+
34+
public LoginPage(WebDriver webDriver) {
35+
this.webDriver = webDriver;
36+
this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class);
37+
}
38+
39+
public LoginPage assertAt() {
40+
assertThat(this.webDriver.getTitle()).isEqualTo("Login Page");
41+
return this;
42+
}
43+
44+
public LoginForm loginForm() {
45+
return this.loginForm;
46+
}
47+
48+
public static class LoginForm {
49+
private WebDriver webDriver;
50+
private WebElement username;
51+
private WebElement password;
52+
@FindBy(css = "input[type=submit]")
53+
private WebElement submit;
54+
55+
public LoginForm(WebDriver webDriver) {
56+
this.webDriver = webDriver;
57+
}
58+
59+
public LoginForm username(String username) {
60+
this.username.sendKeys(username);
61+
return this;
62+
}
63+
64+
public LoginForm password(String password) {
65+
this.password.sendKeys(password);
66+
return this;
67+
}
68+
69+
public HomePage submit() {
70+
this.submit.click();
71+
return PageFactory.initElements(this.webDriver, HomePage.class);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)