1 package org.apache.maven.archiva.repository.project.writers;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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.codehaus.plexus.spring.PlexusInSpringTestCase;
29 import org.custommonkey.xmlunit.DetailedDiff;
30 import org.custommonkey.xmlunit.Diff;
33 import java.io.IOException;
34 import java.io.StringWriter;
37 * ProjectModel400WriterTest
41 public class ProjectModel400WriterTest
42 extends PlexusInSpringTestCase
44 private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
46 private ProjectModelWriter modelWriter;
49 protected void setUp()
54 modelWriter = new ProjectModel400Writer();
57 public void testSimpleWrite()
60 ArchivaProjectModel model = new ArchivaProjectModel();
61 model.setGroupId( "org.apache.archiva.test" );
62 model.setArtifactId( "simple-model-write" );
63 model.setVersion( "1.0" );
65 String actualModel = writeToString( model );
66 String expectedModel = getExpectedModelString( "model-write-400-simple.pom" );
68 assertModelSimilar( expectedModel, actualModel );
71 public void testReadWriteSimple()
74 String pathToModel = DEFAULT_REPOSITORY + "/org/apache/maven/A/1.0/A-1.0.pom";
75 ArchivaProjectModel model = createArchivaProjectModel( pathToModel );
77 String actualModel = writeToString( model );
78 String expectedModel = FileUtils.readFileToString( new File( pathToModel ), null );
80 assertModelSimilar( expectedModel, actualModel );
83 public void testReadWriteMavenParent()
86 ArchivaProjectModel model = createArchivaProjectModel( DEFAULT_REPOSITORY
87 + "/org/apache/maven/maven-parent/4/maven-parent-4.pom" );
89 String actualModel = writeToString( model );
90 String expectedModel = getExpectedModelString( "maven-parent-4.pom" );
92 assertModelSimilar( expectedModel, actualModel );
95 public void testReadWriteCocoon()
98 ArchivaProjectModel model = createArchivaProjectModel( DEFAULT_REPOSITORY
99 + "/org/apache/cocoon/cocoon/1/cocoon-1.pom" );
101 String actualModel = writeToString( model );
102 String expectedModel = getExpectedModelString( "cocoon-1.pom" );
104 assertModelSimilar( expectedModel, actualModel );
107 private void assertModelSimilar( String expectedModel, String actualModel )
110 Diff diff = new Diff( expectedModel, actualModel );
111 DetailedDiff detailedDiff = new DetailedDiff( diff );
112 if ( !detailedDiff.similar() )
114 // If it isn't similar, dump the difference.
115 System.out.println( detailedDiff.toString() );
116 System.out.println( "-- Actual Model --\n" + actualModel + "\n---------------\n\n" );
117 System.out.println( "-- Expected Model --\n" + expectedModel + "\n---------------\n\n" );
119 assertEquals( expectedModel, actualModel );
123 private String getExpectedModelString( String pomfilename )
126 File pomFile = getTestFile( "src/test/expected-poms/" + pomfilename );
127 return FileUtils.readFileToString( pomFile, null );
130 private ArchivaProjectModel createArchivaProjectModel( String path )
131 throws ProjectModelException
133 ProjectModelReader reader = new ProjectModel400Reader();
135 File pomFile = new File( getBasedir(), path );
137 return reader.read( pomFile );
140 private String writeToString( ArchivaProjectModel model )
141 throws ProjectModelException, IOException
143 StringWriter writer = new StringWriter();
145 modelWriter.write( model, writer );
147 return writer.toString();