From b767367ba091a26f935227bf886262f51d6b2eb9 Mon Sep 17 00:00:00 2001 From: Jonathan Robles Date: Fri, 21 Jan 2022 11:31:27 -0600 Subject: [PATCH 1/2] test-lecture --- src/main/java/CodeupCrypt.java | 39 +++++++++++++++++++ src/main/java/HelloWorld.java | 12 ++++++ src/test/java/CodeupCryptTest.java | 26 +++++++++++++ src/test/java/HelloWorldTest.java | 33 ++++++++++++++++ src/test/java/MyFirstTest.java | 60 ++++++++++++++++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 src/main/java/CodeupCrypt.java create mode 100644 src/main/java/HelloWorld.java create mode 100644 src/test/java/CodeupCryptTest.java create mode 100644 src/test/java/HelloWorldTest.java create mode 100644 src/test/java/MyFirstTest.java diff --git a/src/main/java/CodeupCrypt.java b/src/main/java/CodeupCrypt.java new file mode 100644 index 00000000..75a1b973 --- /dev/null +++ b/src/main/java/CodeupCrypt.java @@ -0,0 +1,39 @@ +public class CodeupCrypt { + public static double version = 0.0; + + public static String hashPassword(String password){ + String hash = ""; + for (char character : password.toCharArray()){ + switch (character){ + case 'a': + case 'A': + hash += 4; + break; + case 'e': + case 'E': + hash += 3; + break; + case 'i': + case 'I': + hash += 1; + break; + case 'o': + case'O': + hash += 0; + break; + case 'u': + case 'U': + hash += 9; + break; + default: + hash += character; + break; + } + } + return hash; + } + + public static boolean checkPassword(String password, String hash){ + return hash.equals(hashPassword(password)); + } +} diff --git a/src/main/java/HelloWorld.java b/src/main/java/HelloWorld.java new file mode 100644 index 00000000..1d6ec9f5 --- /dev/null +++ b/src/main/java/HelloWorld.java @@ -0,0 +1,12 @@ +public class HelloWorld { + public static String hello(){ + return "Hello World"; + } + + public static String hello(String name){ + if(name == null){ + throw new IllegalArgumentException("People can't not have a name..."); + } + return "Hello, " + name + "!"; + } +} diff --git a/src/test/java/CodeupCryptTest.java b/src/test/java/CodeupCryptTest.java new file mode 100644 index 00000000..f6db8199 --- /dev/null +++ b/src/test/java/CodeupCryptTest.java @@ -0,0 +1,26 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class CodeupCryptTest { + @Test + public void testIfVersionIsSet(){ + assertEquals(0.0, CodeupCrypt.version, 0); + CodeupCrypt.version = 1.2; + assertEquals(1.2, CodeupCrypt.version, 0); + } + + @Test + public void testHashPassword(){ + assertEquals("C0d39p", CodeupCrypt.hashPassword("Codeup")); + assertEquals("F3r", CodeupCrypt.hashPassword("Fer")); + assertEquals("123", CodeupCrypt.hashPassword("123")); + assertEquals("124", CodeupCrypt.hashPassword("12a")); + } + + @Test + public void testCheckPassword(){ + assertTrue("check password", CodeupCrypt.checkPassword("fer", "f3r")); + assertFalse("check password", CodeupCrypt.checkPassword("fer", "fer")); + } +} diff --git a/src/test/java/HelloWorldTest.java b/src/test/java/HelloWorldTest.java new file mode 100644 index 00000000..2156eef9 --- /dev/null +++ b/src/test/java/HelloWorldTest.java @@ -0,0 +1,33 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class HelloWorldTest { + private String expected; + private String hiJay; + + @Before + public void setUp(){ + this.expected = "Hello, World"; + this.hiJay = "Hello, Jay!"; + } + + @Test + public void testForHelloWorld(){ + Assert.assertEquals(this.expected, HelloWorld.hello()); + } + + @Test + public void testForHelloJay(){ + Assert.assertEquals(this.hiJay, HelloWorld.hello()); + } + + @Test public void testForHelloDavid(){ + Assert.assertEquals("Hello, David!", HelloWorld.hello("David")); + } + + @Test(expected = IllegalArgumentException.class) + public void testForNull(){ + Assert.assertNotEquals("Hello, null!", HelloWorld.hello(null)); + } +} diff --git a/src/test/java/MyFirstTest.java b/src/test/java/MyFirstTest.java new file mode 100644 index 00000000..c1924eb4 --- /dev/null +++ b/src/test/java/MyFirstTest.java @@ -0,0 +1,60 @@ +import org.junit.Test; +import org.junit.Assert; + +import java.util.ArrayList; +import java.util.List; + +public class MyFirstTest { + @Test + public void codeupIsNotCodeUp(){ + String correct = "Codeup"; + String commonly = "CodeUp"; + + Assert.assertNotEquals(correct, commonly); + } + + @Test + public void davidIsDavid(){ + String instructor = "David"; + String testWriter = "David"; + + Assert.assertEquals(testWriter, instructor); + } + + @Test + public void listsAreDifferent(){ + List languages = new ArrayList<>(); + List names = new ArrayList<>(); + + //assertEquals checks to see if the values are the same + // test fails +// languages.add("Latin"); +// names.add("Jennifer"); +// Assert.assertEquals(languages, names); + + //assertSame checks if the objects are the same + // test fails +// languages.add("Latin"); +// names.add("Latin"); +// Assert.assertSame(languages, names); + + } + + @Test + public void arraysAreEqual(){ + int[] numbers = {1,2,3}; + int[] otherNumbers = new int[3]; + otherNumbers[0] = 1; + otherNumbers[1] = 2; + otherNumbers[2] = 3; + + Assert.assertArrayEquals(numbers, otherNumbers); + } + + @Test + public void funWithPHP(){ + String language = "PHP"; + Assert.assertTrue(language.contains("H")); + Assert.assertFalse(language.contains("J")); + } +} From c9aafd6aea8ffa16e07c4d8ea9219cc6ea0e89d4 Mon Sep 17 00:00:00 2001 From: Jonathan Robles Date: Fri, 21 Jan 2022 15:59:52 -0600 Subject: [PATCH 2/2] Created test for Student Class --- src/main/java/Student.java | 53 +++++++++++++++++++++++++++ src/test/java/StudentTest.java | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/main/java/Student.java create mode 100644 src/test/java/StudentTest.java diff --git a/src/main/java/Student.java b/src/main/java/Student.java new file mode 100644 index 00000000..8a8a1dea --- /dev/null +++ b/src/main/java/Student.java @@ -0,0 +1,53 @@ +import java.util.ArrayList; + +public class Student { + private long id; + private String name; + private ArrayList grades; + + public Student(long id, String name){ + this.id = id; + this.name = name; + this.grades = new ArrayList<>(); + } + + // returns the student's id + public long getId(){ + return id; + } + + // returns the student's name + public String getName(){ + return name; + } + + // adds the given grade to the grades list + public void addGrade(int grade){ + grades.add(grade); + } + + // returns the list of grades + public ArrayList getGrades(){ + return this.grades; + } + + // returns the average of the students grades + public double getGradeAverage(){ + double sum = 0; + double numOfGrades = grades.size(); + for(double grade : grades){ + sum += grade; + } + return sum/numOfGrades; + } + +// // update grade() +// public int updateGrade(){ +// +// } +// +// // delete grade() +// public int deleteGrade(){ +// +// } +} diff --git a/src/test/java/StudentTest.java b/src/test/java/StudentTest.java new file mode 100644 index 00000000..5d7139c3 --- /dev/null +++ b/src/test/java/StudentTest.java @@ -0,0 +1,65 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StudentTest { + Student studentOne; + Student studentTwo; + Student studentThree; + + + @Before + public void setUp() throws Exception { + studentOne = new Student(1,"John"); + studentOne.addGrade(90); + studentOne.addGrade(100); + studentTwo = new Student(2, "James"); + studentTwo.addGrade(50); + studentTwo.addGrade(100); + studentThree = new Student(3, "Tiffany"); + + } + + + + @Test + public void testConstructor() { + Student s1 = new Student(1,"name"); + assertEquals("name", s1.getName()); + assertEquals(1,s1.getId()); + assertTrue(s1.getGrades().isEmpty()); + } + + @Test + public void testGetId(){ + assertEquals(1,studentOne.getId()); + assertEquals(2, studentTwo.getId()); + } + + @Test + public void testGetName(){ + assertEquals("John", studentOne.getName()); + assertEquals("James", studentTwo.getName()); + } + + @Test + public void testAddGrade(){ + assertTrue(studentThree.getGrades().isEmpty()); + studentThree.addGrade(98); + assertFalse(studentThree.getGrades().isEmpty()); + } + + @Test + public void testGetGrades(){ + assertSame(90,studentOne.getGrades().get(0)); + assertSame(100,studentTwo.getGrades().get(1)); + } + + @Test + public void testGetGradeAverage(){ + assertEquals(75, studentTwo.getGradeAverage(),0); + } + + +}