aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/android/AndroidResourceOutputsTest.java
blob: 9c109442851104d2dfb06219d067eda3076a0cbf (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
// Copyright 2017 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.jimfs.Jimfs;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link AndroidResourceOutputsTest}. */
@RunWith(JUnit4.class)
public class AndroidResourceOutputsTest {
  private Path tmp;

  @Before
  public void setUp() throws Exception {
    tmp =
        Files.createTempDirectory(
            Jimfs.newFileSystem().getRootDirectories().iterator().next(),
            getClass().getSimpleName());
  }

  @Test
  public void testZipEntryPaths() throws Exception {
    Path output = tmp.resolve("actual.zip");
    Files.createDirectories(tmp.resolve("foo/bar"));
    Files.write(tmp.resolve("foo/data1.txt"), "hello".getBytes(Charset.defaultCharset()));
    Files.write(tmp.resolve("foo/bar/data2.txt"), "world".getBytes(Charset.defaultCharset()));

    try (ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(output))) {
      AndroidResourceOutputs.ZipBuilderVisitor visitor =
          new AndroidResourceOutputs.ZipBuilderVisitor(zout, tmp.resolve("foo"), "some/prefix");
      Files.walkFileTree(tmp.resolve("foo"), visitor);
      visitor.writeEntries();
    }

    List<String> entries = new ArrayList<>();
    try (ZipInputStream zin = new ZipInputStream(Files.newInputStream(output))) {
      ZipEntry entry = null;
      while ((entry = zin.getNextEntry()) != null) {
        entries.add(entry.getName());
      }
    }
    assertThat(entries).containsExactly("some/prefix/data1.txt", "some/prefix/bar/data2.txt");
  }
}