View Javadoc

1   package org.codehaus.modello.core.io;
2   
3   /*
4    * Copyright (c) 2004, Jason van Zyl
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.core.ModelloCore;
26  import org.codehaus.modello.model.Model;
27  import org.codehaus.modello.model.ModelAssociation;
28  import org.codehaus.modello.model.ModelClass;
29  import org.codehaus.modello.model.ModelField;
30  import org.codehaus.modello.model.Version;
31  import org.codehaus.modello.model.VersionRange;
32  import org.codehaus.plexus.PlexusTestCase;
33  
34  import java.util.List;
35  
36  /**
37   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
38   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
39   */
40  public class ModelReaderTest
41      extends PlexusTestCase
42  {
43      public void testBasic()
44          throws Exception
45      {
46          ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
47  
48          Model model = modello.loadModel( getTestFile( "src/test/resources/models/simple.mdo" ) );
49  
50          assertNotNull( model );
51  
52          assertEquals( "field", model.getVersionDefinition().getType() );
53          assertEquals( "foo", model.getVersionDefinition().getValue() );
54  
55          assertEquals( "simple", model.getId() );
56  
57          assertEquals( "Simple Modello Test Model", model.getName() );
58  
59          assertEquals( "foo.bar", model.getDefaultPackageName( false, null ) );
60  
61          assertEquals( "Boy", model.getRoot( new Version( "1.0.0" ) ) );
62  
63          List<ModelClass> classes = model.getAllClasses();
64  
65          assertNotNull( classes );
66  
67          assertEquals( 2, classes.size() );
68  
69          assertEquals( 2, model.getClasses( new Version( "1.0.0" ) ).size() );
70  
71          assertBoy( classes.get( 0 ) );
72  
73          assertBoy( model.getClass( "Boy", new VersionRange( "1.0.0" ) ) );
74  
75          assertGirl( model.getClass( "Girl", new VersionRange( "1.0.0" ) ) );
76      }
77  
78      public void testAssociationDefaultValues()
79          throws Exception
80      {
81          ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE );
82  
83          Model model = modello.loadModel( getTestFile( "src/test/resources/models/association.mdo" ) );
84  
85          ModelField field = model.getClass( "Foo", new VersionRange( "1.0.0" ) ).getField( "bars", new VersionRange( "1.0.0" ) );
86  
87          assertTrue( field instanceof ModelAssociation );
88  
89          ModelAssociation association = (ModelAssociation) field;
90  
91          assertEquals( "bars", association.getName() );
92  
93          assertEquals( "Foo", association.getModelClass().getName() );
94  
95          assertEquals( "Bar", association.getTo() );
96  
97          assertEquals( "Bar", association.getToClass().getName() );
98  
99  //        assertEquals( "bars", association.getFromRole() );
100 
101 //        assertEquals( "foo", association.getToRole() );
102 
103 //        assertEquals( "1", association.getFromMultiplicity() );
104 
105 //        assertEquals( "*", association.getToMultiplicity() );
106     }
107 
108     private void assertBoy( Object boyObject )
109     {
110         assertTrue( boyObject instanceof ModelClass );
111 
112         ModelClass boy = (ModelClass)boyObject;
113 
114         assertNotNull( boy );
115 
116         assertEquals( "Boy", boy.getName() );
117 
118         assertEquals( "1.0.0", boy.getVersionRange().toString() );
119 
120         List<ModelField> fields = boy.getFields( new Version( "1.0.0" ) );
121 
122         assertEquals( 2, fields.size() );
123 
124         assertBoyName( fields.get( 0 ) );
125 
126         assertBoyName( boy.getField( "name", new VersionRange( "1.0.0" ) ) );
127     }
128 
129     private void assertBoyName( Object nameObject )
130     {
131         assertTrue( nameObject instanceof ModelField );
132 
133         ModelField name = (ModelField) nameObject;
134 
135         assertEquals( "name", name.getName() );
136 
137         assertEquals( "1.0.0", name.getVersionRange().toString() );
138 
139         assertEquals( "String", name.getType() );
140 
141         assertEquals( "moniker", name.getAlias() );
142     }
143 
144     private void assertGirl( Object girlObject )
145     {
146         assertTrue( girlObject instanceof ModelClass );
147 
148         ModelClass girl = (ModelClass)girlObject;
149 
150         assertNotNull( girl );
151 
152         assertEquals( "Girl", girl.getName() );
153 
154         assertEquals( "1.0.0", girl.getVersionRange().toString() );
155 
156         List<ModelField> fields = girl.getFields( new Version( "1.0.0" ) );
157 
158         assertEquals( 1, fields.size() );
159 
160         assertGirlAge( fields.get( 0 ) );
161 
162         assertGirlAge( girl.getField( "age", new VersionRange( "1.0.0" ) ) );
163     }
164 
165     private void assertGirlAge( Object ageObject )
166     {
167         assertTrue( ageObject instanceof ModelField );
168 
169         ModelField age = (ModelField) ageObject;
170 
171         assertEquals( "age", age.getName() );
172 
173         assertEquals( "1.0.0+", age.getVersionRange().toString() );
174 
175         assertEquals( "int", age.getType() );
176     }
177 }