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.PlexusTestCase;
29 import org.custommonkey.xmlunit.DetailedDiff;
30 import org.custommonkey.xmlunit.Diff;
33 import java.io.IOException;
34 import java.io.StringWriter;
37 * ProjectModel400WriterTest
39 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
42 public class ProjectModel400WriterTest
43 extends PlexusTestCase
45 private static final String DEFAULT_REPOSITORY = "src/test/repositories/default-repository";
47 private ProjectModelWriter modelWriter;
50 protected void setUp()
55 modelWriter = (ProjectModelWriter) lookup( ProjectModelWriter.class, "model400" );
58 public void testSimpleWrite()
61 ArchivaProjectModel model = new ArchivaProjectModel();
62 model.setGroupId( "org.apache.archiva.test" );
63 model.setArtifactId( "simple-model-write" );
64 model.setVersion( "1.0" );
66 String actualModel = writeToString( model );
67 String expectedModel = getExpectedModelString( "model-write-400-simple.pom" );
69 assertModelSimilar( expectedModel, actualModel );
72 public void testReadWriteSimple()
75 String pathToModel = DEFAULT_REPOSITORY + "/org/apache/maven/A/1.0/A-1.0.pom";
76 ArchivaProjectModel model = createArchivaProjectModel( pathToModel );
78 String actualModel = writeToString( model );
79 String expectedModel = FileUtils.readFileToString( new File( pathToModel ), null );
81 assertModelSimilar( expectedModel, actualModel );
84 public void testReadWriteMavenParent()
87 ArchivaProjectModel model = createArchivaProjectModel( DEFAULT_REPOSITORY
88 + "/org/apache/maven/maven-parent/4/maven-parent-4.pom" );
90 String actualModel = writeToString( model );
91 String expectedModel = getExpectedModelString( "maven-parent-4.pom" );
93 assertModelSimilar( expectedModel, actualModel );
96 public void testReadWriteCocoon()
99 ArchivaProjectModel model = createArchivaProjectModel( DEFAULT_REPOSITORY
100 + "/org/apache/cocoon/cocoon/1/cocoon-1.pom" );
102 String actualModel = writeToString( model );
103 String expectedModel = getExpectedModelString( "cocoon-1.pom" );
105 assertModelSimilar( expectedModel, actualModel );
108 private void assertModelSimilar( String expectedModel, String actualModel )
111 Diff diff = new Diff( expectedModel, actualModel );
112 DetailedDiff detailedDiff = new DetailedDiff( diff );
113 if ( !detailedDiff.similar() )
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" );
120 assertEquals( expectedModel, actualModel );
124 private String getExpectedModelString( String pomfilename )
127 File pomFile = getTestFile( "src/test/expected-poms/" + pomfilename );
128 return FileUtils.readFileToString( pomFile, null );
131 private ArchivaProjectModel createArchivaProjectModel( String path )
132 throws ProjectModelException
134 ProjectModelReader reader = new ProjectModel400Reader();
136 File pomFile = new File( getBasedir(), path );
138 return reader.read( pomFile );
141 private String writeToString( ArchivaProjectModel model )
142 throws ProjectModelException, IOException
144 StringWriter writer = new StringWriter();
146 modelWriter.write( model, writer );
148 return writer.toString();