View Javadoc

1   package org.apache.archiva.redback.components.modello.jpox;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.modello.ModelloParameterConstants;
23  import org.codehaus.modello.core.ModelloCore;
24  import org.codehaus.modello.model.Model;
25  import org.codehaus.plexus.util.ReaderFactory;
26  import org.dom4j.Document;
27  import org.dom4j.io.SAXReader;
28  
29  import java.io.File;
30  import java.util.Properties;
31  
32  
33  /**
34   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
35   * @version $Id: JPoxJdoMappingModelloGeneratorTest.java 1414999 2012-11-28 23:55:28Z olamy $
36   */
37  public class JPoxJdoMappingModelloGeneratorTest
38      extends AbstractJpoxGeneratorTestCase
39  {
40      public JPoxJdoMappingModelloGeneratorTest()
41      {
42          super( "jpox-jdo-mapping" );
43      }
44  
45      public void testSimpleInvocation()
46          throws Exception
47      {
48          ModelloCore core = (ModelloCore) lookup( ModelloCore.ROLE );
49  
50          Model model =
51              core.loadModel( ReaderFactory.newXmlReader( getTestFile( "src/test/resources/mergere-tissue.mdo" ) ) );
52  
53          // ----------------------------------------------------------------------
54          // Generate the code
55          // ----------------------------------------------------------------------
56  
57          Properties parameters = new Properties();
58  
59          parameters.setProperty( ModelloParameterConstants.OUTPUT_DIRECTORY, getOutputDirectory().getAbsolutePath() );
60  
61          parameters.setProperty( ModelloParameterConstants.VERSION, "1.0.0" );
62  
63          parameters.setProperty( ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.FALSE.toString() );
64  
65          core.generate( model, "jpox-jdo-mapping", parameters );
66  
67          // ----------------------------------------------------------------------
68          // Assert
69          // ----------------------------------------------------------------------
70  
71          assertTrue( new File( getOutputDirectory(), "package.jdo" ).exists() );
72  
73          //assertGeneratedFileExists( "package.jdo" );
74  
75          SAXReader reader = new SAXReader();
76          reader.setEntityResolver( new JdoEntityResolver() );
77          Document jdoDocument = reader.read( new File( getOutputDirectory() + "/package.jdo" ) );
78  
79          assertNotNull( jdoDocument );
80  
81          // Tree should consist of only elements with attributes. NO TEXT.
82          assertNoTextNodes( jdoDocument, "//jdo", true );
83  
84          assertAttributeEquals( jdoDocument, "//class[@name='TissueModelloMetadata']/field[@name='modelVersion']/column",
85                                 "default-value", "1.0.0" );
86  
87          assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='summary']", "persistence-modifier",
88                                 "none" );
89  
90          // -----------------------------------------------------------------------
91          // Association Tests.
92  
93          //   mdo/association/jpox.dependent-element == false (only on association with "*" multiplicity (default type)
94          assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='friends']/collection",
95                                 "dependent-element", "false" );
96  
97          //   mdo/association (default type) with "1" multiplicity.
98          assertElementNotExists( jdoDocument, "//class[@name='Issue']/field[@name='assignee']/collection" );
99  
100         //   mdo/association (map) with "*" multiplicity.
101         assertElementExists( jdoDocument, "//class[@name='Issue']/field[@name='configuration']/map" );
102 
103         // -----------------------------------------------------------------------
104         // Fetch Group Tests
105         assertAttributeMissing( jdoDocument, "//class[@name='Issue']/field[@name='reporter']", "default-fetch-group" );
106         assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='configuration']",
107                                "default-fetch-group", "true" );
108         assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='assignee']", "default-fetch-group",
109                                "false" );
110 
111         // -----------------------------------------------------------------------
112         // Value Strategy Tests
113         //   defaulted
114         assertAttributeEquals( jdoDocument, "//class[@name='ComplexIdentity']/field[@name='id']", "value-strategy",
115                                "native" );
116         assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='accountId']", "value-strategy",
117                                "native" );
118         //   intentionally unset
119         assertAttributeMissing( jdoDocument, "//class[@name='User']/field[@name='id']", "value-strategy" );
120 
121         // -----------------------------------------------------------------------
122         // Superclass Tests
123         assertAttributeEquals( jdoDocument, "//class[@name='User']", "persistence-capable-superclass",
124                                "org.mergere.user.AbstractUser" );
125 
126         // -----------------------------------------------------------------------
127         // Primary Key Tests
128         assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='accountId']", "primary-key", "true" );
129         assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='summary']", "primary-key", "false" );
130 
131         assertAttributeEquals( jdoDocument, "//class[@name='ComplexIdentity']/field[@name='id']", "primary-key",
132                                "true" );
133         assertAttributeEquals( jdoDocument, "//class[@name='ComplexIdentity']/field[@name='username']", "primary-key",
134                                "false" );
135         assertAttributeEquals( jdoDocument, "//class[@name='ComplexIdentity']/field[@name='fullName']", "primary-key",
136                                "false" );
137         assertAttributeEquals( jdoDocument, "//class[@name='ComplexIdentity']/field[@name='email']", "primary-key",
138                                "false" );
139         assertAttributeEquals( jdoDocument, "//class[@name='ComplexIdentity']/field[@name='locked']", "primary-key",
140                                "false" );
141         assertAttributeEquals( jdoDocument, "//class[@name='ComplexIdentity']/field[@name='lastLoginDate']",
142                                "primary-key", "false" );
143 
144         // -----------------------------------------------------------------------
145         // Alternate Table and Column Names Tests.
146         assertAttributeEquals( jdoDocument, "//class[@name='DifferentTable']", "table", "MyTable" );
147         assertAttributeEquals( jdoDocument, "//class[@name='Issue']/field[@name='accountId']/column", "name", "id" );
148     }
149 }