aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/OptionFileExpanderTest.java
blob: eea87a5796253cb71c2b0698f1a4f2c388d4b032 (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
// 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.fail;

import com.google.devtools.build.singlejar.OptionFileExpander.OptionFileProvider;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Unit tests for {@link OptionFileExpander}.
 */
@RunWith(JUnit4.class)
public class OptionFileExpanderTest {

  private static class StoredOptionFileProvider implements OptionFileProvider {

    private Map<String, byte[]> availableFiles = new HashMap<>();

    void addFile(String filename, String content) {
      availableFiles.put(filename, content.getBytes(UTF_8));
    }

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

  @Test
  public void testNoExpansion() throws IOException {
    OptionFileExpander expander = new OptionFileExpander(new StoredOptionFileProvider());
    assertEquals(Arrays.asList("--some", "option", "list"),
        expander.expandArguments(Arrays.asList("--some", "option", "list")));
  }

  @Test
  public void testExpandSimpleOptionsFile() throws IOException {
    StoredOptionFileProvider provider = new StoredOptionFileProvider();
    provider.addFile("options", "--some option list");
    OptionFileExpander expander = new OptionFileExpander(provider);
    assertEquals(Arrays.asList("--some", "option", "list"),
        expander.expandArguments(Arrays.asList("@options")));
  }

  @Test
  public void testIllegalOptionsFile() {
    StoredOptionFileProvider provider = new StoredOptionFileProvider();
    provider.addFile("options", "'missing apostrophe");
    OptionFileExpander expander = new OptionFileExpander(provider);
    try {
      expander.expandArguments(Arrays.asList("@options"));
      fail();
    } catch (IOException e) {
      // Expected exception.
    }
  }
}