View Javadoc
1   package org.eclipse.aether.spi.connector.layout;
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 java.net.URI;
23  
24  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm;
25  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
26  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport;
27  import org.junit.Test;
28  
29  import org.eclipse.aether.spi.connector.layout.RepositoryLayout.ChecksumLocation;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  public class ChecksumLocationTest
34  {
35      private ChecksumAlgorithmFactory SHA512 = new ChecksumAlgorithmFactorySupport("SHA-512", "sha512") {
36          @Override
37          public ChecksumAlgorithm getAlgorithm() {
38              throw new RuntimeException("this should not happen");
39          }
40      };
41  
42      private ChecksumAlgorithmFactory SHA256 = new ChecksumAlgorithmFactorySupport("SHA-256", "sha256") {
43          @Override
44          public ChecksumAlgorithm getAlgorithm() {
45              throw new RuntimeException("this should not happen");
46          }
47      };
48  
49      private ChecksumAlgorithmFactory SHA1 = new ChecksumAlgorithmFactorySupport("SHA-1", "sha1") {
50          @Override
51          public ChecksumAlgorithm getAlgorithm() {
52              throw new RuntimeException("this should not happen");
53          }
54      };
55  
56      private ChecksumAlgorithmFactory MD5 = new ChecksumAlgorithmFactorySupport("MD5", "md5") {
57          @Override
58          public ChecksumAlgorithm getAlgorithm() {
59              throw new RuntimeException("this should not happen");
60          }
61      };
62  
63      @Test
64      public void testForLocation()
65      {
66          ChecksumLocation cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), SHA512 );
67          assertEquals( SHA512, cs.getChecksumAlgorithmFactory() );
68          assertEquals( "dir/sub%20dir/file.txt.sha512", cs.getLocation().toString() );
69  
70          cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), SHA256 );
71          assertEquals( SHA256, cs.getChecksumAlgorithmFactory() );
72          assertEquals( "dir/sub%20dir/file.txt.sha256", cs.getLocation().toString() );
73  
74          cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), SHA1 );
75          assertEquals( SHA1, cs.getChecksumAlgorithmFactory() );
76          assertEquals( "dir/sub%20dir/file.txt.sha1", cs.getLocation().toString() );
77  
78          cs = ChecksumLocation.forLocation( URI.create( "dir/sub%20dir/file.txt" ), MD5 );
79          assertEquals( MD5, cs.getChecksumAlgorithmFactory() );
80          assertEquals( "dir/sub%20dir/file.txt.md5", cs.getLocation().toString() );
81      }
82  
83      @Test( expected = IllegalArgumentException.class )
84      public void testForLocation_WithQueryParams()
85      {
86          ChecksumLocation.forLocation( URI.create( "file.php?param=1" ), SHA1 );
87      }
88  
89      @Test( expected = IllegalArgumentException.class )
90      public void testForLocation_WithFragment()
91      {
92          ChecksumLocation.forLocation( URI.create( "file.html#fragment" ), SHA1 );
93      }
94  
95  }