ScopedInstanceCompleter implements Completer { @Override public int complete( Session session, CommandLine commandLine, List<String> candidates ) { String applicationName = null; int position = commandLine.getCursorArgumentIndex(); if( position > 0 ) applicationName = commandLine.getArguments()[ position - 1 ]; StringsCompleter delegate = new StringsCompleter( false ); ManagedApplication ma; if( ! Utils.isEmptyOrWhitespaces( applicationName ) && ( ma = this.manager.applicationMngr().findManagedApplicationByName( applicationName )) != null ) { for( Instance inst : InstanceHelpers.findAllScopedInstances( ma.getApplication())) delegate.getStrings().add( InstanceHelpers.computeInstancePath( inst )); } return delegate.complete( session, commandLine, candidates ); }  @Override int complete( Session session, CommandLine commandLine, List<String> candidates );  }
@Test public void testComplete() throws Exception { TestApplication app = new TestApplication(); app.directory( this.folder.newFolder()); ManagedApplication ma = new ManagedApplication( app ); Session session = Mockito.mock( Session.class ); CommandLine commandLine = Mockito.mock( CommandLine.class ); Mockito.when( commandLine.getCursorArgument()).thenReturn( "" ); Mockito.when( commandLine.getCursorArgumentIndex()).thenReturn( 0 ); Mockito.when( commandLine.getArgumentPosition()).thenReturn( 0 ); Mockito.when( commandLine.getArguments()).thenReturn( new String[ 0 ]); IApplicationMngr applicationMngr = Mockito.mock( IApplicationMngr.class ); Mockito.when( applicationMngr.findManagedApplicationByName( app.getName())).thenReturn( ma ); Manager manager = Mockito.mock( Manager.class ); Mockito.when( manager.applicationMngr()).thenReturn( applicationMngr ); ScopedInstanceCompleter completer = new ScopedInstanceCompleter(); completer.manager = manager; List<String> candidates = new ArrayList<> (); Assert.assertEquals( 0, candidates.size()); Assert.assertEquals( -1, completer.complete( session, commandLine, candidates )); Assert.assertEquals( 0, candidates.size()); candidates = new ArrayList<> (); Mockito.when( commandLine.getCursorArgument()).thenReturn( "/my" ); Mockito.when( commandLine.getArgumentPosition()).thenReturn( 3 ); Assert.assertEquals( 0, candidates.size()); Assert.assertEquals( -1, completer.complete( session, commandLine, candidates )); Assert.assertEquals( 0, candidates.size()); Mockito.when( commandLine.getArguments()).thenReturn( new String[] { "inalid" }); Mockito.when( commandLine.getCursorArgumentIndex()).thenReturn( 1 ); Assert.assertEquals( 0, candidates.size()); Assert.assertEquals( -1, completer.complete( session, commandLine, candidates )); Assert.assertEquals( 0, candidates.size()); Mockito.when( commandLine.getArguments()).thenReturn( new String[] { app.getName()}); Mockito.when( commandLine.getCursorArgumentIndex()).thenReturn( 1 ); Assert.assertEquals( -3, completer.complete( session, commandLine, candidates )); Assert.assertEquals( 1, candidates.size()); Assert.assertEquals( "/mysql-vm", candidates.get( 0 ).trim()); candidates = new ArrayList<> (); Mockito.when( commandLine.getCursorArgument()).thenReturn( "/MY" ); Mockito.when( commandLine.getArgumentPosition()).thenReturn( 3 ); Assert.assertEquals( 0, candidates.size()); Assert.assertEquals( -3, completer.complete( session, commandLine, candidates )); Assert.assertEquals( 1, candidates.size()); Assert.assertEquals( "/mysql-vm", candidates.get( 0 ).trim()); candidates = new ArrayList<> (); Mockito.when( commandLine.getCursorArgument()).thenReturn( "unknown" ); Assert.assertEquals( 0, candidates.size()); Assert.assertEquals( -1, completer.complete( session, commandLine, candidates )); }