1717import static org .hamcrest .CoreMatchers .containsString ;
1818import static org .hamcrest .CoreMatchers .is ;
1919import static org .hamcrest .MatcherAssert .assertThat ;
20+ import static org .junit .Assert .fail ;
2021import static org .junit .Assume .assumeTrue ;
2122import static org .neo4j .driver .internal .messaging .request .MultiDatabaseUtil .ABSENT_DB_NAME ;
2223import static org .neo4j .shell .DatabaseManager .DEFAULT_DEFAULT_DB_NAME ;
@@ -35,94 +36,141 @@ public class CypherShellMultiDatabaseIntegrationTest
3536 private CypherShell shell ;
3637
3738 @ Before
38- public void setUp () throws Exception {
39+ public void setUp () throws Exception
40+ {
3941 linePrinter .clear ();
40- shell = new CypherShell (linePrinter , new PrettyConfig (Format .PLAIN , true , 1000 ) );
41- useCommand = new Use (shell );
42- beginCommand = new Begin (shell );
43- rollbackCommand = new Rollback (shell );
42+ shell = new CypherShell ( linePrinter , new PrettyConfig ( Format .PLAIN , true , 1000 ), false );
43+ useCommand = new Use ( shell );
44+ beginCommand = new Begin ( shell );
45+ rollbackCommand = new Rollback ( shell );
4446
45- shell .connect (new ConnectionConfig ("bolt://" , "localhost" , 7687 , "neo4j" , "neo" , true , ABSENT_DB_NAME ) );
47+ shell .connect ( new ConnectionConfig ( "bolt://" , "localhost" , 7687 , "neo4j" , "neo" , true , ABSENT_DB_NAME ) );
4648
4749 // Multiple databases are only available from 4.0
4850 assumeTrue ( majorVersion ( shell .getServerVersion () ) >= 4 );
4951 }
5052
5153 @ After
52- public void tearDown () throws Exception {
54+ public void tearDown () throws Exception
55+ {
5356 }
5457
5558 @ Test
56- public void switchingToSystemDatabaseWorks () throws CommandException {
57- useCommand .execute (SYSTEM_DB_NAME );
59+ public void switchingToSystemDatabaseWorks () throws CommandException
60+ {
61+ useCommand .execute ( SYSTEM_DB_NAME );
5862
59- assertThat (linePrinter .output (), is ("" ) );
63+ assertThat ( linePrinter .output (), is ( "" ) );
6064 assertOnSystemDB ();
6165 }
6266
6367 @ Test
64- public void switchingToSystemDatabaseIsNotCaseSensitive () throws CommandException {
65- useCommand .execute ("SyStEm" );
68+ public void switchingToSystemDatabaseIsNotCaseSensitive () throws CommandException
69+ {
70+ useCommand .execute ( "SyStEm" );
6671
67- assertThat (linePrinter .output (), is ("" ) );
72+ assertThat ( linePrinter .output (), is ( "" ) );
6873 assertOnSystemDB ();
6974 }
7075
7176 @ Test
72- public void switchingToSystemDatabaseAndBackToNeo4jWorks () throws CommandException {
73- useCommand .execute (SYSTEM_DB_NAME );
74- useCommand .execute (DEFAULT_DEFAULT_DB_NAME );
77+ public void switchingToSystemDatabaseAndBackToNeo4jWorks () throws CommandException
78+ {
79+ useCommand .execute ( SYSTEM_DB_NAME );
80+ useCommand .execute ( DEFAULT_DEFAULT_DB_NAME );
7581
76- assertThat (linePrinter .output (), is ("" ) );
82+ assertThat ( linePrinter .output (), is ( "" ) );
7783 assertOnRegularDB ();
7884 }
7985
8086 @ Test
81- public void switchingToSystemDatabaseAndBackToDefaultWorks () throws CommandException {
82- useCommand .execute (SYSTEM_DB_NAME );
83- useCommand .execute (ABSENT_DB_NAME );
87+ public void switchingToSystemDatabaseAndBackToDefaultWorks () throws CommandException
88+ {
89+ useCommand .execute ( SYSTEM_DB_NAME );
90+ useCommand .execute ( ABSENT_DB_NAME );
8491
85- assertThat (linePrinter .output (), is ("" ) );
92+ assertThat ( linePrinter .output (), is ( "" ) );
8693 assertOnRegularDB ();
8794 }
8895
8996 @ Test
90- public void switchingDatabaseInOpenTransactionShouldFail () throws CommandException {
91- thrown .expect (CommandException .class );
92- thrown .expectMessage ("There is an open transaction." );
97+ public void switchingDatabaseInOpenTransactionShouldFail () throws CommandException
98+ {
99+ thrown .expect ( CommandException .class );
100+ thrown .expectMessage ( "There is an open transaction." );
93101
94- beginCommand .execute ("" );
95- useCommand .execute ("another_database" );
102+ beginCommand .execute ( "" );
103+ useCommand .execute ( "another_database" );
96104 }
97105
98106 @ Test
99- public void switchingDatabaseAfterRollbackTransactionWorks () throws CommandException {
100- beginCommand .execute ("" );
101- rollbackCommand .execute ("" );
102- useCommand .execute (SYSTEM_DB_NAME );
107+ public void switchingDatabaseAfterRollbackTransactionWorks () throws CommandException
108+ {
109+ beginCommand .execute ( "" );
110+ rollbackCommand .execute ( "" );
111+ useCommand .execute ( SYSTEM_DB_NAME );
103112
104- assertThat (linePrinter .output (), is ("" ) );
113+ assertThat ( linePrinter .output (), is ( "" ) );
105114 assertOnSystemDB ();
106115 }
107116
108117 @ Test
109- public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServer () throws CommandException {
110- thrown .expect (ClientException .class );
111- thrown .expectMessage ("Database does not exist" );
118+ public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServer () throws CommandException
119+ {
120+ useCommand .execute ( SYSTEM_DB_NAME );
121+
122+ try
123+ {
124+ useCommand .execute ( "this_database_name_does_not_exist_in_test_container" );
125+ fail ( "No ClientException thrown" );
126+ }
127+ catch ( ClientException e )
128+ {
129+ // In non-interactive we want to switch even if the database does not exist (in case we don't have fail-fast)
130+ assertOnNoValidDB ();
131+ }
132+ }
112133
113- useCommand .execute ("this_database_name_does_not_exist_in_test_container" );
134+ @ Test
135+ public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServerInteractive () throws CommandException
136+ {
137+ shell = new CypherShell ( linePrinter , new PrettyConfig ( Format .PLAIN , true , 1000 ), true );
138+ useCommand = new Use ( shell );
139+ shell .connect ( new ConnectionConfig ( "bolt://" , "localhost" , 7687 , "neo4j" , "neo" , true , ABSENT_DB_NAME ) );
140+
141+ useCommand .execute ( SYSTEM_DB_NAME );
142+
143+ try
144+ {
145+ useCommand .execute ( "this_database_name_does_not_exist_in_test_container" );
146+ fail ( "No ClientException thrown" );
147+ }
148+ catch ( ClientException e )
149+ {
150+ // In interactive we do not want to switch if the database does not exist
151+ assertOnSystemDB ();
152+ }
114153 }
115154
116155 // HELPERS
117156
118- private void assertOnRegularDB () throws CommandException {
119- shell .execute ("RETURN 'toadstool'" );
120- assertThat (linePrinter .output (), containsString ("toadstool" ));
157+ private void assertOnRegularDB () throws CommandException
158+ {
159+ shell .execute ( "RETURN 'toadstool'" );
160+ assertThat ( linePrinter .output (), containsString ( "toadstool" ) );
161+ }
162+
163+ private void assertOnSystemDB () throws CommandException
164+ {
165+ shell .execute ( "SHOW DATABASES" );
166+ assertThat ( linePrinter .output (), containsString ( "neo4j" ) );
167+ assertThat ( linePrinter .output (), containsString ( "system" ) );
121168 }
122169
123- private void assertOnSystemDB () throws CommandException {
124- shell .execute ("SHOW DATABASES" );
125- assertThat (linePrinter .output (), containsString ("neo4j" ));
126- assertThat (linePrinter .output (), containsString ("system" ));
170+ private void assertOnNoValidDB () throws CommandException
171+ {
172+ thrown .expect ( ClientException .class );
173+ thrown .expectMessage ( "Database does not exist" );
174+ shell .execute ( "RETURN 1" );
127175 }
128176}
0 commit comments