List of usage examples for org.springframework.util ReflectionUtils getField
@Nullable public static Object getField(Field field, @Nullable Object target)
From source file:nl.minbzk.dwr.zoeken.enricher.service.EnricherService.java
/** * Post process envelopes to fetch mongoDB data * * @param envelopes//from www . j a v a 2 s .com */ protected void postProcessEnvelopes(final List<ImportEnvelope> envelopes) { for (ImportEnvelope envelope : envelopes) { if (envelope.getUri() != null && envelope.getUri().startsWith("data://")) { String databaseId = envelope.getUri().substring(7); CompositeEntry compositeEntry = mongoTemplate.findById(databaseId, CompositeEntry.class, defaultCollection); EnricherDocument enricherDocument = compositeEntry.getEnricherDocument(); if (enricherDocument == null) logger.error(format( "Composite entry from data storage does not contain an enricher document (current = %s, failure = %s, id = %s)", compositeEntry.getCurrentState(), compositeEntry.getFailureState(), compositeEntry.getId())); else { // Now set the URI reference to the modified or original content reference envelope.setUri("data://" + (compositeEntry.getModifiedContent() != null ? compositeEntry.getModifiedContent().getId() : compositeEntry.getOriginalContent().getId())); envelope.setReference(enricherDocument.getReference()); envelope.setTitle(enricherDocument.getTitle()); if (envelope.getFields() == null) { envelope.setFields(new HashMap<String, List<String>>()); } for (Field field : schemaFieldCache.keySet()) { String schemaField = schemaFieldCache.get(field); Object value = ReflectionUtils.getField(field, enricherDocument); if (value != null) { this.mapEnricherDocumentToImportEnvelope(envelope, value, schemaField); } } } } } }
From source file:com.ciphertool.sentencebuilder.etl.importers.WordListImporterImplTest.java
@Test public void testImportWord_BatchSizeNotReached() { WordListImporterImpl wordListImporterImpl = new WordListImporterImpl(); Field rowCountField = ReflectionUtils.findField(WordListImporterImpl.class, "rowCount"); ReflectionUtils.makeAccessible(rowCountField); AtomicInteger rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl);/* w ww.j ava 2s .c om*/ assertEquals(0, rowCountFromObject.intValue()); WordDao wordDaoMock = mock(WordDao.class); when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true); wordListImporterImpl.setWordDao(wordDaoMock); wordListImporterImpl.setPersistenceBatchSize(4); 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(0, rowCountFromObject.intValue()); verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class)); assertEquals(3, wordBatch.size()); assertSame(word1, wordBatch.get(0)); assertSame(word2, wordBatch.get(1)); assertSame(word3, wordBatch.get(2)); }
From source file:com.ciphertool.sentencebuilder.etl.importers.FrequencyListImporterImplTest.java
@SuppressWarnings("unchecked") @Test// w ww . ja v a 2 s. c o m public void testImportWord() { FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl(); Field rowUpdateCountField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "rowUpdateCount"); ReflectionUtils.makeAccessible(rowUpdateCountField); AtomicInteger rowUpdateCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowUpdateCountField, frequencyListImporterImpl); assertEquals(0, rowUpdateCountFromObject.intValue()); Field rowInsertCountField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "rowInsertCount"); ReflectionUtils.makeAccessible(rowInsertCountField); AtomicInteger rowInsertCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowInsertCountField, frequencyListImporterImpl); assertEquals(0, rowInsertCountFromObject.intValue()); WordDao wordDaoMock = mock(WordDao.class); when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true); when(wordDaoMock.updateBatch(anyListOf(Word.class))).thenReturn(true); frequencyListImporterImpl.setWordDao(wordDaoMock); frequencyListImporterImpl.setPersistenceBatchSize(2); List<Word> insertBatch = new ArrayList<Word>(); List<Word> updateBatch = new ArrayList<Word>(); Word word1 = new Word(new WordId("george", PartOfSpeechType.NOUN), 100); Word word2 = new Word(new WordId("belden", PartOfSpeechType.NOUN), 200); Word word3 = new Word(new WordId("is", PartOfSpeechType.VERB_PARTICIPLE), 300); Word word4 = new Word(new WordId("awesome", PartOfSpeechType.ADJECTIVE), 400); Word wordFromDatabase1 = new Word(new WordId("george", PartOfSpeechType.NOUN)); Word wordFromDatabase2 = new Word(new WordId("belden", PartOfSpeechType.NOUN)); when(wordDaoMock.findByWordString(anyString())).thenReturn(Arrays.asList(wordFromDatabase1), Arrays.asList(wordFromDatabase2), null, null); frequencyListImporterImpl.importWord(word1, insertBatch, updateBatch); verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class)); verify(wordDaoMock, never()).updateBatch(anyListOf(Word.class)); assertEquals(1, updateBatch.size()); assertTrue(insertBatch.isEmpty()); frequencyListImporterImpl.importWord(word2, insertBatch, updateBatch); verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class)); verify(wordDaoMock, times(1)).updateBatch(anyListOf(Word.class)); assertTrue(updateBatch.isEmpty()); assertTrue(insertBatch.isEmpty()); frequencyListImporterImpl.importWord(word3, insertBatch, updateBatch); verify(wordDaoMock, never()).insertBatch(anyListOf(Word.class)); verify(wordDaoMock, times(1)).updateBatch(anyListOf(Word.class)); assertTrue(updateBatch.isEmpty()); assertEquals(1, insertBatch.size()); frequencyListImporterImpl.importWord(word4, insertBatch, updateBatch); verify(wordDaoMock, times(1)).insertBatch(anyListOf(Word.class)); verify(wordDaoMock, times(1)).updateBatch(anyListOf(Word.class)); assertTrue(updateBatch.isEmpty()); assertTrue(insertBatch.isEmpty()); rowUpdateCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowUpdateCountField, frequencyListImporterImpl); rowInsertCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowInsertCountField, frequencyListImporterImpl); assertEquals(100, wordFromDatabase1.getFrequencyWeight()); assertEquals(200, wordFromDatabase2.getFrequencyWeight()); assertEquals(2, rowUpdateCountFromObject.intValue()); assertEquals(2, rowInsertCountFromObject.intValue()); verify(wordDaoMock, times(4)).findByWordString(anyString()); }
From source file:com.ciphertool.genetics.algorithms.MultigenerationalGeneticAlgorithmTest.java
@Test public void testFinish() { Date beforeFinish = new Date(); ExecutionStatisticsDao executionStatisticsDaoToSet = mock(ExecutionStatisticsDao.class); MultigenerationalGeneticAlgorithm multigenerationalGeneticAlgorithm = new MultigenerationalGeneticAlgorithm(); multigenerationalGeneticAlgorithm.setExecutionStatisticsDao(executionStatisticsDaoToSet); ExecutionStatistics executionStatistics = new ExecutionStatistics(); Field executionStatisticsField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "executionStatistics"); ReflectionUtils.makeAccessible(executionStatisticsField); ReflectionUtils.setField(executionStatisticsField, multigenerationalGeneticAlgorithm, executionStatistics); Field generationCountField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "generationCount"); ReflectionUtils.makeAccessible(generationCountField); ReflectionUtils.setField(generationCountField, multigenerationalGeneticAlgorithm, 1); multigenerationalGeneticAlgorithm.finish(); assertTrue(executionStatistics.getEndDateTime().getTime() >= beforeFinish.getTime()); verify(executionStatisticsDaoToSet, times(1)).insert(same(executionStatistics)); ExecutionStatistics executionStatisticsFromObject = (ExecutionStatistics) ReflectionUtils .getField(executionStatisticsField, multigenerationalGeneticAlgorithm); assertNull(executionStatisticsFromObject); }
From source file:com.ciphertool.sentencebuilder.etl.importers.WordListImporterImplTest.java
@Test public void testBatchWordImportTask() { WordListImporterImpl wordListImporterImpl = new WordListImporterImpl(); 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> threadBatch = new ArrayList<Word>(); threadBatch.add(word1);//from w w w . ja va 2 s .c o m threadBatch.add(word2); threadBatch.add(word3); WordListImporterImpl.BatchWordImportTask batchWordImportTask = wordListImporterImpl.new BatchWordImportTask( threadBatch); 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); try { batchWordImportTask.call(); } catch (Exception e) { fail(e.getMessage()); } rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl); assertEquals(3, rowCountFromObject.intValue()); verify(wordDaoMock, times(1)).insertBatch(anyListOf(Word.class)); }
From source file:com.ciphertool.sentencebuilder.etl.importers.WordListImporterImplTest.java
@Test public void testBatchWordImportTask_LeftoversFromBatch() { WordListImporterImpl wordListImporterImpl = new WordListImporterImpl(); 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> threadBatch = new ArrayList<Word>(); threadBatch.add(word1);/*from w w w . j a v a 2 s .c o m*/ threadBatch.add(word2); threadBatch.add(word3); WordListImporterImpl.BatchWordImportTask batchWordImportTask = wordListImporterImpl.new BatchWordImportTask( threadBatch); 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 = 2; int concurrencyBatchSizeToSet = 3; wordListImporterImpl.setWordDao(wordDaoMock); wordListImporterImpl.setPersistenceBatchSize(persistenceBatchSizeToSet); wordListImporterImpl.setConcurrencyBatchSize(concurrencyBatchSizeToSet); try { batchWordImportTask.call(); } catch (Exception e) { fail(e.getMessage()); } rowCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowCountField, wordListImporterImpl); assertEquals(3, rowCountFromObject.intValue()); verify(wordDaoMock, times(2)).insertBatch(anyListOf(Word.class)); }
From source file:com.ciphertool.sentencebuilder.etl.importers.FrequencyListImporterImplTest.java
@Test public void testImportNullWord() { FrequencyListImporterImpl frequencyListImporterImpl = new FrequencyListImporterImpl(); Field rowUpdateCountField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "rowUpdateCount"); ReflectionUtils.makeAccessible(rowUpdateCountField); AtomicInteger rowUpdateCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowUpdateCountField, frequencyListImporterImpl);/*from ww w .j a va 2s. co m*/ assertEquals(0, rowUpdateCountFromObject.intValue()); Field rowInsertCountField = ReflectionUtils.findField(FrequencyListImporterImpl.class, "rowInsertCount"); ReflectionUtils.makeAccessible(rowInsertCountField); AtomicInteger rowInsertCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowInsertCountField, frequencyListImporterImpl); assertEquals(0, rowInsertCountFromObject.intValue()); WordDao wordDaoMock = mock(WordDao.class); when(wordDaoMock.insertBatch(anyListOf(Word.class))).thenReturn(true); when(wordDaoMock.updateBatch(anyListOf(Word.class))).thenReturn(true); frequencyListImporterImpl.setWordDao(wordDaoMock); frequencyListImporterImpl.setPersistenceBatchSize(2); List<Word> insertBatch = new ArrayList<Word>(); List<Word> updateBatch = new ArrayList<Word>(); frequencyListImporterImpl.importWord(null, insertBatch, updateBatch); rowUpdateCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowUpdateCountField, frequencyListImporterImpl); rowInsertCountFromObject = (AtomicInteger) ReflectionUtils.getField(rowInsertCountField, frequencyListImporterImpl); assertTrue(updateBatch.isEmpty()); assertTrue(insertBatch.isEmpty()); assertEquals(0, rowUpdateCountFromObject.intValue()); assertEquals(0, rowInsertCountFromObject.intValue()); verifyZeroInteractions(wordDaoMock); }
From source file:com.glaf.core.util.ReflectUtils.java
public static Object getFieldValue(Object object, String fieldName) { try {/*from w w w. j a va2 s . c o m*/ Field field = ReflectionUtils.findField(object.getClass(), fieldName); if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } return ReflectionUtils.getField(field, object); } catch (Exception ex) { try { return BeanUtils.getProperty(object, fieldName); } catch (Exception e) { } } return null; }
From source file:com.ciphertool.genetics.algorithms.MultigenerationalGeneticAlgorithmTest.java
@Test public void testProceedWithNextGeneration() { MultigenerationalGeneticAlgorithm multigenerationalGeneticAlgorithm = new MultigenerationalGeneticAlgorithm(); int initialPopulationSize = 100; int populationSize = 100; int index = 0; double survivalRate = 0.9; double mutationRate = 0.1; double crossoverRate = 0.1; Population populationMock = mock(Population.class); List<Chromosome> individuals = new ArrayList<Chromosome>(); for (int i = 0; i < initialPopulationSize; i++) { individuals.add(new MockKeylessChromosome()); }/*ww w. j a v a 2s. com*/ when(populationMock.selectIndex()).thenReturn(index); when(populationMock.getIndividuals()).thenReturn(individuals); when(populationMock.size()).thenReturn(initialPopulationSize); when(populationMock.selectIndex()).thenReturn(0); multigenerationalGeneticAlgorithm.setPopulation(populationMock); GeneticAlgorithmStrategy strategyToSet = new GeneticAlgorithmStrategy(); strategyToSet.setPopulationSize(populationSize); strategyToSet.setSurvivalRate(survivalRate); strategyToSet.setMutationRate(mutationRate); strategyToSet.setCrossoverRate(crossoverRate); Field strategyField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "strategy"); ReflectionUtils.makeAccessible(strategyField); ReflectionUtils.setField(strategyField, multigenerationalGeneticAlgorithm, strategyToSet); SelectionAlgorithm selectionAlgorithmMock = mock(SelectionAlgorithm.class); Field selectionAlgorithmField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "selectionAlgorithm"); ReflectionUtils.makeAccessible(selectionAlgorithmField); ReflectionUtils.setField(selectionAlgorithmField, multigenerationalGeneticAlgorithm, selectionAlgorithmMock); Field generationCountField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "generationCount"); ReflectionUtils.makeAccessible(generationCountField); ReflectionUtils.setField(generationCountField, multigenerationalGeneticAlgorithm, 0); MutationAlgorithm mutationAlgorithmMock = mock(MutationAlgorithm.class); Field mutationAlgorithmField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "mutationAlgorithm"); ReflectionUtils.makeAccessible(mutationAlgorithmField); ReflectionUtils.setField(mutationAlgorithmField, multigenerationalGeneticAlgorithm, mutationAlgorithmMock); CrossoverAlgorithm crossoverAlgorithmMock = mock(CrossoverAlgorithm.class); Field crossoverAlgorithmField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "crossoverAlgorithm"); ReflectionUtils.makeAccessible(crossoverAlgorithmField); ReflectionUtils.setField(crossoverAlgorithmField, multigenerationalGeneticAlgorithm, crossoverAlgorithmMock); Chromosome chromosomeToReturn = new MockKeylessChromosome(); when(crossoverAlgorithmMock.crossover(any(Chromosome.class), any(Chromosome.class))) .thenReturn(Arrays.asList(chromosomeToReturn)); ExecutionStatistics executionStatistics = new ExecutionStatistics(); Field executionStatisticsField = ReflectionUtils.findField(MultigenerationalGeneticAlgorithm.class, "executionStatistics"); ReflectionUtils.makeAccessible(executionStatisticsField); ReflectionUtils.setField(executionStatisticsField, multigenerationalGeneticAlgorithm, executionStatistics); assertEquals(0, executionStatistics.getGenerationStatisticsList().size()); multigenerationalGeneticAlgorithm.proceedWithNextGeneration(); assertEquals(1, executionStatistics.getGenerationStatisticsList().size()); /* * The population size should be reduced by the number of parents used * during crossover. */ assertEquals(100, populationMock.size()); int generationCountFromObject = (int) ReflectionUtils.getField(generationCountField, multigenerationalGeneticAlgorithm); assertEquals(1, generationCountFromObject); verify(selectionAlgorithmMock, times(1)).select(same(populationMock), eq(populationSize), eq(survivalRate)); verifyNoMoreInteractions(selectionAlgorithmMock); verify(populationMock, times(30)).selectIndex(); verify(populationMock, times(30)).getIndividuals(); verify(populationMock, times(30)).makeIneligibleForReproduction(index); verify(populationMock, times(20)).addIndividualAsIneligible(any(Chromosome.class)); verify(populationMock, times(5)).size(); verify(populationMock, times(1)).increaseAge(); verify(populationMock, times(1)).resetEligibility(); verify(populationMock, times(1)).breed(populationSize); verify(populationMock, times(1)).evaluateFitness(any(GenerationStatistics.class)); verifyNoMoreInteractions(populationMock); verify(mutationAlgorithmMock, times(10)).mutateChromosome(any(Chromosome.class)); verifyNoMoreInteractions(mutationAlgorithmMock); verify(crossoverAlgorithmMock, times(10)).crossover(any(Chromosome.class), any(Chromosome.class)); verifyNoMoreInteractions(crossoverAlgorithmMock); }