001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper;
016
017import org.basepom.mojo.propertyhelper.macros.MacroType;
018
019import java.io.File;
020import java.security.SecureRandom;
021import java.util.Collections;
022import java.util.Map;
023import java.util.Properties;
024import java.util.Random;
025
026import org.apache.maven.project.MavenProject;
027import org.apache.maven.settings.Settings;
028
029/**
030 * All accessible values from the Maven build system for a Field.
031 */
032public interface FieldContext {
033
034    /** Returns a fixed instance for testing. Do not use outside test code. */
035    static FieldContext forTesting() {
036        return forTesting(new SecureRandom());
037    }
038
039    /** Returns a fixed instance for testing. The Random instance can be set to simulate reproducible builds. */
040    static FieldContext forTesting(Random random) {
041        return new FieldContext() {
042
043            @Override
044            public Map<String, MacroType> getMacros() {
045                return Collections.emptyMap();
046            }
047
048            @Override
049            public Properties getProjectProperties() {
050                return new Properties();
051            }
052
053            @Override
054            public MavenProject getProject() {
055                return new MavenProject();
056            }
057
058            @Override
059            public Settings getSettings() {
060                return new Settings();
061            }
062
063            @Override
064            public File getBasedir() {
065                throw new UnsupportedOperationException();
066            }
067
068            @Override
069            public InterpolatorFactory getInterpolatorFactory() {
070                return InterpolatorFactory.forTesting();
071            }
072
073            @Override
074            public TransformerRegistry getTransformerRegistry() {
075                return TransformerRegistry.INSTANCE;
076            }
077
078            @Override
079            public Random getRandom() {
080                return random;
081            }
082        };
083    }
084
085    /**
086     * Returns a map with all known macros. Key value is the macro hint as given by the plexus component annotation.
087     * @return A map with all known macros.
088     */
089    Map<String, MacroType> getMacros();
090
091    /**
092     * Return the maven project properties.
093     * @return A properties object for the project properties.
094     */
095    Properties getProjectProperties();
096
097    /**
098     * Returns a reference to the {@link MavenProject}.
099     * @return A {@link MavenProject} object
100     */
101    MavenProject getProject();
102
103    /**
104     * Returns the current maven {@link Settings} object
105     * @return A {@link Settings} object
106     */
107    Settings getSettings();
108
109    /**
110     * Returns the base dir for this maven build execution.
111     *
112     * @return A {@link File} object.
113     */
114    File getBasedir();
115
116    /**
117     * Returns the {@link InterpolatorFactory} that can interpolate "late resolution" properties.
118     * @return An {@link InterpolatorFactory} reference.
119     */
120    InterpolatorFactory getInterpolatorFactory();
121
122    /**
123     * Returns a reference to the {@link TransformerRegistry}.
124     * @return A {@link TransformerRegistry} object.
125     */
126    TransformerRegistry getTransformerRegistry();
127
128    /**
129     * Returns a {@link java.security.SecureRandom} instance for generating random values.
130     */
131    Random getRandom();
132}