Skip to content

Commit

Permalink
Fix UT
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Jun 9, 2022
1 parent e0f0ed6 commit b21a566
Showing 1 changed file with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.mockito.ArgumentCaptor;
import org.slf4j.Logger;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* @author Olivier Lamy
* @since 1.0-beta-1
*
*/
public class PropertyUtilsTest
extends TestSupport
extends TestSupport
{
private static File testDirectory = new File( getBasedir(), "target/test-classes/" );

public void testBasic()
throws Exception
throws Exception
{
File basicProp = new File( testDirectory, "basic.properties" );

Expand All @@ -66,7 +66,7 @@ public void testBasic()
}

public void testSystemProperties()
throws Exception
throws Exception
{
File systemProp = new File( testDirectory, "system.properties" );

Expand All @@ -87,7 +87,7 @@ public void testSystemProperties()
}

public void testException()
throws Exception
throws Exception
{
File nonExistent = new File( testDirectory, "not_existent_file" );

Expand All @@ -105,7 +105,7 @@ public void testException()
}

public void testloadpropertiesFile()
throws Exception
throws Exception
{
File propertyFile = new File( getBasedir() + "/src/test/units-files/propertyutils-test.properties" );
Properties baseProps = new Properties();
Expand All @@ -124,7 +124,7 @@ public void testloadpropertiesFile()
* @throws IOException if problem writing file
*/
public void testCircularReferences()
throws IOException
throws IOException
{
File basicProp = new File( testDirectory, "circular.properties" );

Expand All @@ -134,21 +134,21 @@ public void testCircularReferences()
}

basicProp.createNewFile();
try( FileWriter writer = new FileWriter( basicProp ) )
try ( FileWriter writer = new FileWriter( basicProp ) )
{
writer.write( "test=${test2}\n" );
writer.write( "test2=${test2}\n" );
writer.flush();
}

Logger logger = mock(Logger.class);
Logger logger = mock( Logger.class );

Properties prop = PropertyUtils.loadPropertyFile( basicProp, null, logger );
assertEquals( "${test2}", prop.getProperty( "test" ) );
assertEquals( "${test2}", prop.getProperty( "test2" ) );
verify( logger, times( 2 ) ).warn( anyString() );
assertWarn( "Circular reference between properties detected: test2 => test2", logger );
assertWarn( "Circular reference between properties detected: test => test2 => test2", logger );
assertWarn( logger,
"Circular reference between properties detected: test2 => test2",
"Circular reference between properties detected: test => test2 => test2" );
}

/**
Expand All @@ -157,7 +157,7 @@ public void testCircularReferences()
* @throws IOException if problem writing file
*/
public void testCircularReferences3Vars()
throws IOException
throws IOException
{
File basicProp = new File( testDirectory, "circular.properties" );

Expand All @@ -175,20 +175,26 @@ public void testCircularReferences3Vars()
writer.flush();
}

Logger logger = mock(Logger.class);
Logger logger = mock( Logger.class );

Properties prop = PropertyUtils.loadPropertyFile( basicProp, null, logger );
assertEquals( "${test2}", prop.getProperty( "test" ) );
assertEquals( "${test3}", prop.getProperty( "test2" ) );
assertEquals( "${test}", prop.getProperty( "test3" ) );
verify( logger, times( 3 ) ).warn( anyString() );
assertWarn( "Circular reference between properties detected: test3 => test => test2 => test3", logger );
assertWarn( "Circular reference between properties detected: test2 => test3 => test => test2", logger );
assertWarn( "Circular reference between properties detected: test => test2 => test3 => test", logger );
assertWarn( logger,
"Circular reference between properties detected: test3 => test => test2 => test3",
"Circular reference between properties detected: test2 => test3 => test => test2",
"Circular reference between properties detected: test => test2 => test3 => test" );
}

private void assertWarn( String expected, Logger mock )
private void assertWarn( Logger mock, String... expected )
{
// assertTrue( logger.warnMsgs.contains( expected ) );
ArgumentCaptor<String> argument = ArgumentCaptor.forClass( String.class );
verify( mock, times( expected.length ) ).warn( argument.capture() );
List<String> messages = argument.getAllValues();
for ( String str : expected )
{
assertTrue( messages.contains( str ) );
}
}
}

0 comments on commit b21a566

Please sign in to comment.