/* Ċ Ġ* ĠCopyright Ġ 2 0 1 1 - 2 0 1 7 ĠTim ĠBer gl und Ġand ĠSte ven ĠC . ĠSal im an Ċ Ġ* ĠKotlin Ġconversion Ġdone Ġby ĠJ ason ĠBlack well Ċ Ġ* Ċ Ġ* Ġ ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġ Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* Ġ ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* Ġ ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġ Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* Ġ ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* Ġ ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġ Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġorg . li quibase . kotlin Ċ Ċ import Ġli quibase . change . Constraints Config Ċ import Ġli quibase . changelog . Change Log Parameters Ċ import Ġli quibase . changelog . Database Change Log Ċ import Ġorg . junit . After Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ import Ġjava . io . ByteArray OutputStream Ċ import Ġjava . io . Print Stream Ċ import Ġkotlin . test .* Ċ Ċ /** Ċ Ġ* ĠThis Ġclass Ġtests Ġthat Ġa Ġconstraint Ġclosure Ġcan Ġbe Ġparsed Ġcorrectly Ġby Ġthe Ċ Ġ* ĠKotlin ĠDSL . Ġ ĠAs Ġwith Ġthe Ġother Ġtests , Ġwe Ġare Ġnot Ġinterested Ġin Ġwhether Ġor Ċ Ġ* Ġnot Ġwe Ġend Ġup Ġwith Ġa Ġvalid Ġconstraint , Ġjust Ġwhether Ġor Ġnot Ġwe Ġfa ith fully Ċ Ġ* Ġcreated Ġa ĠLi quibase Ġobject Ġfrom Ġthe ĠKotlin Ġclosure Ġwe Ġwere Ġgiven . Ġ ĠWe Ċ Ġ* Ġdefer Ġto ĠLi quibase Ġon Ġmatters Ġof Ġvalidity . Ġ ĠOnce Ġagain , ĠLi quibase Ġhas Ċ Ġ* Ġsome Ġoptions Ġthat Ġare Ġnot Ġdocumented , Ġso Ġwe 'll Ġtest Ġthem . Ċ Ġ* Ċ Ġ* Ġ@ author ĠSte ven ĠC . ĠSal im an Ċ Ġ* Ġ@ author ĠJ ason ĠBlack well Ċ Ġ*/ Ċ class ĠConstraint Tests Ġ{ Ċ ĉ private Ġval Ġold Std Out Ġ= ĠSystem . out Ċ ĉ private Ġval Ġbuf Str Ġ= ĠByteArrayOutputStream () ĊĊ ĉ /** Ċĉ Ġ* ĠSet Ġup Ġfor Ġeach Ġtest Ġby Ġand Ġcapturing Ġstandard Ġout Ġso Ġthat Ġtests Ġcan Ġcheck Ċĉ Ġ* Ġfor Ġthe Ġpresence / abs ence Ġof Ġmessages . Ċĉ Ġ*/ Ċ ĉ @ Before Ċ ĉ fun Ġcapture Std Out () Ġ{ Ċĉ ĉ System . set Out ( Print Stream ( buf Str )) Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠAfter Ġeach Ġtest , Ġmake Ġsure Ġstdout Ġis Ġback Ġto Ġwhat Ġit Ġshould Ġbe , Ġand Ġfor Ċĉ Ġ* Ġgood Ġmeasure , Ġprint Ġout Ġany Ġoutput Ġwe Ġgot . Ċĉ Ġ*/ Ċ ĉ @ After Ċ ĉ fun Ġrestore Std Out () Ġ{ Ċĉ ĉ if Ġ( old Std Out Ġ!= Ġnull ) Ġ{ Ċĉĉ ĉ System . set Out ( old Std Out ) Ċĉ ĉ } Ċĉ ĉ val Ġtest Output Ġ= Ġbuf Str . toString () Ċĉ ĉ if Ġ( test Output . isNotEmpty ()) Ġ{ Ċĉĉ ĉ println (" Test Ġoutput :\ n $ test Output ") Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠTest Ġparsing Ġconstraints Ġwhen Ġwe Ġhave Ġno Ġargs Ġand Ġno Ġclosure . Ġ ĠThis Ċĉ Ġ* Ġvalidates Ġthat Ġwe Ġdon 't Ġset Ġany Ġun int ended Ġdefaults . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġconstraint Without Args Or Closure () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints () Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertNull ( constraint . is Nullable ) Ċĉ ĉ assertNull ( constraint . is PrimaryKey ) Ċĉ ĉ assertNull ( constraint . primary KeyName ) Ċĉ ĉ assertNull ( constraint . primaryKey Table space ) Ċĉ ĉ assertNull ( constraint . foreign KeyName ) Ċĉ ĉ assertNull ( constraint . references ) Ċĉ ĉ assertNull ( constraint . referenced TableName ) Ċĉ ĉ assertNull ( constraint . referenced Column Names ) Ċĉ ĉ assertNull ( constraint . is Unique ) Ċĉ ĉ assertNull ( constraint . unique Constraint Name ) Ċĉ ĉ assertNull ( constraint . check Constraint ) Ċĉ ĉ assertNull ( constraint . is Delete Cascade ) Ċĉ ĉ assertNull ( constraint . is Init ially Deferred ) Ċĉ ĉ assertNull ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠTest Ġparsing Ġconstraints Ġwhen Ġwe Ġset Ġall Ġthe Ġattributes Ġvia Ġthe Ġargument Ċĉ Ġ* Ġmap . Ġ ĠWe Ġhave Ġtoo Ġmany Ġb ooleans Ġto Ġisolate Ġthem , Ġso Ġwe 'll Ġneed Ġsome Ġmore Ċĉ Ġ* Ġtests Ġthere ... Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġfull Arguments No Closure () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( Ċĉĉĉĉ ĉ nullable Ġ= Ġtrue , Ċĉĉĉĉ ĉ primaryKey Ġ= Ġtrue , Ċĉĉĉĉ ĉ primary KeyName Ġ= Ġ" my PrimaryKey ", Ċĉĉĉĉ ĉ primaryKey Table space Ġ= Ġ" my Primary Table space ", Ċĉĉĉĉ ĉ foreign KeyName Ġ= Ġ" fk _ monkey ", Ċĉĉĉĉ ĉ references Ġ= Ġ" monkey ( id )", Ċĉĉĉĉ ĉ referenced TableName Ġ= Ġ" monkey ", Ċĉĉĉĉ ĉ referenced Column Names Ġ= Ġ" id ", Ċĉĉĉĉ ĉ unique Ġ= Ġtrue , Ċĉĉĉĉ ĉ unique Constraint Name Ġ= Ġ" my Unique Key ", Ċĉĉĉĉ ĉ check Constraint Ġ= Ġ" my Check Constraint ", Ċĉĉĉĉ ĉ delete Cascade Ġ= Ġtrue , Ċĉĉĉĉ ĉ init ially Deferred Ġ= Ġtrue , Ċĉĉĉĉ ĉ def err able Ġ= Ġtrue Ċĉĉ ĉ ) Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertTrue ( constraint . is Nullable ) Ċĉ ĉ assertTrue ( constraint . is PrimaryKey ) Ċĉ ĉ assertEquals (" my PrimaryKey ", Ġconstraint . primary KeyName ) Ċĉ ĉ assertEquals (" my Primary Table space ", Ġconstraint . primaryKey Table space ) Ċĉ ĉ assertEquals (" fk _ monkey ", Ġconstraint . foreign KeyName ) Ċĉ ĉ assertEquals (" monkey ( id )", Ġconstraint . references ) Ċĉ ĉ assertEquals (" monkey ", Ġconstraint . referenced TableName ) Ċĉ ĉ assertEquals (" id ", Ġconstraint . referenced Column Names ) Ċĉ ĉ assertTrue ( constraint . is Unique ) Ċĉ ĉ assertEquals (" my Unique Key ", Ġconstraint . unique Constraint Name ) Ċĉ ĉ assertEquals (" my Check Constraint ", Ġconstraint . check Constraint ) Ċĉ ĉ assertTrue ( constraint . is Delete Cascade ) Ċĉ ĉ assertTrue ( constraint . is Init ially Deferred ) Ċĉ ĉ assertTrue ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠSet Ġall Ġthe Ġboolean Ġattributes Ġvia Ġarguments , Ġbut Ġonly Ġ" nullable " Ġis Ġtrue . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġonly Nullable Arg IsTrue () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( Ċĉĉĉĉ ĉ nullable Ġ= Ġtrue , Ċĉĉĉĉ ĉ primaryKey Ġ= Ġfalse , Ċĉĉĉĉ ĉ unique Ġ= Ġfalse , Ċĉĉĉĉ ĉ delete Cascade Ġ= Ġfalse , Ċĉĉĉĉ ĉ init ially Deferred Ġ= Ġfalse , Ċĉĉĉĉ ĉ def err able Ġ= Ġfalse Ċĉĉ ĉ ) Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertTrue ( constraint . is Nullable ) Ċĉ ĉ assertFalse ( constraint . is PrimaryKey ) Ċĉ ĉ assertFalse ( constraint . is Unique ) Ċĉ ĉ assertFalse ( constraint . is Delete Cascade ) Ċĉ ĉ assertFalse ( constraint . is Init ially Deferred ) Ċĉ ĉ assertFalse ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠSet Ġall Ġthe Ġboolean Ġattributes Ġvia Ġarguments , Ġbut Ġonly Ġ" primaryKey " Ġis Ġtrue . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġonly PrimaryKey Arg IsTrue () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( Ċĉĉĉĉ ĉ nullable Ġ= Ġfalse , Ċĉĉĉĉ ĉ primaryKey Ġ= Ġtrue , Ċĉĉĉĉ ĉ unique Ġ= Ġfalse , Ċĉĉĉĉ ĉ delete Cascade Ġ= Ġfalse , Ċĉĉĉĉ ĉ init ially Deferred Ġ= Ġfalse , Ċĉĉĉĉ ĉ def err able Ġ= Ġfalse Ċĉĉ ĉ ) Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertFalse ( constraint . is Nullable ) Ċĉ ĉ assertTrue ( constraint . is PrimaryKey ) Ċĉ ĉ assertFalse ( constraint . is Unique ) Ċĉ ĉ assertFalse ( constraint . is Delete Cascade ) Ċĉ ĉ assertFalse ( constraint . is Init ially Deferred ) Ċĉ ĉ assertFalse ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠSet Ġall Ġthe Ġboolean Ġattributes Ġvia Ġarguments , Ġbut Ġonly Ġ" unique " Ġis Ġtrue . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġonly Unique Arg IsTrue () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( Ċĉĉĉĉ ĉ nullable Ġ= Ġfalse , Ċĉĉĉĉ ĉ primaryKey Ġ= Ġfalse , Ċĉĉĉĉ ĉ unique Ġ= Ġtrue , Ċĉĉĉĉ ĉ delete Cascade Ġ= Ġfalse , Ċĉĉĉĉ ĉ init ially Deferred Ġ= Ġfalse , Ċĉĉĉĉ ĉ def err able Ġ= Ġfalse Ċĉĉ ĉ ) Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertFalse ( constraint . is Nullable ) Ċĉ ĉ assertFalse ( constraint . is PrimaryKey ) Ċĉ ĉ assertTrue ( constraint . is Unique ) Ċĉ ĉ assertFalse ( constraint . is Delete Cascade ) Ċĉ ĉ assertFalse ( constraint . is Init ially Deferred ) Ċĉ ĉ assertFalse ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠSet Ġall Ġthe Ġboolean Ġattributes Ġvia Ġarguments , Ġbut Ġonly Ġ" delete Cascade " Ġis Ġtrue . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġonly Delete Cascade Arg IsTrue () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( Ċĉĉĉĉ ĉ nullable Ġ= Ġfalse , Ċĉĉĉĉ ĉ primaryKey Ġ= Ġfalse , Ċĉĉĉĉ ĉ unique Ġ= Ġfalse , Ċĉĉĉĉ ĉ delete Cascade Ġ= Ġtrue , Ċĉĉĉĉ ĉ init ially Deferred Ġ= Ġfalse , Ċĉĉĉĉ ĉ def err able Ġ= Ġfalse Ċĉĉ ĉ ) Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertFalse ( constraint . is Nullable ) Ċĉ ĉ assertFalse ( constraint . is PrimaryKey ) Ċĉ ĉ assertFalse ( constraint . is Unique ) Ċĉ ĉ assertTrue ( constraint . is Delete Cascade ) Ċĉ ĉ assertFalse ( constraint . is Init ially Deferred ) Ċĉ ĉ assertFalse ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠSet Ġall Ġthe Ġboolean Ġattributes Ġvia Ġarguments , Ġbut Ġonly Ġ" init ially Deferred " Ċĉ Ġ* Ġis Ġtrue . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġonly Init ially Deferred Arg IsTrue () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( Ċĉĉĉĉ ĉ nullable Ġ= Ġfalse , Ċĉĉĉĉ ĉ primaryKey Ġ= Ġfalse , Ċĉĉĉĉ ĉ unique Ġ= Ġfalse , Ċĉĉĉĉ ĉ delete Cascade Ġ= Ġfalse , Ċĉĉĉĉ ĉ init ially Deferred Ġ= Ġtrue , Ċĉĉĉĉ ĉ def err able Ġ= Ġfalse Ċĉĉ ĉ ) Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertFalse ( constraint . is Nullable ) Ċĉ ĉ assertFalse ( constraint . is PrimaryKey ) Ċĉ ĉ assertFalse ( constraint . is Unique ) Ċĉ ĉ assertFalse ( constraint . is Delete Cascade ) Ċĉ ĉ assertTrue ( constraint . is Init ially Deferred ) Ċĉ ĉ assertFalse ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠSet Ġall Ġthe Ġboolean Ġattributes Ġvia Ġarguments , Ġbut Ġonly Ġ" def err able " Ġis Ġtrue . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġonly Def err able Arg IsTrue () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( Ċĉĉĉĉ ĉ nullable Ġ= Ġfalse , Ċĉĉĉĉ ĉ primaryKey Ġ= Ġfalse , Ċĉĉĉĉ ĉ unique Ġ= Ġfalse , Ċĉĉĉĉ ĉ delete Cascade Ġ= Ġfalse , Ċĉĉĉĉ ĉ init ially Deferred Ġ= Ġfalse , Ċĉĉĉĉ ĉ def err able Ġ= Ġtrue Ċĉĉ ĉ ) Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertFalse ( constraint . is Nullable ) Ċĉ ĉ assertFalse ( constraint . is PrimaryKey ) Ċĉ ĉ assertFalse ( constraint . is Unique ) Ċĉ ĉ assertFalse ( constraint . is Delete Cascade ) Ċĉ ĉ assertFalse ( constraint . is Init ially Deferred ) Ċĉ ĉ assertTrue ( constraint . is Def err able ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠConstraints Ġhave Ġan Ġinteresting Ġwr ink le . Ġ ĠWhere Ġmultiple Ġcalls Ġto Ġ" column " Ċĉ Ġ* Ġresults Ġin Ġmultiple Ġcolumns Ġbeing Ġcreated , Ġmultiple Ġcalls Ġto Ġ" constraint " Ċĉ Ġ* Ġresults Ġin Ġjust Ġone , Ġcombined Ġconstraint . Ġ ĠThis Ġis Ġuseful Ġif Ġa Ġcolumn Ċĉ Ġ* Ġhas Ġboth Ġa Ġunique Ġconstraint Ġand Ġa Ġforeign Ġkey Ġconstraint . Ġ ĠTest Ġthis . Ċĉ Ġ*/ Ċ ĉ @ Test Ċ ĉ fun Ġconstraints From Map With Multiple Calls () Ġ{ Ċĉ ĉ val Ġconstraint Ġ= Ġbuild Constraint Ġ{ Ċĉĉ ĉ constraints ( foreign KeyName Ġ= Ġ" fk _ monkey ", Ġreferences Ġ= Ġ" monkey ( id )") Ċĉĉ ĉ constraints ( unique Ġ= Ġtrue , Ġunique Constraint Name Ġ= Ġ" uk _ monkey ") Ċĉ ĉ } ĊĊĉ ĉ assertNotNull ( constraint ) Ċĉ ĉ assertEquals (" fk _ monkey ", Ġconstraint . foreign KeyName ) Ċĉ ĉ assertEquals (" monkey ( id )", Ġconstraint . references ) Ċĉ ĉ assertTrue ( constraint . is Unique ) Ċĉ ĉ assertEquals (" uk _ monkey ", Ġconstraint . unique Constraint Name ) ĊĊĉ ĉ assert No Output () Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠMake Ġsure Ġthe Ġtest Ġdid Ġnot Ġhave Ġany Ġoutput Ġto Ġstandard Ġout . Ġ ĠThis Ġcan Ġbe Ċĉ Ġ* Ġused Ġto Ġmake Ġsure Ġthere Ġare Ġno Ġdeprecation Ġwarnings . Ċĉ Ġ*/ Ċ ĉ private Ġfun Ġassert No Output () Ġ{ Ċĉ ĉ val Ġtest Output Ġ= Ġbuf Str . toString () Ċĉ ĉ assertTrue ( test Output . isEmpty (), Ġ" Did Ġnot Ġexpect Ġto Ġhave Ġoutput , Ġbut Ġgot :\ n $ test Output ") Ċ ĉ } ĊĊ ĉ /** Ċĉ Ġ* ĠHelper Ġmethod Ġto Ġexecute Ġa Ġconstraint Ġclosure Ġand Ġreturn Ġthe Ġconstraint Ċĉ Ġ* Ġcreated Ġfrom Ġit . Ċĉ Ġ* Ġ@ param Ġclosure Ġthe Ġclosure Ġto Ġexecute Ċĉ Ġ* Ġ@ return Ġthe Ġclosure Ġobject Ġbuilt . Ċĉ Ġ*/ Ċ ĉ private Ġfun Ġbuild Constraint ( closure : Ġ( Kotlin Column Constraint ). () Ġ-> ĠUnit ) Ġ: ĠConstraints Config Ġ{ Ċĉ ĉ val Ġdatabase Change Log Ġ= ĠDatabase Change Log (). apply Ġ{ Ċĉĉ ĉ change Log Parameters Ġ= ĠChange Log Parameters () Ċĉ ĉ } ĊĊĉ ĉ val Ġconstraint Ġ= ĠKotlin Column Constraint ( database Change Log ) Ċĉ ĉ constraint . closure () Ċĉ ĉ return Ġconstraint . constraint Ċ ĉ } Ċ Ċ } Ċ