001package org.apache.maven.wagon.tck.http;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static junit.framework.Assert.assertEquals;
023import static org.codehaus.plexus.util.FileUtils.fileRead;
024
025import org.codehaus.plexus.util.IOUtil;
026
027import java.io.File;
028import java.io.IOException;
029import java.io.InputStream;
030
031public final class Assertions
032{
033
034    public static void assertFileContentsFromResource( final String resourceBase, final String resourceName,
035                                                       final File output, final String whyWouldItFail )
036        throws IOException
037    {
038        String content = readResource( resourceBase, resourceName );
039        String test = fileRead( output );
040
041        assertEquals( whyWouldItFail, content, test );
042    }
043
044    private static String readResource( final String base, final String name )
045        throws IOException
046    {
047        String url = base;
048        if ( !url.endsWith( "/" ) && !name.startsWith( "/" ) )
049        {
050            url += "/";
051        }
052        url += name;
053
054        ClassLoader cloader = Thread.currentThread().getContextClassLoader();
055        InputStream stream = cloader.getResourceAsStream( url );
056
057        if ( stream == null )
058        {
059            return null;
060        }
061
062        return IOUtil.toString( stream );
063    }
064
065}