ScriptUtils { public static Map<String,String> formatExportedVars( Instance instance ) { Map<String, String> exportedVars = new HashMap<> (); for( Instance inst = instance; inst != null; inst = inst.getParent()) { String prefix = ""; if( inst != instance ) prefix = "ANCESTOR_" + inst.getComponent().getName() + "_"; Map<String,String> exports = InstanceHelpers.findAllExportedVariables( inst ); for(Entry<String, String> entry : exports.entrySet()) { String vname = prefix + VariableHelpers.parseVariableName( entry.getKey()).getValue(); vname = vname.replaceAll( "(-|%s)+", "_" ); exportedVars.put( vname, entry.getValue()); } } return exportedVars; } private  ScriptUtils(); static Map<String,String> formatExportedVars( Instance instance ); static Map<String,String> formatImportedVars( Instance instance ); static void setScriptsExecutable( File fileOrDir );  }
@Test public void testFormatExportedVars_singleInstance() { Component c = new Component( "c" ); c.exportedVariables.put( "forced-ip", new ExportedVariable( "forced-ip", "127.0.0.1" )); c.exportedVariables.put( "port", new ExportedVariable( "port", "49535" )); Instance i = new Instance( "i" ).component( c ); i.overriddenExports.put( "something", "else" ); Map<String,String> exportedVariables = ScriptUtils.formatExportedVars( i ); Assert.assertEquals( 3, exportedVariables.size()); Assert.assertEquals( "127.0.0.1", exportedVariables.get( "forced_ip" )); Assert.assertEquals( "49535", exportedVariables.get( "port" )); Assert.assertEquals( "else", exportedVariables.get( "something" )); }
@Test public void testFormatExportedVars_severalInstances() { TestApplication app = new TestApplication(); app.getTomcat().getComponent().exportedVariables.put( "path", new ExportedVariable( "path", "apps" )); Map<String,String> exportedVariables = ScriptUtils.formatExportedVars( app.getTomcatVm()); Assert.assertEquals( 0, exportedVariables.size()); exportedVariables = ScriptUtils.formatExportedVars( app.getTomcat()); Assert.assertEquals( 1, exportedVariables.size()); Assert.assertEquals( "apps", exportedVariables.get( "path" )); exportedVariables = ScriptUtils.formatExportedVars( app.getWar()); Assert.assertEquals( 3, exportedVariables.size()); Assert.assertEquals( "apps", exportedVariables.get( "ANCESTOR_tomcat_path" )); Assert.assertEquals( "8080", exportedVariables.get( "port" )); Assert.assertNull( exportedVariables.get( "ip" )); }