Gridarta Editor
MapPathTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.mapmodel;
21 
22 import org.jetbrains.annotations.NotNull;
23 import org.junit.Assert;
24 import org.junit.Test;
25 
30 public class MapPathTest {
31 
35  @Test
36  public void testAbsolute1() {
37  final AbsoluteMapPath p1 = check("/", new AbsoluteMapPath());
38  final AbsoluteMapPath p2 = check("/x", new AbsoluteMapPath(p1, "x"));
39  final AbsoluteMapPath p3 = check("/x/y", new AbsoluteMapPath(p2, "y"));
40  final AbsoluteMapPath p4 = check("/x/y/z", new AbsoluteMapPath(p3, "z"));
41  final AbsoluteMapPath p5 = check("/x/y", new AbsoluteMapPath(p4, ".."));
42  final AbsoluteMapPath p6 = check("/x/y", new AbsoluteMapPath(p5, "."));
43  final AbsoluteMapPath p7 = check("/x", new AbsoluteMapPath(p6, ".."));
44  final AbsoluteMapPath p8 = check("/", new AbsoluteMapPath(p7, ".."));
45  check("/", new AbsoluteMapPath(p8, ".."));
46  }
47 
51  @Test
52  public void testRelative1() {
53  final RelativeMapPath p1 = check("", new RelativeMapPath());
54  final RelativeMapPath p2 = check("x", new RelativeMapPath(p1, "x"));
55  final RelativeMapPath p3 = check("x/y", new RelativeMapPath(p2, "y"));
56  final RelativeMapPath p4 = check("x/y/z", new RelativeMapPath(p3, "z"));
57  final RelativeMapPath p5 = check("x/y", new RelativeMapPath(p4, ".."));
58  final RelativeMapPath p6 = check("x/y", new RelativeMapPath(p5, "."));
59  final RelativeMapPath p7 = check("x", new RelativeMapPath(p6, ".."));
60  final RelativeMapPath p8 = check("", new RelativeMapPath(p7, ".."));
61  final RelativeMapPath p9 = check("..", new RelativeMapPath(p8, ".."));
62  final RelativeMapPath p10 = check("..", new RelativeMapPath(p9, "."));
63  final RelativeMapPath p11 = check("../..", new RelativeMapPath(p10, ".."));
64  final RelativeMapPath p12 = check("../../x", new RelativeMapPath(p11, "x"));
65  final RelativeMapPath p13 = check("../../x", new RelativeMapPath(p12, "."));
66  final RelativeMapPath p14 = check("../../x/x", new RelativeMapPath(p13, "x"));
67  final RelativeMapPath p15 = check("../../x", new RelativeMapPath(p14, ".."));
68  final RelativeMapPath p16 = check("../..", new RelativeMapPath(p15, ".."));
69  check("../../..", new RelativeMapPath(p16, ".."));
70  }
71 
75  @Test(expected = IllegalArgumentException.class)
76  public void testInvalid1() {
77  new AbsoluteMapPath(new AbsoluteMapPath(), "");
78  }
79 
83  @Test(expected = IllegalArgumentException.class)
84  public void testInvalid2() {
85  new AbsoluteMapPath(new AbsoluteMapPath(), "/");
86  }
87 
91  @Test(expected = IllegalArgumentException.class)
92  public void testInvalid3() {
93  new AbsoluteMapPath(new AbsoluteMapPath(), "abc/def");
94  }
95 
100  @Test
102  check("../../../x/y/a/b", MapPathUtils.newMapPath("/x/y/a/b").getRelativeMapPathFrom(MapPathUtils.newAbsoluteMapPath("/a/b/c/d")));
103  check("../x/y/z", MapPathUtils.newMapPath("/a/b/x/y/z").getRelativeMapPathFrom(MapPathUtils.newAbsoluteMapPath("/a/b/c/d")));
109  }
110 
114  @Test
115  public void testGetMapComponent1() {
116  Assert.assertNull(MapPathUtils.newMapPath("").getMapComponent());
117  Assert.assertNull(MapPathUtils.newMapPath("a").getMapComponent());
118  Assert.assertNull(MapPathUtils.newMapPath("a/b").getMapComponent());
119  Assert.assertNull(MapPathUtils.newMapPath("a/b/c").getMapComponent());
120  Assert.assertNull(MapPathUtils.newMapPath("../..").getMapComponent());
121 
122  Assert.assertNull(MapPathUtils.newMapPath("/").getMapComponent());
123  Assert.assertNull(MapPathUtils.newMapPath("/a").getMapComponent());
124  Assert.assertEquals("a", MapPathUtils.newMapPath("/a/b").getMapComponent());
125  Assert.assertEquals("a", MapPathUtils.newMapPath("/a/b/c").getMapComponent());
126  Assert.assertEquals("abc", MapPathUtils.newMapPath("/abc/def/ghi").getMapComponent());
127  }
128 
136  @NotNull
137  private static <T extends MapPath> T check(@NotNull final String expected, @NotNull final T mapPath) {
138  Assert.assertEquals(expected, mapPath.toString());
139  return mapPath;
140  }
141 
142 }
net.sf.gridarta.model.mapmodel.MapPathTest.testInvalid2
void testInvalid2()
Checks that invalid path components are detected.
Definition: MapPathTest.java:84
net.sf.gridarta.model.mapmodel.AbsoluteMapPath
A MapPath that is absolute, that is, it starts with a "/".
Definition: AbsoluteMapPath.java:29
net.sf.gridarta.model.mapmodel.MapPathTest.testRelative1
void testRelative1()
Checks that relative map paths are correctly assembled.
Definition: MapPathTest.java:52
net.sf.gridarta.model.mapmodel.MapPathTest.testInvalid1
void testInvalid1()
Checks that invalid path components are detected.
Definition: MapPathTest.java:76
net.sf.gridarta.model.mapmodel.MapPathUtils.newMapPath
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
Definition: MapPathUtils.java:43
net.sf.gridarta.model.mapmodel.MapPathUtils
Utility class for MapPath related functions.
Definition: MapPathUtils.java:28
net.sf.gridarta.model.mapmodel.MapPathTest.check
static< T extends MapPath > T check(@NotNull final String expected, @NotNull final T mapPath)
Checks that a MapPath's string representation matches the expected value.
Definition: MapPathTest.java:137
net.sf.gridarta.model.mapmodel.MapPath.getMapComponent
String getMapComponent()
Returns the initial path component of a map path.
net.sf.gridarta.model.mapmodel.MapPathUtils.newAbsoluteMapPath
static AbsoluteMapPath newAbsoluteMapPath(@NotNull final String string)
Creates an AbsoluteMapPath instance from string representation.
Definition: MapPathUtils.java:57
net.sf.gridarta.model.mapmodel.MapPathTest.testInvalid3
void testInvalid3()
Checks that invalid path components are detected.
Definition: MapPathTest.java:92
net.sf.gridarta.model.mapmodel.RelativeMapPath
Represents a maps directory local map path.
Definition: RelativeMapPath.java:29
net.sf.gridarta.model.mapmodel.MapPathTest.testGetMapComponent1
void testGetMapComponent1()
Checks that MapPath#getMapComponent() works as expected.
Definition: MapPathTest.java:115
net.sf.gridarta.model.mapmodel.MapPathTest.testAbsolute1
void testAbsolute1()
Checks that absolute map paths are correctly assembled.
Definition: MapPathTest.java:36
net.sf.gridarta.model.mapmodel.MapPath.getRelativeMapPathFrom
RelativeMapPath getRelativeMapPathFrom(@NotNull AbsoluteMapPath mapPath)
Returns this map path relative to another map path.
net.sf.gridarta.model.mapmodel.MapPathTest.testGetRelativeMapPathTo1
void testGetRelativeMapPathTo1()
Checks that MapPath#getRelativeMapPathFrom(AbsoluteMapPath) works as expected.
Definition: MapPathTest.java:101
net.sf.gridarta.model.mapmodel.MapPathTest
Regression tests for MapPath.
Definition: MapPathTest.java:30