1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.invoker;
20
21 import java.io.File;
22 import java.io.Reader;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.apache.maven.model.Scm;
29 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
31 import org.apache.maven.settings.Settings;
32 import org.codehaus.plexus.util.IOUtil;
33 import org.codehaus.plexus.util.ReaderFactory;
34
35
36
37
38
39
40 public class InterpolationTest
41 extends AbstractMojoTestCase
42 {
43
44 protected MavenProjectStub buildMavenProjectStub()
45 {
46 ExtendedMavenProjectStub project = new ExtendedMavenProjectStub();
47 project.setVersion( "1.0-SNAPSHOT" );
48 project.setArtifactId( "foo" );
49 project.setGroupId( "bar" );
50 Properties properties = new Properties();
51 properties.put( "fooOnProject", "barOnProject" );
52 project.setProperties( properties );
53 Scm scm = new Scm();
54 scm.setConnection( "http://blabla" );
55 project.setScm( scm );
56 return project;
57 }
58
59 public void testCompositeMap()
60 throws Exception
61 {
62
63 Properties properties = new Properties();
64 properties.put( "foo", "bar" );
65 properties.put( "version", "2.0-SNAPSHOT" );
66 CompositeMap compositeMap = new CompositeMap( buildMavenProjectStub(), (Map) properties );
67 assertEquals( "1.0-SNAPSHOT", compositeMap.get( "pom.version" ) );
68 assertEquals( "bar", compositeMap.get( "foo" ) );
69 assertEquals( "bar", compositeMap.get( "pom.groupId" ) );
70 assertEquals( "http://blabla", compositeMap.get( "pom.scm.connection" ) );
71 assertEquals( "barOnProject", compositeMap.get( "fooOnProject" ) );
72 }
73
74 public void testInterpolationGoalsFile()
75 throws Exception
76 {
77 InvokerMojo invokerMojo = new InvokerMojo();
78 setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
79 setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
80 setVariableValueToObject( invokerMojo, "settings", new Settings() );
81 Properties properties = new Properties();
82 properties.put( "cleanProps", "clean" );
83 properties.put( "version", "2.0-SNAPSHOT" );
84 setVariableValueToObject( invokerMojo, "interpolationsProperties", properties );
85 String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
86 + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
87 List<String> goals = invokerMojo.getGoals( new File( dirPath ) );
88 assertEquals( goals.toString(), 2, goals.size() );
89 assertEquals( "clean", goals.get( 0 ) );
90 assertEquals( "bar:foo:1.0-SNAPSHOT:mygoal", goals.get( 1 ) );
91 }
92
93 public void testPomInterpolation()
94 throws Exception
95 {
96 Reader reader = null;
97 File interpolatedPomFile = null;
98 try
99 {
100 InvokerMojo invokerMojo = new InvokerMojo();
101 setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
102 setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
103 setVariableValueToObject( invokerMojo, "settings", new Settings() );
104 Properties properties = new Properties();
105 properties.put( "foo", "bar" );
106 properties.put( "version", "2.0-SNAPSHOT" );
107 setVariableValueToObject( invokerMojo, "interpolationsProperties", properties );
108 String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
109 + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
110
111 interpolatedPomFile = new File( getBasedir(), "target/interpolated-pom.xml" );
112 invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
113 reader = ReaderFactory.newXmlReader( interpolatedPomFile );
114 String content = IOUtil.toString( reader );
115 assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
116 reader.close();
117
118 invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
119 reader = ReaderFactory.newXmlReader( interpolatedPomFile );
120 content = IOUtil.toString( reader );
121 assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
122 reader.close();
123 }
124 finally
125 {
126 IOUtil.close( reader );
127 }
128 }
129
130 public void testProfilesFromFile()
131 throws Exception
132 {
133 InvokerMojo invokerMojo = new InvokerMojo();
134 setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
135 setVariableValueToObject( invokerMojo, "profilesFile", "profiles.txt" );
136 setVariableValueToObject( invokerMojo, "settings", new Settings() );
137 String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
138 + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
139 List<String> profiles = invokerMojo.getProfiles( new File( dirPath ) );
140 assertEquals( 2, profiles.size() );
141 assertTrue( profiles.contains( "foo" ) );
142 }
143
144 public void testEmptyProfilesFromFile()
145 throws Exception
146 {
147
148 InvokerMojo invokerMojo = new InvokerMojo();
149 setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
150 setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( new String[] { "zloug" } ) );
151 setVariableValueToObject( invokerMojo, "profilesFile", "emptyProfiles.txt" );
152 setVariableValueToObject( invokerMojo, "settings", new Settings() );
153 String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
154 + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
155 List<String> profiles = invokerMojo.getProfiles( new File( dirPath ) );
156 assertFalse( profiles.contains( "zloug" ) );
157 assertEquals( 0, profiles.size() );
158
159 }
160
161 public void testProfilesWithNoFile()
162 throws Exception
163 {
164
165 InvokerMojo invokerMojo = new InvokerMojo();
166 setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( new String[] { "zloug" } ) );
167 setVariableValueToObject( invokerMojo, "profilesFile", "zorglubProfiles.txt" );
168 setVariableValueToObject( invokerMojo, "settings", new Settings() );
169 String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
170 + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
171 List<String> profiles = invokerMojo.getProfiles( new File( dirPath ) );
172 assertTrue( profiles.contains( "zloug" ) );
173 assertEquals( 1, profiles.size() );
174
175 }
176 }