List of usage examples for org.springframework.util ReflectionUtils findField
@Nullable public static Field findField(Class<?> clazz, String name)
From source file:com.ciphertool.genetics.algorithms.crossover.LiberalUnevaluatedCrossoverAlgorithmTest.java
@Test public void testSetCoin() { Coin coinToSet = mock(Coin.class); LiberalUnevaluatedCrossoverAlgorithm liberalUnevaluatedCrossoverAlgorithm = new LiberalUnevaluatedCrossoverAlgorithm(); liberalUnevaluatedCrossoverAlgorithm.setCoin(coinToSet); Field coinField = ReflectionUtils.findField(LiberalUnevaluatedCrossoverAlgorithm.class, "coin"); ReflectionUtils.makeAccessible(coinField); Coin coinFromObject = (Coin) ReflectionUtils.getField(coinField, liberalUnevaluatedCrossoverAlgorithm); assertSame(coinToSet, coinFromObject); }
From source file:com.ciphertool.genetics.algorithms.mutation.LiberalMutationAlgorithmTest.java
@Test public void testSetMaxMutationsPerChromosome() { Integer maxMutationsPerChromosomeToSet = 3; LiberalMutationAlgorithm liberalMutationAlgorithm = new LiberalMutationAlgorithm(); liberalMutationAlgorithm.setMaxMutationsPerChromosome(maxMutationsPerChromosomeToSet); Field maxMutationsPerChromosomeField = ReflectionUtils.findField(LiberalMutationAlgorithm.class, "maxMutationsPerChromosome"); ReflectionUtils.makeAccessible(maxMutationsPerChromosomeField); Integer maxMutationsPerChromosomeFromObject = (Integer) ReflectionUtils .getField(maxMutationsPerChromosomeField, liberalMutationAlgorithm); assertSame(maxMutationsPerChromosomeToSet, maxMutationsPerChromosomeFromObject); }
From source file:com.ciphertool.zodiacengine.fitness.AbstractSolutionEvaluatorBaseTest.java
@Test public void testClearHasMatchValues() { ConcreteSolutionEvaluatorBase abstractSolutionEvaluatorBase = new ConcreteSolutionEvaluatorBase(); Field logField = ReflectionUtils.findField(ConcreteSolutionEvaluatorBase.class, "log"); Logger mockLogger = mock(Logger.class); ReflectionUtils.makeAccessible(logField); ReflectionUtils.setField(logField, abstractSolutionEvaluatorBase, mockLogger); for (PlaintextSequence plaintextSequence : simpleSolution.getPlaintextCharacters()) { plaintextSequence.setHasMatch(true); assertTrue(plaintextSequence.getHasMatch()); }//from ww w. j a va2s . c o m abstractSolutionEvaluatorBase.clearHasMatchValues(simpleSolution); for (PlaintextSequence plaintextSequence : simpleSolution.getPlaintextCharacters()) { assertFalse(plaintextSequence.getHasMatch()); } verifyZeroInteractions(mockLogger); }
From source file:org.cleverbus.test.AbstractTest.java
/** * Sets value of private field./*w w w . j a v a 2 s . c om*/ * * @param target the target object * @param name the field name * @param value the value for setting to the field */ public static void setPrivateField(Object target, String name, Object value) { Field countField = ReflectionUtils.findField(target.getClass(), name); ReflectionUtils.makeAccessible(countField); ReflectionUtils.setField(countField, target, value); }
From source file:com.ciphertool.genetics.algorithms.ConcurrentMultigenerationalGeneticAlgorithmTest.java
@SuppressWarnings("unchecked") @Test/*ww w . ja v a 2s. c o m*/ public void testCrossover() { ConcurrentMultigenerationalGeneticAlgorithm concurrentMultigenerationalGeneticAlgorithm = new ConcurrentMultigenerationalGeneticAlgorithm(); concurrentMultigenerationalGeneticAlgorithm.setTaskExecutor(taskExecutor); Population population = new Population(); FitnessEvaluator fitnessEvaluatorMock = mock(FitnessEvaluator.class); when(fitnessEvaluatorMock.evaluate(any(Chromosome.class))).thenReturn(DEFAULT_FITNESS_VALUE); population.setFitnessEvaluator(fitnessEvaluatorMock); Selector selectorMock = mock(Selector.class); population.setSelector(selectorMock); when(selectorMock.getNextIndex(anyListOf(Chromosome.class), any(Double.class))).thenReturn(0, 1, 2, 3, 4); int initialPopulationSize = 50; for (int i = 0; i < initialPopulationSize; i++) { population.addIndividual(new MockKeylessChromosome()); } concurrentMultigenerationalGeneticAlgorithm.setPopulation(population); CrossoverAlgorithm crossoverAlgorithmMock = mock(CrossoverAlgorithm.class); Field crossoverAlgorithmField = ReflectionUtils.findField(ConcurrentMultigenerationalGeneticAlgorithm.class, "crossoverAlgorithm"); ReflectionUtils.makeAccessible(crossoverAlgorithmField); ReflectionUtils.setField(crossoverAlgorithmField, concurrentMultigenerationalGeneticAlgorithm, crossoverAlgorithmMock); Chromosome chromosomeToReturn = new MockKeylessChromosome(); when(crossoverAlgorithmMock.crossover(any(Chromosome.class), any(Chromosome.class))) .thenReturn(Arrays.asList(chromosomeToReturn)); GeneticAlgorithmStrategy strategy = new GeneticAlgorithmStrategy(); strategy.setCrossoverRate(0.1); Field strategyField = ReflectionUtils.findField(ConcurrentMultigenerationalGeneticAlgorithm.class, "strategy"); ReflectionUtils.makeAccessible(strategyField); ReflectionUtils.setField(strategyField, concurrentMultigenerationalGeneticAlgorithm, strategy); 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(initialPopulationSize); ineligibleForReproductionFromObject = (List<Chromosome>) ReflectionUtils .getField(ineligibleForReproductionField, population); /* * The population size should be reduced by the number of parents used * during crossover. */ assertEquals(40, population.size()); assertEquals(5, childrenProduced); // There should be 10 ineligible parents, along with the 5 children assertEquals(15, ineligibleForReproductionFromObject.size()); verify(selectorMock, times(10)).getNextIndex(anyListOf(Chromosome.class), any(Double.class)); verify(crossoverAlgorithmMock, times(5)).crossover(any(Chromosome.class), any(Chromosome.class)); }
From source file:com.ciphertool.sentencebuilder.etl.importers.WordListImporterImplTest.java
@Test public void testImportWordList() { ThreadPoolTaskExecutor taskExecutorSpy = spy(new ThreadPoolTaskExecutor()); taskExecutorSpy.setCorePoolSize(4);//from w w w . jav a 2s .c o m taskExecutorSpy.setMaxPoolSize(4); taskExecutorSpy.setQueueCapacity(100); taskExecutorSpy.setKeepAliveSeconds(1); taskExecutorSpy.setAllowCoreThreadTimeOut(true); taskExecutorSpy.initialize(); WordListImporterImpl wordListImporterImpl = new WordListImporterImpl(); wordListImporterImpl.setTaskExecutor(taskExecutorSpy); Field rowCountField = ReflectionUtils.findField(WordListImporterImpl.class, "rowCount"); ReflectionUtils.makeAccessible(rowCountField); AtomicInteger rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl); assertEquals(0, rowCountFromObject.intValue()); WordDao wordDaoMock = mock(WordDao.class); when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true); int persistenceBatchSizeToSet = 3; int concurrencyBatchSizeToSet = 3; wordListImporterImpl.setWordDao(wordDaoMock); wordListImporterImpl.setPersistenceBatchSize(persistenceBatchSizeToSet); wordListImporterImpl.setConcurrencyBatchSize(concurrencyBatchSizeToSet); Word word1 = new Word(new WordId("george", PartOfSpeechType.NOUN)); Word word2 = new Word(new WordId("elmer", PartOfSpeechType.NOUN)); Word word3 = new Word(new WordId("belden", PartOfSpeechType.NOUN)); List<Word> wordsToReturn = new ArrayList<Word>(); wordsToReturn.add(word1); wordsToReturn.add(word2); wordsToReturn.add(word3); PartOfSpeechFileParser fileParserMock = mock(PartOfSpeechFileParser.class); when(fileParserMock.parseFile()).thenReturn(wordsToReturn); wordListImporterImpl.setFileParser(fileParserMock); wordListImporterImpl.importWordList(); rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl); assertEquals(3, rowCountFromObject.intValue()); verify(wordDaoMock, times(1)).insertBatch(anyListOf(Word.class)); verify(taskExecutorSpy, times(1)).execute(any(Runnable.class)); }
From source file:com.ciphertool.zodiacengine.service.GeneticCipherSolutionServiceTest.java
@Test public void testBegin() 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); SolutionDao solutionDaoMock = mock(SolutionDao.class); geneticCipherSolutionService.setSolutionDao(solutionDaoMock); GenericCallback genericCallbackMock = mock(GenericCallback.class); Runtime runtimeMock = mock(Runtime.class); Field runtimeField = ReflectionUtils.findField(GeneticCipherSolutionService.class, "runtime"); ReflectionUtils.makeAccessible(runtimeField); ReflectionUtils.setField(runtimeField, geneticCipherSolutionService, runtimeMock); String[] commandsAfter = { "command1", "command2" }; geneticCipherSolutionService.setCommandsAfter(commandsAfter); String[] commandsBefore = { "command3", "command4" }; geneticCipherSolutionService.setCommandsBefore(commandsBefore); GeneticAlgorithmStrategy geneticAlgorithmStrategy = new GeneticAlgorithmStrategy(); // Before the algorithm begins, this will be false assertFalse(geneticCipherSolutionService.isRunning()); geneticCipherSolutionService.begin(geneticAlgorithmStrategy, genericCallbackMock, false); /*/*from w w w . j a v a2 s . com*/ * After the algorithm ends, this will again be false. It is only true * actually during the algorithm. */ assertFalse(geneticCipherSolutionService.isRunning()); verify(solutionDaoMock, times(1)).insert(same(solutionChromosome)); verify(genericCallbackMock, times(1)).doCallback(); /* * These commands should still get called even though an exception was * caught */ verify(runtimeMock, times(1)).exec(eq("command1")); verify(runtimeMock, times(1)).exec(eq("command2")); verify(runtimeMock, times(1)).exec(eq("command3")); verify(runtimeMock, times(1)).exec(eq("command4")); verifyNoMoreInteractions(runtimeMock); verify(geneticAlgorithm, times(1)).evolveAutonomously(); verify(geneticAlgorithm, times(1)).setStrategy(same(geneticAlgorithmStrategy)); }
From source file:org.grails.datastore.mapping.redis.util.JedisTemplate.java
private void disconnect() { // hack in to get the private client since disconnect() isn't in the interface Field field = ReflectionUtils.findField(pipeline.getClass(), "client"); field.setAccessible(true);/* w w w . j a v a 2 s . c om*/ try { Client client = (Client) field.get(pipeline); client.disconnect(); } catch (Exception e) { ReflectionUtils.handleReflectionException(e); } }
From source file:com.qcadoo.maven.plugins.jetty.JettyMojo.java
private void setField(final String name, final Object value) { Field field = ReflectionUtils.findField(getClass(), name); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, this, value); }
From source file:org.springjutsu.validation.rules.ValidationRulesContainer.java
/** * Read from include annotations to further * populate include paths already parsed from XML. *//*from w ww .j ava 2 s. c om*/ @SuppressWarnings({ "unchecked", "rawtypes" }) private void initIncludePaths() { for (ValidationEntity entity : validationEntityMap.values()) { // no paths to check on an interface. if (entity.getValidationClass().isInterface()) { continue; } BeanWrapper entityWrapper = new BeanWrapperImpl(entity.getValidationClass()); for (PropertyDescriptor descriptor : entityWrapper.getPropertyDescriptors()) { if (!entityWrapper.isReadableProperty(descriptor.getName()) || !entityWrapper.isWritableProperty(descriptor.getName())) { continue; } for (Class includeAnnotation : includeAnnotations) { try { if (ReflectionUtils.findField(entity.getValidationClass(), descriptor.getName()) .getAnnotation(includeAnnotation) != null) { entity.getIncludedPaths().add(descriptor.getName()); } } catch (SecurityException se) { throw new IllegalStateException("Unexpected error while checking for included properties", se); } } } } }