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