/* Ċ Ġ* ĠCopyright Ġ 2 0 0 0 - 2 0 1 5 ĠJetBrains Ġs . r . o . Ċ Ġ* Ċ Ġ* Ġ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 Ġcom . intellij . configuration Store Ċ Ċ import Ġcom . intellij . ide . highlighter . Project FileType Ċ import Ġcom . intellij . openapi . application . run Write Action Ċ import Ġcom . intellij . openapi . components . Persistent State Component Ċ import Ġcom . intellij . openapi . components . State Ċ import Ġcom . intellij . openapi . components . impl . stores . I Project Store Ċ import Ġcom . intellij . openapi . components . state Store Ċ import Ġcom . intellij . openapi . project . Project Ċ import Ġcom . intellij . openapi . project . Project Manager Ċ import Ġcom . intellij . openapi . project . ex . Project Ex Ċ import Ġcom . intellij . openapi . project . ex . Project Manager Ex Ċ import Ġcom . intellij . openapi . project . impl . Project Impl Ċ import Ġcom . intellij . openapi . vfs . Virtual File Ċ import Ġcom . intellij . test Framework .* Ċ import Ġcom . intellij . test Framework . Assertions . assertThat Ċ import Ġcom . intellij . util . Path Util Ċ import Ġcom . intellij . util . read Text Ċ import Ġcom . intellij . util . system In dependent Path Ċ import Ġcom . intellij . util . write Ċ import Ġorg . intellij . lang . annotations . Language Ċ import Ġorg . junit . Class Rule Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġjava . nio . file . Paths Ċ Ċ fun Ġcreate Project And Use In Load Component State Mode ( temp Dir Manager : ĠTemporary Directory , Ġdirectory Based : ĠBoolean Ġ= Ġfalse , Ġtask : Ġ( Project ) Ġ-> ĠUnit ) Ġ{ ĊĠ Ġcreate Or Load Project ( temp Dir Manager , Ġtask , Ġdirectory Based Ġ= Ġdirectory Based ) Ċ } Ċ Ċ fun Ġload And Use Project ( temp Dir Manager : ĠTemporary Directory , Ġproject Creator : Ġ(( Virtual File ) Ġ-> ĠString ), Ġtask : Ġ( Project ) Ġ-> ĠUnit ) Ġ{ ĊĠ Ġcreate Or Load Project ( temp Dir Manager , Ġtask , Ġproject Creator , Ġfalse ) Ċ } Ċ Ċ private Ġfun Ġcreate Or Load Project ( temp Dir Manager : ĠTemporary Directory , Ġtask : Ġ( Project ) Ġ-> ĠUnit , Ġproject Creator : Ġ(( Virtual File ) Ġ-> ĠString )? Ġ= Ġnull , Ġdirectory Based : ĠBoolean ) Ġ{ ĊĠ Ġrun In Ed t And Wait Ġ{ ĊĠĠĠ Ġval ĠfilePath : ĠString ĊĠĠĠ Ġif Ġ( project Creator Ġ== Ġnull ) Ġ{ ĊĠĠĠĠĠ ĠfilePath Ġ= Ġtemp Dir Manager . new Path (" test ${ if Ġ( directory Based ) Ġ"" Ġelse ĠProject FileType . DOT _ DEFAULT _ EXTENSION } "). system In dependent Path ĊĠĠĠ Ġ} ĊĠĠĠ Ġelse Ġ{ ĊĠĠĠĠĠ ĠfilePath Ġ= Ġrun Write Action Ġ{ Ġproject Creator ( temp Dir Manager . new Virtual Directory ()) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġproject Ġ= Ġif Ġ( project Creator Ġ== Ġnull ) Ġcreate He avy Project ( filePath , Ġtrue ) Ġelse ĠProject Manager Ex . getInstance Ex (). load Project ( filePath ) !! ĊĠĠĠ Ġproject . run In Load Component State Mode Ġ{ ĊĠĠĠĠĠ Ġproject . use ( task ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ Ċ internal Ġclass ĠProject Store Test Ġ{ ĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġ@ Class Rule ĊĠĠĠ Ġval Ġproject Rule Ġ= ĠProject Rule () ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġtemp Dir Manager Ġ= ĠTemporary Directory () ĊĊĠ Ġ@ Rule ĊĠ Ġ@ Jvm Field ĊĠ Ġval Ġrule Chain Ġ= ĠRule Chain ( temp Dir Manager ) ĊĊĠ Ġ@ Language (" XML ") ĊĠ Ġprivate Ġval Ġi pr FileContent Ġ= ĊĠĠĠ Ġ"<? xml Ġversion =\" 1 . 0 \" Ġencoding =\" UTF - 8 \" ?> \ n < project Ġversion =\" 4 \">\ n Ġ Ġ< component Ġname =\" A AT est Component \">\ n ĠĠĠ Ġ< option Ġname =\" value \" Ġvalue =\" custom Value \" Ġ/>\ n Ġ Ġ</ component >\ n </ project >" ĊĊĠ Ġ@ State ( name Ġ= Ġ" A AT est Component ") ĊĠ Ġprivate Ġclass ĠTest Component Ġ: ĠPersistent State Component < Test State > Ġ{ ĊĠĠĠ Ġprivate Ġvar Ġstate : ĠTest State ? Ġ= Ġnull ĊĊĠĠĠ Ġoverride Ġfun ĠgetState () Ġ= Ġstate ĊĊĠĠĠ Ġoverride Ġfun Ġload State ( state : ĠTest State ) Ġ{ ĊĠĠĠĠĠ Ġthis . state Ġ= Ġstate ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġdata Ġclass ĠTest State ( var Ġvalue : ĠString Ġ= Ġ" default ") ĊĊĠ Ġ@ Test Ġfun Ġdirectory Based Storage () Ġ{ ĊĠĠĠ Ġload And Use Project ( temp Dir Manager , Ġ{ ĊĠĠĠĠĠ Ġit . write Child ("${ Project . DIRECTORY _ STORE _ FOLDER }/ misc . xml ", Ġi pr FileContent ) ĊĠĠĠĠĠ Ġit . path ĊĠĠĠ Ġ}) Ġ{ Ġproject Ġ-> ĊĠĠĠĠĠ Ġval Ġtest Component Ġ= Ġtest ( project Ġas ĠProject Ex ) ĊĊĠĠĠĠĠ ĠassertThat ( project . basePath ). isEqualTo ( Path Util . getParent Path (( Path Util . getParent Path ( project . project FilePath !! )))) ĊĊĠĠĠĠĠ Ġ// Ġtest Ġreload Ġon Ġexternal Ġchange ĊĠĠĠĠĠ Ġval Ġfile Ġ= ĠPaths . get ( project . state Store . state Storage Manager . expand Macros ( PROJECT _ FILE )) ĊĠĠĠĠĠ Ġfile . write ( file . read Text (). replace (""" < option Ġname =" value " Ġvalue =" foo " Ġ/> """, Ġ""" < option Ġname =" value " Ġvalue =" newValue " Ġ/> "" ")) ĊĊĠĠĠĠĠ Ġproject . base Dir . refresh ( false , Ġtrue ) ĊĠĠĠĠĠ Ġ( Project Manager . getInstance () Ġas ĠStore Aware Project Manager ). flush Changed Alarm () ĊĊĠĠĠĠĠ ĠassertThat ( test Component . state ). isEqualTo ( Test State (" newValue ")) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġfile Based Storage () Ġ{ ĊĠĠĠ Ġload And Use Project ( temp Dir Manager , Ġ{ Ġit . write Child (" test ${ Project FileType . DOT _ DEFAULT _ EXTENSION }", Ġi pr FileContent ). path Ġ}) Ġ{ Ġproject Ġ-> ĊĠĠĠĠĠ Ġtest ( project ) ĊĊĠĠĠĠĠ ĠassertThat ( project . basePath ). isEqualTo ( Path Util . getParent Path ( project . project FilePath !! )) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġsave ProjectName () Ġ{ ĊĠĠĠ Ġload And Use Project ( temp Dir Manager , Ġ{ ĊĠĠĠĠĠ Ġit . write Child ("${ Project . DIRECTORY _ STORE _ FOLDER }/ misc . xml ", Ġi pr FileContent ) ĊĠĠĠĠĠ Ġit . path ĊĠĠĠ Ġ}) Ġ{ Ġproject Ġ-> ĊĠĠĠĠĠ Ġval Ġstore Ġ= Ġproject . state Store Ġas ĠI Project Store ĊĠĠĠĠĠ ĠassertThat ( store . name File ). does NotExist () ĊĠĠĠĠĠ Ġval ĠnewName Ġ= Ġ" Foo " ĊĠĠĠĠĠ Ġval Ġold Name Ġ= Ġproject . name ĊĠĠĠĠĠ Ġ( project Ġas ĠProject Impl ). set ProjectName ( new Name ) ĊĠĠĠĠĠ Ġproject . save Store () ĊĠĠĠĠĠ ĠassertThat ( store . name File ). has Content ( new Name ) ĊĊĠĠĠĠĠ Ġproject . set ProjectName ( old Name ) ĊĠĠĠĠĠ Ġproject . save Store () ĊĠĠĠĠĠ ĠassertThat ( store . name File ). does NotExist () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġ` saved Ġproject Ġname Ġmust Ġbe Ġnot Ġremoved Ġjust Ġon Ġopen `() Ġ{ ĊĠĠĠ Ġval Ġname Ġ= Ġ" saved Ġproject Ġname Ġmust Ġbe Ġnot Ġremoved Ġjust Ġon Ġopen " ĊĠĠĠ Ġload And Use Project ( temp Dir Manager , Ġ{ ĊĠĠĠĠĠ Ġit . write Child ("${ Project . DIRECTORY _ STORE _ FOLDER }/ misc . xml ", Ġi pr FileContent ) ĊĠĠĠĠĠ Ġit . write Child ("${ Project . DIRECTORY _ STORE _ FOLDER }/. name ", Ġname ) ĊĠĠĠĠĠ Ġit . path ĊĠĠĠ Ġ}) Ġ{ Ġproject Ġ-> ĊĠĠĠĠĠ Ġval Ġstore Ġ= Ġproject . state Store Ġas ĠI Project Store ĊĠĠĠĠĠ ĠassertThat ( store . name File ). has Content ( name ) ĊĊĠĠĠĠĠ Ġproject . save Store () ĊĠĠĠĠĠ ĠassertThat ( store . name File ). has Content ( name ) ĊĊĠĠĠĠĠ Ġ( project Ġas ĠProject Impl ). set ProjectName ( name ) ĊĠĠĠĠĠ Ġproject . save Store () ĊĠĠĠĠĠ ĠassertThat ( store . name File ). has Content ( name ) ĊĊĠĠĠĠĠ Ġproject . set ProjectName (" foo ") ĊĠĠĠĠĠ Ġproject . save Store () ĊĠĠĠĠĠ ĠassertThat ( store . name File ). has Content (" foo ") ĊĊĠĠĠĠĠ Ġproject . set ProjectName ( name ) ĊĠĠĠĠĠ Ġproject . save Store () ĊĠĠĠĠĠ ĠassertThat ( store . name File ). does NotExist () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġtest ( project : ĠProject ): ĠTest Component Ġ{ ĊĠĠĠ Ġval Ġtest Component Ġ= ĠTest Component () ĊĠĠĠ Ġproject . state Store . init Component ( test Component , Ġtrue ) ĊĠĠĠ ĠassertThat ( test Component . state ). isEqualTo ( Test State (" custom Value ")) ĊĊĠĠĠ Ġtest Component . state !!. value Ġ= Ġ" foo " ĊĠĠĠ Ġproject . save Store () ĊĊĠĠĠ Ġval Ġfile Ġ= ĠPaths . get ( project . state Store . state Storage Manager . expand Macros ( PROJECT _ FILE )) ĊĠĠĠ ĠassertThat ( file ). is Regular File () ĊĠĠĠ Ġ// Ġtest Ġexact Ġstring Ġ- Ġxml Ġpro log , Ġline Ġsepar ators , Ġindentation Ġand Ġso Ġon Ġmust Ġbe Ġexactly Ġthe Ġsame ĊĠĠĠ Ġ// Ġtodo Ġget Ġrid Ġof Ġdefault Ġcomponent Ġstates Ġhere ĊĠĠĠ ĠassertThat ( file . read Text ()). startsWith ( ipr FileContent . replace (" custom Value ", Ġ" foo "). replace ("</ project >", Ġ"")) ĊĊĠĠĠ Ġreturn Ġtest Component ĊĠ Ġ} Ċ }