aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/android/ConvertersTest.java
blob: 92c9cf2f229f032e20e62e7a6fbf6a321d7fd861 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2016 The Bazel Authors. 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.android;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.base.Joiner;
import com.google.devtools.build.android.Converters.ExistingPathConverter;
import com.google.devtools.build.android.Converters.ExistingPathListConverter;
import com.google.devtools.build.android.Converters.ExistingPathStringDictionaryConverter;
import com.google.devtools.build.android.Converters.PathConverter;
import com.google.devtools.build.android.Converters.PathListConverter;
import com.google.devtools.build.android.Converters.StringDictionaryConverter;
import com.google.devtools.common.options.OptionsParsingException;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link Converters}.
 */
@RunWith(JUnit4.class)
public final class ConvertersTest {

  private static final String SEPARATOR = File.pathSeparator;

  @Rule public final TemporaryFolder tmp = new TemporaryFolder();

  @Rule
  public final ExpectedException expected = ExpectedException.none();

  @Test
  public void testPathConverter_empty() throws Exception {
    PathConverter converter = new PathConverter();
    Path result = converter.convert("");
    assertThat((Object) result).isEqualTo(Paths.get(""));
  }

  @Test
  public void testPathConverter_invalid() throws Exception {
    String arg = "\u0000";
    expected.expect(OptionsParsingException.class);
    expected.expectMessage(String.format("%s is not a valid path:", arg));
    PathConverter converter = new PathConverter();
    converter.convert(arg);
  }

  @Test
  public void testPathConverter_valid() throws Exception {
    PathConverter converter = new PathConverter();
    Path result = converter.convert("test_file");
    assertThat((Object) result).isEqualTo(Paths.get("test_file"));
  }

  @Test
  public void testExistingPathConverter_nonExisting() throws Exception {
    String arg = "test_file";
    expected.expect(OptionsParsingException.class);
    expected.expectMessage(String.format("%s is not a valid path: it does not exist.", arg));
    ExistingPathConverter converter = new ExistingPathConverter();
    converter.convert(arg);
  }

  @Test
  public void testExistingPathConverter_existing() throws Exception {
    Path testFile = tmp.newFile("test_file").toPath();
    ExistingPathConverter converter = new ExistingPathConverter();
    Path result = converter.convert(testFile.toString());
    assertThat((Object) result).isEqualTo(testFile);
  }

  @Test
  public void testPathListConverter() throws Exception {
    PathListConverter converter = new PathListConverter();
    List<Path> result =
        converter.convert("foo" + SEPARATOR + "bar" + SEPARATOR + SEPARATOR + "baz" + SEPARATOR);
    assertThat(result)
        .containsAllOf(Paths.get("foo"), Paths.get("bar"), Paths.get("baz"))
        .inOrder();
  }

  @Test
  public void testExisingPathListConverter() throws Exception {
    String arg = "non-existing";
    Path existingFile = tmp.newFile("existing").toPath();
    expected.expect(OptionsParsingException.class);
    expected.expectMessage(String.format("%s is not a valid path: it does not exist.", arg));
    ExistingPathListConverter converter = new ExistingPathListConverter();
    converter.convert(Joiner.on(SEPARATOR).join(existingFile.toString(), arg));
  }

  @Test
  public void testStringDictionaryConverter_emptyEntry() throws Exception {
    expected.expect(OptionsParsingException.class);
    expected.expectMessage("Dictionary entry [] does not contain both a key and a value.");
    StringDictionaryConverter converter = new StringDictionaryConverter();
    converter.convert("foo:bar,,baz:bar");
  }

  @Test
  public void testStringDictionaryConverter_missingKeyOrValue() throws Exception {
    String badEntry = "foo";
    expected.expect(OptionsParsingException.class);
    expected.expectMessage(String.format(
        "Dictionary entry [%s] does not contain both a key and a value.", badEntry));
    StringDictionaryConverter converter = new StringDictionaryConverter();
    converter.convert(badEntry);
  }

  @Test
  public void testStringDictionaryConverter_extraFields() throws Exception {
    String badEntry = "foo:bar:baz";
    expected.expect(OptionsParsingException.class);
    expected.expectMessage(String.format(
        "Dictionary entry [%s] contains too many fields.", badEntry));
    StringDictionaryConverter converter = new StringDictionaryConverter();
    converter.convert(badEntry);
  }

  @Test
  public void testStringDictionaryConverter_duplicateKey() throws Exception {
    String key = "foo";
    String arg = String.format("%s:%s,%s:%s", key, "bar", key, "baz");
    expected.expect(OptionsParsingException.class);
    expected.expectMessage(String.format(
        "Dictionary already contains the key [%s].", key));
    StringDictionaryConverter converter = new StringDictionaryConverter();
    converter.convert(arg);
  }

  @Test
  public void testStringDictionaryConverter() throws Exception {
    StringDictionaryConverter converter = new StringDictionaryConverter();
    Map<String, String> result = converter.convert("foo:bar,baz:messy\\:stri\\,ng");
    assertThat(result).containsExactly("foo", "bar", "baz", "messy:stri,ng");
  }

  @Test
  public void testExistingPathStringDictionaryConverter() throws Exception {
    Path existingFile = tmp.newFile("existing").toPath();
    ExistingPathStringDictionaryConverter converter = new ExistingPathStringDictionaryConverter();
    Map<Path, String> result =
        converter
            // On Windows, the path starts like C:\foo\bar\..., we need to escape the colon.
            .convert(String.format("%s:string", existingFile.toString().replace(":", "\\:")));
    assertThat(result).containsExactly(existingFile, "string");
  }
}