View Javadoc

1   package org.codehaus.modello.plugins.xml;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import org.codehaus.modello.ModelloRuntimeException;
26  import org.codehaus.modello.core.ModelloCore;
27  import org.codehaus.modello.metadata.MetadataPlugin;
28  import org.codehaus.modello.model.Model;
29  import org.codehaus.modello.model.ModelClass;
30  import org.codehaus.modello.model.ModelField;
31  import org.codehaus.modello.model.Version;
32  import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
33  import org.codehaus.modello.plugins.xml.metadata.XmlMetadataPlugin;
34  import org.codehaus.plexus.PlexusTestCase;
35  
36  import java.util.List;
37  
38  /**
39   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l </a>
40   */
41  public class XmlModelloPluginTest
42      extends PlexusTestCase
43  {
44      public void testConfiguration()
45          throws Exception
46      {
47          Object object = lookup( MetadataPlugin.ROLE, "xml" );
48  
49          assertNotNull( object );
50  
51          assertTrue( object instanceof XmlMetadataPlugin );
52      }
53  
54      public void testXmlPlugin()
55          throws Exception
56      {
57          ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
58  
59          Model model = modello.loadModel( getTestFile( "src/test/resources/model.mdo" ) );
60  
61          List<ModelClass> classes = model.getClasses( new Version( "4.0.0" ) );
62  
63          assertEquals( 2, classes.size() );
64  
65          ModelClass clazz = (ModelClass) classes.get( 0 );
66  
67          assertEquals( "Model", clazz.getName() );
68  
69          assertEquals( 3, clazz.getFields( new Version( "4.0.0" ) ).size() );
70  
71          ModelField extend = clazz.getField( "extend", new Version( "4.0.0" ) );
72  
73          assertTrue( extend.hasMetadata( XmlFieldMetadata.ID ) );
74  
75          XmlFieldMetadata xml = (XmlFieldMetadata) extend.getMetadata( XmlFieldMetadata.ID );
76  
77          assertNotNull( xml );
78  
79          assertFalse( xml.isAttribute() );
80  
81          extend = clazz.getField( "extend", new Version( "4.1.0" ) );
82  
83          assertTrue( extend.hasMetadata( XmlFieldMetadata.ID ) );
84  
85          xml = (XmlFieldMetadata) extend.getMetadata( XmlFieldMetadata.ID );
86  
87          assertNotNull( xml );
88  
89          assertTrue( xml.isAttribute() );
90  
91          ModelField parent = clazz.getField( "parent", new Version( "4.0.0" ) );
92  
93          try
94          {
95              parent.getMetadata( "foo" );
96  
97              fail( "Expected ModelloException" );
98          }
99          catch( ModelloRuntimeException ex )
100         {
101             // expected
102         }
103 
104         ModelField builder = clazz.getField( "builder", new Version( "4.0.0" ) );
105 
106         assertTrue( builder.hasMetadata( XmlFieldMetadata.ID ) );
107 
108         xml = (XmlFieldMetadata) builder.getMetadata( XmlFieldMetadata.ID );
109 
110         assertNotNull( xml );
111 
112         assertEquals( "build", xml.getTagName() );
113 
114         assertTrue( xml.isTrim() );
115     }
116 }