package Ġat . cp ick l . g ads u . persistence Ċ Ċ import Ġat . cp ick l . g ads u . service . Clock Ċ import Ġat . cp ick l . g ads u . service . FileSystem Ċ import Ġat . cp ick l . g ads u . service . Meta Inf Ċ import Ġat . cp ick l . g ads u . test infra . Simple Test able Clock Ċ import Ġat . cp ick l . g ads u . test infra . TEST _ DATETIME 2 Ċ import Ġat . cp ick l . g ads u . test infra . TEST _ VERSION 0 0 0 SS Ċ import Ġat . cp ick l . g ads u . test infra . TEST _ VERSION 1 Ċ import Ġat . cp ick l . g ads u . test infra . TEST _ VERSION 1 2 3 R Ċ import Ġat . cp ick l . g ads u . version . Version Ċ import Ġorg . hamcrest . Matcher Assert . assertThat Ċ import Ġorg . hamcrest . Matchers . equalTo Ċ import Ġorg . joda . time . DateTime Ċ import Ġorg . mockito . Mockito Ċ import Ġorg . mockito . Mockito .* Ċ import Ġorg . testng . annotations . Before Method Ċ import Ġorg . testng . annotations . Test Ċ import Ġjava . io . File Ċ Ċ @ Test Ġclass ĠBackup Controller Test Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġversion 1 Ġ= ĠTEST _ VERSION 1 ĊĠĠĠ Ġprivate Ġval Ġdate 1 Ġ= ĠTEST _ DATETIME 2 ĊĠĠĠ Ġprivate Ġval Ġdate 1 Y esterday Ġ= Ġdate 1 . minus Days ( 1 ) ĊĠĠĠ Ġprivate Ġval Ġdate 1 Tom orrow Ġ= Ġdate 1 . plus Days ( 1 ) ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġclock : ĠClock ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġassist : ĠBackup Ass ist ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmeta Inf : ĠMeta Inf ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġfiles : ĠFileSystem ĊĊĠĠĠ Ġ@ Before Method ĊĠĠĠ Ġfun Ġinit State () Ġ{ ĊĠĠĠĠĠĠĠ Ġclock Ġ= ĠSimple Test able Clock ( date 1 ) ĊĠĠĠĠĠĠĠ Ġassist Ġ= ĠBackup Ass ist ( File ( java Class . getResource ("/ g ads u _ test / backup _ test _ db "). to URI ()), ĠFile (" test _ backup ")) ĊĠĠĠĠĠĠĠ Ġmeta Inf Ġ= ĠMeta Inf ( version 1 , Ġdate 1 ) ĊĠĠĠĠĠĠĠ Ġfiles Ġ= ĠMockito . mock ( FileSystem :: class . java ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` has Today Backup , Ġempty Ġfiles Ġshould Ġbe Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen List Files () ĊĠĠĠĠĠĠĠ ĠassertThat ( test ee (). has Today Backup (), ĠequalTo ( false )) ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` has Today Backup , Ġwith Ġto days Ġbackup Ġshould Ġbe Ġtrue `() Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen List Files ( backup Item ( date 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( test ee (). has Today Backup (), ĠequalTo ( true )) ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` has Today Backup , Ġwith Ġy ester days Ġbackup Ġshould Ġbe Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen List Files ( backup Item ( date 1 Y esterday )) ĊĠĠĠĠĠĠĠ ĠassertThat ( test ee (). has Today Backup (), ĠequalTo ( false )) ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġfun Ġ` has Today Backup , Ġwith Ġy ester days Ġand Ġto days Ġbackup Ġshould Ġbe Ġtrue `() Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen List Files ( backup Item ( date 1 ), Ġbackup Item ( date 1 Y esterday )) ĊĠĠĠĠĠĠĠ ĠassertThat ( test ee (). has Today Backup (), ĠequalTo ( true )) ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` has Today Backup , Ġwith Ġy ester days Ġand Ġy est ery esterday Ġbackup Ġshould Ġbe Ġfalse `() Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen List Files ( backup Item ( date 1 Y esterday . minus Days ( 1 )), Ġbackup Item ( date 1 Y esterday )) ĊĠĠĠĠĠĠĠ ĠassertThat ( test ee (). has Today Backup (), ĠequalTo ( false )) ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` create Backup Ġshould Ġsimply Ġdelegate Ġto ĠFileSystem s Ġzip Ġmethod `() Ġ{ ĊĠĠĠĠĠĠĠ Ġtest ee (). create Backup () ĊĠĠĠĠĠĠĠ Ġverify ( files ). zip ( assist . database Directory , ĠBackup Item ( clock . now (), Ġmeta Inf . application Version ). to File ( assist . backup Directory )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` ensure Max , Ġno Ġfiles , Ġshould Ġdo Ġnothing `() Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen List Files () ĊĠĠĠĠĠĠĠ Ġtest ee (). ensure Max Backups () ĊĊĠĠĠĠĠĠĠ Ġverify Backup Dir Exists () ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠĠĠĠĠ Ġverify No More Interactions ( files ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` ensure Max , Ġtwo Ġfiles , Ġshould Ġdo Ġnothing `() Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen List Files ( backup Item ( date 1 ), Ġbackup Item ( date 1 Y esterday )) ĊĠĠĠĠĠĠĠ Ġtest ee (). ensure Max Backups () ĊĊĠĠĠĠĠĠĠ Ġverify Backup Dir Exists () ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠĠĠĠĠ Ġverify No More Interactions ( files ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` ensure Max , Ġfour Ġfiles , Ġshould Ġdelete Ġoldest `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġoldest Backup Ġ= Ġbackup Item ( date 1 Y esterday ) ĊĠĠĠĠĠĠĠ Ġwhen List Files ( backup Item ( date 1 ), Ġoldest Backup , Ġbackup Item ( date 1 Y esterday ), Ġbackup Item ( date 1 Tom orrow . plus Days ( 1 ))) ĊĊĠĠĠĠĠĠĠ Ġtest ee (). ensure Max Backups () ĊĊĠĠĠĠĠĠĠ Ġverify Backup Dir Exists () ĊĠĠĠĠĠĠĠ Ġverify List Backups () ĊĠĠĠĠĠĠĠ Ġverify ( files ). delete ( ol dest Backup . to File ( assist . backup Directory )) ĊĠĠĠĠĠĠĠ Ġverify No More Interactions ( files ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġtest ee () Ġ= ĠBackup Controller Impl ( clock , Ġassist , Ġmeta Inf , Ġfiles ) ĊĊĠĠĠ Ġprivate Ġfun Ġbackup Item ( date : ĠDateTime ) Ġ= ĠBackup Item ( date , Ġversion 1 ) ĊĊĠĠĠ Ġprivate Ġfun Ġwhen List Files ( var arg Ġbackup Items : ĠBackup Item ) Ġ{ ĊĠĠĠĠĠĠĠ Ġ` when `( files . list Files ( assist . backup Directory , Ġ" zip ")). thenReturn ( backup Items . map Ġ{ Ġit . to File ( assist . backup Directory ) Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġverify Backup Dir Exists () Ġ{ ĊĠĠĠĠĠĠĠ Ġverify ( files ). ensure Exists ( assist . backup Directory ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġverify List Backups () Ġ{ ĊĠĠĠĠĠĠĠ Ġverify ( files ). list Files ( assist . backup Directory , Ġ" zip ") ĊĠĠĠ Ġ} Ċ Ċ } Ċ Ċ @ Test Ġclass ĠBackup Extensions Test Ġ{ ĊĊĠĠĠ Ġfun Ġ` Backup Item , Ġto File `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġparent Directory Ġ= ĠFile (".") ĊĠĠĠĠĠĠĠ ĠassertThat ( Backup Item ( TEST _ DATETIME 2 , ĠTEST _ VERSION 1 2 3 R ). to File ( parent Directory ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠequalTo ( File ( parent Directory , Ġ" g ads u _ backup - v 1 _ 2 _ 3 - 2 0 0 2 _ 1 2 _ 3 1 _ 2 3 _ 5 9 _ 5 8 . zip "))) ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġfun Ġ` Version , Ġto FileName `() Ġ{ ĊĠĠĠĠĠĠĠ ĠassertThat ( TEST _ VERSION 1 2 3 R . to FileName (), ĠequalTo (" v 1 _ 2 _ 3 ")) ĊĠĠĠĠĠĠĠ ĠassertThat ( TEST _ VERSION 0 0 0 SS . to FileName (), ĠequalTo (" v 0 _ 0 _ 0 _ SS ")) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġ` Version , Ġparse By File `() Ġ{ ĊĠĠĠĠĠĠĠ ĠassertThat ( Version . parse By File (" 1 _ 2 _ 3 "), ĠequalTo ( TEST _ VERSION 1 2 3 R )) ĊĠĠĠĠĠĠĠ ĠassertThat ( Version . parse By File (" 0 _ 0 _ 0 _ SS "), ĠequalTo ( TEST _ VERSION 0 0 0 SS )) ĊĠĠĠ Ġ} Ċ Ċ } Ċ