review modifications
This commit is contained in:
parent
d5fd9a0d9a
commit
5a1a468c78
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.accessmodifiers.publicmodifier;
|
||||||
|
|
||||||
|
import java.util.AbstractList;
|
||||||
|
|
||||||
|
public class ListOfThree<E> extends AbstractList<E> {
|
||||||
|
|
||||||
|
private static final int MAX_LENGTH = 3;
|
||||||
|
private int size;
|
||||||
|
private Object[] elements = new Object[MAX_LENGTH];
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public E get(int index) {
|
||||||
|
return (E)elements[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean add(E e) {
|
||||||
|
|
||||||
|
elements[size] = e;
|
||||||
|
size++;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.accessmodifiers;
|
package com.baeldung.accessmodifiers.publicmodifier;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@ -7,12 +7,16 @@ import java.sql.SQLException;
|
|||||||
|
|
||||||
public class Student {
|
public class Student {
|
||||||
|
|
||||||
private BigDecimal grades; //new representation
|
private BigDecimal grade; //new representation
|
||||||
private String name;
|
private String name;
|
||||||
private int age;
|
private int age;
|
||||||
|
|
||||||
public int getGrades() {
|
public int getGrade() {
|
||||||
return grades.intValue(); //Backward compatibility
|
return grade.intValue(); //Backward compatibility
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal bigDecimalGrade() {
|
||||||
|
return grade;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection() throws SQLException {
|
public Connection getConnection() throws SQLException {
|
||||||
@ -21,10 +25,6 @@ public class Student {
|
|||||||
return DriverManager.getConnection(URL, "sa", "");
|
return DriverManager.getConnection(URL, "sa", "");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BigDecimal bigDecimalGrades() {
|
|
||||||
return grades;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(int age) {
|
public void setAge(int age) {
|
||||||
if (age < 0 || age > 150)
|
if (age < 0 || age > 150)
|
@ -3,34 +3,76 @@ package com.baeldung.accessmodifiers;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestInstance;
|
import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
import com.baeldung.accessmodifiers.publicmodifier.ListOfThree;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class PublicAccessModifierUnitTest {
|
public class PublicAccessModifierUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingIntValue_valuesAreEqual() {
|
public void whenUsingBigDecimalIntValueMethod_correspondingIntIsReturned() {
|
||||||
|
assertEquals(0, new BigDecimal(0).intValue()); //instance member
|
||||||
assertEquals(0, new BigDecimal(0).intValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingToLowerCase_valuesAreEqual() {
|
public void whenUsingIntegerMaxValueField_maxPossibleIntValueIsReturned() {
|
||||||
|
assertEquals(2147483647, Integer.MAX_VALUE); //static field
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingStringToLowerCase_stringTurnsToLowerCase() {
|
||||||
assertEquals("alex", "ALEX".toLowerCase());
|
assertEquals("alex", "ALEX".toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenParsingStringOne_parseIntReturns1() {
|
||||||
|
assertEquals(1, Integer.parseInt("1"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConnectingToH2_connectionInstanceIsReturned() throws SQLException {
|
public void whenConnectingToH2_connectionInstanceIsReturned() throws SQLException {
|
||||||
|
|
||||||
final String URL = "jdbc:h2:~/test";
|
final String url = "jdbc:h2:~/test";
|
||||||
Connection conn = DriverManager.getConnection(URL, "sa", "");
|
Connection conn = DriverManager.getConnection(url, "sa", "");
|
||||||
assertNotNull(conn);
|
assertNotNull(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
|
||||||
|
|
||||||
|
List<String> list1 = new ListOfThree<String>();
|
||||||
|
list1.add("zero"); //inherited implementation
|
||||||
|
list1.add("one");
|
||||||
|
list1.add("two");
|
||||||
|
|
||||||
|
//our implemented methods
|
||||||
|
assertEquals("zero", list1.get(0));
|
||||||
|
|
||||||
|
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
|
||||||
|
list1.get(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
|
||||||
|
list1.add("three");
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(3, list1.size());
|
||||||
|
|
||||||
|
list1.indexOf("one"); //inherited implementation
|
||||||
|
|
||||||
|
List<String> list2 = new ListOfThree<String>();
|
||||||
|
list2.add("zero");
|
||||||
|
list2.add("one");
|
||||||
|
|
||||||
|
assertTrue(list1.containsAll(list2)); //inherited implementation
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user