Java Code Examples for org.apache.commons.lang.reflect.FieldUtils#writeDeclaredField()

The following examples show how to use org.apache.commons.lang.reflect.FieldUtils#writeDeclaredField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PomIOTest.java    From pom-manipulation-ext with Apache License 2.0 8 votes vote down vote up
@Test
public void testRoundTripPOMs()
                throws Exception
{
    URL resource = PomIOTest.class.getResource( filename );
    assertNotNull( resource );
    File pom = new File( resource.getFile() );
    assertTrue( pom.exists() );

    File targetFile = folder.newFile( "target.xml" );
    FileUtils.copyFile( pom, targetFile );

    List<Project> projects = pomIO.parseProject( targetFile );

    assertNull( projects.get( 0 ).getModel().getModelEncoding() );

    // We don't want this to be the execution root so that it doesn't add "Modified by" which breaks the comparison
    FieldUtils.writeDeclaredField( projects.get( 0 ), "executionRoot", false, true);
    HashSet<Project> changed = new HashSet<>(projects);
    pomIO.rewritePOMs( changed );

    assertTrue( FileUtils.contentEqualsIgnoreEOL( pom, targetFile, StandardCharsets.UTF_8.toString() ) );
    assertTrue( FileUtils.contentEquals( targetFile, pom ) );
}
 
Example 2
Source File: TestConfigCenterConfigurationSource.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void destroy_inited() throws IllegalAccessException {
  AtomicInteger count = new AtomicInteger();
  ConfigCenterClient configCenterClient = new MockUp<ConfigCenterClient>() {
    @Mock
    void destroy() {
      count.incrementAndGet();
    }
  }.getMockInstance();
  ConfigCenterConfigurationSourceImpl configCenterSource = new ConfigCenterConfigurationSourceImpl();
  FieldUtils.writeDeclaredField(configCenterSource, "configCenterClient", configCenterClient, true);

  configCenterSource.destroy();

  Assert.assertEquals(1, count.get());
}
 
Example 3
Source File: TestKieConfigurationSource.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void destroy_inited() throws IllegalAccessException {
  AtomicInteger count = new AtomicInteger();
  KieClient kieClient = new MockUp<KieClient>() {
    @Mock
    void destroy() {
      count.incrementAndGet();
    }
  }.getMockInstance();
  KieConfigurationSourceImpl kieSource = new KieConfigurationSourceImpl();
  FieldUtils
      .writeDeclaredField(kieSource, "kieClient", kieClient, true);

  kieSource.destroy();

  Assert.assertEquals(1, count.get());
}
 
Example 4
Source File: ConnectionsControllerTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  // a tricky initialisation - first inject private fields
  controller = new ConnectionsController();
  connectionsTable = mock( XulTree.class );
  FieldUtils.writeDeclaredField( controller, "connectionsTable", connectionsTable, true );

  // and then spy the controller
  controller = spy( controller );

  databaseDialog = mock( DatabaseDialog.class );
  doReturn( databaseDialog ).when( controller ).getDatabaseDialog();
  databaseMeta = mock( DatabaseMeta.class );
  doReturn( databaseMeta ).when( databaseDialog ).getDatabaseMeta();
  doNothing().when( controller ).refreshConnectionList();
  doNothing().when( controller ).showAlreadyExistsMessage();

  repository = mock( Repository.class );
  controller.init( repository );
}
 
Example 5
Source File: PomIO.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
private void write( final Project project, final File pom, final Model model )
    throws ManipulationException
{
    try
    {
        // We possibly could store the EOL type in the Project when we first read
        // the file but we would then have to do a dual read, then write as opposed
        // to a read, then read + write now.
        LineSeparator ls = FileIO.determineEOL( pom );

        MavenProject mp = new MavenProject(model);
        ModelETLRequest request = new ModelETLRequest();
        request.setLineSeparator( ls.value() );
        request.setProject( mp );
        request.setReleaseDescriptor( ReleaseUtils.buildReleaseDescriptor( releaseDescriptorBuilder ) );

        ModelETL etl = modelETLFactories.newInstance( request );

        // Reread in order to fill in JdomModelETL
        etl.extract( pom );

        // Annoyingly the document is private but we need to access it in order to ensure the model is written to the Document.
        //
        // Currently the fields we want to access are private - https://issues.apache.org/jira/browse/MRELEASE-1044 requests
        // them to be protected to avoid this reflection.
        Document doc = (Document) FieldUtils.getDeclaredField( JDomModelETL.class, "document", true ).get( etl );

        jdomModelConverter.convertModelToJDOM( model, doc );

        if ( project.isExecutionRoot() )
        {
            // Previously it was possible to add a comment outside of the root element (which maven3-model-jdom-support handled)
            // but the release plugin code only takes account of code within the root element and everything else is handled separately.
            //
            String outtro = (String) FieldUtils.getDeclaredField( JDomModelETL.class, "outtro", true ).get( etl );

            String commentStart = ls.value() +
                            "<!--" +
                            ls.value();
            String commentEnd = ls.value() +
                            "-->" +
                            ls.value();

            if ( outtro.equals( ls.value() ) )
            {
                logger.debug( "Outtro contains newlines only" );

                outtro = commentStart + manifestComment + commentEnd;
            }
            else
            {
                outtro = outtro.replaceAll( "Modified by.*", manifestComment );
            }
            FieldUtils.writeDeclaredField( etl,
                                           "outtro",
                                           outtro,
                                           true);
        }

        etl.load( pom );
    }
    catch ( ReleaseExecutionException | IllegalAccessException e )
    {
        throw new ManipulationException( "Failed to parse POM for rewrite: {}. Reason: ", pom, e.getMessage(), e );
    }
}