VariableHelpers { public static Map<String,ExportedVariable> parseExportedVariables( String line ) { Pattern randomPattern = Pattern.compile( ParsingConstants.PROPERTY_GRAPH_RANDOM_PATTERN, Pattern.CASE_INSENSITIVE ); Pattern varPattern = Pattern.compile( "([^,=]+)(\\s*=\\s*(\"([^\",]+)\"|([^,]+)))?" ); Map<String,ExportedVariable> result = new LinkedHashMap<> (); Matcher varMatcher = varPattern.matcher( line ); while( varMatcher.find()) { String key = varMatcher.group( 1 ).trim(); if( Utils.isEmptyOrWhitespaces( key )) continue; String value = null; if( varMatcher.group( 3 ) != null ) { if( varMatcher.group( 5 ) != null ) value = varMatcher.group( 5 ).trim(); else value = varMatcher.group( 4 ); } ExportedVariable var = new ExportedVariable(); Matcher m = randomPattern.matcher( key ); if( m.matches()) { var.setRandom( true ); var.setRawKind( m.group( 1 )); key = m.group( 2 ).trim(); } var.setName( key.trim()); if( value != null ) var.setValue( value ); result.put( var.getName(), var ); } return result; } private  VariableHelpers(); static Map.Entry<String,String> parseVariableName( String variableName ); static Map.Entry<String,String> parseExportedVariable( String exportedVariable ); static Map<String,ExportedVariable> parseExportedVariables( String line ); static Set<String> findPrefixesForExportedVariables( Instance instance ); static Set<String> findPrefixesForImportedVariables( Instance instance ); static Set<String> findPrefixesForMandatoryImportedVariables( Instance instance ); static Set<String> findPrefixesForExternalImports( AbstractApplication app );  }
@Test public void testParseExportedVariables_simpleVariables() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key = value" ); Assert.assertEquals( 1, variables.size()); ExportedVariable var = variables.get( "key" ); Assert.assertEquals( "value", var.getValue()); Assert.assertFalse( var.isRandom()); variables = VariableHelpers.parseExportedVariables( "key=value" ); Assert.assertEquals( 1, variables.size()); var = variables.get( "key" ); Assert.assertEquals( "value", var.getValue()); Assert.assertFalse( var.isRandom()); variables = VariableHelpers.parseExportedVariables( "" ); Assert.assertEquals( 0, variables.size()); variables = VariableHelpers.parseExportedVariables( " " ); Assert.assertEquals( 0, variables.size()); variables = VariableHelpers.parseExportedVariables( "ip" ); Assert.assertEquals( 1, variables.size()); var = variables.get( "ip" ); Assert.assertNull( var.getValue()); Assert.assertFalse( var.isRandom()); }
@Test public void testParseExportedVariables_simpleListOfVariables() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key1 = value1, key2=value2 , key3 = value3 " ); Assert.assertEquals( 3, variables.size()); ExportedVariable var = variables.get( "key1" ); Assert.assertEquals( "value1", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key2" ); Assert.assertEquals( "value2", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key3" ); Assert.assertEquals( "value3", var.getValue()); Assert.assertFalse( var.isRandom()); }
@Test public void testParseExportedVariables_variableWithComplexValue() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key1 = \"value1\"" ); Assert.assertEquals( 1, variables.size()); ExportedVariable var = variables.get( "key1" ); Assert.assertEquals( "value1", var.getValue()); Assert.assertFalse( var.isRandom()); variables = VariableHelpers.parseExportedVariables( " key1=\" value1 \" " ); Assert.assertEquals( 1, variables.size()); var = variables.get( "key1" ); Assert.assertEquals( " value1 ", var.getValue()); Assert.assertFalse( var.isRandom()); }
@Test public void testParseExportedVariables_listWithMixedValues() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key1 = \"value1 is here\" , key2= value2, key3 = \"key33\", key4 = oops " ); Assert.assertEquals( 4, variables.size()); ExportedVariable var = variables.get( "key1" ); Assert.assertEquals( "value1 is here", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key2" ); Assert.assertEquals( "value2", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key3" ); Assert.assertEquals( "key33", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key4" ); Assert.assertEquals( "oops", var.getValue()); Assert.assertFalse( var.isRandom()); }
@Test public void testParseExportedVariables_listWithMixedValuesAndRandom() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key1 = \"value1 is here\" , random[port] key2= value2, key3 = \"key33\", key4 = oops " ); Assert.assertEquals( 4, variables.size()); ExportedVariable var = variables.get( "key1" ); Assert.assertEquals( "value1 is here", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key2" ); Assert.assertEquals( "value2", var.getValue()); Assert.assertTrue( var.isRandom()); Assert.assertEquals( RandomKind.PORT, var.getRandomKind()); var = variables.get( "key3" ); Assert.assertEquals( "key33", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key4" ); Assert.assertEquals( "oops", var.getValue()); Assert.assertFalse( var.isRandom()); }