Merge pull request #7489 from M-Abdelbaset/public-access-modifier
BAEL-3148
This commit is contained in:
commit
5473d1c8d4
|
@ -14,6 +14,19 @@
|
|||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<h2.version>1.4.199</h2.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-oop-2</finalName>
|
||||
<resources>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.accessmodifiers.publicmodifier;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ListOfThree<E> extends AbstractList<E> {
|
||||
|
||||
private static final int LENGTH = 3;
|
||||
private Object[] elements;
|
||||
|
||||
public ListOfThree(E[] data) {
|
||||
if(data == null
|
||||
|| data.length != LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
this.elements = Arrays.copyOf(data, data.length); //shallow copy
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public E get(int index) {
|
||||
return (E)elements[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return LENGTH;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.accessmodifiers.publicmodifier;
|
||||
|
||||
public class SpecialCharacters {
|
||||
|
||||
public static final String SLASH = "/";
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.accessmodifiers.publicmodifier;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
|
||||
public class Student {
|
||||
|
||||
private StudentGrade grade; //new data representation
|
||||
// private int grade; //old data representation
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public void setGrade(int grade) {
|
||||
this.grade = new StudentGrade(grade);
|
||||
}
|
||||
|
||||
public int getGrade() {
|
||||
return this.grade.getGrade().intValue(); //int is returned for backward compatibility
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
|
||||
final String URL = "jdbc:h2:~/test";
|
||||
return DriverManager.getConnection(URL, "sa", "");
|
||||
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
if (age < 0 || age > 150) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
private class StudentGrade {
|
||||
private BigDecimal grade = BigDecimal.ZERO;
|
||||
private Date updatedAt;
|
||||
|
||||
public StudentGrade(int grade) {
|
||||
this.grade = new BigDecimal(grade);
|
||||
this.updatedAt = new Date();
|
||||
}
|
||||
|
||||
public BigDecimal getGrade() {
|
||||
return grade;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return updatedAt;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package com.baeldung.accessmodifiers;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
import com.baeldung.accessmodifiers.publicmodifier.ListOfThree;
|
||||
import com.baeldung.accessmodifiers.publicmodifier.Student;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
public class PublicAccessModifierUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingBigDecimalIntValueMethod_correspondingIntIsReturned() {
|
||||
assertEquals(0, new BigDecimal(0).intValue()); //instance member
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingIntegerMaxValueField_maxPossibleIntValueIsReturned() {
|
||||
assertEquals(2147483647, Integer.MAX_VALUE); //static field
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChangingStudentInternalRepresentation_clientCodeWillNotBreak() {
|
||||
|
||||
Student student = new Student();
|
||||
student.setGrade(100);
|
||||
|
||||
assertEquals(100, student.getGrade());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEntrySet_keyValuePairsAreReturned() {
|
||||
|
||||
Map<String, String> mapObject = new HashMap<String, String>();
|
||||
mapObject.put("name", "Alex");
|
||||
|
||||
for(Map.Entry<String, String> entry : mapObject.entrySet()) {
|
||||
assertEquals("name", entry.getKey());
|
||||
assertEquals("Alex", entry.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStringToLowerCase_stringTurnsToLowerCase() {
|
||||
assertEquals("alex", "ALEX".toLowerCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenParsingStringOne_parseIntReturns1() {
|
||||
assertEquals(1, Integer.parseInt("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConnectingToH2_connectionInstanceIsReturned() throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:~/test";
|
||||
Connection conn = DriverManager.getConnection(url, "sa", "");
|
||||
assertNotNull(conn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
|
||||
|
||||
String[] dataSet1 = new String[] {"zero", "one", "two"};
|
||||
|
||||
List<String> list1 = new ListOfThree<String>(dataSet1);
|
||||
|
||||
//our implemented methods
|
||||
assertEquals("one", list1.get(1));
|
||||
assertEquals(3, list1.size());
|
||||
|
||||
//inherited implementations
|
||||
assertEquals(1, list1.indexOf("one"));
|
||||
|
||||
String[] dataSet2 = new String[] {"two", "zero", "one"};
|
||||
List<String> list2 = new ListOfThree<String>(dataSet2);
|
||||
|
||||
assertTrue(list1.containsAll(list2));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue