001package org.eclipse.aether.spi.connector.layout; 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 java.net.URI; 023 024import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm; 025import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; 026import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport; 027import org.junit.Test; 028 029import org.eclipse.aether.spi.connector.layout.RepositoryLayout.ChecksumLocation; 030 031import static org.junit.Assert.assertEquals; 032 033public class ChecksumLocationTest 034{ 035 private ChecksumAlgorithmFactory SHA512 = new ChecksumAlgorithmFactorySupport("SHA-512", "sha512") { 036 @Override 037 public ChecksumAlgorithm getAlgorithm() { 038 throw new RuntimeException("this should not happen"); 039 } 040 }; 041 042 private ChecksumAlgorithmFactory SHA256 = new ChecksumAlgorithmFactorySupport("SHA-256", "sha256") { 043 @Override 044 public ChecksumAlgorithm getAlgorithm() { 045 throw new RuntimeException("this should not happen"); 046 } 047 }; 048 049 private ChecksumAlgorithmFactory SHA1 = new ChecksumAlgorithmFactorySupport("SHA-1", "sha1") { 050 @Override 051 public ChecksumAlgorithm getAlgorithm() { 052 throw new RuntimeException("this should not happen"); 053 } 054 }; 055 056 private ChecksumAlgorithmFactory MD5 = new ChecksumAlgorithmFactorySupport("MD5", "md5") { 057 @Override 058 public ChecksumAlgorithm getAlgorithm() { 059 throw new RuntimeException("this should not happen"); 060 } 061 }; 062 063 @Test 064 public void testForLocation() 065 { 066 ChecksumLocation cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), SHA512 ); 067 assertEquals( SHA512, cs.getChecksumAlgorithmFactory() ); 068 assertEquals( "dir/sub%20dir/file.txt.sha512", cs.getLocation().toString() ); 069 070 cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), SHA256 ); 071 assertEquals( SHA256, cs.getChecksumAlgorithmFactory() ); 072 assertEquals( "dir/sub%20dir/file.txt.sha256", cs.getLocation().toString() ); 073 074 cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), SHA1 ); 075 assertEquals( SHA1, cs.getChecksumAlgorithmFactory() ); 076 assertEquals( "dir/sub%20dir/file.txt.sha1", cs.getLocation().toString() ); 077 078 cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), MD5 ); 079 assertEquals( MD5, cs.getChecksumAlgorithmFactory() ); 080 assertEquals( "dir/sub%20dir/file.txt.md5", cs.getLocation().toString() ); 081 } 082 083 @Test( expected = IllegalArgumentException.class ) 084 public void testForLocation_WithQueryParams() 085 { 086 ChecksumLocation.forLocation( URI.create( "file.php?param=1" ), SHA1 ); 087 } 088 089 @Test( expected = IllegalArgumentException.class ) 090 public void testForLocation_WithFragment() 091 { 092 ChecksumLocation.forLocation( URI.create( "file.html#fragment" ), SHA1 ); 093 } 094 095}