freemarker.ext.beans.StringModel Java Examples

The following examples show how to use freemarker.ext.beans.StringModel. 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: UpdateDocExtensionsListMojo.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException {
    if (arguments.size() != 1) {
        throw new TemplateModelException("Expected one argument for getDocLink(); found " + arguments.size());
    }
    ArtifactModel<?> model = (ArtifactModel<?>) DeepUnwrap.unwrap((StringModel) arguments.get(0));
    final String artifactIdBase = CqUtils.getArtifactIdBase(model);
    final String extensionPageName = artifactIdBase + ".adoc";
    final Path extensionPagePath = extensionsDocPath.resolve(extensionPageName);
    if (!Files.exists(extensionPagePath)) {
        throw new IllegalStateException(
                "File " + extensionPagePath + " must exist to be able to refer to it from " + extensionListPath
                        + ".\nYou may need to add\n\n    org.apache.camel.quarkus:camel-quarkus-maven-plugin:update-extension-doc-page\n\nmojo in "
                        + artifactIdBase + " runtime module");
    }
    return "xref:extensions/" + extensionPageName;
}
 
Example #2
Source File: ProjectModelToApplicationIndexMethod.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (ProjectModel)");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    if (stringModelArg == null)
    {
        throw new IllegalArgumentException("FreeMarker Method " + NAME + " called with null project model");
    }
    ProjectModel projectModel = (ProjectModel) stringModelArg.getWrappedObject();
    ApplicationReportIndexModel index = service.getApplicationReportIndexForProjectModel(projectModel);
    if (index == null)
    {
        LOG.warning("Could not find an application index for project model: " + projectModel.getName() + " (Vertex ID: "
                    + projectModel.getElement().id() + ")");
    }
    ExecutionStatistics.get().end(NAME);
    return index;
}
 
Example #3
Source File: GetTechReportPunchCardStatsMethod.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);

    // Function arguments
    ProjectModel projectModel = null;

    // The project. May be null -> all input applications.
    if (arguments.size() >= 1) {
        StringModel projectArg = (StringModel) arguments.get(0);
        projectModel = (ProjectModel) projectArg.getWrappedObject();
    }

    MatrixAndAggregated result = computeProjectAndTagsMatrix(this.graphContext, projectModel);

    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #4
Source File: GetClassificationForFile.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (" + FileModel.class.getSimpleName() + ")");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    FileModel fileModel = (FileModel) stringModelArg.getWrappedObject();
    
    ClassificationService classificationService = new ClassificationService(context);
    Iterable<ClassificationModel> result = classificationService.getClassifications(fileModel);
    
    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #5
Source File: GetNumberOfLibrariesMethod.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);

    // Function arguments
    ProjectModel projectModel = null;

    // The project. May *not* be null. All input applications should be passed into this method individually
    if (arguments.size() >= 1) {
        StringModel projectArg = (StringModel) arguments.get(0);
        projectModel = (ProjectModel) projectArg.getWrappedObject();
    }

    if (projectModel == null) {
        String errorMessage = "GetNumberOfLibrariesMethod: No project present to count libraries";
        throw new WindupException(errorMessage);
    }

    Integer result = countLibrariesInModel(this.graphContext, projectModel);

    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #6
Source File: SortTechUsageStatsMethod.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);

    // Function arguments
    if (arguments.size() > 1)
        throw new TemplateModelException("Expected 0 or 1 argument - project.");

    // The project. May be null -> count from all applications.
    ProjectModel application = null;
    if (arguments.size() == 1)
    {
        StringModel projectArg = (StringModel) arguments.get(0);
        if (null != projectArg)
            application = (ProjectModel) projectArg.getWrappedObject();
    }

    TechReportService.TechStatsMatrix matrix = techReportService.getTechStatsMap(application);

    ExecutionStatistics.get().end(NAME);
    return matrix;
}
 
Example #7
Source File: IsFileADirectoryMethod.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);

    // Function arguments
    FileModel fileModel = null;

    // The file may *not* be null.
    if (arguments.size() >= 1) {
        StringModel fileArg = (StringModel) arguments.get(0);
        fileModel = (FileModel) fileArg.getWrappedObject();
    }

    if (fileModel == null) {
        String errorMessage = "IsFileADirectoryMethod: No file present to check";
        throw new WindupException(errorMessage);
    }

    Boolean result = fileModel.isDirectory();

    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #8
Source File: FormatRule.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (Rule)");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    Rule rule = (Rule) stringModelArg.getWrappedObject();

    String result = RuleUtils.ruleToRuleContentsString(rule, 0);

    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #9
Source File: RelativeTime.java    From kaif with Apache License 2.0 6 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException {
  if (arguments.size() < 1) {
    throw new TemplateModelException("require an Instant as argument");
  }
  if (arguments.size() > 2) {
    throw new TemplateModelException("too many arguments");
  }
  PrettyTime prettyTime = new PrettyTime(Environment.getCurrentEnvironment().getLocale());
  // only support day unit now
  if (arguments.size() == 2 && arguments.get(1).toString().equals("Day")) {
    List<TimeUnit> units = prettyTime.getUnits()
        .stream()
        .filter(timeUnit -> timeUnit.getMillisPerUnit() > new Day().getMillisPerUnit())
        .collect(Collectors.toList());
    units.forEach(prettyTime::removeUnit);
  }

  StringModel stringModel = (StringModel) arguments.get(0);
  Instant instant = (Instant) stringModel.getAdaptedObject(Instant.class);
  return prettyTime.format(Date.from(instant));
}
 
Example #10
Source File: ProjectModelToSha1.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
	Object result = null;
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (ProjectModel)");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    if (stringModelArg == null)
    {
        throw new IllegalArgumentException("FreeMarker Method " + NAME + " called with null project model");
    }
    ProjectModel projectModel = (ProjectModel) stringModelArg.getWrappedObject();
    if(projectModel.getRootFileModel() instanceof IdentifiedArchiveModel) {
    	result = projectModel.getRootFileModel().getSHA1Hash();
    }
    ExecutionStatistics.get().end(NAME);

    return result;
}
 
Example #11
Source File: GetRuleExecutionResults.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (AbstractRuleProvider)");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    AbstractRuleProvider ruleProvider = (AbstractRuleProvider) stringModelArg.getWrappedObject();
    List<RuleExecutionInformation> result = RuleExecutionResultsListener.instance(this.event).getRuleExecutionInformation(ruleProvider);
    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #12
Source File: IsTagUnderTagMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);

    // Function arguments
    if (arguments.size() < 2) {
        throw new TemplateModelException("Expected 2 or 3 arguments - a subsector tag, a row tag and optionally, a project.");
    }

    StringModel tag1Arg = (StringModel) arguments.get(0);
    TagModel subTag = (TagModel) tag1Arg.getWrappedObject();

    StringModel tag2Arg = (StringModel) arguments.get(1);
    TagModel maybeSuperTag = (TagModel) tag2Arg.getWrappedObject();

    // The project. May be null -> count from all applications.
    boolean countIfSame = false;
    if (arguments.size() >= 3)
    {
        BooleanModel countRootArg = (BooleanModel) arguments.get(2);
        if (null != countRootArg)
            countIfSame = countRootArg.getAsBoolean();
    }

    boolean isUnder = new TagGraphService(this.graphContext).isTagUnderTag(subTag, maybeSuperTag, countIfSame);

    ExecutionStatistics.get().end(NAME);
    return isUnder;
}
 
Example #13
Source File: AbstractGetPrettyPathForFile.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    try
    {
        if (arguments.size() != 1)
        {
            throw new TemplateModelException("Error, method expects one argument (FileModel)");
        }
        StringModel stringModelArg = (StringModel) arguments.get(0);
        FileModel fileModel = (FileModel) stringModelArg.getWrappedObject();
        if (fileModel instanceof JavaClassFileModel)
        {
            return getPath((JavaClassFileModel) fileModel);
        }
        else if (fileModel instanceof ReportResourceFileModel)
        {
            return getPath((ReportResourceFileModel) fileModel);
        }
        else if (fileModel instanceof JavaSourceFileModel)
        {
            return getPath((JavaSourceFileModel) fileModel);
        }
        else
        {
            return getPath(fileModel);
        }
    }
    finally
    {
        ExecutionStatistics.get().end(NAME);
    }
}
 
Example #14
Source File: GetEffortForFile.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (FileModel)");
    }
    StringModel fileModelArg = (StringModel) arguments.get(0);
    FileModel fileModel = (FileModel) fileModelArg.getWrappedObject();

    Object result = classificationService.getMigrationEffortPoints(fileModel) + inlineHintService.getMigrationEffortPoints(fileModel);
    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #15
Source File: FreemarkerTemplateEngine.java    From enkan with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public Object createFunction(Function<List, Object> func) {
    return (TemplateMethodModelEx) arguments ->
            func.apply((List)arguments.stream().map(arg -> {
                if (arg instanceof BeanModel) {
                    return ((StringModel) arg).getWrappedObject();
                } else {
                    return arg;
                }
            }).collect(Collectors.toList()));
}
 
Example #16
Source File: ProjectModelToOrganizationsMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    Object result = null;

    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (ProjectModel)");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    if (stringModelArg == null)
    {
        throw new IllegalArgumentException("FreeMarker Method " + NAME + " called with null project model");
    }
    ProjectModel projectModel = (ProjectModel) stringModelArg.getWrappedObject();

    if (projectModel.getRootFileModel() instanceof ArchiveModel)
    {
        result = ((ArchiveModel) projectModel.getRootFileModel()).getOrganizationModels();
    }

    ExecutionStatistics.get().end(NAME);
    
    if(result == null)
    {
        result = new ArrayList<>();
    }
    return result;
}
 
Example #17
Source File: Sha1HexMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    if (arguments.size() != 1)
    {
        throw new TemplateModelException(
                    "Error, method expects one argument (FileModel)");
    }
    StringModel stringModel = (StringModel) arguments.get(0);
    FileModel fileModel = (FileModel) stringModel.getWrappedObject();
    return !fileModel.isDirectory() ? fileModel.getSHA1Hash() : DigestUtils.sha1Hex(fileModel.getFileName());
}
 
Example #18
Source File: IsReportableFile.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    try
    {
        if (arguments.size() != 3)
        {
            throw new TemplateModelException("Error, method expects one argument (FileModel, includeTags:Set<String>, excludeTags:Set<String>)");
        }
        StringModel stringModelArg = (StringModel) arguments.get(0);
        FileModel fileModel = (FileModel) stringModelArg.getWrappedObject();

        Set<String> includeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(1));
        Set<String> excludeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(2));

        SourceReportModel result = sourceReportService.getSourceReportForFileModel(fileModel);

        if (result == null)
            return false;

        return TagUtil.hasHintsOrClassificationsWithRelevantTags(result.getSourceFileModel(), includeTags, excludeTags);
    }
    finally
    {
        ExecutionStatistics.get().end(NAME);
    }
}
 
Example #19
Source File: FileModelToSourceReportModelMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (FileModel)");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    FileModel fileModel = (FileModel) stringModelArg.getWrappedObject();
    SourceReportModel result = sourceReportService.getSourceReportForFileModel(fileModel);
    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #20
Source File: IsRulePhaseMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    if (arguments.size() != 1)
    {
        throw new TemplateModelException(
                    "Error, method expects one argument (Object)");
    }
    StringModel stringModel = (StringModel) arguments.get(0);
    Object object = stringModel.getWrappedObject();
    return object instanceof RulePhase;
}
 
Example #21
Source File: GetTagsFromFileClassificationsAndHints.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
        throw new TemplateModelException("Error, method expects one argument (" + FileModel.class.getSimpleName() + ")");

    StringModel stringModelArg = (StringModel) arguments.get(0);
    FileModel fileModel = (FileModel) stringModelArg.getWrappedObject();
    Set<String> tags = this.findTagsFromFileClassificationsAndHints(fileModel);
    ExecutionStatistics.get().end(NAME);
    return tags;
}
 
Example #22
Source File: GetProblemSummariesMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException
{
    if (arguments.isEmpty())
        throw new TemplateModelException("Method " + getMethodName() + " requires the following parameters (GraphRewrite event, ProjectModel project, Set<String> includeTags, Set<String> excludeTags)");

    // Gets the graph rewrite event
    final GraphRewrite event = (GraphRewrite)((StringModel)arguments.get(0)).getWrappedObject();

    // Get the project if one was passed in
    final ProjectModel projectModel;

    StringModel projectModelArg = (StringModel) arguments.get(1);
    if (projectModelArg == null)
        projectModel = null;
    else
        projectModel = (ProjectModel) projectModelArg.getWrappedObject();

    Set<String> includeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(2));
    Set<String> excludeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(3));

    Set<ProjectModel> projectModels = getProjects(projectModel);
    Map<IssueCategoryModel, List<ProblemSummary>> problemSummariesOriginal
            = ProblemSummaryService.getProblemSummaries(event.getGraphContext(), projectModels, includeTags, excludeTags);

    // Convert the keys to String to make Freemarker happy
    Comparator<IssueCategoryModel> severityComparator = new IssueCategoryModel.IssueSummaryPriorityComparator();
    Map<IssueCategoryModel, List<ProblemSummary>> problemSummaries = new TreeMap<>(severityComparator);
    problemSummaries.putAll(problemSummariesOriginal);

    Map<String, List<ProblemSummary>> primarySummariesByString = new LinkedHashMap<>(problemSummariesOriginal.size());
    for (Map.Entry<IssueCategoryModel, List<ProblemSummary>> entry : problemSummaries.entrySet())
    {
        String severityString = entry.getKey() == null ? null : entry.getKey().getName();
        primarySummariesByString.put(severityString, entry.getValue());
    }

    return primarySummariesByString;
}
 
Example #23
Source File: GetArchiveSHA1ToPathsMapperMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() < 1)
    {
        throw new TemplateModelException("Error, method expects at least one argument (" + ProjectModel.class.getSimpleName() + ")");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    ProjectModelTraversal traversal = (ProjectModelTraversal) stringModelArg.getWrappedObject();
    ArchiveSHA1ToFilePathMapper mapper = new ArchiveSHA1ToFilePathMapper(traversal);

    ExecutionStatistics.get().end(NAME);
    return mapper;
}
 
Example #24
Source File: GetTechnologyTagsForProject.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (" + ProjectModelTraversal.class.getSimpleName() + ")");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    ProjectModelTraversal projectTraversal = (ProjectModelTraversal) stringModelArg.getWrappedObject();
    Iterable<TechnologyTagModel> result = new TechnologyTagService(this.context).findTechnologyTagsForProject(projectTraversal);
    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #25
Source File: GetProjectTraversalMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() < 1)
    {
        throw new TemplateModelException("Error, method expects at least one argument (" + ProjectModel.class.getSimpleName() + ")");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    ProjectModel projectModel = (ProjectModel) stringModelArg.getWrappedObject();

    String traversalStrategyString = ALL;
    if (arguments.size() > 1)
        traversalStrategyString = ((SimpleScalar)arguments.get(1)).getAsString();

    TraversalStrategy traversalStrategy;
    if (traversalStrategyString == null)
        traversalStrategyString = ALL;
    switch (traversalStrategyString)
    {
        case ONLY_ONCE: traversalStrategy = new OnlyOnceTraversalStrategy(); break;
        case SHARED:    traversalStrategy = new SharedLibsTraversalStrategy(); break;
        default:
            Logger.getLogger(GetProjectTraversalMethod.class.getName()).warning("Unknown strategy name: " + traversalStrategyString);
        case ALL: traversalStrategy = new AllTraversalStrategy(); break;
    }

    ProjectModelTraversal traversal = new ProjectModelTraversal(projectModel, traversalStrategy);
    ExecutionStatistics.get().end(NAME);
    return traversal;
}
 
Example #26
Source File: GetTechnologyTagsForFile.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (" + FileModel.class.getSimpleName() + ")");
    }
    StringModel stringModelArg = (StringModel) arguments.get(0);
    FileModel fileModel = (FileModel) stringModelArg.getWrappedObject();
    Iterable<TechnologyTagModel> result = new TechnologyTagService(this.context).findTechnologyTagsForFile(fileModel);
    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example #27
Source File: TestWBFreeMarkerModuleDirective.java    From cms with Apache License 2.0 5 votes vote down vote up
@Test
public void test_execute_noPageModule()
{
	Environment envMock = PowerMock.createMock(Environment.class);
	TemplateModel[] loopVars = null;
	TemplateDirectiveBody directiveBodyMock = null;
	
	Map params = new HashMap();
	String name = "testXYZ";
	StringModel nameModel = new StringModel(name, new DefaultObjectWrapper() );
	params.put("name", nameModel);

	try
	{

		WPBPageModule pageModuleMock = PowerMock.createMock(WPBPageModule.class);		
		WPBPageModulesCache pageModuleCacheMock = PowerMock.createMock(WPBPageModulesCache.class);
		EasyMock.expect(pageModuleCacheMock.getByExternalKey(name)).andReturn(null);
		
		EasyMock.expect(cacheInstancesMock.getPageModuleCache()).andReturn(pageModuleCacheMock);

		EasyMock.replay(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		
		FreeMarkerModuleDirective templateDirective = new FreeMarkerModuleDirective();
		Whitebox.setInternalState(templateDirective, "templateEngine",templateEngineMock);
		Whitebox.setInternalState(templateDirective, "cacheInstances",cacheInstancesMock);
		PowerMock.suppressMethod(FreeMarkerModuleDirective.class, "copyParams");
		templateDirective.execute(envMock, params, loopVars, directiveBodyMock);
	
		assertTrue(false);
		
	} catch (Exception e)
	{
		assertTrue(e instanceof TemplateModelException);
	}
}
 
Example #28
Source File: TestWBFreeMarkerModuleDirective.java    From cms with Apache License 2.0 5 votes vote down vote up
@Test
public void test_execute_catch_exception()
{
	Environment envMock = PowerMock.createMock(Environment.class);
	TemplateModel[] loopVars = null;
	TemplateDirectiveBody directiveBodyMock = null;
	
	Map params = new HashMap();
	String name = "testXYZ";
	StringModel nameModel = new StringModel(name, new DefaultObjectWrapper() );
	params.put("name", nameModel);

	try
	{
		
		WPBPageModule pageModuleMock = PowerMock.createMock(WPBPageModule.class);		
		WPBPageModulesCache pageModuleCacheMock = PowerMock.createMock(WPBPageModulesCache.class);
		EasyMock.expect(pageModuleCacheMock.getByExternalKey(name)).andThrow(new WPBIOException(""));
		
		EasyMock.expect(cacheInstancesMock.getPageModuleCache()).andReturn(pageModuleCacheMock);

		EasyMock.replay(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		
		FreeMarkerModuleDirective templateDirective = new FreeMarkerModuleDirective();
		Whitebox.setInternalState(templateDirective, "templateEngine",templateEngineMock);
		Whitebox.setInternalState(templateDirective, "cacheInstances",cacheInstancesMock);
		PowerMock.suppressMethod(FreeMarkerModuleDirective.class, "copyParams");
		templateDirective.execute(envMock, params, loopVars, directiveBodyMock);
		
		assertTrue(false);		
	} catch (Exception e)
	{
		assertTrue(e instanceof TemplateModelException);
	}
}
 
Example #29
Source File: SpringBeansTemplateHashModelEx.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Override
public KeyValuePairIterator keyValuePairIterator() throws TemplateModelException {
	return new KeyValuePairIterator() {
		private int currentIdx = 0;

		@Override
		public KeyValuePair next() throws TemplateModelException {
			if (!hasNext()) {
				throw new NoSuchElementException();
			}
			KeyValuePair pair = new KeyValuePair() {

				@Override
				public TemplateModel getValue() throws TemplateModelException {
					String name = applicationContext.getBeanDefinitionNames()[currentIdx];
					return new LazySpringBeanAccessModel(applicationContext, beansWrapper, name);
				}

				@Override
				public TemplateModel getKey() throws TemplateModelException {
					return new StringModel(applicationContext.getBeanDefinitionNames()[currentIdx], beansWrapper);
				}
			};
			currentIdx++;
			return pair;
		}

		@Override
		public boolean hasNext() throws TemplateModelException {
			return currentIdx < applicationContext.getBeanDefinitionCount();
		}
	};
}
 
Example #30
Source File: HeadInfoTag.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String extractAttribute() throws JspException {
if (null == this.getVar()) {
	return null;
}
String objectToString = null;
try {
	Object object = this.pageContext.getAttribute(this.getVar());
	if (null == object) {
		Environment environment = Environment.getCurrentEnvironment();
		if (null != environment) {
			Object wrapper = environment.getVariable(this.getVar());
			if (null != wrapper) {
				if (wrapper instanceof StringModel) {
					object = ((StringModel) wrapper).getWrappedObject();
				} else {
					object = wrapper;
				}
			}
		}
	}
	if (null != object) {
		objectToString = object.toString();
	}
      } catch (Throwable t) {
      	_logger.error("error extracting freemarker attribute", t);
          throw new JspException("Error extracting freemarker attribute", t);
      }
return objectToString;
  }