这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.baeldung.corecopy;

public class Address {
private String street;
private String city;
private String country;

public Address(Address that) {
this(that.getStreet(), that.getCity(), that.getCountry());
}

public Address(String street, String city, String country) {
this.street = street;
this.city = city;
this.country = country;
}

public Address() {
}

@Override
public Object clone() {
try {
return (Address) super.clone();
} catch (CloneNotSupportedException e) {
return new Address(this.getStreet(), this.getCity(), this.getCountry());
}
}


public String getStreet() {
return street;
}

public String getCity() {
return city;
}

public String getCountry() {
return country;
}

public void setStreet(String street) {
this.street = street;
}

public void setCity(String city) {
this.city = city;
}

public void setCountry(String country) {
this.country = country;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.corecopy;

public class User {
private String name;
private Address address;

public User(String name, Address address) {
this.name = name;
this.address = address;
}

public User(User that) {
this(that.getName(), new Address(that.getAddress()));
}

public User() {
}

@Override
public Object clone() {
User user;
try {
user = (User) super.clone();
} catch (CloneNotSupportedException e) {
user = new User(this.getName(), this.getAddress());
}
user.address = (Address) this.address.clone();
return user;
}

public String getName() {
return name;
}

public Address getAddress() {
return address;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.corecopy;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class DeepCopyUnitTest {

@Test
public void whenCreatingDeepCopyWithCopyConstructor_thenObjectsShouldNotBeSame() {

Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime Minister", address);

User deepCopy = new User(pm);

assertThat(deepCopy).isNotSameAs(pm);
}

@Test
public void whenModifyingOriginalObject_thenCopyShouldNotChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime Minister", address);
User deepCopy = new User(pm);

address.setCountry("Great Britain");

assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
}

@Test
public void whenModifyingOriginalObject_thenCloneCopyShouldNotChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime Minister", address);
User deepCopy = (User) pm.clone();

address.setCountry("Great Britain");

assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.corecopy;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class ShallowCopyUnitTest {

@Test
public void whenShallowCopying_thenObjectsShouldNotBeSame() {

Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime Minister", address);

User shallowCopy = new User(pm.getName(), pm.getAddress());

assertThat(shallowCopy)
.isNotSameAs(pm);
}

@Test
public void whenModifyingOriginalObject_thenCopyShouldChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime Minister", address);
User shallowCopy = new User(pm.getName(), pm.getAddress());

address.setCountry("Great Britain");

assertThat(shallowCopy.getAddress().getCountry())
.isEqualTo(pm.getAddress().getCountry());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.baeldung.selenium.select;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class SeleniumSelectLiveTest {

private WebDriver driver;

private static final String URL = "https://www.baeldung.com/contact";
private static final String INPUT_XPATH = "//select[@name='question-recipient']";
private static final String OPTION_XPATH = "//select[@name='question-recipient']/option[@value='Bug reporting']";
private static final String OPTION_TEXT = "Bug reporting";
private static final int OPTION_INDEX = 4;
@BeforeEach
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}

@AfterEach
public void tearDown() {
driver.quit();
}

@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenContainsOptionBugs() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(OPTION_XPATH));
assertEquals("Bug reporting", inputElement.getText());
}

@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByValue() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));

Select select = new Select(inputElement);
select.selectByValue(OPTION_TEXT);
assertTrue(optionBug.isSelected());
}

@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByText() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));

Select select = new Select(inputElement);
select.selectByVisibleText(OPTION_TEXT);
assertTrue(optionBug.isSelected());
}

@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByIndex() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));

Select select = new Select(inputElement);
select.selectByIndex(OPTION_INDEX);
assertTrue(optionBug.isSelected());
}

}