Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Practica_1.odt
Binary file not shown.
2 changes: 1 addition & 1 deletion src/ubu/gii/dass/c01/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class Client{


public static void main(String arg[]) throws NotFreeInstanceException, DuplicatedInstanceException{
public static void main(String arg[]) throws NotFreeInstanceException, DuplicatedInstanceException, FullPoolException{
ReusablePool pool;
Reusable r1,r2,r3;
Logger logger = Logger.getLogger("c01");
Expand Down
8 changes: 8 additions & 0 deletions src/ubu/gii/dass/c01/FullPoolException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ubu.gii.dass.c01;

public class FullPoolException extends Exception {

public FullPoolException(String msg){
super(msg);
}
}
2 changes: 1 addition & 1 deletion src/ubu/gii/dass/c01/NotFreeInstanceException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class NotFreeInstanceException extends Exception{
private static final long serialVersionUID = 1L;

public NotFreeInstanceException(){
super("No hay m�s instancias reutilizables disponibles. Reintentalo m�s tarde");
super("No hay más instancias reutilizables disponibles. Reintentalo más tarde");
}
}
19 changes: 14 additions & 5 deletions src/ubu/gii/dass/c01/ReusablePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
import java.util.*;
/**
* Pool que gestiona dos objetos de tipo Reusables para poder ser compartidos.
* @author Carlos L�pez [email protected]
* @author Carlos López [email protected]
*/

public final class ReusablePool{

private Vector<Reusable> reusables;
private static ReusablePool instance;
private int size;

private ReusablePool(int size){
this.size = size;
reusables = new Vector<Reusable>(size);
for(int i=0;i<size;i++)
reusables.add(new Reusable());
}

/**
* M�todo singleton que crea u obtiene la �nica instancia del Pool que gestiona dos objetos Reusables
* @return la instancia �nica del Pool
* Método singleton que crea u obtiene la única instancia del Pool que gestiona dos objetos Reusables
* @return la instancia única del Pool
*
*/
public static ReusablePool getInstance(){
Expand Down Expand Up @@ -49,11 +51,18 @@ public Reusable acquireReusable() throws NotFreeInstanceException{
* El cliente libera una instancia del objeto Reusable y se guarda en el Pool para poder ser utilizada por otro cliente.
* @param r una instancia objeto reusable
* @exception DuplicatedInstanceException si el objeto reusable ya existe en el pool
* @throws FullPoolException
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falta documentación de la excepción FullPoolException

*
*/

public void releaseReusable(Reusable r) throws DuplicatedInstanceException {
if (reusables.contains(r)==false){
public void releaseReusable(Reusable r) throws DuplicatedInstanceException, FullPoolException {
if (r == null){
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

El nombre de la excepción no es no es muy apropiado, ya que se lanza cuando recibe un objeto nulo.

throw new FullPoolException("No se puede liberar un objeto nulo");
}
else if (reusables.size() >= this.size){
throw new FullPoolException("No se puede liberar más objetos que la dimensión del pool");
}
else if (reusables.contains(r)==false){
reusables.add(r);
}
else{
Expand Down
201 changes: 145 additions & 56 deletions src/ubu/gii/dass/test/c01/ReusablePoolTest.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,145 @@
/**
*
*/
package ubu.gii.dass.test.c01;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* @author alumno
*
*/
public class ReusablePoolTest {

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

/**
* Test method for {@link ubu.gii.dass.c01.ReusablePool#getInstance()}.
*/
@Test
public void testGetInstance() {
fail("Not yet implemented");
}

/**
* Test method for {@link ubu.gii.dass.c01.ReusablePool#acquireReusable()}.
*/
@Test
public void testAcquireReusable() {
fail("Not yet implemented");
}

/**
* Test method for {@link ubu.gii.dass.c01.ReusablePool#releaseReusable(ubu.gii.dass.c01.Reusable)}.
*/
@Test
public void testReleaseReusable() {
fail("Not yet implemented");
}

}
/**
*
*/
package ubu.gii.dass.test.c01;

import static org.junit.Assert.*;
import ubu.gii.dass.c01.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Clase Test que prueba la clase ReusablePool con 100% de cobertura.
*
* @author CUEVAS DÍEZ JOSÉ RAMÓN
* @author LÓPEZ MARÍN LAURA
* @author CUADRADO GARCÍA IRENE
* @author EPIKHIN ANTON
*/
public class ReusablePoolTest {

/**
* Pool que contendrá los objetos reusables
*/
private ReusablePool pool;
/**
* Objetos reusables;
*/
private Reusable reusable1, reusable2;

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
pool = ReusablePool.getInstance();
reusable1 = pool.acquireReusable();
reusable2 = pool.acquireReusable();
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
pool.releaseReusable(reusable1);
pool.releaseReusable(reusable2);
}

/**
* Test method for {@link ubu.gii.dass.c01.ReusablePool#getInstance()}.
*/
@Test
public void testGetInstance() {
// Adquiere instancia de ReusablePool.
ReusablePool poolTest = ReusablePool.getInstance();

// Comprueba que pool y poolTest son instancia de ReusablePool.
assertTrue(pool instanceof ReusablePool);
assertTrue(poolTest instanceof ReusablePool);

// Compara los dos objetos son de la misma instancia.
// Debido a que el metodo 'equals' no es sobreescrito, 'equals' y '=='
// darán el mismo resultado.
assertTrue(pool == poolTest);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Añadir el parámetro de mensaje informativo en el AssertTrue.
AssertTrue("La instancia Singleton no es la misma", pool == poolTest)

Este comentario de texto debería estar extraido de la documentación de los casos de prueba de la memoria.

assertTrue(pool.equals(poolTest));
}

/**
* Test method for {@link ubu.gii.dass.c01.ReusablePool#acquireReusable()}.
*/
@Test
public void testAcquireReusable() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dividir el método en varios. Uno por cada caso de prueba. Utiliza el mismo nombre de método numerado.

// acquirir correctamente
try {
pool.releaseReusable(reusable2);
Reusable reusable3 = pool.acquireReusable();
} catch (Exception e) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cambiar estilo de comprobación de lanzamiento de excepciones guía

fail("No se esperaba ninguna excepción ");
}
try {
// acquirir sin posibilidad de hacerlo, NotFreeInstance
Reusable reusable4 = pool.acquireReusable();
} catch (NotFreeInstanceException e) {
assertEquals("No hay más instancias reutilizables disponibles. Reintentalo más tarde", e.getMessage());
}

}

/**
* Test method for
* {@link ubu.gii.dass.c01.ReusablePool#releaseReusable(ubu.gii.dass.c01.Reusable)}
* .
*
* @throws NotFreeInstanceException
*/
@Test
public void testReleaseReusable() throws NotFreeInstanceException {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

El método implementa múltiples casos de prueba.
Mejor crear un método por cada uno de los casos de prueba.
El nombre de los nuevos métodos puede ser testReleaseReusablexx, donde xx se corresponden con dos dígitos decimales que ayuden a identificar el caso de prueba en la documentación.


try {
//se intenta liberar un objeto nulo, Excepcion
pool.releaseReusable(null);
fail("Se esperaba excepción de tipo Exception que controle la liberación de nulos");
}catch (Exception e){
assertEquals("No se puede liberar un objeto nulo", e.getMessage());
}

try {
// se libera del pool un objeto reusable, correcto
pool.releaseReusable(reusable1);
//se libera del pool un objeto reusable, correcto
pool.releaseReusable(reusable2);
// se intenta liberar el mismo objeto reusable,
} catch (Exception e) {
fail("No se espera ninguna excepcion");
}
try{
// DuplicatedInstanceException
reusable2 = pool.acquireReusable();
pool.releaseReusable(reusable1);
fail("Se esperaba excepción de tipo DuplicatedInstanceException");

} catch (DuplicatedInstanceException | FullPoolException e) {
assertEquals("Ya existe esa instancia en el pool.", e.getMessage());
}

try{
//se intenta liberar un objeto reusable, cuando ya esta completo el pool
pool.releaseReusable(reusable2);
Reusable reusable3=new Reusable();
pool.releaseReusable(reusable3);
fail("Se esperaba excepción de tipo Exception que controle la dimensión máxima del pool");
}catch(Exception e){
assertEquals("No se puede liberar más objetos que la dimensión del pool", e.getMessage());

}

// se añade los objetos reusables para dejar el pool vacio (estado inicial)
reusable1 = pool.acquireReusable();
reusable2 = pool.acquireReusable();

}

}