working effectively with legacy code 2009 test - andstudy/forge GitHub Wiki
μμΈ λ°μ ν μ€νΈ
μ§λ¬Έ
-
http://cafe.naver.com/ArticleRead.nhn?clubid=13516524&articleid=1056
-
μλ μ½λμμ IOException μ΄ λ°μν λ G3P1EP2Exception μ΄ λ°μνλ κ²μ λν ν μ€νΈλ₯Ό μμ±νλ €κ³ ν©λλ€.
-
κ·Έλ°λ° fos μμ IOException μ λ°μμμΌμΌ νλλ° μ΄λ»κ² νλκ² μ’μκΉμ?
public void saveData() throws G3P1EP2Exception { File file = new File(_strFilePath); String strWorkingDirectory = file.getParent(); if(createDirectory(strWorkingDirectory) == false) { throw new G3P1EP2Exception(strWorkingDirectory + " μμ± μ€ν¨", ErrorCodeSet.ERR_SaveData_FailCreateDirectory); } FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(bbos.getByteBuffer(), 0, bbos.getSize()); } catch(IOException e) { throw new G3P1EP2Exception(_strFilePath + " μμ± μ€ν¨", ErrorCodeSet.ERR_SaveData_FailSaveFile); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { } } } }
ν μ€νΈ μ½λ μμ± μμ
- FileOutputStream μμλ°μ κ°μ§ ν΄λμ€ λ§λ€κΈ° - FakeFileOutputStream
- κ°μ§ ν΄λμ€μμ μμΈλ₯Ό λμ§λ ν¨μ μμ±
- κ°μ§ ν΄λμ€λ₯Ό μ¬μ©νκΈ°μν΄ λΆλ¦¬μ ν©μ§μ (seam λ΄ν©) λ§λ€κΈ°
- κ°μ§ ν΄λμ€λ₯Ό μ¬μ©νμ¬ ν μ€νΈ μμ±
κ°μ§ ν΄λμ€ λ§λ€κΈ°
-
FileOutputStream μμλ°μ κ°μ§ ν΄λμ€ λ§λ€κΈ° - FakeFileOutputStream
-
λμΆ© λ€μ μ½λμ κ°μ κ²μ΄λ€
-
write(...) ν¨μλ 무쑰건 μμΈλ₯Ό λμ§κ² λ§λ€μλ€
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FakeOutputStream extends FileOutputStream { public FakeOutputStream(File file) throws FileNotFoundException { super(file); } @Override public void write(byte[] b, int off, int len) throws IOException { throw new IOException(); } }
λΆλ¦¬μ ν©μ§μ (seam λ΄ν©) λ§λ€κΈ°
-
κ°μ§ ν΄λμ€λ₯Ό μ¬μ©νκΈ°μν΄ λΆλ¦¬μ ν©μ§μ λ§λ λ€
-
FileOutputStream μ νλΌλ―Έν°λ‘ κ°μ§λ saveData(...) ν¨μ λ§λ€κΈ°
public void saveData(FileOutputStream fos) throws G3P1EP2Exception { try { fos.write(bbos .getByteBuffer(), 0, bbos.getSize()); } catch(IOException e) { throw new G3P1EP2Exception(_strFilePath + " μμ± μ€ν¨", ErrorCodeSet.ERR_SaveData_FailSaveFile); } } -
κΈ°μ‘΄μ saveData() ν¨μλ λ€μκ³Ό κ°μ΄ λ°λλ€
public void saveData() throws G3P1EP2Exception { // μλ΅ try { fos = new FileOutputStream(file); saveData(fos); // **** μ¬κΈ°κ° λ°λμμ **** } // μλ΅ }
ν μ€νΈ μ½λ λ§λ€κΈ°
-
λΆλ¦¬μ ν©μ§μ μ κ°μ§ κ°μ²΄λ₯Ό μ¬μ©νμ¬ ν μ€νΈ μμ±
public void testSaveData() throws IOException { // μμΈλ₯Ό ν μ€νΈνλ μμ λ°©μ, JUnit λ²μ μ΄ μ¬λμΌλ λκ° λ λ©μ§ λ°©λ²μ΄ μμ§ μμκΉ? try { new Data().saveData(new FakeOutputStream(File.createTempFile("temp", "tmp"))); assertTrue(false); // λλ¬νλ©΄ μλ¨ } catch (G3P1EP2Exception e) { assertTrue(true); // μ¬κΈ°λ₯Ό ν΅κ³Ό ν΄μΌ ν¨ assertEquals(ErrorCodeSet.ERR_SaveData_FailSaveFile, e.getErrorCode()); } catch (FileNotFoundException e) { assertTrue(false); // λλ¬νλ©΄ μλ¨ } catch (IOException e) { assertTrue(false); // λλ¬νλ©΄ μλ¨ } }
κΈ°ν
- μλͺ»λ λΆλΆμ΄ μλ λͺ¨λ₯΄κ² λ€μ
- μ’ κ±°μ¬λ¦¬λ λΆλΆλ μμΌλ λμΆ© μ°Έκ³ νμκ³ λ λμ ν μ€νΈλ₯Ό λ§λ€μ΄ 보μκΈ° λ°λλλ€
- FileOutputStream μ λνΌ ν΄λμ€λ₯Ό λ§λ€κ³ κ±°κΈ°μ μΈν°νμ΄μ€λ₯Ό λ½μ μ¬λ €λ μ’μκ² κ°μ΅λλ€