View Javadoc
1   package org.eclipse.aether.internal.impl;
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.assertThrows;
26  
27  import org.eclipse.aether.DefaultRepositorySystemSession;
28  import org.eclipse.aether.internal.test.util.TestUtils;
29  import org.eclipse.aether.repository.RemoteRepository;
30  import org.eclipse.aether.repository.RepositoryPolicy;
31  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
32  import org.eclipse.aether.transfer.TransferResource;
33  import static org.hamcrest.MatcherAssert.assertThat;
34  import static org.hamcrest.core.Is.is;
35  import org.junit.After;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  public class DefaultChecksumPolicyProviderTest
40  {
41  
42      private static final String CHECKSUM_POLICY_UNKNOWN = "unknown";
43  
44      private DefaultRepositorySystemSession session;
45  
46      private DefaultChecksumPolicyProvider provider;
47  
48      private RemoteRepository repository;
49  
50      private TransferResource resource;
51  
52      @Before
53      public void setup()
54      {
55          session = TestUtils.newSession();
56          provider = new DefaultChecksumPolicyProvider();
57          repository = new RemoteRepository.Builder( "test", "default", "file:/void" ).build();
58          resource = new TransferResource( repository.getId(), repository.getUrl(), "file.txt", null, null );
59      }
60  
61      @After
62      public void teardown()
63      {
64          provider = null;
65          session = null;
66          repository = null;
67          resource = null;
68      }
69  
70      @Test
71      public void testNewChecksumPolicy_Fail()
72      {
73          ChecksumPolicy policy =
74              provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_FAIL );
75          assertNotNull( policy );
76          assertEquals( FailChecksumPolicy.class, policy.getClass() );
77      }
78  
79      @Test
80      public void testNewChecksumPolicy_Warn()
81      {
82          ChecksumPolicy policy =
83              provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_WARN );
84          assertNotNull( policy );
85          assertEquals( WarnChecksumPolicy.class, policy.getClass() );
86      }
87  
88      @Test
89      public void testNewChecksumPolicy_Ignore()
90      {
91          ChecksumPolicy policy =
92              provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_IGNORE );
93          assertNull( policy );
94      }
95  
96      @Test( expected = IllegalArgumentException.class )
97      public void testNewChecksumPolicy_Unknown()
98      {
99          ChecksumPolicy policy = provider.newChecksumPolicy( session, repository, resource, CHECKSUM_POLICY_UNKNOWN );
100         assertNotNull( policy );
101         assertEquals( WarnChecksumPolicy.class, policy.getClass() );
102     }
103 
104     @Test
105     public void testGetEffectiveChecksumPolicy_EqualPolicies()
106     {
107         String[] policies =
108             { RepositoryPolicy.CHECKSUM_POLICY_FAIL, RepositoryPolicy.CHECKSUM_POLICY_WARN,
109                 RepositoryPolicy.CHECKSUM_POLICY_IGNORE };
110         for ( String policy : policies )
111         {
112             assertEquals( policy, policy, provider.getEffectiveChecksumPolicy( session, policy, policy ) );
113         }
114     }
115 
116     @Test
117     public void testGetEffectiveChecksumPolicy_DifferentPolicies()
118     {
119         String[][] testCases =
120             { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
121                 { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
122                 { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_WARN } };
123         for ( String[] testCase : testCases )
124         {
125             assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
126                           provider.getEffectiveChecksumPolicy( session, testCase[0], testCase[1] ) );
127             assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
128                           provider.getEffectiveChecksumPolicy( session, testCase[1], testCase[0] ) );
129         }
130     }
131 
132     @Test
133     public void testGetEffectiveChecksumPolicy_UnknownPolicies()
134     {
135         String[][] testCases =
136             { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
137                 { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_WARN },
138                 { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_IGNORE } };
139         for ( String[] testCase : testCases )
140         {
141             IllegalArgumentException e = assertThrows( IllegalArgumentException.class,
142                     () -> provider.getEffectiveChecksumPolicy( session, CHECKSUM_POLICY_UNKNOWN, testCase[1] ) );
143             assertThat( e.getMessage(), is("Unsupported policy: unknown") );
144             e = assertThrows( IllegalArgumentException.class,
145                     () -> provider.getEffectiveChecksumPolicy( session, testCase[1], CHECKSUM_POLICY_UNKNOWN ) );
146             assertThat( e.getMessage(), is("Unsupported policy: unknown") );
147         }
148     }
149 
150 }