Example usage for org.apache.commons.lang3.reflect FieldUtils writeField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils writeField

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils writeField.

Prototype

public static void writeField(final Object target, final String fieldName, final Object value,
        final boolean forceAccess) throws IllegalAccessException 

Source Link

Document

Writes a Field .

Usage

From source file:org.treeingwalker.LessCompilerTest.java

@Test
public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourceModified() throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "compiler", compiler, true);
    FieldUtils.writeField(lessCompiler, "out", out, true);
    when(cx.newObject(scope)).thenReturn(compileScope);

    when(outputFile.exists()).thenReturn(true);
    when(outputFile.lastModified()).thenReturn(1l);

    when(lessSource.getLastModifiedIncludingImports()).thenReturn(2l);
    when(lessSource.getNormalizedContent()).thenReturn(less);

    when(out.toString()).thenReturn(css);

    mockStatic(FileUtils.class);

    lessCompiler.compile(lessSource, outputFile, false);

    verify(outputFile).exists();//from   w ww  .  j  av  a  2  s .com
    verify(outputFile).lastModified();

    verify(lessSource).getLastModifiedIncludingImports();
    verify(lessSource).getNormalizedContent();

    verify(compiler).call(cx, compileScope, null, new Object[] {});

    verifyStatic();
    FileUtils.writeStringToFile(outputFile, css, (String) null);
}

From source file:org.treeingwalker.LessCompilerTest.java

@Test
public void testCompileLessSourceToFileWithForceFalseAndOutputExistsAndLessSourceNotModified()
        throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "compiler", compiler, true);
    FieldUtils.writeField(lessCompiler, "out", out, true);
    when(cx.newObject(scope)).thenReturn(compileScope);

    when(outputFile.exists()).thenReturn(true);
    when(outputFile.lastModified()).thenReturn(2l);

    when(lessSource.getLastModifiedIncludingImports()).thenReturn(1l);

    lessCompiler.compile(lessSource, outputFile, false);

    verify(outputFile).exists();/*from ww w .  j a  va 2 s . co m*/
    verify(outputFile).lastModified();

    verify(lessSource).getLastModifiedIncludingImports();
}

From source file:org.treeingwalker.LessCompilerTest.java

@Test(expected = LessException.class)
public void testCompileThrowsLessExceptionWhenCompilationFails() throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "compiler", compiler, true);
    FieldUtils.writeField(lessCompiler, "out", out, true);
    when(cx.newObject(scope)).thenReturn(compileScope);

    JavaScriptException javaScriptException = new JavaScriptException(null, null, 0);
    when(compiler.call(cx, compileScope, null, new Object[] {})).thenThrow(javaScriptException);

    assertEquals(css, lessCompiler.compile(less));
}

From source file:org.treeingwalker.LessCompilerTest.java

@Test
public void testCompress() throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    lessCompiler.setCompress(true);/*w w w. j  ava 2s .  c  o m*/
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "compiler", compiler, true);
    FieldUtils.writeField(lessCompiler, "out", out, true);
    when(cx.newObject(scope)).thenReturn(compileScope);

    when(out.toString()).thenReturn(css);

    assertEquals(css, lessCompiler.compile(less));

    verify(compiler).call(cx, compileScope, null, new Object[] {});
}

From source file:org.treeingwalker.LessCompilerTest.java

@Test
public void testEncoding() throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    lessCompiler.setEncoding("utf-8");
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "compiler", compiler, true);
    FieldUtils.writeField(lessCompiler, "out", out, true);
    when(cx.newObject(scope)).thenReturn(compileScope);

    whenNew(LessSource.class).withArguments(inputFile).thenReturn(lessSource);
    when(lessSource.getNormalizedContent()).thenReturn(less);

    when(out.toString("utf-8")).thenReturn(css);

    mockStatic(FileUtils.class);

    lessCompiler.compile(inputFile, outputFile);

    verify(compiler).call(cx, compileScope, null, new Object[] {});

    verifyStatic();// w ww  .ja va  2s.c o  m
    FileUtils.writeStringToFile(outputFile, css, "utf-8");
}

From source file:org.treeingwalker.LessSourceTest.java

@Test
public void testLastModifiedIncludingImportsWhenNoImportModifiedLater() throws Exception {
    mockFile(true, "content", "absolutePath");

    when(import1.getLastModifiedIncludingImports()).thenReturn(0l);
    when(import2.getLastModifiedIncludingImports()).thenReturn(0l);
    when(import3.getLastModifiedIncludingImports()).thenReturn(0l);

    lessSource = new LessSource(new FileResource(file));
    FieldUtils.writeField(lessSource, "imports", imports, true);

    assertEquals(1l, lessSource.getLastModifiedIncludingImports());
}

From source file:org.treeingwalker.LessSourceTest.java

@Test
public void testLastModifiedIncludingImportsWhenImportModifiedLater() throws Exception {
    mockFile(true, "content", "absolutePath");

    when(import1.getLastModifiedIncludingImports()).thenReturn(0l);
    when(import2.getLastModifiedIncludingImports()).thenReturn(2l);
    when(import3.getLastModifiedIncludingImports()).thenReturn(0l);

    lessSource = new LessSource(new FileResource(file));
    FieldUtils.writeField(lessSource, "imports", imports, true);

    assertEquals(2l, lessSource.getLastModifiedIncludingImports());
}

From source file:org.xwiki.rendering.internal.macro.DisplayMacroTest.java

@Test
public void testDisplayMacroWithRecursiveDisplay() throws Exception {
    final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);

    this.displayMacro.setDocumentAccessBridge(mockSetup.bridge);
    FieldUtils.writeField(this.displayMacro, "documentDisplayer", mockDocumentDisplayer, true);

    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);

    final DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("wiki:space.page");

    getMockery().checking(new Expectations() {
        {/*  w w w  .  j  a v  a 2  s . com*/
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page",
                    macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));

            allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
            will(returnValue(true));
            allowing(mockSetup.bridge).getDocument(with(any(DocumentReference.class)));
            will(returnValue(null));

            allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)),
                    with(any(DocumentDisplayerParameters.class)));
            will(new CustomAction("recursively call the include macro again") {
                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    try {
                        displayMacro.execute(parameters, null, macroContext);
                    } catch (Exception expected) {
                        if (expected.getMessage().contains("Found recursive display")) {
                            throw new ExpectedRecursiveInclusionException();
                        }
                    }
                    return true;
                }
            });
        }
    });

    try {
        this.displayMacro.execute(parameters, null, macroContext);
        Assert.fail("The display macro hasn't checked the recursive display");
    } catch (MacroExecutionException expected) {
        if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
            throw expected;
        }
    }
}

From source file:pl.datamatica.traccar.api.StopMoveDetectorTest.java

void initPositions(List<Position> positions) throws IllegalAccessException {
    long id = 1;/*from   ww w  .j a v a2 s.c o  m*/
    final Map<Long, Position> positionsMap = new HashMap<>(positions.size());
    for (Position position : positions) {
        position.setDevice(device);
        FieldUtils.writeField(position, "id", id++, true);
        positionsMap.put(position.getId(), position);
    }

    when(entityManager.find(eq(Position.class), any())).thenAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Long id = invocationOnMock.getArgumentAt(1, Long.class);
            return positionsMap.get(id);
        }
    });
    this.positions = positions;
}

From source file:ubic.gemma.core.loader.expression.arrayDesign.ArrayDesignSequenceAlignmentServiceImpl.java

@Override
public Collection<BlatResult> processArrayDesign(ArrayDesign ad, Taxon taxon,
        Collection<BlatResult> rawBlatResults) {

    ArrayDesignSequenceAlignmentServiceImpl.log.info("Looking for old results to remove...");

    ad = arrayDesignService.thaw(ad);//from  w  ww  .  ja  va2 s .  c om

    arrayDesignService.deleteAlignmentData(ad);
    // Blat file processing can only be run on one taxon at a time
    taxon = this.validateTaxaForBlatFile(ad, taxon);

    Collection<BioSequence> sequencesToBlat = ArrayDesignSequenceAlignmentServiceImpl.getSequences(ad);
    sequencesToBlat = bioSequenceService.thaw(sequencesToBlat);

    // if the blat results were loaded from a file, we have to replace the
    // query sequences with the actual ones
    // attached to the array design. We have to do this by name because the
    // sequence name is what the files contain.
    // Note that if there is ambiguity there will be problems!
    Map<String, BioSequence> seqMap = new HashMap<>();
    for (BioSequence bioSequence : sequencesToBlat) {
        seqMap.put(bioSequence.getName(), bioSequence);
    }

    ExternalDatabase searchedDatabase = ShellDelegatingBlat.getSearchedGenome(taxon);

    Collection<BlatResult> toSkip = new HashSet<>();
    for (BlatResult result : rawBlatResults) {

        /*
         * If the sequences don't have ids, replace them with the actual sequences associated with the array design.
         */
        if (result.getQuerySequence().getId() == null) {
            String querySeqName = result.getQuerySequence().getName();
            BioSequence actualSequence = seqMap.get(querySeqName);
            if (actualSequence == null) {
                ArrayDesignSequenceAlignmentServiceImpl.log
                        .debug("Array design does not contain a sequence with name " + querySeqName);
                toSkip.add(result);
                continue;
            }
            result.setQuerySequence(actualSequence);
        } else {
            result.getQuerySequence().setTaxon(taxon);
        }

        result.setSearchedDatabase(searchedDatabase);
        try {
            FieldUtils.writeField(result.getTargetChromosome(), "taxon", taxon, true);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        result.getTargetChromosome().getSequence().setTaxon(taxon);

    }

    if (toSkip.size() > 0) {
        ArrayDesignSequenceAlignmentServiceImpl.log.warn(
                toSkip.size() + " blat results were for sequences not on " + ad + "; they will be ignored.");
        rawBlatResults.removeAll(toSkip);
    }

    Map<BioSequence, Collection<BlatResult>> goldenPathAlignments = new HashMap<>();
    this.getGoldenPathAlignments(sequencesToBlat, taxon, goldenPathAlignments);
    for (BioSequence sequence : goldenPathAlignments.keySet()) {
        rawBlatResults.addAll(goldenPathAlignments.get(sequence));
    }

    Collection<BlatResult> results = this.persistBlatResults(rawBlatResults);

    arrayDesignReportService.generateArrayDesignReport(ad.getId());

    return results;
}