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.rat.document;
20  
21  import java.io.IOException;
22  import java.nio.file.Path;
23  import org.apache.commons.io.FileUtils;
24  import org.apache.rat.api.Document;
25  import org.apache.rat.api.MetaData;
26  import org.apache.rat.test.utils.Resources;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  import java.io.BufferedReader;
31  import java.io.File;
32  import java.io.Reader;
33  import org.junit.jupiter.api.io.TempDir;
34  
35  import static org.assertj.core.api.Assertions.assertThat;
36  import static org.junit.jupiter.api.Assertions.assertThrows;
37  import static org.mockito.Mockito.mock;
38  import static org.mockito.Mockito.spy;
39  import static org.mockito.Mockito.when;
40  
41  public class FileDocumentTest {
42      private Document document;
43  
44      @TempDir
45      private static Path tempDir;
46  
47      @BeforeEach
48      public void setUp() throws Exception {
49  
50          File basedir = new File(tempDir.toFile(), Resources.SRC_TEST_RESOURCES);
51          basedir.mkdirs();
52          File sourceData = Resources.getExampleResource("exampleData/Source.java");
53          File file = new File(basedir, "Source.java");
54          FileUtils.copyFile(sourceData, file);
55          assertThat(file).exists();
56  
57          DocumentName docName = DocumentName.builder(basedir).build();
58          document = new FileDocument(docName, file, DocumentNameMatcher.MATCHES_ALL);
59      }
60  
61      @Test
62      public void reader() throws Exception {
63          Reader reader = document.reader();
64          assertThat(reader).isNotNull();
65          assertThat(new BufferedReader(reader).readLine()).isEqualTo("package elements;");
66      }
67  
68      @Test
69      public void getName() {
70          final DocumentName name = document.getName();
71          assertThat(name).isNotNull();
72      }
73  
74      /**
75       * As MetaData is set within the constructor of the underlying document,
76       * we use a combination of spy and mock to explicitly cover this code branch.
77       */
78      @Test
79      void handleFlowIfNoEncodingFound() {
80          Document spyDocument = spy(document);
81          MetaData mockMetaData = mock(MetaData.class);
82  
83          when(spyDocument.getMetaData()).thenReturn(mockMetaData);
84          assertThat(spyDocument.getMetaData()).isSameAs(mockMetaData);
85  
86          IOException expectedException = assertThrows(IOException.class, spyDocument::reader);
87          assertThat(expectedException.getMessage()).startsWith("No charset detected for document");
88  
89          // just raise test coverage and test uncovered branch
90          assertThat(spyDocument).isNotEqualTo(document.getName()); // NOSONAR we want to raise code coverage only
91      }
92  }