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