From 2a68e1e9486c3ce768257d8d2588432ee28e8565 Mon Sep 17 00:00:00 2001 From: Sarai Besada Date: Tue, 9 May 2023 17:59:36 +0200 Subject: [PATCH 1/4] Coverage al 100% --- test/org/hmis/CocheTest.java | 123 ++++++++++++++++++++++++++++++ test/org/hmis/JsonReaderTest.java | 34 +++++++-- test/org/hmis/MainTest.java | 27 +++++++ 3 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 test/org/hmis/CocheTest.java create mode 100644 test/org/hmis/MainTest.java diff --git a/test/org/hmis/CocheTest.java b/test/org/hmis/CocheTest.java new file mode 100644 index 0000000..9de1eef --- /dev/null +++ b/test/org/hmis/CocheTest.java @@ -0,0 +1,123 @@ +package org.hmis; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +class CocheTest { + @Test + void testEqualsObject() { + Coche coche1 = new Coche (); // Arrange + assertTrue(coche1.equals(coche1)); // Action // Assert + } + + @Test + void testEqualsObjectNull() { + Coche coche1 = new Coche (); // Arrange + Coche coche2 = null; + assertFalse(coche1.equals(coche2)); // Action // Assert + } + + @Test + void testEqualsObjectString() { + Coche coche1 = new Coche (); // Arrange + String coche2 = ""; + assertFalse(coche1.equals(coche2)); // Action // Assert + } + + @Test + void testEqualsObjectMultiple1() { + Coche primero = new Coche ("Chevrolet", "Camaro", 2023, 52000); + Coche segundo = new Coche ("Chevrolet", "Camaro", 2023, 52000); + assertEquals(true, primero.equals(segundo)); // Action // Assert + } + @Test + void testEqualsObjectMultiple2() { + Coche primero = new Coche ("Chevrolet", "Camaro", 2023, 52000); + Coche segundo = new Coche ("Chevrolet", "Camaro", 2021, 52000); + assertEquals(false, primero.equals(segundo)); // Action // Assert + } + @Test + void testEqualsObjectMultiple3() { + Coche primero = new Coche ("Chevrolet", "Camaro", 2023, 52000); + Coche segundo = new Coche ("Ford", "Camaro", 2023, 52000); + assertEquals(false, primero.equals(segundo)); // Action // Assert + } + @Test + void testEqualsObjectMultiple4() { + Coche primero = new Coche ("Chevrolet", "Camaro", 2023, 52000); + Coche segundo = new Coche ("Chevrolet", "Mustang", 2023, 52000); + assertEquals(false, primero.equals(segundo)); // Action // Assert + } + @Test + void testEqualsObjectMultiple5() { + Coche primero = new Coche ("Chevrolet", "Camaro", 2023, 52000); + Coche segundo = new Coche ("Chevrolet", "Camaro", 2023, 45000); + assertEquals(false, primero.equals(segundo)); // Action // Assert + } + + @Test + void testEqualsGetMarca() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + String resultado = "Chevrolet"; + assertEquals(resultado,coche1.getMarca()); // Action // Assert + } + + @Test + void testEqualsSetMarca() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + String resultado = "Honda"; + coche1.setMarca("Honda"); + assertEquals(resultado, coche1.getMarca()); // Action // Assert + } + + @Test + void testEqualsGetModelo() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + String resultado = "Camaro"; + assertEquals(resultado, coche1.getModelo()); // Action // Assert + } + + @Test + void testEqualsSetModelo() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + String resultado = "Civic"; + coche1.setModelo("Civic"); + assertEquals(resultado, coche1.getModelo()); // Action // Assert + } + + @Test + void testEqualsGetAño() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + int resultado = 2023; + assertEquals(resultado, coche1.getAño()); // Action // Assert + } + + @Test + void testEqualsSetAño() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + int resultado = 2023; + coche1.setAño(2023); + assertEquals(resultado, coche1.getAño()); // Action // Assert + } + + @Test + void testEqualsGetPrecio() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + int resultado = 52000; + assertEquals(resultado, coche1.getPrecio()); // Action // Assert + } + + @Test + void testEqualsSetPrecio() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + int resultado = 40000; + coche1.setPrecio(40000); + assertEquals(resultado, coche1.getPrecio()); // Action // Assert + } + + @Test + void testEqualsString() { + Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); + String resultado = "Coche [marca = Chevrolet, modelo = Camaro, año = 2023, precio = 52000]"; + assertEquals(resultado, coche1.toString()); // Action // Assert + } + +} \ No newline at end of file diff --git a/test/org/hmis/JsonReaderTest.java b/test/org/hmis/JsonReaderTest.java index 549c69a..388b2b4 100644 --- a/test/org/hmis/JsonReaderTest.java +++ b/test/org/hmis/JsonReaderTest.java @@ -9,20 +9,38 @@ class JsonReaderTest { @Test void testLeerCochesJSON() { - String ruta = "data/coches.json"; - Coche [] coches = JsonReader.leerCochesJSON(ruta); + String filepath = "data/coches.json"; + Coche [] coches = JsonReader.leerCochesJSON(filepath); assertEquals (4, coches.length); } @Test void testLeerCochesJSONprimero() { - String ruta = "data/coches.json"; - Coche primero = new Coche ("Toyota", "Corolla", 2022, 22000); - Coche [] coches = JsonReader.leerCochesJSON(ruta); - assertEquals(primero, coches[0]); - assertTrue (primero.equals(coches[0])); - assertTrue (coches[0].equals(primero)); + String filepath = "data/coches.json"; + Coche primero = new Coche ("Chevrolet", "Camaro", 2023, 52000); + Coche [] coches = JsonReader.leerCochesJSON(filepath); + assertEquals(primero, coches[2]); + assertTrue (primero.equals(coches[2])); + assertTrue (coches[2].equals(primero)); } + @Test + void testError() { + String filepath = "data/coches.json"; + Coche primero = new Coche ("Chevrolet", "Camaro", 2023, 52000); + Coche [] coches = JsonReader.leerCochesJSON(filepath); + + } + + @Test + void testExcepcionLeerArchivo() { + String filepath = "data/excepcion.json"; + assertDoesNotThrow(() -> JsonReader.leerCochesJSON(filepath)); + } + @Test + void testObjectoNoNull() { + JsonReader jsonReader = new JsonReader(); + assertNotEquals(jsonReader, null); + } } diff --git a/test/org/hmis/MainTest.java b/test/org/hmis/MainTest.java new file mode 100644 index 0000000..1eec4f8 --- /dev/null +++ b/test/org/hmis/MainTest.java @@ -0,0 +1,27 @@ +package org.hmis; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import org.junit.jupiter.api.Test; + +public class MainTest { + @Test + void testObjectNotNull() { + Main main = new Main(); + + assertNotEquals(main, null); + } + + @Test + void testMain() { + String filepath = "data/coches.json"; + Coche[] coches = JsonReader.leerCochesJSON(filepath); + + String resultado = ""; + for (Coche coche : coches) { + resultado = resultado + coche + "\n"; + } + + Main.main(null); + } + +} From f120b52b88245e6dc532111c7765f554cee873fd Mon Sep 17 00:00:00 2001 From: Sarai Besada Date: Tue, 9 May 2023 18:00:33 +0200 Subject: [PATCH 2/4] Coverage al 100% --- .classpath | 4 ++-- .settings/org.eclipse.jdt.core.prefs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.classpath b/.classpath index fc3a199..c3b8090 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,6 @@ - + @@ -13,9 +13,9 @@ + - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 43c8d71..eddfc24 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,8 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=15 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.compliance=15 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate @@ -12,4 +12,4 @@ org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=17 +org.eclipse.jdt.core.compiler.source=15 From 7ac5779437225af460919921b0897f71bc69e125 Mon Sep 17 00:00:00 2001 From: Sarai Besada Date: Tue, 9 May 2023 18:44:56 +0200 Subject: [PATCH 3/4] Test Parametrizado --- .classpath | 13 ++-- data/coches2.json | 10 ++++ data/coches3.json | 10 ++++ data/coches4.json | 10 ++++ pom.xml | 2 +- src/org/hmis/Coche.java | 6 +- test/org/hmis/CocheTest.java | 98 ++++++++++++++++++++++++++++++- test/org/hmis/JsonReaderTest.java | 31 +++++++++- 8 files changed, 169 insertions(+), 11 deletions(-) create mode 100644 data/coches2.json create mode 100644 data/coches3.json create mode 100644 data/coches4.json diff --git a/.classpath b/.classpath index c3b8090..64d5270 100644 --- a/.classpath +++ b/.classpath @@ -1,20 +1,20 @@ - + + - + + - + - - @@ -23,5 +23,6 @@ + - + \ No newline at end of file diff --git a/data/coches2.json b/data/coches2.json new file mode 100644 index 0000000..2c11fc4 --- /dev/null +++ b/data/coches2.json @@ -0,0 +1,10 @@ +{ + "coches": [ + { + "marca": "Chevrolet", + "modelo": "Camaro", + "año": 2023, + "precio": 52000 + } + ] +} diff --git a/data/coches3.json b/data/coches3.json new file mode 100644 index 0000000..2b01592 --- /dev/null +++ b/data/coches3.json @@ -0,0 +1,10 @@ +{ + "coches": [ + { + "marca": "Ford", + "modelo": "Mustang", + "año": 2021, + "precio": 45000 + } + ] +} diff --git a/data/coches4.json b/data/coches4.json new file mode 100644 index 0000000..6e15e88 --- /dev/null +++ b/data/coches4.json @@ -0,0 +1,10 @@ +{ + "coches": [ + { + "marca": "Toyota", + "modelo": "Corolla", + "año": 2022, + "precio": 22000 + } + ] +} diff --git a/pom.xml b/pom.xml index 0220a89..823205b 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ maven-compiler-plugin 3.11.0 - 17 + 11 diff --git a/src/org/hmis/Coche.java b/src/org/hmis/Coche.java index e0a0701..ee20309 100644 --- a/src/org/hmis/Coche.java +++ b/src/org/hmis/Coche.java @@ -65,8 +65,10 @@ public boolean equals(Object obj) { if (getClass() != obj.getClass()) return false; Coche other = (Coche) obj; - return año == other.año && Objects.equals(marca, other.marca) && Objects.equals(modelo, other.modelo) - && precio == other.precio; + return año == other.año && + Objects.equals(marca, other.marca) && + Objects.equals(modelo, other.modelo) && + precio == other.precio; } diff --git a/test/org/hmis/CocheTest.java b/test/org/hmis/CocheTest.java index 9de1eef..7ed3e09 100644 --- a/test/org/hmis/CocheTest.java +++ b/test/org/hmis/CocheTest.java @@ -1,6 +1,11 @@ package org.hmis; + import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + class CocheTest { @Test void testEqualsObject() { @@ -116,8 +121,99 @@ void testEqualsSetPrecio() { @Test void testEqualsString() { Coche coche1 = new Coche ("Chevrolet", "Camaro", 2023, 52000); - String resultado = "Coche [marca = Chevrolet, modelo = Camaro, año = 2023, precio = 52000]"; + String resultado = "Coche [marca=Chevrolet, modelo=Camaro, año=2023, precio=52000]"; assertEquals(resultado, coche1.toString()); // Action // Assert } + @ParameterizedTest + @CsvSource({"Toyota, Corolla, 2020, 22000", + "Ford, Mustang, 2021, 45000", + "Chevrolet, Camaro, 2023, 22000", + "Toyota, Civic, 2022, 25000"}) + + void testEqualsObjectMultiple(String marca, String modelo, int año, int precio) { + Coche coche1 = new Coche("Chevrolet", "Camaro", 2023, 52000); + Coche coche2 = new Coche(marca, modelo, año, precio); + assertEquals(false, coche1.equals(coche2)); + } + + @ParameterizedTest + @ValueSource(strings = {"Toyota", "Ford", "Chevrolet", "Honda"}) + + void testGetMarca(String marca){ + Coche coche1 = new Coche(marca, "-", 0, 0); + assertEquals(marca, coche1.getMarca()); + } + + @ParameterizedTest + @ValueSource(strings = {"Corolla", "Mustang", "Camaro", "Civic"}) + + void testGetModelo(String modelo){ + Coche coche1 = new Coche("-", modelo, 2, 2); + assertEquals(modelo, coche1.getModelo()); + } + + @ParameterizedTest + @ValueSource(ints = {2022, 2021, 2023, 2022}) + + void testGetAño(int año){ + Coche coche1 = new Coche("-", "-", año, 2); + assertEquals(año, coche1.getAño()); + } + + @ParameterizedTest + @ValueSource(ints = {22000, 45000, 52000, 25000}) + + void testGetPrecio(int precio){ + Coche coche1 = new Coche("-", "-", 2, precio); + assertEquals(precio, coche1.getPrecio()); + } + + @ParameterizedTest + @ValueSource(strings = {"Toyota", "Ford", "Chevrolet", "Honda"}) + + void testSetMarca(String marca){ + Coche coche1 = new Coche("-", "-", 2, 2); + coche1.setMarca(marca); + assertEquals(marca, coche1.getMarca()); + } + + @ParameterizedTest + @ValueSource(strings = {"Corolla", "Mustang", "Camaro", "Civic"}) + + void testSetModelo(String modelo){ + Coche coche1 = new Coche("-", "-", 2, 2); + coche1.setModelo(modelo); + assertEquals(modelo, coche1.getModelo()); + } + + @ParameterizedTest + @ValueSource(ints = {2022, 2021, 2023, 2022}) + + void testSetAño(int año){ + Coche coche1 = new Coche("-", "-", 2, 2); + coche1.setAño(año); + assertEquals(año, coche1.getAño()); + } + + @ParameterizedTest + @ValueSource(ints = {22000, 45000, 52000, 25000}) + + void testSetPrecio(int precio){ + Coche coche1 = new Coche("-", "-", 2, 2); + coche1.setPrecio(precio); + assertEquals(precio, coche1.getPrecio()); + } + + @ParameterizedTest + @CsvSource({"Toyota, Corolla, 2022, 22000", + "Ford, Mustang, 2021, 45000", + "Chevrolet, Camaro, 2023, 52000", + "Honda, Civic, 2022, 25000"}) + void testToString(String marca, String modelo, int año, int precio) { + String result = "Coche [marca=" + marca + ", modelo=" + modelo + ", año=" + año + ", precio=" + precio + "]"; + Coche coche1 = new Coche(marca, modelo, año, precio); + assertEquals(result, coche1.toString()); + } + } \ No newline at end of file diff --git a/test/org/hmis/JsonReaderTest.java b/test/org/hmis/JsonReaderTest.java index 388b2b4..8026f0c 100644 --- a/test/org/hmis/JsonReaderTest.java +++ b/test/org/hmis/JsonReaderTest.java @@ -2,8 +2,10 @@ import static org.junit.jupiter.api.Assertions.*; - import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; class JsonReaderTest { @@ -43,4 +45,31 @@ void testObjectoNoNull() { JsonReader jsonReader = new JsonReader(); assertNotEquals(jsonReader, null); } + + @ParameterizedTest + @CsvSource({"data/coches2.json, 1", + "data/coches3.json, 1", + "data/coches4.json, 1"}) + void testLeerCochesJSON(String filepath, int nCoches) { + Coche[] coches = JsonReader.leerCochesJSON(filepath); + assertEquals(nCoches, coches.length); + } + + @ParameterizedTest + @CsvSource({"data/coches2.json, Chevrolet, Camaro, 2023, 52000", + "data/coches3.json, Ford, Mustang, 2021, 45000", + "data/coches4.json, Toyota, Corolla, 2022, 22000"}) + void testLeerCochesJSONprimero(String filepath, String marca, String modelo, int año, int precio) { + Coche[] coches = JsonReader.leerCochesJSON(filepath); + Coche coche1 = new Coche (marca, modelo, año, precio); + assertEquals(coche1, coches[0]); + assertTrue(coche1.equals(coches[0])); + assertTrue(coches[0].equals(coche1)); + } + + @ParameterizedTest + @ValueSource(strings = {"data/excepcion.json", "data/coche.json", "data/otro.json"}) + void testExcepcionLeerCochesJSON(String filepath) { + assertDoesNotThrow(() -> JsonReader.leerCochesJSON(filepath)); + } } From a8101c19535c81c7ddf54a1160cefae31da9f4ae Mon Sep 17 00:00:00 2001 From: Sarai Besada Date: Tue, 9 May 2023 18:49:27 +0200 Subject: [PATCH 4/4] Cambios archivos --- .settings/org.eclipse.jdt.core.prefs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index eddfc24..1db2692 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,8 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=15 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=15 +org.eclipse.jdt.core.compiler.compliance=11 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate @@ -12,4 +12,4 @@ org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=15 +org.eclipse.jdt.core.compiler.source=11