-
Notifications
You must be signed in to change notification settings - Fork 136
Revisión Pruebas Reusable-DAS Grupo1 #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
949ac54
ca8598d
7717a0b
2bd8fc5
dc9929c
bb8e534
18040f4
29ed027
edf89d2
b02fc80
a774839
ac3c454
99487b3
373b726
db8a3f1
6ddd26a
89fad57
8cdf804
0b0f75c
9aa9fda
8ee37f8
416af28
e1d97c9
0123e15
e034ee7
b60657f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,62 +2,232 @@ | |
| * | ||
| */ | ||
| package ubu.gii.dass.c01; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.fail; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Disabled; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * @author alumno | ||
| * @author Sofía Calavia | ||
| * @author Andrés Puentes | ||
| * @author Mario Cea | ||
| * @author Alejandro García | ||
| * | ||
| */ | ||
| public class ReusablePoolTest { | ||
|
|
||
|
|
||
| /** | ||
| * @throws java.lang.Exception | ||
| */ | ||
| @BeforeAll | ||
| public static void setUp(){ | ||
| public static void setUp() throws Exception{ | ||
| } | ||
|
|
||
|
|
||
| @AfterAll | ||
| public static void tearDown() throws Exception { | ||
| } | ||
|
|
||
| /** | ||
| * Test method for {@link ubu.gii.dass.c01.ReusablePool#getInstance()}. | ||
| */ | ||
| @Test | ||
| @DisplayName("testGetInstance") | ||
| @Disabled("Not implemented yet") | ||
| public void testGetInstance() { | ||
| // Crear una instancia del pool | ||
| ReusablePool instance1 = ReusablePool.getInstance(); | ||
| //Verifica que la instancia no sea nula | ||
| assertNotNull (instance1,"La instancia adquirida no debe ser nula"); | ||
| //Comprueba si la instancia 1 es igual que la instancia 2 | ||
| ReusablePool instance2 = ReusablePool.getInstance(); | ||
| assertSame(instance1, instance2); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Se están implementando dos casos de prueba en el mismo método testGetInstance. Otra alternativa es separar cada caso de prueba en métodos distintos testGetInstance_cu01instancia testGetInstance_cu02instanciaunica. Además, para mejorar la comprensión assetSame puede incorporar un mensaje en "Singleton devuelve siempre la misma instancia" |
||
| } | ||
| /** | ||
| * Test method for {@link ubu.gii.dass.c01.ReusablePool#acquireReusable()}. | ||
| */ | ||
| @Test | ||
| @DisplayName("testAcquireReusable") | ||
| public void testAcquireReusable() { | ||
| // Crear una instancia del pool | ||
| ReusablePool pool = ReusablePool.getInstance(); | ||
| try { | ||
| //Va a adquirir un objeto reusable del pool | ||
| Reusable reusable = pool.acquireReusable(); | ||
| // Verifica que el objeto reusable adquirido no es nulo | ||
| assertNotNull(reusable,"El objeto reusable adquirido no debería ser nulo"); | ||
| // Verifica que el objeto adquirido es una instancia de la clase Reusable | ||
| assertTrue(reusable instanceof Reusable, "El objeto adquirido debe ser una instancia de Reusable"); | ||
| } catch (NotFreeInstanceException e) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comprobar el lanzamiento de excepciones es otro caso de prueba y se ha implementado en otro método de prueba. Es correcto. Para mejorar la comprensión sugiero ser coherentes, es decir, o todos los casos de prueba en el mismo método o todos en métodos separados. Además los pondría todos seguidos en el fichero. |
||
| // Si hay excepción, imprimirá un mensaje de error | ||
| fail("No se lanzaría una excepción si hemos adquirirido un objeto reusable del pool"); | ||
| } | ||
|
|
||
| } | ||
| /** | ||
| * Test method for {@link ubu.gii.dass.c01.ReusablePool#releaseReusable(ubu.gii.dass.c01.Reusable)}. | ||
| * @throws NotFreeInstanceException | ||
| */ | ||
| @Test | ||
| @DisplayName("testReleaseReusable") | ||
| public void testReleaseReusable() throws DuplicatedInstanceException, NotFreeInstanceException { | ||
| // Crear una instancia del pool | ||
| ReusablePool pool = ReusablePool.getInstance(); | ||
| //Crea un nuevo objeto reusable | ||
| Reusable reusable = new Reusable(); | ||
| try{ | ||
| //Libera este objeto del pool | ||
| pool.releaseReusable(reusable); | ||
| //Intenta adquirir un nuevo objeto de este | ||
| Reusable acqReusable = pool.acquireReusable(); | ||
| // Verifica si el objeto adquirido es el mismo que el objeto liberado | ||
| assertEquals(reusable, acqReusable, "El objeto que ha sido adquirido debe ser el mismo que el liberado"); | ||
| //Verifica que el pool tiene un objeto antes de liberarlo | ||
| assertNotNull(pool.acquireReusable(), "El pool debería tener al menos un objeto reusable después de liberarlo"); | ||
| } catch (DuplicatedInstanceException e) { | ||
| //Si se lanza una excepción, imprime un mensaje de error | ||
| fail("No debería saltar una excepcion al liberar un objeto reusable"); | ||
| } catch (NotFreeInstanceException e) { | ||
| //Si se lanza una excepción, imprime un mensaje de error | ||
| fail("No se debe lanzar una excepción al adquirir un objeto reusable"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test method for {@link ubu.gii.dass.c01.ReusablePool#acquireReusable()}. | ||
| * Test method for exception when no free instances are available. | ||
| */ | ||
| @Test | ||
| @DisplayName("testAcquireReusable") | ||
| @Disabled("Not implemented yet") | ||
| @DisplayName("testAcquireReusableWhenNoFreeInstances") | ||
| public void testAcquireReusableWhenNoFreeInstances() { | ||
| // Instancia un pool | ||
| ReusablePool pool = ReusablePool.getInstance(); | ||
|
|
||
| try { | ||
| // Adquiere 2 pools disponibles | ||
| Reusable r1 = pool.acquireReusable(); | ||
| Reusable r2 = pool.acquireReusable(); | ||
|
|
||
| // Verifica que ambos objetos no son nulls y que estan instanciados | ||
| assertNotNull(r1, "First reusable should not be null"); | ||
| assertNotNull(r2, "Second reusable should not be null"); | ||
|
|
||
| // Trata de adquirir un tercer reusable (deberia hacer un throw NotFreeInstanceException) | ||
| try { | ||
| pool.acquireReusable(); | ||
| fail("Should have thrown NotFreeInstanceException"); | ||
| } catch (NotFreeInstanceException e) { | ||
| // Excepcion esperada | ||
| assertEquals("No hay más instancias reutilizables disponibles. Reintentalo más tarde", e.getMessage()); | ||
| } | ||
| // Limpia y devuelve los objetos al pool | ||
| pool.releaseReusable(r1); | ||
| pool.releaseReusable(r2); | ||
|
|
||
| } catch (Exception e) { | ||
| fail("Unexpected exception: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void testAcquireReusable() { | ||
| /** | ||
| * Test method for exception when releasing a duplicate instance. | ||
| */ | ||
| @Test | ||
| @DisplayName("testReleaseReusableDuplicated") | ||
| public void testReleaseReusableDuplicated() { | ||
| // Instancia un pool | ||
| ReusablePool pool = ReusablePool.getInstance(); | ||
|
|
||
| try { | ||
| // Adquiere a reusable | ||
| Reusable reusable = pool.acquireReusable(); | ||
| pool.releaseReusable(reusable); | ||
|
|
||
| // Intenta hacerlo de nuevo (sino throw DuplicatedInstanceException) | ||
| try { | ||
| pool.releaseReusable(reusable); | ||
| fail("Should have thrown DuplicatedInstanceException"); | ||
| } catch (DuplicatedInstanceException e) { | ||
| // Excepcion esperada | ||
| assertEquals("Ya existe esa instancia en el pool.", e.getMessage()); | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| fail("Unexpected exception: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test method for {@link ubu.gii.dass.c01.ReusablePool#releaseReusable(ubu.gii.dass.c01.Reusable)}. | ||
| * Test for proper functioning of the pool with multiple operations. | ||
| */ | ||
| @Test | ||
| @DisplayName("testPoolCycleOperations") | ||
| public void testPoolCycleOperations() { | ||
| // Consigue la instancia pool | ||
| ReusablePool pool = ReusablePool.getInstance(); | ||
|
|
||
| try { | ||
| // Paso 1: adquiere todas las variables reusables disponibles | ||
| Reusable r1 = pool.acquireReusable(); | ||
| Reusable r2 = pool.acquireReusable(); | ||
|
|
||
| // Paso 2: Verifica que sean objetos diferentes | ||
| assertNotNull(r1, "First reusable should not be null"); | ||
| assertNotNull(r2, "Second reusable should not be null"); | ||
| assertTrue(r1 != r2, "Should be different objects"); | ||
|
|
||
| // Paso 3: Cambia el orden | ||
| pool.releaseReusable(r2); | ||
| pool.releaseReusable(r1); | ||
|
|
||
| // Paso 4: Readquiere y verifica el orden LIFO | ||
| Reusable r3 = pool.acquireReusable(); | ||
| Reusable r4 = pool.acquireReusable(); | ||
|
|
||
| // El pool deberia de retornar objetos siguiendo el orden LIFO | ||
| assertSame(r1, r3, "First released object should be first acquired"); | ||
| assertSame(r2, r4, "Second released object should be second acquired"); | ||
|
|
||
| pool.releaseReusable(r3); | ||
| pool.releaseReusable(r4); | ||
|
|
||
| } catch (Exception e) { | ||
| fail("Unexpected exception: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test method for Reusable.util() functionality. | ||
| */ | ||
| @Test | ||
| @DisplayName("testReleaseReusable") | ||
| @Disabled("Not implemented yet") | ||
| public void testReleaseReusable() { | ||
| @DisplayName("testReusableUtil") | ||
| public void testReusableUtil() { | ||
| // Crea un objeto reusable y un string | ||
| Reusable reusable = new Reusable(); | ||
| String utilString = reusable.util(); | ||
|
|
||
| // Verifica si contiene lo esperado | ||
| assertTrue(utilString.contains(String.valueOf(reusable.hashCode())), | ||
| "Util string should contain the object's hashcode"); | ||
| assertTrue(utilString.contains(":Uso del objeto Reutilizable"), | ||
| "Util string should contain the expected message"); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Esta prueba esta fuera del contexto de prueba Reusablepool se debería eliminar de este fichero. Además, Reusable es una implementación de compromiso que no debería ser probada. Entiendo que se ha añadido para conseguir el 100% de cobertura del sistema. Si se quiere eliminar el seguimiento de su cobertura se puede indicar en el fichero de configuración de |
||
|
|
||
| /** | ||
| * Test method for exception classes. | ||
| */ | ||
| @Test | ||
| @DisplayName("testExceptionMessages") | ||
| public void testExceptionMessages() { | ||
| // Test NotFreeInstanceException | ||
| NotFreeInstanceException notFreeEx = new NotFreeInstanceException(); | ||
| assertEquals("No hay más instancias reutilizables disponibles. Reintentalo más tarde", | ||
| notFreeEx.getMessage(), | ||
| "NotFreeInstanceException should have correct message"); | ||
|
|
||
| // Test DuplicatedInstanceException | ||
| DuplicatedInstanceException dupEx = new DuplicatedInstanceException(); | ||
| assertEquals("Ya existe esa instancia en el pool.", | ||
| dupEx.getMessage(), | ||
| "DuplicatedInstanceException should have correct message"); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nunca había visto estas pruebas de comprobar el mensaje de las excepciones. No aconsejo hacerlo ya que los mensajes pueden ser muy inestables y no interfieren en requisitos funcionales. Las pruebas se deberían definir para comprobar el funcionamiento correcto del sistema. La prueba fundamental es que se lanza la excepción. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
¿Qué sentido tiene añadir throws Exception?