aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/MockSimpleFileSystem.java
blob: d6f801f23e698b427ceab54cc2f3ea1171c7898d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.singlejar;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;

/**
 * FileSystem for testing. FileSystem supports exactly one one OutputStream for filename
 * specified in constructor.
 * Workflow for using this class in tests are following:
 * <ul>
 *   <li> Construct with exactly one outputFile. </li>
 *   <li> add some input files using method addFile </li>
 *   <li> check content of outputFile calling toByteArray </li>
 * </ul>
 */
public final class MockSimpleFileSystem implements SimpleFileSystem {

  private final String outputFileName;
  private ByteArrayOutputStream out;
  private final Map<String, byte[]> files = new HashMap<>();

  public MockSimpleFileSystem(String outputFileName) {
    this.outputFileName = outputFileName;
  }

  public void addFile(String name, byte[] content) {
    files.put(name, content);
  }

  public void addFile(String name, String content) {
    files.put(name, content.getBytes(UTF_8));
  }

  @Override
  public OutputStream getOutputStream(String filename) {
    assertEquals(outputFileName, filename);
    assertNull(out);
    out = new ByteArrayOutputStream();
    return out;
  }

  @Override
  public InputStream getInputStream(String filename) throws IOException {
    byte[] data = files.get(filename);
    if (data == null) {
      throw new FileNotFoundException();
    }
    return new ByteArrayInputStream(data);
  }

  @Override
  public File getFile(String filename) throws IOException {
    byte[] data = files.get(filename);
    if (data == null) {
      throw new FileNotFoundException();
    }
    File file = File.createTempFile(filename, null);
    Files.copy(new ByteArrayInputStream(data), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    return file;
  }

  @Override
  public boolean delete(String filename) {
    assertEquals(outputFileName, filename);
    assertNotNull(out);
    out = null;
    return true;
  }

  public byte[] toByteArray() {
    assertNotNull(out);
    return out.toByteArray();
  }
}