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