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      {
50          session = TestUtils.newSession();
51          provider = new DefaultChecksumPolicyProvider();
52          repository = new RemoteRepository.Builder( "test", "default", "file:/void" ).build();
53          resource = new TransferResource( repository.getId(), repository.getUrl(), "file.txt", null, null );
54      }
55  
56      @After
57      public void teardown()
58      {
59          provider = null;
60          session = null;
61          repository = null;
62          resource = null;
63      }
64  
65      @Test
66      public void testNewChecksumPolicy_Fail()
67      {
68          ChecksumPolicy policy =
69              provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_FAIL );
70          assertNotNull( policy );
71          assertEquals( FailChecksumPolicy.class, policy.getClass() );
72      }
73  
74      @Test
75      public void testNewChecksumPolicy_Warn()
76      {
77          ChecksumPolicy policy =
78              provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_WARN );
79          assertNotNull( policy );
80          assertEquals( WarnChecksumPolicy.class, policy.getClass() );
81      }
82  
83      @Test
84      public void testNewChecksumPolicy_Ignore()
85      {
86          ChecksumPolicy policy =
87              provider.newChecksumPolicy( session, repository, resource, RepositoryPolicy.CHECKSUM_POLICY_IGNORE );
88          assertNull( policy );
89      }
90  
91      @Test
92      public void testNewChecksumPolicy_Unknown()
93      {
94          ChecksumPolicy policy = provider.newChecksumPolicy( session, repository, resource, CHECKSUM_POLICY_UNKNOWN );
95          assertNotNull( policy );
96          assertEquals( WarnChecksumPolicy.class, policy.getClass() );
97      }
98  
99      @Test
100     public void testGetEffectiveChecksumPolicy_EqualPolicies()
101     {
102         String[] policies =
103             { RepositoryPolicy.CHECKSUM_POLICY_FAIL, RepositoryPolicy.CHECKSUM_POLICY_WARN,
104                 RepositoryPolicy.CHECKSUM_POLICY_IGNORE, CHECKSUM_POLICY_UNKNOWN };
105         for ( String policy : policies )
106         {
107             assertEquals( policy, policy, provider.getEffectiveChecksumPolicy( session, policy, policy ) );
108         }
109     }
110 
111     @Test
112     public void testGetEffectiveChecksumPolicy_DifferentPolicies()
113     {
114         String[][] testCases =
115             { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
116                 { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
117                 { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_WARN } };
118         for ( String[] testCase : testCases )
119         {
120             assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
121                           provider.getEffectiveChecksumPolicy( session, testCase[0], testCase[1] ) );
122             assertEquals( testCase[0] + " vs " + testCase[1], testCase[0],
123                           provider.getEffectiveChecksumPolicy( session, testCase[1], testCase[0] ) );
124         }
125     }
126 
127     @Test
128     public void testGetEffectiveChecksumPolicy_UnknownPolicies()
129     {
130         String[][] testCases =
131             { { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_FAIL },
132                 { RepositoryPolicy.CHECKSUM_POLICY_WARN, RepositoryPolicy.CHECKSUM_POLICY_WARN },
133                 { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_IGNORE } };
134         for ( String[] testCase : testCases )
135         {
136             assertEquals( "unknown vs " + testCase[1], testCase[0],
137                           provider.getEffectiveChecksumPolicy( session, CHECKSUM_POLICY_UNKNOWN, testCase[1] ) );
138             assertEquals( "unknown vs " + testCase[1], testCase[0],
139                           provider.getEffectiveChecksumPolicy( session, testCase[1], CHECKSUM_POLICY_UNKNOWN ) );
140         }
141     }
142 
143 }