下面列出了org.apache.commons.lang.reflect.FieldUtils#readField ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void checkLocalRepositoryWithDefaults() throws Exception
{
Cli c = new Cli();
File settings = writeSettings( temp.newFile());
TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-s", settings.toString()} } );
ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );
assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
ms.getRequest().getLocalRepositoryPath().toString() );
assertEquals( "File " + new File( ms.getRequest().getLocalRepository().getBasedir() ).getParentFile().toString()
+ " was not equal to " + System.getProperty( "user.home" ) + File.separatorChar
+ ".m2",
new File( ms.getRequest().getLocalRepository().getBasedir() ).getParentFile().toString(),
System.getProperty( "user.home" ) + File.separatorChar + ".m2" );
}
@Test
public void checkLocalRepositoryWithExplicitMavenRepoAndSettings() throws Exception
{
File folder = temp.newFolder();
Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[]
{ "--settings=" + getClass().getResource("/settings-test.xml").getFile(),
"-Dmaven.repo.local=" + folder.toString() }} );
ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );
assertEquals( ms.getLocalRepository().getBasedir(), folder.toString() );
assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
ms.getRequest().getLocalRepositoryPath().toString() );
}
default Object readField(Object target, String fieldName) {
try {
return FieldUtils.readField(target, fieldName, true);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Test
public void checkTargetDefaultMatches() throws Exception
{
Cli c = new Cli();
c.run( new String[] {} );
ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
File defaultTarget = (File) FieldUtils.readField( c, "target", true );
assertEquals( "Session file should match", defaultTarget, session.getPom() );
}
@Test
public void checkLocalRepositoryWithSettings() throws Exception
{
Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-settings=" + getClass().getResource( "/settings-test.xml").getFile() }} );
ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );
assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
ms.getRequest().getLocalRepositoryPath().toString() );
}
@Test
public void checkLocalRepositoryWithExplicitMavenRepo() throws Exception
{
File folder = temp.newFolder();
Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-Dmaven.repo.local=" + folder.toString() }} );
ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );
assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
ms.getRequest().getLocalRepositoryPath().toString() );
}
<T extends AmazonWebServiceClient> String getEndpointFor(T client) {
try {
URI endpointUri = (URI) FieldUtils.readField(client, "endpoint", true);
return endpointUri.toASCIIString();
} catch (Exception e) {
return null;
}
}
/**
* Returns the object of the (nested) property.
*/
public static Object getNestedObject(Object instance, String propertyName) throws IllegalAccessException {
if (PropertyUtils.isWeatherProperty(propertyName)) {
return instance;
}
while (PropertyResolver.hasNested(propertyName)) {
instance = FieldUtils.readField(instance, PropertyResolver.first(propertyName), true);
propertyName = PropertyResolver.removeFirst(propertyName);
}
return instance;
}
@Test
public void checkLocalRepositoryWithDefaultsAndModifiedUserSettings() throws Exception
{
boolean restore = false;
Path source = Paths.get ( System.getProperty( "user.home" ) + File.separatorChar + ".m2" + File.separatorChar + "settings.xml");
Path backup = Paths.get( source.toString() + '.' + UUID.randomUUID().toString() );
Path tmpSettings = Paths.get ( getClass().getResource("/settings-test.xml").toURI() );
try
{
if ( source.toFile().exists() )
{
System.out.println ("Backing up settings.xml to " + backup);
restore = true;
Files.move( source, backup, StandardCopyOption.ATOMIC_MOVE);
}
Files.copy( tmpSettings, source );
Cli c = new Cli();
TestUtils.executeMethod( c, "run", new Object[] { new String[] {} } );
ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
MavenSession ms = (MavenSession) FieldUtils.readField( session, "mavenSession", true );
assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
ms.getRequest().getLocalRepositoryPath().toString() );
assertEquals( ms.getLocalRepository().getBasedir(),
System.getProperty( "user.home" ) + File.separatorChar + ".m2-mead-test" );
}
finally
{
if ( restore )
{
Files.move( backup, source, StandardCopyOption.ATOMIC_MOVE);
}
else
{
Files.delete( source );
}
}
}