1 package org.apache.maven.plugin.doap;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.StringWriter;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.apache.maven.model.Contributor;
29 import org.apache.maven.model.Developer;
30 import org.apache.maven.model.License;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.plexus.PlexusTestCase;
33 import org.codehaus.plexus.i18n.I18N;
34 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
35 import org.codehaus.plexus.util.xml.XMLWriter;
36
37
38
39
40
41
42
43 public class DoapUtilTest
44 extends PlexusTestCase
45 {
46
47 protected void setUp()
48 throws Exception
49 {
50 super.setUp();
51 }
52
53
54 protected void tearDown()
55 throws Exception
56 {
57 super.tearDown();
58 }
59
60
61
62
63
64
65 public void testWriteElement()
66 throws Exception
67 {
68 StringWriter w = new StringWriter();
69 XMLWriter writer = new PrettyPrintXMLWriter( w );
70 DoapUtil.writeElement( writer, null, "name", "value" );
71 w.close();
72 assertEquals( w.toString(), "<name>value</name>" );
73
74 w = new StringWriter();
75 writer = new PrettyPrintXMLWriter( w );
76 try
77 {
78 DoapUtil.writeElement( writer, null, null, null );
79 assertTrue( "Null not catched", false );
80 }
81 catch ( IllegalArgumentException e )
82 {
83 assertTrue( "IllegalArgumentException catched", true );
84 }
85 finally
86 {
87 w.close();
88 }
89 }
90
91
92
93
94
95
96 public void testWriteRdfResourceElement()
97 throws Exception
98 {
99 StringWriter w = new StringWriter();
100 XMLWriter writer = new PrettyPrintXMLWriter( w );
101 DoapUtil.writeRdfResourceElement( writer, null, "name", "value" );
102 w.close();
103 assertEquals( w.toString(), "<name " + DoapUtil.RDF_RESOURCE + "=\"value\"/>" );
104
105 w = new StringWriter();
106 writer = new PrettyPrintXMLWriter( w );
107 try
108 {
109 DoapUtil.writeRdfResourceElement( writer, null, null, null );
110 assertTrue( "Null not catched", false );
111 }
112 catch ( IllegalArgumentException e )
113 {
114 assertTrue( "IllegalArgumentException catched", true );
115 }
116 finally
117 {
118 w.close();
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 public void testDevelopersOrContributorsByDoapRoles()
135 throws Exception
136 {
137 I18N i18n = (I18N) getContainer().lookup( I18N.ROLE );
138 assertNotNull( i18n );
139 assertNotNull( i18n.getBundle() );
140
141 List<Contributor> developersOrContributors = new ArrayList<Contributor>();
142
143
144 Developer dev = new Developer();
145 dev.setId( "dev1" );
146 dev.addRole( "maintainer" );
147
148 developersOrContributors.add( dev );
149
150 assertTrue( DoapUtil.getContributorsWithDeveloperRole( i18n, developersOrContributors ).isEmpty() );
151 assertTrue( DoapUtil.getContributorsWithDocumenterRole( i18n, developersOrContributors ).isEmpty() );
152 assertTrue( DoapUtil.getContributorsWithHelperRole( i18n, developersOrContributors ).isEmpty() );
153 assertFalse( DoapUtil.getContributorsWithMaintainerRole( i18n, developersOrContributors ).isEmpty() );
154 assertTrue( DoapUtil.getContributorsWithTesterRole( i18n, developersOrContributors ).isEmpty() );
155 assertTrue( DoapUtil.getContributorsWithTranslatorRole( i18n, developersOrContributors ).isEmpty() );
156 assertTrue( DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors ).isEmpty() );
157
158
159 developersOrContributors.clear();
160
161 dev = new Developer();
162 dev.setId( "dev1" );
163 dev.addRole( " MAINTAINER" );
164 dev.addRole( "tesTER " );
165 dev.addRole( "blabla" );
166 dev.addRole( "translato r" );
167
168 developersOrContributors.add( dev );
169
170 assertTrue( DoapUtil.getContributorsWithDeveloperRole( i18n, developersOrContributors ).isEmpty() );
171 assertTrue( DoapUtil.getContributorsWithDocumenterRole( i18n, developersOrContributors ).isEmpty() );
172 assertTrue( DoapUtil.getContributorsWithHelperRole( i18n, developersOrContributors ).isEmpty() );
173 assertFalse( DoapUtil.getContributorsWithMaintainerRole( i18n, developersOrContributors ).isEmpty() );
174 assertFalse( DoapUtil.getContributorsWithTesterRole( i18n, developersOrContributors ).isEmpty() );
175 assertTrue( DoapUtil.getContributorsWithTranslatorRole( i18n, developersOrContributors ).isEmpty() );
176 assertFalse( DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors ).isEmpty() );
177
178
179 developersOrContributors.clear();
180
181 dev = new Developer();
182 dev.setId( "dev1" );
183 dev.addRole( "maintainer" );
184 dev.addRole( "unknown" );
185
186 developersOrContributors.add( dev );
187
188 int sizeBeforeEmeritus = DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors).size();
189 dev.addRole( " Emeritus" );
190
191 assertTrue( DoapUtil.getContributorsWithUnknownRole( i18n, developersOrContributors).size() == sizeBeforeEmeritus );
192
193 }
194
195
196
197
198
199
200
201 public void testValidate()
202 throws Exception
203 {
204 File doapFile = new File( getBasedir(), "src/test/resources/generated-doap-1.0.rdf" );
205 assertFalse( DoapUtil.validate( doapFile ).isEmpty() );
206 }
207
208
209
210
211
212
213
214 public void testInterpolate()
215 throws Exception
216 {
217 License license = new License();
218 license.setName( "licenseName" );
219 license.setUrl( "licenseUrl" );
220
221 List<Developer> developers = new ArrayList<Developer>();
222 Developer developer1 = new Developer();
223 developer1.setId( "id1" );
224 developer1.setName( "developerName1" );
225 developers.add( developer1 );
226 Developer developer2 = new Developer();
227 developer2.setId( "id1" );
228 developer2.setName( "developerName2" );
229 developers.add( developer2 );
230
231 MavenProject project = new MavenProject();
232 project.setName( "projectName" );
233 project.setDescription( "projectDescription" );
234 project.setLicenses( Collections.singletonList( license ) );
235 project.setDevelopers( developers );
236 project.getProperties().put( "myKey", "myValue" );
237
238 assertEquals( DoapUtil.interpolate( "${project.name}", project, null ), "projectName" );
239 assertEquals( DoapUtil.interpolate( "my name is ${project.name}", project, null ), "my name is projectName" );
240 assertEquals( DoapUtil.interpolate( "my name is ${project.invalid}", project, null ), "my name is ${project.invalid}" );
241 assertEquals( DoapUtil.interpolate( "${pom.description}", project, null ), "projectDescription" );
242 assertNull( DoapUtil.interpolate( "${project.licenses.name}", project, null ) );
243 assertEquals( DoapUtil.interpolate( "${project.licenses[0].name}", project, null ), "licenseName" );
244 assertNull( DoapUtil.interpolate( "${project.licenses[1].name}", project, null ) );
245 assertNotNull( DoapUtil.interpolate( "${project.developers}", project, null ) );
246 assertEquals( DoapUtil.interpolate( "${project.developers[0].name}", project, null ), "developerName1" );
247 assertEquals( DoapUtil.interpolate( "${project.developers[1].name}", project, null ), "developerName2" );
248 assertEquals( DoapUtil.interpolate( "${myKey}", project, null ), "myValue" );
249 }
250 }