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.testhelpers;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.Reader;
25  import java.util.Collections;
26  import java.util.SortedSet;
27  
28  import org.apache.commons.io.function.IOSupplier;
29  import org.apache.rat.api.Document;
30  import org.apache.rat.document.DocumentNameMatcher;
31  import org.apache.rat.document.DocumentName;
32  import org.apache.rat.document.FSInfoTest;
33  
34  public class TestingDocument extends Document {
35  
36      private final Reader reader;
37      private final IOSupplier<InputStream> input;
38  
39      public TestingDocument() {
40          this("name");
41      }
42  
43      public TestingDocument(String name) {
44          this(name, null);
45      }
46  
47      public TestingDocument(DocumentName documentName) {
48          super(documentName, DocumentNameMatcher.MATCHES_ALL);
49          this.reader = null;
50          this.input = null;
51      }
52  
53      public TestingDocument(String name, DocumentNameMatcher matcher) {
54          super(DocumentName.builder().setName(name).setBaseName("").build(), matcher);
55          this.reader = null;
56          this.input = null;
57      }
58  
59      public TestingDocument(Reader reader, String name) {
60          super(DocumentName.builder().setName(name).setBaseName("").build(), DocumentNameMatcher.MATCHES_ALL);
61          this.reader = reader;
62          this.input = null;
63      }
64  
65      public TestingDocument(IOSupplier<InputStream> inputStream, String name) {
66          super(DocumentName.builder(FSInfoTest.UNIX).setName(name).setBaseName("").build(), DocumentNameMatcher.MATCHES_ALL);
67          this.input = inputStream;
68          this.reader = null;
69      }
70  
71      @Override
72      public Reader reader() throws IOException {
73              return reader == null ? new InputStreamReader(input.get()) : reader;
74      }
75  
76      @Override
77      public boolean isDirectory() {
78          return false;
79      }
80  
81      @Override
82      public SortedSet<Document> listChildren() {
83          return Collections.emptySortedSet();
84      }
85  
86      @Override
87      public InputStream inputStream() throws IOException {
88          if (input != null) {
89              return input.get();
90          }
91          throw new UnsupportedOperationException();
92      }
93  }