diff --git a/core-java-modules/core-java-lang-oop-2/pom.xml b/core-java-modules/core-java-lang-oop-2/pom.xml index 669a37b0f5..b7bd72372b 100644 --- a/core-java-modules/core-java-lang-oop-2/pom.xml +++ b/core-java-modules/core-java-lang-oop-2/pom.xml @@ -14,6 +14,19 @@ ../../parent-java + + + com.h2database + h2 + ${h2.version} + test + + + + + 1.4.199 + + core-java-lang-oop-2 diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/ListOfThree.java b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/ListOfThree.java new file mode 100644 index 0000000000..2ded0ba5d3 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/ListOfThree.java @@ -0,0 +1,31 @@ +package com.baeldung.accessmodifiers.publicmodifier; + +import java.util.AbstractList; +import java.util.Arrays; + +public class ListOfThree extends AbstractList { + + 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; + } + +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/SpecialCharacters.java b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/SpecialCharacters.java new file mode 100644 index 0000000000..5556e9aa57 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/SpecialCharacters.java @@ -0,0 +1,7 @@ +package com.baeldung.accessmodifiers.publicmodifier; + +public class SpecialCharacters { + + public static final String SLASH = "/"; + +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/Student.java b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/Student.java new file mode 100644 index 0000000000..83a0dcb30f --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/accessmodifiers/publicmodifier/Student.java @@ -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; + } + + } + +} diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/accessmodifiers/PublicAccessModifierUnitTest.java b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/accessmodifiers/PublicAccessModifierUnitTest.java new file mode 100644 index 0000000000..a2d891ac93 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/accessmodifiers/PublicAccessModifierUnitTest.java @@ -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 mapObject = new HashMap(); + mapObject.put("name", "Alex"); + + for(Map.Entry 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 list1 = new ListOfThree(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 list2 = new ListOfThree(dataSet2); + + assertTrue(list1.containsAll(list2)); + } + +}