|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
[PAIR] Fran vs Anonimo - The Beginning
Il task consiste nel restituire un oggetto NullAudio, che implementa l'interfaccia AudioInterface, e che fa nulla, quando la creazione di un oggetto Audio fallisce.
Questo e' il test che alla fine del task deve passare: Codice:
public void testAudioCreationFailure()
{
AudioFactory audioFactory = new FailingAudioFactory();
AudioInterface audio = environment.createAudio(audioFactory);
assertTrue(audio.isNull());
}
- il metodo isNull() in AudioInterface - la classe audioFactory() - una versione del metodo createAudio() di environment che accetti un oggetto AudioFactory Manca inoltre una classe NullAudio. Partiamo con il metodo isNull(), questo e' il test in TestAudio.java: Codice:
public void setUp()
{
audio = MockAudio.create();
}
public void testIsNullNotTrueForMockAudio()
{
assertFalse(audio.isNull());
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Eccomi.
Per permettere la compilazione del test è stato aggiunto un metodo all'interfaccia AudioInterface : Codice:
boolean isNull(); Codice:
public boolean isNull()
{
return false;
}
Codice:
public boolean isNull()
{
return true;
}
Per farlo passare è stato cambiato in MockAudio in questo modo : Codice:
public boolean isNull()
{
return false;
}
Test passato e avanti il prossimo
__________________
GPU Compiler Engineer |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Il prossimo test richiede una classe NullAudio che sia nulla:
Codice:
public void testNullAudioIsNull()
{
audio = new NullAudio();
assertTrue(audio.isNull());
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Ok, creata una classe NullAudio che implementa AudioInterface.
Eclipse ha creato automaticamente questa classe : Codice:
package it.diamonds.engine.audio;
public class NullAudio implements AudioInterface
{
public Sound createSound(String name)
{
// TODO Auto-generated method stub
return null;
}
public boolean isCreated()
{
// TODO Auto-generated method stub
return false;
}
public boolean isInitialised()
{
// TODO Auto-generated method stub
return false;
}
public boolean isMusicPlaying()
{
// TODO Auto-generated method stub
return false;
}
public boolean isNull()
{
// TODO Auto-generated method stub
return false;
}
public void playMusic()
{
// TODO Auto-generated method stub
}
public void shutDown()
{
// TODO Auto-generated method stub
}
public void stopMusic()
{
// TODO Auto-generated method stub
}
}
Codice:
public boolean isNull()
{
return true;
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Ora ci serve l'AudioFactory, questo e' il test:
Codice:
package it.diamonds.tests.engine;
import junit.framework.TestCase;
public class TestAudioFactory extends TestCase
{
public void testAudioFactoryCreate()
{
AudioFactory factory = new AudioFactory();
assertNotNull(factory.create());
}
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Ok
Creazione di una classe it.diamonds.engine.AudioFactory : Codice:
package it.diamonds.engine;
public class AudioFactory
{
public Object create()
{
return null;
}
}
Cambiando in : Codice:
public Object create()
{
return 0;
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
C'e' un bug in NullAudio, faccio prima a metterti il test che a spiegartelo:
Codice:
package it.diamonds.tests.engine.audio;
import it.diamonds.engine.audio.AudioInterface;
import it.diamonds.engine.audio.NullAudio;
import junit.framework.TestCase;
public class TestNullAudio extends TestCase
{
public void testNullAudioIsCreated()
{
AudioInterface audio = new NullAudio();
assertTrue(audio.isCreated());
}
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Non avevo mai visto un bug così
Il bug è stato risolto cambiando in it.diamonds.engine.audio.NullAudio : Codice:
public boolean isCreated()
{
return false;
}
Codice:
public boolean isCreated()
{
return true;
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Prossimo test:
Codice:
package it.diamonds.tests.engine;
import it.diamonds.engine.AudioFactory;
import junit.framework.TestCase;
public class TestAudioFactory extends TestCase
{
public void testAudioFactoryCreate()
{
AudioFactory factory = new AudioFactory();
assertNotNull(factory.create());
}
public void testAudioFactoryCreateMockAudio()
{
AudioFactory factory = new MockAudioFactory();
assertNotNull((MockAudio) factory.create());
}
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Creata la classe it.diamonds.tests.mocks.MockAudioFactory :
Codice:
public class MockAudioFactory extends AudioFactory
{
}
Aggiungendo un override della create nella classe : Codice:
public AudioInterface create()
{
return MockAudio.create();
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Chiudiamo il conto con AudioFactory
Codice:
package it.diamonds.tests.engine;
import it.diamonds.engine.AudioFactory;
import it.diamonds.engine.audio.AudioInterface;
import it.diamonds.tests.mocks.MockAudio;
import it.diamonds.tests.mocks.MockAudioFactory;
import junit.framework.TestCase;
public class TestAudioFactory extends TestCase
{
public void testAudioFactoryCreate()
{
AudioFactory factory = new AudioFactory();
assertNotNull(factory.create());
}
public void testAudioFactoryCreateMockAudio()
{
AudioFactory factory = new MockAudioFactory();
assertNotNull((MockAudio) factory.create());
}
public void testAudioFactoryCreateAudioInterface()
{
AudioFactory factory = new AudioFactory();
assertNotNull((AudioInterface) factory.create());
}
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#12 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Cambiamo :
Codice:
public Object create()
{
return 0;
}
Codice:
public AudioInterface create()
{
return new NullAudio();
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#13 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
In TestEnvironment.java, un test che chieda di passare un AudioFactory al metodo createAudio di Environment:
Codice:
public void testAudioCreationWithFactory()
{
AudioFactory audioFactory = new AudioFactory();
assertNotNull((AudioInterface) environment.createAudio(audioFactory));
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#14 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Aggiunto il metodo in it.diamonds.Envionment :
Codice:
public AudioInterface createAudio(AudioFactory factory)
{
return factory.create();
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#15 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Ora creiamo una classe mock che ritorni un eccezione quando tenta di creare un Audio.
Questo e' il test: Codice:
public void testFailingAudioFactoryMockException()
{
try
{
AudioFactory audioFactory = new FailingAudioFactoryMock();
audioFactory.create();
fail();
}
catch (RuntimeException e)
{
}
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#16 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Creata la classe it.diamonds.tests.mocks.FailingAudioFactoryMock :
Codice:
public class FailingAudioFactoryMock extends AudioFactory
{
public AudioInterface create()
{
throw new RuntimeException();
}
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#17 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Proviamo a dare un colpo all'ultimo test, vediamo se si riesce a far passare con un paio di righe di codice:
Codice:
public void testAudioCreationFailure()
{
AudioFactory audioFactory = new FailingAudioFactoryMock();
AudioInterface audio = environment.createAudio(audioFactory);
assertTrue(audio.isNull());
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#18 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
In it.diamonds.Environment la funzione createAudio(AudioFactory) è stata cambiata così :
Codice:
public AudioInterface createAudio(AudioFactory factory)
{
return new NullAudio();
}
__________________
GPU Compiler Engineer |
|
|
|
|
|
#19 |
|
Senior Member
Iscritto dal: Oct 2002
Città: San Jose, California
Messaggi: 11794
|
Questo vuol dire che il primo test di verifica che ho scritto non dava abbastanza informazioni, ci serve un altro test:
Codice:
public void testAudioNotNullCreationWithFactory()
{
AudioFactory audioFactory = new MockAudioFactory();
AudioInterface audio = environment.createAudio(audioFactory);
assertFalse(audio.isNull());
}
__________________
"We in the game industry are lucky enough to be able to create our visions" @ NVIDIA |
|
|
|
|
|
#20 |
|
Senior Member
Iscritto dal: Aug 2001
Città: San Francisco, CA, USA
Messaggi: 13827
|
Dopo grandi ed epiche lotte intestine tra Eclipse e il repository SVN ecco le modifiche al codice
Metodo Environment.createAudio(AudioFactory) Codice:
public AudioInterface createAudio(AudioFactory factory)
{
try
{
audio = factory.create();
} catch (RuntimeException e)
{
audio = new NullAudio();
}
return audio;
}
__________________
GPU Compiler Engineer |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:32.



















