Example usage for org.springframework.util ReflectionUtils makeAccessible

List of usage examples for org.springframework.util ReflectionUtils makeAccessible

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils makeAccessible.

Prototype

@SuppressWarnings("deprecation") 
public static void makeAccessible(Field field) 

Source Link

Document

Make the given field accessible, explicitly setting it accessible if necessary.

Usage

From source file:com.ciphertool.zodiacengine.service.GeneticCipherSolutionServiceTest.java

@Test
public void testBegin_DebugMode() throws IOException {
    GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService();

    GeneticAlgorithm geneticAlgorithm = mock(GeneticAlgorithm.class);

    Population population = new Population();
    SolutionChromosome solutionChromosome = new SolutionChromosome();
    solutionChromosome.setEvaluationNeeded(false);
    population.addIndividual(solutionChromosome);
    when(geneticAlgorithm.getPopulation()).thenReturn(population);

    geneticCipherSolutionService.setGeneticAlgorithm(geneticAlgorithm);

    GenericCallback genericCallbackMock = mock(GenericCallback.class);

    GeneticAlgorithmStrategy geneticAlgorithmStrategy = new GeneticAlgorithmStrategy();

    Runtime runtimeMock = mock(Runtime.class);

    Field runtimeField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "runtime");
    ReflectionUtils.makeAccessible(runtimeField);
    ReflectionUtils.setField(runtimeField, geneticCipherSolutionService, runtimeMock);

    String[] commandsBefore = { "command3", "command4" };
    geneticCipherSolutionService.setCommandsBefore(commandsBefore);

    assertFalse(geneticCipherSolutionService.isRunning());
    geneticCipherSolutionService.begin(geneticAlgorithmStrategy, genericCallbackMock, true);
    assertTrue(geneticCipherSolutionService.isRunning());

    verify(runtimeMock, times(1)).exec(eq("command3"));
    verify(runtimeMock, times(1)).exec(eq("command4"));
    verifyNoMoreInteractions(runtimeMock);

    verifyZeroInteractions(genericCallbackMock);

    verify(geneticAlgorithm, times(1)).initialize();
    verify(geneticAlgorithm, times(1)).getPopulation();
    verify(geneticAlgorithm, times(1)).proceedWithNextGeneration();

    verify(geneticAlgorithm, times(1)).setStrategy(same(geneticAlgorithmStrategy));
}

From source file:com.github.gavlyukovskiy.boot.jdbc.decorator.flexypool.FlexyPoolConfigurationTests.java

@SuppressWarnings("unchecked")
private <T extends DataSource> FlexyPoolDataSource<T> assertDataSourceOfType(DataSource dataSource,
        Class<T> realDataSourceClass) {
    assertThat(dataSource).isInstanceOf(DecoratedDataSource.class);
    DataSource decoratedDataSource = ((DecoratedDataSource) dataSource).getDecoratedDataSource();
    assertThat(decoratedDataSource).isInstanceOf(FlexyPoolDataSource.class);
    Field field = ReflectionUtils.findField(FlexyPoolDataSource.class, "targetDataSource");
    ReflectionUtils.makeAccessible(field);
    Object targetDataSource = ReflectionUtils.getField(field, decoratedDataSource);
    assertThat(targetDataSource).isInstanceOf(realDataSourceClass);
    return (FlexyPoolDataSource<T>) decoratedDataSource;
}

From source file:com.ciphertool.genetics.PopulationTest.java

@Test
public void testSetCompareToKnownSolution() {
    Population population = new Population();

    Boolean compareToKnownSolution = true;
    population.setCompareToKnownSolution(compareToKnownSolution);

    Field compareToKnownSolutionField = ReflectionUtils.findField(Population.class, "compareToKnownSolution");
    ReflectionUtils.makeAccessible(compareToKnownSolutionField);
    Boolean compareToKnownSolutionFromObject = (Boolean) ReflectionUtils.getField(compareToKnownSolutionField,
            population);/*from ww w .  ja v a2  s  .  c om*/

    assertSame(compareToKnownSolution, compareToKnownSolutionFromObject);
}

From source file:com.ciphertool.genetics.algorithms.ConcurrentMultigenerationalGeneticAlgorithmTest.java

@SuppressWarnings("unchecked")
@Test/*from w w w  .java 2s. c  o  m*/
public void testCrossover_SmallPopulation() {
    ConcurrentMultigenerationalGeneticAlgorithm concurrentMultigenerationalGeneticAlgorithm = new ConcurrentMultigenerationalGeneticAlgorithm();

    Population population = new Population();

    Chromosome chromosome = new MockKeylessChromosome();
    population.addIndividual(chromosome);
    concurrentMultigenerationalGeneticAlgorithm.setPopulation(population);

    CrossoverAlgorithm crossoverAlgorithmMock = mock(CrossoverAlgorithm.class);

    Field crossoverAlgorithmField = ReflectionUtils.findField(ConcurrentMultigenerationalGeneticAlgorithm.class,
            "crossoverAlgorithm");
    ReflectionUtils.makeAccessible(crossoverAlgorithmField);
    ReflectionUtils.setField(crossoverAlgorithmField, concurrentMultigenerationalGeneticAlgorithm,
            crossoverAlgorithmMock);

    Field ineligibleForReproductionField = ReflectionUtils.findField(Population.class,
            "ineligibleForReproduction");
    ReflectionUtils.makeAccessible(ineligibleForReproductionField);
    List<Chromosome> ineligibleForReproductionFromObject = (List<Chromosome>) ReflectionUtils
            .getField(ineligibleForReproductionField, population);

    assertEquals(0, ineligibleForReproductionFromObject.size());

    int childrenProduced = concurrentMultigenerationalGeneticAlgorithm.crossover(10);

    ineligibleForReproductionFromObject = (List<Chromosome>) ReflectionUtils
            .getField(ineligibleForReproductionField, population);

    assertEquals(1, population.size());

    assertEquals(0, ineligibleForReproductionFromObject.size());

    assertEquals(0, childrenProduced);

    verifyZeroInteractions(crossoverAlgorithmMock);
}

From source file:com.ciphertool.genetics.PopulationTest.java

@Test
public void testSetCompareToKnownSolutionDefault() {
    Population population = new Population();

    Field compareToKnownSolutionField = ReflectionUtils.findField(Population.class, "compareToKnownSolution");
    ReflectionUtils.makeAccessible(compareToKnownSolutionField);
    Boolean compareToKnownSolutionFromObject = (Boolean) ReflectionUtils.getField(compareToKnownSolutionField,
            population);//ww  w.j ava2  s . c  o m

    assertEquals(false, compareToKnownSolutionFromObject);
}

From source file:com.github.gavlyukovskiy.boot.jdbc.decorator.flexypool.FlexyPoolConfigurationTests.java

@SuppressWarnings("unchecked")
private <T extends ConnectionAcquiringStrategy> T findStrategy(FlexyPoolDataSource<?> flexyPoolDataSource,
        Class<T> factoryClass) {
    Field field = ReflectionUtils.findField(FlexyPoolDataSource.class, "connectionAcquiringStrategies");
    ReflectionUtils.makeAccessible(field);
    Set<ConnectionAcquiringStrategy> strategies = (Set<ConnectionAcquiringStrategy>) ReflectionUtils
            .getField(field, flexyPoolDataSource);
    return (T) strategies.stream().filter(factoryClass::isInstance).findFirst().orElse(null);
}

From source file:org.terasoluna.gfw.web.token.transaction.TransactionTokenContextImplTest.java

@Test
public void TestCancelReservation01() throws IllegalArgumentException, IllegalAccessException {
    // setup parameters
    TransactionTokenInfo beginTransactionToken = new TransactionTokenInfo("testTokenAttribute1",
            TransactionTokenType.BEGIN);
    TransactionToken receivedToken = new TransactionToken("aaa");

    // setup target
    TransactionTokenContextImpl contextImpl = new TransactionTokenContextImpl(beginTransactionToken,
            receivedToken);/*  w w  w.  j a  v a  2 s .  co m*/

    // setup up expected result
    ReserveCommand expectedCommand = ReserveCommand.CREATE_TOKEN;

    // run
    contextImpl.cancelReservation();

    // test
    Field field = ReflectionUtils.findField(TransactionTokenContextImpl.class, "defaultCommand");
    ReflectionUtils.makeAccessible(field);
    ReserveCommand resultCommand = (ReserveCommand) field.get(contextImpl);
    assertThat(resultCommand, is(expectedCommand));

}

From source file:org.javelin.sws.ext.bind.internal.metadata.PropertyCallback.java

@Override
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
    log.trace("- Analyzing field: {}.{}", field.getDeclaringClass().getSimpleName(), field.getName());
    String fieldName = field.getName();
    // metadata for a field
    ReflectionUtils.makeAccessible(field);
    PropertyMetadata<T, ?> metadata = PropertyMetadata.newPropertyMetadata(this.clazz, field.getType(),
            fieldName, field, PropertyKind.FIELD);
    this.doWithPropertySafe(metadata);
}

From source file:com.ciphertool.zodiacengine.service.GeneticCipherSolutionServiceTest.java

@Test
public void testSetUp() throws IOException {
    GeneticCipherSolutionService geneticCipherSolutionService = new GeneticCipherSolutionService();

    GeneticAlgorithm geneticAlgorithm = mock(GeneticAlgorithm.class);
    geneticCipherSolutionService.setGeneticAlgorithm(geneticAlgorithm);

    Runtime runtimeMock = mock(Runtime.class);

    Field runtimeField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "runtime");
    ReflectionUtils.makeAccessible(runtimeField);
    ReflectionUtils.setField(runtimeField, geneticCipherSolutionService, runtimeMock);

    String[] commandsBefore = { "command1", "command2" };
    geneticCipherSolutionService.setCommandsBefore(commandsBefore);

    GeneticAlgorithmStrategy geneticAlgorithmStrategy = new GeneticAlgorithmStrategy();
    geneticCipherSolutionService.setUp(geneticAlgorithmStrategy);

    verify(runtimeMock, times(1)).exec(eq("command1"));
    verify(runtimeMock, times(1)).exec(eq("command2"));
    verifyNoMoreInteractions(runtimeMock);

    verify(geneticAlgorithm, times(1)).setStrategy(same(geneticAlgorithmStrategy));
}

From source file:com.ciphertool.sentencebuilder.etl.importers.WordListImporterImplTest.java

@Test
public void testImportWord() {
    WordListImporterImpl wordListImporterImpl = new WordListImporterImpl();

    Field rowCountField = ReflectionUtils.findField(WordListImporterImpl.class, "rowCount");
    ReflectionUtils.makeAccessible(rowCountField);
    AtomicInteger rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField,
            wordListImporterImpl);/*from  w ww .  j  a v a2  s. c o  m*/

    assertEquals(0, rowCountFromObject.intValue());

    WordDao wordDaoMock = mock(WordDao.class);
    when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true);

    wordListImporterImpl.setWordDao(wordDaoMock);
    wordListImporterImpl.setPersistenceBatchSize(3);

    List<Word> wordBatch = new ArrayList<Word>();

    Word word1 = new Word(new WordId("george", PartOfSpeechType.NOUN));
    wordListImporterImpl.importWord(word1, wordBatch);

    verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class));
    assertEquals(1, wordBatch.size());

    Word word2 = new Word(new WordId("elmer", PartOfSpeechType.NOUN));
    wordListImporterImpl.importWord(word2, wordBatch);

    verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class));
    assertEquals(2, wordBatch.size());

    Word word3 = new Word(new WordId("belden", PartOfSpeechType.NOUN));
    wordListImporterImpl.importWord(word3, wordBatch);

    rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl);

    assertEquals(3, rowCountFromObject.intValue());
    verify(wordDaoMock, times(1)).insertBatch(same(wordBatch));
    assertTrue(wordBatch.isEmpty());
}