View Javadoc

1   package org.apache.maven.archiva.repository.project.writers;
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.apache.commons.io.FileUtils;
23  import org.apache.maven.archiva.model.ArchivaProjectModel;
24  import org.apache.maven.archiva.repository.project.ProjectModelException;
25  import org.apache.maven.archiva.repository.project.ProjectModelReader;
26  import org.apache.maven.archiva.repository.project.ProjectModelWriter;
27  import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
28  import org.apache.maven.archiva.xml.XMLException;
29  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
30  import org.custommonkey.xmlunit.DetailedDiff;
31  import org.custommonkey.xmlunit.Diff;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.io.StringWriter;
36  
37  /**
38   * ProjectModel400WriterTest 
39   *
40   * @version $Id: ProjectModel400WriterTest.java 751932 2009-03-10 01:09:34Z brett $
41   */
42  public class ProjectModel400WriterTest
43      extends PlexusInSpringTestCase
44  {
45      private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
46  
47      private ProjectModelWriter modelWriter;
48  
49      @Override
50      protected void setUp()
51          throws Exception
52      {
53          super.setUp();
54  
55          modelWriter = new ProjectModel400Writer();
56      }
57  
58      public void testSimpleWrite()
59          throws Exception
60      {
61          ArchivaProjectModel model = new ArchivaProjectModel();
62          model.setGroupId( "org.apache.archiva.test" );
63          model.setArtifactId( "simple-model-write" );
64          model.setVersion( "1.0" );
65  
66          String actualModel = writeToString( model );
67          String expectedModel = getExpectedModelString( "model-write-400-simple.pom" );
68  
69          assertModelSimilar( expectedModel, actualModel );
70      }
71  
72      public void testReadWriteSimple()
73          throws Exception
74      {
75          String pathToModel = DEFAULT_REPOSITORY + "/org/apache/maven/A/1.0/A-1.0.pom";
76          ArchivaProjectModel model = createArchivaProjectModel( pathToModel );
77  
78          String actualModel = writeToString( model );
79          String expectedModel = FileUtils.readFileToString( new File( pathToModel ), null );
80  
81          assertModelSimilar( expectedModel, actualModel );
82      }
83  
84      public void testReadWriteMavenParent()
85          throws Exception
86      {
87          ArchivaProjectModel model = createArchivaProjectModel( DEFAULT_REPOSITORY
88              + "/org/apache/maven/maven-parent/4/maven-parent-4.pom" );
89  
90          String actualModel = writeToString( model );
91          String expectedModel = getExpectedModelString( "maven-parent-4.pom" );
92  
93          assertModelSimilar( expectedModel, actualModel );
94      }
95      
96      public void testReadWriteCocoon()
97          throws Exception
98      {
99          ArchivaProjectModel model = createArchivaProjectModel( DEFAULT_REPOSITORY
100             + "/org/apache/cocoon/cocoon/1/cocoon-1.pom" );
101 
102         String actualModel = writeToString( model );
103         String expectedModel = getExpectedModelString( "cocoon-1.pom" );
104 
105         assertModelSimilar( expectedModel, actualModel );
106     }
107 
108     private void assertModelSimilar( String expectedModel, String actualModel )
109         throws Exception
110     {
111         Diff diff = new Diff( expectedModel, actualModel );
112         DetailedDiff detailedDiff = new DetailedDiff( diff );
113         if ( !detailedDiff.similar() )
114         {
115             // If it isn't similar, dump the difference.
116             System.out.println( detailedDiff.toString() );
117             System.out.println( "-- Actual Model --\n" + actualModel + "\n---------------\n\n" );
118             System.out.println( "-- Expected Model --\n" + expectedModel + "\n---------------\n\n" );
119 
120             assertEquals( expectedModel, actualModel );
121         }
122     }
123 
124     private String getExpectedModelString( String pomfilename )
125         throws IOException
126     {
127         File pomFile = getTestFile( "src/test/expected-poms/" + pomfilename );
128         return FileUtils.readFileToString( pomFile, null );
129     }
130 
131     private ArchivaProjectModel createArchivaProjectModel( String path )
132         throws XMLException
133     {
134         ProjectModelReader reader = new ProjectModel400Reader();
135 
136         File pomFile = new File( getBasedir(), path );
137 
138         return reader.read( pomFile );
139     }
140 
141     private String writeToString( ArchivaProjectModel model )
142         throws ProjectModelException, IOException
143     {
144         StringWriter writer = new StringWriter();
145 
146         modelWriter.write( model, writer );
147 
148         return writer.toString();
149     }
150 }