소스 검색

Migrate xml-contacts groovy->java

See #4939.
Michael J. Simons 7 년 전
부모
커밋
1517e9b222

+ 16 - 1
samples/xml/contacts/spring-security-samples-xml-contacts.gradle

@@ -1,3 +1,18 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed 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.
+ */
 apply plugin: 'io.spring.convention.spring-sample-war'
 
 dependencies {
@@ -24,5 +39,5 @@ dependencies {
 	runtime 'org.slf4j:jcl-over-slf4j'
 	runtime 'org.springframework:spring-context-support'
 
-	integrationTestCompile gebDependencies
+	integrationTestCompile seleniumDependencies
 }

+ 0 - 87
samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/ContactsTests.groovy

@@ -1,87 +0,0 @@
-/*
- * Copyright 2002-2011 the original author or authors.
- *
- * Licensed 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 org.springframework.security.samples
-
-import geb.spock.*
-import spock.lang.Stepwise
-import org.springframework.security.samples.pages.*
-
-/**
- * Tests the CAS sample application using service tickets.
- *
- * @author Rob Winch
- */
-@Stepwise
-class ContactsTests extends GebReportingSpec {
-	def 'access home page with unauthenticated user success'() {
-		when: 'Unauthenticated user accesses the Home Page'
-		to HomePage
-		then: 'The page is displayed'
-		at HomePage
-	}
-
-	def 'access manage page with unauthenticated user sends to login page'() {
-		when: 'Unauthenticated user accesses the Manage Page'
-		manage.click(LoginPage)
-		then: 'The login page is displayed'
-		at LoginPage
-	}
-
-	def 'authenticated user is sent to original page'() {
-		when: 'user authenticates'
-		login()
-		then: 'The manage page is displayed'
-		at ContactsPage
-	}
-
-	def 'add contact link works'() {
-		when: 'user clicks add link'
-		addContact.click(AddPage)
-		then: 'The add page is displayed'
-		at AddPage
-	}
-
-	def 'add contact'() {
-		when: 'add a contact'
-		addContact
-		then: 'The add page is displayed'
-		at ContactsPage
-		and: 'The new contact is displayed'
-		contacts.find { it.email == 'rob@example.com' }?.name ==  'Rob Winch'
-	}
-
-	def 'delete contact'() {
-		when: 'delete a contact'
-		contacts.find { it.email == 'rob@example.com' }.delete()
-		then: 'Delete confirmation displayed'
-		at DeleteConfirmPage
-		when: 'View Manage Page'
-		manage.click()
-		then: 'New contact has been removed'
-		!contacts.find { it.email == 'rob@example.com' }
-	}
-
-	def 'authenticated user logs out'() {
-		when: 'user logs out'
-		logout.click()
-		then: 'the default logout success page is displayed'
-		at HomePage
-		when: 'Unauthenticated user accesses the Manage Page'
-		via ContactsPage
-		then: 'The login page is displayed'
-		at LoginPage
-	}
-}

+ 0 - 37
samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/AddPage.groovy

@@ -1,37 +0,0 @@
-/*
- * Copyright 2002-2011 the original author or authors.
- *
- * Licensed 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 org.springframework.security.samples.pages
-
-import geb.Page
-
-/**
- * The login page.
- *
- * @author Rob Winch
- */
-class AddPage extends Page {
-	static url = 'add'
-	static at = { assert driver.title == 'Add New Contact'; true}
-	static content = {
-		addContact(required:false) { name = 'Rob Winch', email = 'rob@example.com'->
-			addForm.name = name
-			addForm.email = email
-			submit.click()
-		}
-		addForm { $('form') }
-		submit { $('input', type: 'submit') }
-	}
-}

+ 0 - 44
samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/ContactsPage.groovy

@@ -1,44 +0,0 @@
-/*
- * Copyright 2002-2011 the original author or authors.
- *
- * Licensed 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 org.springframework.security.samples.pages
-
-import geb.*
-
-/**
- * The home page
- *
- * @author Rob Winch
- */
-class ContactsPage extends Page {
-	static url = 'secure/'
-	static at = { assert driver.title == 'Your Contacts'; true}
-	static content = {
-		addContact(to: AddPage) { $('a', text: 'Add') }
-		contacts { moduleList Contact, $("table tr").tail() }
-		logout { $("input[type=submit]", value: "Logoff") }
-	}
-}
-
-class Contact extends Module {
-	static content = {
-		cell { $("td", it) }
-		id { cell(0).text().toInteger() }
-		name { cell(1).text() }
-		email { cell(2).text() }
-		delete { cell(3).$('a').click() }
-		adminPermission { cell(4).$('a') }
-	}
-}

+ 0 - 30
samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/DeleteConfirmPage.groovy

@@ -1,30 +0,0 @@
-/*
- * Copyright 2002-2011 the original author or authors.
- *
- * Licensed 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 org.springframework.security.samples.pages
-
-import geb.Page
-
-/**
- * The home page
- *
- * @author Rob Winch
- */
-class DeleteConfirmPage extends Page {
-	static at = { assert driver.title == 'Deletion completed'; true}
-	static content = {
-		manage(to: ContactsPage) { $('a', text: 'Manage') }
-	}
-}

+ 0 - 33
samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy

@@ -1,33 +0,0 @@
-/*
- * Copyright 2002-2011 the original author or authors.
- *
- * Licensed 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 org.springframework.security.samples.pages;
-
-import geb.*
-
-/**
- * The home page
- *
- * @author Rob Winch
- */
-class HomePage extends Page {
-	static url = ''
-	static at = { assert driver.title == 'Contacts Security Demo'; true}
-	static content = {
-		manage(to: [ContactsPage,LoginPage]) { $('a', text: 'Manage') }
-		debug { $('a', text: 'Debug').click() }
-		frames { $('a', text: 'Frames').click() }
-	}
-}

+ 0 - 37
samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy

@@ -1,37 +0,0 @@
-/*
- * Copyright 2002-2011 the original author or authors.
- *
- * Licensed 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 org.springframework.security.samples.pages;
-
-import geb.*
-
-/**
- * The login page.
- *
- * @author Rob Winch
- */
-class LoginPage extends Page {
-	static url = 'login'
-	static at = { assert driver.title == 'Login'; true}
-	static content = {
-		login(required:false) { user='rod', password='koala' ->
-			loginForm.username = user
-			loginForm.password = password
-			submit.click()
-		}
-		loginForm { $('form') }
-		submit { $('input', type: 'submit') }
-	}
-}

+ 92 - 0
samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/ContactsTests.java

@@ -0,0 +1,92 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed 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 org.springframework.security.samples;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.htmlunit.HtmlUnitDriver;
+import org.springframework.security.samples.pages.ContactsPage;
+import org.springframework.security.samples.pages.HomePage;
+
+/**
+ * @author Michael Simons
+ */
+public class ContactsTests {
+	private WebDriver driver;
+
+	private int port;
+
+	@Before
+	public void setup() {
+		this.port = Integer.parseInt(System.getProperty("app.httpPort"));
+		this.driver = new HtmlUnitDriver();
+	}
+
+	@After
+	public void tearDown() {
+		this.driver.quit();
+	}
+
+	@Test
+	public void accessHomePageWithUnauthenticatedUserSuccess() {
+		final HomePage homePage = HomePage.to(this.driver, this.port);
+		homePage.assertAt();
+	}
+
+	@Test
+	public void authenticatedUserCanAddContacts() {
+		final String name = "Rob Winch";
+		final String email = "rob@example.com";
+
+		// @formatter:off
+		ContactsPage.accessManagePageWithUnauthenticatedUser(this.driver, this.port)
+			.sendsToLoginPage()
+				.username("rod")
+				.password("koala")
+			.submit()
+			.isAtContactsPage()
+			.addContact()
+				.name(name)
+				.email(email)
+			.submit()
+			.andHasContact(name, email)
+			.delete()
+			.andConfirmDeletion()
+			.isAtContactsPage()
+			.andConctactHasBeenRemoved(name, email);
+		// @formatter:on
+	}
+
+
+	@Test
+	public void authenticatedUserLogsOut() {
+		// @formatter:off
+		final HomePage homePage = ContactsPage.accessManagePageWithUnauthenticatedUser(this.driver, this.port)
+			.sendsToLoginPage()
+				.username("rod")
+				.password("koala")
+			.submit()
+			.isAtContactsPage()
+			.logout();
+		// @formatter:on
+		homePage.assertAt();
+
+		ContactsPage.accessManagePageWithUnauthenticatedUser(this.driver, this.port)
+			.sendsToLoginPage();
+	}
+}

+ 70 - 0
samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/AddPage.java

@@ -0,0 +1,70 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed 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 org.springframework.security.samples.pages;
+
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.FindBy;
+import org.openqa.selenium.support.PageFactory;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Michael Simons
+ */
+public class AddPage {
+
+	private final WebDriver webDriver;
+
+	private final AddForm addForm;
+
+	public AddPage(WebDriver webDriver) {
+		this.webDriver = webDriver;
+		this.addForm = PageFactory.initElements(this.webDriver, AddForm.class);
+	}
+
+	AddForm addForm() {
+		assertThat(this.webDriver.getTitle()).isEqualTo("Add New Contact");
+		return this.addForm;
+	}
+
+	public static class AddForm {
+		private WebDriver webDriver;
+		private WebElement name;
+		private WebElement email;
+		@FindBy(css = "input[type=submit]")
+		private WebElement submit;
+
+		public AddForm(WebDriver webDriver) {
+			this.webDriver = webDriver;
+		}
+
+		public AddForm name(String name) {
+			this.name.sendKeys(name);
+			return this;
+		}
+
+		public AddForm email(String email) {
+			this.email.sendKeys(email);
+			return this;
+		}
+
+		public ContactsPage submit() {
+			this.submit.click();
+			return PageFactory.initElements(this.webDriver, ContactsPage.class);
+		}
+	}
+}

+ 130 - 0
samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/ContactsPage.java

@@ -0,0 +1,130 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed 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 org.springframework.security.samples.pages;
+
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.FindBy;
+import org.openqa.selenium.support.PageFactory;
+import org.springframework.security.samples.pages.AddPage.AddForm;
+
+import java.util.List;
+import java.util.function.Predicate;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The contacts / manage page.
+ *
+ * @author Michael Simons
+ */
+public class ContactsPage {
+	public static LoginPage accessManagePageWithUnauthenticatedUser(WebDriver driver, int port) {
+		driver.get("http://localhost:" + port +"/secure/");
+		return PageFactory.initElements(driver, LoginPage.class);
+	}
+
+	private final WebDriver webDriver;
+
+	@FindBy(linkText = "Add")
+	private WebElement a;
+
+	@FindBy(css = "table tr")
+	private List<WebElement> contacts;
+
+	@FindBy(xpath = "//input[@type='submit' and @value='Logoff']")
+	private WebElement logout;
+
+	public ContactsPage(WebDriver webDriver) {
+		this.webDriver = webDriver;
+	}
+
+	public ContactsPage isAtContactsPage() {
+		assertThat(this.webDriver.getTitle()).isEqualTo("Your Contacts");
+		return this;
+	}
+
+	public AddForm addContact() {
+		a.click();
+		final AddPage addPage = PageFactory.initElements(this.webDriver, AddPage.class);
+		return addPage.addForm();
+	}
+
+	Predicate<WebElement> byEmail(final String val) {
+		return e -> e.findElements(By.xpath("td[position()=3 and normalize-space()='" + val + "']")).size() == 1;
+	}
+
+	Predicate<WebElement> byName(final String val) {
+		return e -> e.findElements(By.xpath("td[position()=2 and normalize-space()='" + val + "']")).size() == 1;
+	}
+
+	public DeleteContactLink andHasContact(final String name, final String email) {
+		return this.contacts.stream()
+			.filter(byEmail(email).and(byName(name)))
+			.map(e -> e.findElement(By.cssSelector("td:nth-child(4) > a")))
+			.findFirst()
+			.map(e -> new DeleteContactLink(webDriver, e))
+			.get();
+	}
+
+	public ContactsPage andConctactHasBeenRemoved(final String name, final String email) {
+		assertThat(this.contacts.stream()
+			.filter(byEmail(email).and(byName(name)))
+			.findAny()).isEmpty();
+		return this;
+	}
+
+	public HomePage logout() {
+		this.logout.click();
+		return PageFactory.initElements(this.webDriver, HomePage.class);
+	}
+
+	public static class DeleteContactLink {
+
+		private final WebDriver webDriver;
+
+		private final WebElement a;
+
+		public DeleteContactLink(WebDriver webDriver, WebElement a) {
+			this.webDriver = webDriver;
+			this.a = a;
+		}
+
+		public DeleteConfirmationPage delete() {
+			this.a.click();
+			return PageFactory.initElements(this.webDriver, DeleteConfirmationPage.class);
+		}
+	}
+
+	public static class DeleteConfirmationPage {
+		private final WebDriver webDriver;
+
+		@FindBy(linkText = "Manage")
+		private WebElement a;
+
+
+		public DeleteConfirmationPage(WebDriver webDriver) {
+			this.webDriver = webDriver;
+		}
+
+		public ContactsPage andConfirmDeletion() {
+			assertThat(this.webDriver.getTitle()).isEqualTo("Deletion completed");
+			this.a.click();
+			return PageFactory.initElements(this.webDriver, ContactsPage.class);
+		}
+	}
+}

+ 69 - 0
samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java

@@ -0,0 +1,69 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed 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 org.springframework.security.samples.pages;
+
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.FindBy;
+import org.openqa.selenium.support.PageFactory;
+import org.springframework.security.samples.pages.LoginPage;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The home page.
+ *
+ * @author Michael Simons
+ */
+public class HomePage {
+
+	public static HomePage to(WebDriver driver, int port) {
+		driver.get("http://localhost:" + port +"/");
+		return PageFactory.initElements(driver, HomePage.class);
+	}
+
+	private final WebDriver webDriver;
+
+	@FindBy(css = "p")
+	private WebElement message;
+
+	@FindBy(css = "input[type=submit]")
+	private WebElement logoutButton;
+
+	public HomePage(WebDriver webDriver) {
+		this.webDriver = webDriver;
+	}
+
+	public Content assertAt() {
+		assertThat(this.webDriver.getTitle()).isEqualTo("Contacts Security Demo");
+		return PageFactory.initElements(this.webDriver, Content.class);
+	}
+
+	public LoginPage logout() {
+		this.logoutButton.submit();
+		return PageFactory.initElements(this.webDriver, LoginPage.class);
+	}
+
+	public static class Content {
+		@FindBy(css = "p")
+		private WebElement message;
+
+		public Content andTheUserNameIsDisplayed() {
+			assertThat(message.getText()).isEqualTo("Hello user");
+			return this;
+		}
+	}
+}

+ 72 - 0
samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java

@@ -0,0 +1,72 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed 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 org.springframework.security.samples.pages;
+
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.FindBy;
+import org.openqa.selenium.support.PageFactory;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The login page.
+ *
+ * @author Michael Simons
+ */
+public class LoginPage {
+
+	private final WebDriver webDriver;
+
+	private final LoginForm loginForm;
+
+	public LoginPage(WebDriver webDriver) {
+		this.webDriver = webDriver;
+		this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class);
+	}
+
+	public LoginForm sendsToLoginPage() {
+		assertThat(this.webDriver.getTitle()).isEqualTo("Login");
+		return this.loginForm;
+	}
+
+	public static class LoginForm {
+		private WebDriver webDriver;
+		private WebElement username;
+		private WebElement password;
+		@FindBy(css = "input[type=submit]")
+		private WebElement submit;
+
+		public LoginForm(WebDriver webDriver) {
+			this.webDriver = webDriver;
+		}
+
+		public LoginForm username(String username) {
+			this.username.sendKeys(username);
+			return this;
+		}
+
+		public LoginForm password(String password) {
+			this.password.sendKeys(password);
+			return this;
+		}
+
+		public ContactsPage submit() {
+			this.submit.click();
+			return PageFactory.initElements(this.webDriver, ContactsPage.class);
+		}
+	}
+}