InstanceHelpers { public static Instance findInstanceByPath( AbstractApplication application, String instancePath ) { Collection<Instance> currentList = new ArrayList<> (); if( application != null ) currentList.addAll( application.getRootInstances()); List<String> instanceNames = new ArrayList<> (); if( instancePath != null ) instanceNames.addAll( Arrays.asList( instancePath.split( "/" ))); if( instanceNames.size() > 0 && Utils.isEmptyOrWhitespaces( instanceNames.get( 0 ))) instanceNames.remove( 0 ); Instance result = null; for( String instanceName : instanceNames ) { result = null; for( Instance instance : currentList ) { if( instanceName.equals( instance.getName())) { result = instance; break; } } if( result == null ) break; currentList = result.getChildren(); } return result; } private  InstanceHelpers(); static String computeInstancePath( Instance inst ); static boolean haveSamePath( Instance i1, Instance i2 ); static String findInstanceName( String instancePath ); static List<Instance> buildHierarchicalList( Instance inst ); static void insertChild( Instance parent, Instance child ); static Map<String,String> findAllExportedVariables( Instance instance ); static Instance findInstanceByPath( AbstractApplication application, String instancePath ); static Instance findInstanceByPath( Instance rootInstance, String instancePath ); static List<Instance> findInstancesByComponentName( AbstractApplication application, String componentName ); static Instance findRootInstance( Instance instance ); static Instance findScopedInstance( Instance instance ); static List<Instance> findAllScopedInstances( AbstractApplication app ); static List<Instance> getAllInstances( AbstractApplication application ); static boolean tryToInsertChildInstance( AbstractApplication application, Instance parentInstance, Instance childInstance ); static boolean hasChildWithThisName( AbstractApplication application, Instance parentInstance, String nameToSearch ); static File findInstanceDirectoryOnAgent( Instance instance ); static int countInstances( String instancePath ); static Instance replicateInstance( Instance instance ); static boolean isTarget( Instance instance ); static void removeOffScopeInstances( Instance scopedInstance ); static String findRootInstancePath( String scopedInstancePath ); static void fixOverriddenExports( Instance instance );  }
@Test public void testFindInstanceByPath() { Instance rootInstance = new Instance( "root" ); Instance current = rootInstance; for( int i=1; i<8; i++ ) { Instance tempInstance = new Instance( "i-" + i ); InstanceHelpers.insertChild( current, tempInstance ); current = tempInstance; } Assert.assertEquals( "root", InstanceHelpers.findInstanceByPath( rootInstance, "/root" ).getName()); Assert.assertEquals( "i-4", InstanceHelpers.findInstanceByPath( rootInstance, "/root/i-1/i-2/i-3/i-4" ).getName()); Assert.assertNull( InstanceHelpers.findInstanceByPath( rootInstance, "whatever" )); Assert.assertNull( InstanceHelpers.findInstanceByPath( rootInstance, "/root/whatever" )); Assert.assertNull( InstanceHelpers.findInstanceByPath( rootInstance, "/root/i-1/i-3" )); Assert.assertNull( InstanceHelpers.findInstanceByPath((Instance) null, "/root/i-1/i-3" )); Assert.assertNull( InstanceHelpers.findInstanceByPath( rootInstance, null )); Assert.assertNull( InstanceHelpers.findInstanceByPath((Application) null, "/root" )); }