View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.checksum;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.charset.Charset;
24  import java.nio.charset.StandardCharsets;
25  import java.util.Properties;
26  
27  import org.apache.maven.enforcer.rule.api.EnforcerLogger;
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29  import org.apache.maven.enforcer.rules.checksum.NormalizeLineSeparatorReader.LineSeparator;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  import org.junit.jupiter.api.extension.ExtendWith;
36  import org.junit.jupiter.api.io.TempDir;
37  import org.mockito.InjectMocks;
38  import org.mockito.Mock;
39  import org.mockito.junit.jupiter.MockitoExtension;
40  
41  import static org.mockito.Mockito.when;
42  
43  /**
44   * Test the "RequireTextFileChecksum" rule
45   */
46  @ExtendWith(MockitoExtension.class)
47  class TestRequireTextFileChecksum {
48  
49      @Mock
50      private MavenProject project;
51  
52      @Mock
53      private EnforcerLogger log;
54  
55      @InjectMocks
56      private RequireTextFileChecksum rule;
57  
58      @TempDir
59      private File temporaryFolder;
60  
61      @BeforeEach
62      void setup() {
63          rule.setLog(log);
64      }
65  
66      @Test
67      void testFileChecksumMd5NormalizedFromUnixToWindows() throws IOException, EnforcerRuleException {
68          File f = File.createTempFile("junit", null, temporaryFolder);
69          FileUtils.fileWrite(f, "line1\nline2\n");
70  
71          rule.setFile(f);
72          rule.setChecksum("c6242222cf6ccdb15a43e0e5b1a08810");
73          rule.setType("md5");
74          rule.setNormalizeLineSeparatorTo(LineSeparator.WINDOWS);
75          rule.setEncoding(StandardCharsets.US_ASCII.name());
76  
77          rule.execute();
78      }
79  
80      @Test
81      void testFileChecksumMd5NormalizedFromWindowsToWindows() throws IOException, EnforcerRuleException {
82          File f = File.createTempFile("junit", null, temporaryFolder);
83          FileUtils.fileWrite(f, "line1\r\nline2\r\n");
84  
85          rule.setFile(f);
86          rule.setChecksum("c6242222cf6ccdb15a43e0e5b1a08810");
87          rule.setType("md5");
88          rule.setNormalizeLineSeparatorTo(LineSeparator.WINDOWS);
89          rule.setEncoding(StandardCharsets.US_ASCII.name());
90  
91          rule.execute();
92      }
93  
94      @Test
95      void testFileChecksumMd5NormalizedFromWindowsToUnix() throws IOException, EnforcerRuleException {
96          File f = File.createTempFile("junit", null, temporaryFolder);
97          FileUtils.fileWrite(f, "line1\r\nline2\r\n");
98  
99          rule.setFile(f);
100         rule.setChecksum("4fcc82a88ee38e0aa16c17f512c685c9");
101         rule.setType("md5");
102         rule.setNormalizeLineSeparatorTo(LineSeparator.UNIX);
103         rule.setEncoding(StandardCharsets.US_ASCII.name());
104 
105         rule.execute();
106     }
107 
108     @Test
109     void testFileChecksumMd5NormalizedFromUnixToUnix() throws IOException, EnforcerRuleException {
110         File f = File.createTempFile("junit", null, temporaryFolder);
111         FileUtils.fileWrite(f, "line1\nline2\n");
112 
113         rule.setFile(f);
114         rule.setChecksum("4fcc82a88ee38e0aa16c17f512c685c9");
115         rule.setType("md5");
116         rule.setNormalizeLineSeparatorTo(LineSeparator.UNIX);
117         rule.setEncoding(StandardCharsets.US_ASCII.name());
118 
119         rule.execute();
120     }
121 
122     @Test
123     void testFileChecksumMd5NormalizedWithMissingFileCharsetParameter() throws IOException, EnforcerRuleException {
124         File f = File.createTempFile("junit", null, temporaryFolder);
125         FileUtils.fileWrite(f, "line1\nline2\n");
126 
127         when(project.getProperties()).thenReturn(new Properties());
128 
129         rule.setFile(f);
130         rule.setChecksum("4fcc82a88ee38e0aa16c17f512c685c9");
131         rule.setType("md5");
132         rule.setNormalizeLineSeparatorTo(LineSeparator.UNIX);
133 
134         rule.execute();
135         // name is not unique therefore compare generated charset
136         Assertions.assertEquals(Charset.forName(System.getProperty("file.encoding")), rule.getEncoding());
137     }
138 }