View Javadoc
1   package org.codehaus.plexus.component.configurator;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.io.StringReader;
29  import java.lang.annotation.ElementType;
30  import java.net.URI;
31  import java.net.URL;
32  import java.util.LinkedList;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Properties;
36  import java.util.Set;
37  import java.util.Vector;
38  
39  import org.codehaus.plexus.PlexusTestCase;
40  import org.codehaus.plexus.classworlds.ClassWorld;
41  import org.codehaus.plexus.classworlds.realm.ClassRealm;
42  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
43  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
44  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
45  import org.codehaus.plexus.component.repository.ComponentDescriptor;
46  import org.codehaus.plexus.component.repository.io.PlexusTools;
47  import org.codehaus.plexus.configuration.PlexusConfiguration;
48  import org.codehaus.plexus.configuration.PlexusConfigurationException;
49  
50  /**
51   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
52   */
53  public abstract class AbstractComponentConfiguratorTest extends PlexusTestCase {
54      protected void configureComponent(Object component, ComponentDescriptor descriptor, ClassRealm realm)
55              throws Exception {
56          ComponentConfigurator cc = getComponentConfigurator();
57          cc.configureComponent(component, descriptor.getConfiguration(), realm);
58      }
59  
60      protected void configureComponent(
61              Object component, ComponentDescriptor descriptor, ClassRealm realm, ExpressionEvaluator expressionEvaluator)
62              throws Exception {
63          ComponentConfigurator cc = getComponentConfigurator();
64          cc.configureComponent(component, descriptor.getConfiguration(), expressionEvaluator, realm);
65      }
66  
67      protected abstract ComponentConfigurator getComponentConfigurator() throws Exception;
68  
69      public void testComponentConfigurator() throws Exception {
70          String xml = "<configuration>" + "  <boolean-value>true</boolean-value>"
71                  + "  <byte-value>64</byte-value>"
72                  + "  <short-value>-128</short-value>"
73                  + "  <int-value>-1</int-value>"
74                  + "  <float-value>1</float-value>"
75                  + "  <long-value>2</long-value>"
76                  + "  <double-value>3</double-value>"
77                  + "  <char-value>X</char-value>"
78                  + "  <string-value>foo</string-value>"
79                  + "  <file-value>test.txt</file-value>"
80                  + "  <uri-value>http://www.apache.org/</uri-value>"
81                  + "  <url-value>http://maven.apache.org/</url-value>"
82                  + "  <important-things>"
83                  + "    <important-thing><name>jason</name></important-thing>"
84                  + "    <important-thing><name>tess</name></important-thing>"
85                  + "  </important-things>"
86                  + "  <configuration>"
87                  + "      <name>jason</name>"
88                  + "  </configuration>"
89                  + "</configuration>";
90  
91          PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
92  
93          ConfigurableComponent component = new ConfigurableComponent();
94  
95          ComponentDescriptor descriptor = new ComponentDescriptor();
96  
97          descriptor.setRole("role");
98  
99          descriptor.setImplementation(component.getClass().getName());
100 
101         descriptor.setConfiguration(configuration);
102 
103         ClassWorld classWorld = new ClassWorld();
104 
105         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
106 
107         configureComponent(component, descriptor, realm);
108 
109         assertEquals("check boolean value", true, component.getBooleanValue());
110 
111         assertEquals("check byte value", 64, component.getByteValue());
112 
113         assertEquals("check short value", -128, component.getShortValue());
114 
115         assertEquals("check integer value", -1, component.getIntValue());
116 
117         assertEquals("check float value", 1.0f, component.getFloatValue(), 0.001f);
118 
119         assertEquals("check long value", 2L, component.getLongValue());
120 
121         assertEquals("check double value", 3.0, component.getDoubleValue(), 0.001);
122 
123         assertEquals('X', component.getCharValue());
124 
125         assertEquals("foo", component.getStringValue());
126 
127         assertEquals(new File("test.txt"), component.getFileValue());
128 
129         assertEquals(new URI("http://www.apache.org/"), component.getUriValue());
130 
131         assertEquals(new URL("http://maven.apache.org/"), component.getUrlValue());
132 
133         List list = component.getImportantThings();
134 
135         assertEquals(2, list.size());
136 
137         assertEquals("jason", ((ImportantThing) list.get(0)).getName());
138 
139         assertEquals("tess", ((ImportantThing) list.get(1)).getName());
140 
141         // Embedded Configuration
142 
143         PlexusConfiguration c = component.getConfiguration();
144 
145         assertEquals("jason", c.getChild("name").getValue());
146     }
147 
148     public void testComponentConfiguratorWithAComponentThatProvidesSettersForConfiguration() throws Exception {
149         String xml = "<configuration>" + "  <int-value>0</int-value>" + "  <float-value>1</float-value>"
150                 + "  <long-value>2</long-value>" + "  <double-value>3</double-value>"
151                 + "  <string-value>foo</string-value>" + "  <important-things>"
152                 + "    <important-thing><name>jason</name></important-thing>"
153                 + "    <important-thing><name>tess</name></important-thing>" + "  </important-things>"
154                 + "  <configuration>" + "      <name>jason</name>" + "  </configuration>" + "</configuration>";
155 
156         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
157 
158         ComponentWithSetters component = new ComponentWithSetters();
159 
160         ComponentDescriptor descriptor = new ComponentDescriptor();
161 
162         descriptor.setRole("role");
163 
164         descriptor.setImplementation(component.getClass().getName());
165 
166         descriptor.setConfiguration(configuration);
167 
168         ClassWorld classWorld = new ClassWorld();
169 
170         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
171 
172         configureComponent(component, descriptor, realm);
173 
174         assertEquals("check integer value", 0, component.getIntValue());
175 
176         assertTrue(component.intValueSet);
177 
178         assertEquals("check float value", 1.0f, component.getFloatValue(), 0.001f);
179 
180         assertTrue(component.floatValueSet);
181 
182         assertEquals("check long value", 2L, component.getLongValue());
183 
184         assertTrue(component.longValueSet);
185 
186         assertEquals("check double value", 3.0, component.getDoubleValue(), 0.001);
187 
188         assertTrue(component.doubleValueSet);
189 
190         assertEquals("foo", component.getStringValue());
191 
192         assertTrue(component.stringValueSet);
193 
194         List list = component.getImportantThings();
195 
196         assertEquals(2, list.size());
197 
198         assertEquals("jason", ((ImportantThing) list.get(0)).getName());
199 
200         assertEquals("tess", ((ImportantThing) list.get(1)).getName());
201 
202         assertTrue(component.importantThingsValueSet);
203 
204         // Embedded Configuration
205 
206         PlexusConfiguration c = component.getConfiguration();
207 
208         assertEquals("jason", c.getChild("name").getValue());
209 
210         assertTrue(component.configurationValueSet);
211     }
212 
213     public void testComponentConfigurationWhereFieldsToConfigureResideInTheSuperclass() throws Exception {
214         String xml = "<configuration>" + "  <name>jason</name>" + "  <address>bollywood</address>" + "</configuration>";
215 
216         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
217 
218         DefaultComponent component = new DefaultComponent();
219 
220         ComponentDescriptor descriptor = new ComponentDescriptor();
221 
222         descriptor.setRole("role");
223 
224         descriptor.setImplementation(component.getClass().getName());
225 
226         descriptor.setConfiguration(configuration);
227 
228         ClassWorld classWorld = new ClassWorld();
229 
230         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
231 
232         configureComponent(component, descriptor, realm);
233 
234         assertEquals("jason", component.getName());
235 
236         assertEquals("bollywood", component.getAddress());
237     }
238 
239     public void testComponentConfigurationWhereFieldsAreCollections() throws Exception {
240         String xml = "<configuration>" + "  <vector>" + "    <important-thing>" + "       <name>life</name>"
241                 + "    </important-thing>" + "  </vector>" + "  <hashSet>" + "    <important-thing>"
242                 + "       <name>life</name>" + "    </important-thing>" + "  </hashSet>"
243                 + "   <list implementation=\"java.util.LinkedList\">" + "     <important-thing>"
244                 + "       <name>life</name>" + "    </important-thing>" + "  </list>" + "  <stringList>"
245                 + "    <something>abc</something>" + "    <somethingElse>def</somethingElse>" + "  </stringList>"
246                 + "   <set><something>abc</something></set>"
247                 + "   <sortedSet><something>abc</something></sortedSet>" +
248                 // TODO: implement List<int> etc..
249                 //  "<intList>" +
250                 //  "  <something>12</something>" +
251                 //  "  <somethingElse>34</somethingElse>" +
252                 //  "</intList>" +
253                 "</configuration>";
254 
255         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
256 
257         ComponentWithCollectionFields component = new ComponentWithCollectionFields();
258 
259         ComponentDescriptor descriptor = new ComponentDescriptor();
260 
261         descriptor.setRole("role");
262 
263         descriptor.setImplementation(component.getClass().getName());
264 
265         descriptor.setConfiguration(configuration);
266 
267         ClassWorld classWorld = new ClassWorld();
268 
269         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
270 
271         configureComponent(component, descriptor, realm);
272 
273         Vector vector = component.getVector();
274 
275         assertEquals("life", ((ImportantThing) vector.get(0)).getName());
276 
277         assertEquals(1, vector.size());
278 
279         Set set = component.getHashSet();
280 
281         assertEquals(1, set.size());
282 
283         Object[] setContents = set.toArray();
284 
285         assertEquals("life", ((ImportantThing) setContents[0]).getName());
286 
287         List list = component.getList();
288 
289         assertEquals(list.getClass(), LinkedList.class);
290 
291         assertEquals("life", ((ImportantThing) list.get(0)).getName());
292 
293         assertEquals(1, list.size());
294 
295         List stringList = component.getStringList();
296 
297         assertEquals("abc", (String) stringList.get(0));
298 
299         assertEquals("def", (String) stringList.get(1));
300 
301         assertEquals(2, stringList.size());
302 
303         set = component.getSet();
304 
305         assertEquals(1, set.size());
306 
307         set = component.getSortedSet();
308 
309         assertEquals(1, set.size());
310     }
311 
312     public void testComponentConfigurationWhereFieldsAreArrays() throws Exception {
313         String xml = "<configuration>" + "  <stringArray>" + "    <first-string>value1</first-string>"
314                 + "    <second-string>value2</second-string>" + "  </stringArray>" + "  <integerArray>"
315                 + "    <firstInt>42</firstInt>" + "    <secondInt>69</secondInt>" + "  </integerArray>"
316                 + "  <importantThingArray>" + "    <importantThing><name>Hello</name></importantThing>"
317                 + "    <importantThing><name>World!</name></importantThing>" + "  </importantThingArray>"
318                 + "  <objectArray>" + "    <java.lang.String>some string</java.lang.String>"
319                 + "    <importantThing><name>something important</name></importantThing>"
320                 + "    <whatever implementation='java.lang.Integer'>303</whatever>" + "  </objectArray>"
321                 + "  <urlArray>"
322                 + "    <url>http://foo.com/bar</url>" + "    <url>file://localhost/c:/windows</url>" + "  </urlArray>"
323                 + "  <fileArray>" + "    <file>c:/windows</file>" + "    <file>/usr/local/bin/foo.sh</file>"
324                 + "  </fileArray>" + "</configuration>";
325 
326         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
327 
328         ComponentWithArrayFields component = new ComponentWithArrayFields();
329 
330         ComponentDescriptor descriptor = new ComponentDescriptor();
331 
332         descriptor.setRole("role");
333 
334         descriptor.setImplementation(component.getClass().getName());
335 
336         descriptor.setConfiguration(configuration);
337 
338         ClassWorld classWorld = new ClassWorld();
339 
340         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
341 
342         configureComponent(component, descriptor, realm);
343 
344         String[] stringArray = component.getStringArray();
345 
346         assertEquals(2, stringArray.length);
347 
348         assertEquals("value1", stringArray[0]);
349 
350         assertEquals("value2", stringArray[1]);
351 
352         Integer[] integerArray = component.getIntegerArray();
353 
354         assertEquals(2, integerArray.length);
355 
356         assertEquals(new Integer(42), integerArray[0]);
357 
358         assertEquals(new Integer(69), integerArray[1]);
359 
360         ImportantThing[] importantThingArray = component.getImportantThingArray();
361 
362         assertEquals(2, importantThingArray.length);
363 
364         assertEquals("Hello", importantThingArray[0].getName());
365 
366         assertEquals("World!", importantThingArray[1].getName());
367 
368         Object[] objectArray = component.getObjectArray();
369 
370         assertEquals(3, objectArray.length);
371 
372         assertEquals("some string", objectArray[0]);
373 
374         assertEquals("something important", ((ImportantThing) objectArray[1]).getName());
375 
376         assertEquals(303, objectArray[2]);
377 
378         URL[] urls = component.getUrlArray();
379 
380         assertEquals(new URL("http://foo.com/bar"), urls[0]);
381 
382         assertEquals(new URL("file://localhost/c:/windows"), urls[1]);
383 
384         File[] files = component.getFileArray();
385 
386         assertEquals(new File("c:/windows"), files[0]);
387 
388         assertEquals(new File("/usr/local/bin/foo.sh"), files[1]);
389     }
390 
391     public void testComponentConfigurationWithCompositeFields() throws Exception {
392 
393         String xml = "<configuration>"
394                 + "  <thing implementation=\"org.codehaus.plexus.component.configurator.ImportantThing\">"
395                 + "     <name>I am not abstract!</name>" + "  </thing>" + "</configuration>";
396 
397         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
398 
399         ComponentWithCompositeFields component = new ComponentWithCompositeFields();
400 
401         ComponentDescriptor descriptor = new ComponentDescriptor();
402 
403         descriptor.setRole("role");
404 
405         descriptor.setImplementation(component.getClass().getName());
406 
407         descriptor.setConfiguration(configuration);
408 
409         ClassWorld classWorld = new ClassWorld();
410 
411         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
412 
413         configureComponent(component, descriptor, realm);
414 
415         assertNotNull(component.getThing());
416 
417         assertEquals("I am not abstract!", component.getThing().getName());
418     }
419 
420     public void testInvalidComponentConfiguration() throws Exception {
421 
422         String xml = "<configuration><goodStartElement>theName</badStopElement></configuration>";
423 
424         try {
425             PlexusTools.buildConfiguration("<Test-Invalid>", new StringReader(xml));
426 
427             fail("Should have caused an error because of the invalid XML.");
428         } catch (PlexusConfigurationException e) {
429             // Error should be caught here.
430         } catch (Exception e) {
431             fail("Should have caught the invalid plexus configuration exception.");
432         }
433     }
434 
435     public void testComponentConfigurationWithPropertiesFields() throws Exception {
436 
437         String xml = "<configuration>" + "  <someProperties>" + "     <property>" + "        <name>firstname</name>"
438                 + "        <value>michal</value>" + "     </property>" + "     <property>"
439                 + "        <name>lastname</name>" + "        <value>maczka</value>" + "     </property>"
440                 + "  </someProperties>" + "</configuration>";
441 
442         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
443 
444         ComponentWithPropertiesField component = new ComponentWithPropertiesField();
445 
446         ComponentDescriptor descriptor = new ComponentDescriptor();
447 
448         descriptor.setRole("role");
449 
450         descriptor.setImplementation(component.getClass().getName());
451 
452         descriptor.setConfiguration(configuration);
453 
454         ClassWorld classWorld = new ClassWorld();
455 
456         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
457 
458         configureComponent(component, descriptor, realm);
459 
460         Properties properties = component.getSomeProperties();
461 
462         assertNotNull(properties);
463 
464         assertEquals("michal", properties.get("firstname"));
465 
466         assertEquals("maczka", properties.get("lastname"));
467     }
468 
469     public void testComponentConfigurationWithPropertiesFieldsWithExpression() throws Exception {
470 
471         String xml =
472                 "<configuration>" + " <someProperties> ${injectedProperties} </someProperties>" + "</configuration>";
473 
474         final Properties propertiesInterpolated = new Properties();
475         propertiesInterpolated.put("firstname", "olivier");
476         propertiesInterpolated.put("lastname", "lamy");
477 
478         ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator() {
479             public Object evaluate(String expression) {
480                 return propertiesInterpolated;
481             }
482 
483             public File alignToBaseDirectory(File file) {
484                 return null;
485             }
486         };
487 
488         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
489 
490         ComponentWithPropertiesField component = new ComponentWithPropertiesField();
491 
492         ComponentDescriptor descriptor = new ComponentDescriptor();
493 
494         descriptor.setRole("role");
495 
496         descriptor.setImplementation(component.getClass().getName());
497 
498         descriptor.setConfiguration(configuration);
499 
500         ClassWorld classWorld = new ClassWorld();
501 
502         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
503 
504         configureComponent(component, descriptor, realm, expressionEvaluator);
505 
506         Properties properties = component.getSomeProperties();
507 
508         assertNotNull(properties);
509 
510         assertEquals("olivier", properties.get("firstname"));
511 
512         assertEquals("lamy", properties.get("lastname"));
513     }
514 
515     public void testComponentConfigurationWithPropertiesFieldsWithExpressions() throws Exception {
516 
517         String xml = "<configuration>" + "<someProperties>" //
518                 + "<property><name>${theName}</name><value>${theValue}</value></property>" //
519                 + "<property><name>empty</name></property>" //
520                 + "</someProperties>" + "</configuration>";
521 
522         final Properties values = new Properties();
523         values.put("${theName}", "test");
524         values.put("${theValue}", "PASSED");
525 
526         ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator() {
527             public Object evaluate(String expression) {
528                 return values.containsKey(expression) ? values.get(expression) : expression;
529             }
530 
531             public File alignToBaseDirectory(File file) {
532                 return null;
533             }
534         };
535 
536         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
537 
538         ComponentWithPropertiesField component = new ComponentWithPropertiesField();
539 
540         ComponentDescriptor descriptor = new ComponentDescriptor();
541 
542         descriptor.setRole("role");
543 
544         descriptor.setImplementation(component.getClass().getName());
545 
546         descriptor.setConfiguration(configuration);
547 
548         ClassWorld classWorld = new ClassWorld();
549 
550         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
551 
552         configureComponent(component, descriptor, realm, expressionEvaluator);
553 
554         Properties properties = component.getSomeProperties();
555 
556         assertNotNull(properties);
557 
558         assertEquals("PASSED", properties.get("test"));
559         assertEquals("", properties.get("empty"));
560     }
561 
562     public void testComponentConfigurationWithMapField() throws Exception {
563         String xml = "<configuration>" + "  <map>" + "     <firstName>Kenney</firstName>"
564                 + "     <lastName>Westerhof</lastName>" + "  </map>" + "</configuration>";
565 
566         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
567 
568         ComponentWithMapField component = new ComponentWithMapField();
569 
570         ComponentDescriptor descriptor = new ComponentDescriptor();
571 
572         descriptor.setRole("role");
573 
574         descriptor.setImplementation(component.getClass().getName());
575 
576         descriptor.setConfiguration(configuration);
577 
578         ClassWorld classWorld = new ClassWorld();
579 
580         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
581 
582         configureComponent(component, descriptor, realm);
583 
584         Map map = component.getMap();
585 
586         assertNotNull(map);
587 
588         assertEquals("Kenney", map.get("firstName"));
589 
590         assertEquals("Westerhof", map.get("lastName"));
591     }
592 
593     public void testComponentConfigurationWhereFieldIsBadArray() throws Exception {
594         String xml = "<configuration>" //
595                 + "  <integerArray><java.lang.String>string</java.lang.String></integerArray>" //
596                 + "</configuration>";
597 
598         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
599 
600         ComponentWithArrayFields component = new ComponentWithArrayFields();
601 
602         ComponentDescriptor descriptor = new ComponentDescriptor();
603 
604         descriptor.setRole("role");
605 
606         descriptor.setImplementation(component.getClass().getName());
607 
608         descriptor.setConfiguration(configuration);
609 
610         ClassWorld classWorld = new ClassWorld();
611 
612         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
613 
614         try {
615             configureComponent(component, descriptor, realm);
616             fail("Configuration did not fail");
617         } catch (ComponentConfigurationException e) {
618         }
619     }
620 
621     public void testComponentConfigurationWhereFieldIsEnum() throws Exception {
622         String xml = "<configuration>" //
623                 + "  <simpleEnum>TYPE</simpleEnum>" //
624                 + "  <nestedEnum>ONE</nestedEnum>" //
625                 + "</configuration>";
626 
627         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
628 
629         ComponentWithEnumFields component = new ComponentWithEnumFields();
630 
631         ComponentDescriptor descriptor = new ComponentDescriptor();
632 
633         descriptor.setRole("role");
634 
635         descriptor.setImplementation(component.getClass().getName());
636 
637         descriptor.setConfiguration(configuration);
638 
639         ClassWorld classWorld = new ClassWorld();
640 
641         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
642 
643         configureComponent(component, descriptor, realm);
644 
645         assertEquals(ElementType.TYPE, component.getSimpleEnum());
646 
647         assertEquals(ComponentWithEnumFields.NestedEnum.ONE, component.getNestedEnum());
648     }
649 
650     public void testComponentConfigurationWithAmbiguousExpressionValue() throws Exception {
651         String xml = "<configuration>" //
652                 + "  <address>${address}</address>" //
653                 + "</configuration>";
654 
655         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
656 
657         DefaultComponent component = new DefaultComponent();
658 
659         ExpressionEvaluator expressionEvaluator = new TypeAwareExpressionEvaluator() {
660             public Object evaluate(String expression) throws ExpressionEvaluationException {
661                 return evaluate(expression, null);
662             }
663 
664             public File alignToBaseDirectory(File file) {
665                 return null;
666             }
667 
668             public Object evaluate(String expression, Class<?> type) throws ExpressionEvaluationException {
669                 if (String.class == type) {
670                     return "PASSED";
671                 } else {
672                     return Boolean.FALSE;
673                 }
674             }
675         };
676 
677         ComponentDescriptor descriptor = new ComponentDescriptor();
678 
679         descriptor.setRole("role");
680 
681         descriptor.setImplementation(component.getClass().getName());
682 
683         descriptor.setConfiguration(configuration);
684 
685         ClassWorld classWorld = new ClassWorld();
686 
687         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
688 
689         configureComponent(component, descriptor, realm, expressionEvaluator);
690 
691         assertEquals("PASSED", component.getAddress());
692     }
693 
694     public void testComponentConfigurationWithPrimitiveValueConversion() throws Exception {
695         String xml = "<configuration>" //
696                 + "  <intValue>${primitive}</intValue>" //
697                 + "</configuration>";
698 
699         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
700 
701         ConfigurableComponent component = new ConfigurableComponent();
702 
703         ExpressionEvaluator expressionEvaluator = new TypeAwareExpressionEvaluator() {
704             public Object evaluate(String expression) throws ExpressionEvaluationException {
705                 return evaluate(expression, null);
706             }
707 
708             public File alignToBaseDirectory(File file) {
709                 return null;
710             }
711 
712             public Object evaluate(String expression, Class<?> type) throws ExpressionEvaluationException {
713                 // java.lang.Short -> short -> int
714                 return (short) 23;
715             }
716         };
717 
718         ComponentDescriptor descriptor = new ComponentDescriptor();
719 
720         descriptor.setRole("role");
721 
722         descriptor.setImplementation(component.getClass().getName());
723 
724         descriptor.setConfiguration(configuration);
725 
726         ClassWorld classWorld = new ClassWorld();
727 
728         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
729 
730         configureComponent(component, descriptor, realm, expressionEvaluator);
731 
732         assertEquals(23, component.getIntValue());
733     }
734 
735     public void testComponentConfigurationWithEmptyContentForBasicField() throws Exception {
736         String xml = "<configuration>" //
737                 + "  <address></address>" //
738                 + "</configuration>";
739 
740         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
741 
742         DefaultComponent component = new DefaultComponent();
743 
744         ComponentDescriptor descriptor = new ComponentDescriptor();
745 
746         descriptor.setRole("role");
747 
748         descriptor.setImplementation(component.getClass().getName());
749 
750         descriptor.setConfiguration(configuration);
751 
752         ClassWorld classWorld = new ClassWorld();
753 
754         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
755 
756         configureComponent(component, descriptor, realm);
757 
758         assertEquals(null, component.getAddress());
759     }
760 
761     public void testComponentConfigurationWithEmptyContentForCompositeField() throws Exception {
762         String xml = "<configuration>" //
763                 + "  <bean/>" //
764                 + "</configuration>";
765 
766         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
767 
768         ComponentWithCompositeFields component = new ComponentWithCompositeFields();
769 
770         ComponentDescriptor descriptor = new ComponentDescriptor();
771 
772         descriptor.setRole("role");
773 
774         descriptor.setImplementation(component.getClass().getName());
775 
776         descriptor.setConfiguration(configuration);
777 
778         ClassWorld classWorld = new ClassWorld();
779 
780         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
781 
782         configureComponent(component, descriptor, realm);
783 
784         assertNotNull(component.getBean());
785     }
786 
787     public void testComponentConfigurationWithUnresolvedExpressionContentForCompositeFieldOfNonInstantiatableType()
788             throws Exception {
789         String xml = "<configuration>" //
790                 + "  <thing>${null-valued-expression}</thing>" //
791                 + "</configuration>";
792 
793         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
794 
795         ComponentWithCompositeFields component = new ComponentWithCompositeFields();
796 
797         ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator() {
798             public Object evaluate(String expression) throws ExpressionEvaluationException {
799                 return null;
800             }
801 
802             public File alignToBaseDirectory(File file) {
803                 return null;
804             }
805         };
806 
807         ComponentDescriptor descriptor = new ComponentDescriptor();
808 
809         descriptor.setRole("role");
810 
811         descriptor.setImplementation(component.getClass().getName());
812 
813         descriptor.setConfiguration(configuration);
814 
815         ClassWorld classWorld = new ClassWorld();
816 
817         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
818 
819         configureComponent(component, descriptor, realm, expressionEvaluator);
820 
821         assertEquals(null, component.getThing());
822     }
823 
824     public void testComponentConfiguratorFileNormalizesSeparator() throws Exception {
825         String xml = "<configuration><fileArray>" + "  <file>dir/test.txt</file>"
826                 + "  <file>dir\\test.txt</file>"
827                 + "</fileArray></configuration>";
828 
829         PlexusConfiguration configuration = PlexusTools.buildConfiguration("<Test>", new StringReader(xml));
830 
831         ComponentWithArrayFields component = new ComponentWithArrayFields();
832 
833         ComponentDescriptor descriptor = new ComponentDescriptor();
834 
835         descriptor.setRole("role");
836 
837         descriptor.setImplementation(component.getClass().getName());
838 
839         descriptor.setConfiguration(configuration);
840 
841         ClassWorld classWorld = new ClassWorld();
842 
843         ClassRealm realm = classWorld.newRealm("test", getClass().getClassLoader());
844 
845         configureComponent(component, descriptor, realm);
846 
847         assertEquals(new File("dir", "test.txt"), component.getFileArray()[0]);
848         assertEquals(new File("dir", "test.txt"), component.getFileArray()[1]);
849     }
850 }