aboutsummaryrefslogtreecommitdiff
path: root/jimfs/src/test/java/com/google/common/jimfs/PathTester.java
blob: b96d77e124b8f6a5ab26454787cf4f832c59d3a0 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright 2013 Google Inc.
 *
 * 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.common.jimfs;

import static com.google.common.base.Functions.toStringFunction;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

/** @author Colin Decker */
public final class PathTester {

  private final PathService pathService;
  private final String string;
  private String root;
  private ImmutableList<String> names = ImmutableList.of();

  public PathTester(PathService pathService, String string) {
    this.pathService = pathService;
    this.string = string;
  }

  public PathTester root(String root) {
    this.root = root;
    return this;
  }

  public PathTester names(Iterable<String> names) {
    this.names = ImmutableList.copyOf(names);
    return this;
  }

  public PathTester names(String... names) {
    return names(Arrays.asList(names));
  }

  public void test(String first, String... more) {
    Path path = pathService.parsePath(first, more);
    test(path);
  }

  public void test(Path path) {
    assertEquals(string, path.toString());

    testRoot(path);
    testNames(path);
    testParents(path);
    testStartsWith(path);
    testEndsWith(path);
    testSubpaths(path);
  }

  private void testRoot(Path path) {
    if (root != null) {
      assertTrue(path + ".isAbsolute() should be true", path.isAbsolute());
      assertNotNull(path + ".getRoot() should not be null", path.getRoot());
      assertEquals(root, path.getRoot().toString());
    } else {
      assertFalse(path + ".isAbsolute() should be false", path.isAbsolute());
      assertNull(path + ".getRoot() should be null", path.getRoot());
    }
  }

  private void testNames(Path path) {
    assertEquals(names.size(), path.getNameCount());
    assertEquals(names, names(path));
    for (int i = 0; i < names.size(); i++) {
      assertEquals(names.get(i), path.getName(i).toString());
      // don't test individual names if this is an individual name
      if (names.size() > 1) {
        new PathTester(pathService, names.get(i)).names(names.get(i)).test(path.getName(i));
      }
    }
    if (names.size() > 0) {
      String fileName = names.get(names.size() - 1);
      assertEquals(fileName, path.getFileName().toString());
      // don't test individual names if this is an individual name
      if (names.size() > 1) {
        new PathTester(pathService, fileName).names(fileName).test(path.getFileName());
      }
    }
  }

  private void testParents(Path path) {
    Path parent = path.getParent();

    if (root != null && names.size() >= 1 || names.size() > 1) {
      assertNotNull(parent);
    }

    if (parent != null) {
      String parentName = names.size() == 1 ? root : string.substring(0, string.lastIndexOf('/'));
      new PathTester(pathService, parentName)
          .root(root)
          .names(names.subList(0, names.size() - 1))
          .test(parent);
    }
  }

  private void testSubpaths(Path path) {
    if (path.getRoot() == null) {
      assertEquals(path, path.subpath(0, path.getNameCount()));
    }

    if (path.getNameCount() > 1) {
      String stringWithoutRoot = root == null ? string : string.substring(root.length());

      // test start + 1 to end and start to end - 1 subpaths... this recursively tests all subpaths
      // actually tests most possible subpaths multiple times but... eh
      Path startSubpath = path.subpath(1, path.getNameCount());
      List<String> startNames =
          ImmutableList.copyOf(Splitter.on('/').split(stringWithoutRoot))
              .subList(1, path.getNameCount());

      new PathTester(pathService, Joiner.on('/').join(startNames))
          .names(startNames)
          .test(startSubpath);

      Path endSubpath = path.subpath(0, path.getNameCount() - 1);
      List<String> endNames =
          ImmutableList.copyOf(Splitter.on('/').split(stringWithoutRoot))
              .subList(0, path.getNameCount() - 1);

      new PathTester(pathService, Joiner.on('/').join(endNames)).names(endNames).test(endSubpath);
    }
  }

  private void testStartsWith(Path path) {
    // empty path doesn't start with any path
    if (root != null || !names.isEmpty()) {
      Path other = path;
      while (other != null) {
        assertTrue(path + ".startsWith(" + other + ") should be true", path.startsWith(other));
        assertTrue(
            path + ".startsWith(" + other + ") should be true", path.startsWith(other.toString()));
        other = other.getParent();
      }
    }
  }

  private void testEndsWith(Path path) {
    // empty path doesn't start with any path
    if (root != null || !names.isEmpty()) {
      Path other = path;
      while (other != null) {
        assertTrue(path + ".endsWith(" + other + ") should be true", path.endsWith(other));
        assertTrue(
            path + ".endsWith(" + other + ") should be true", path.endsWith(other.toString()));
        if (other.getRoot() != null && other.getNameCount() > 0) {
          other = other.subpath(0, other.getNameCount());
        } else if (other.getNameCount() > 1) {
          other = other.subpath(1, other.getNameCount());
        } else {
          other = null;
        }
      }
    }
  }

  private static List<String> names(Path path) {
    return FluentIterable.from(path).transform(toStringFunction()).toList();
  }
}