PathHolder implements Serializable { @Override public String toString() { if (_toString != null) { return _toString; } if (_SEPARATOR_CHAR == File.separatorChar) { _toString = _pathString; } else { _toString = _pathString.replace( _SEPARATOR_CHAR, File.separatorChar); } return _toString; } PathHolder(Path path);  PathHolder(String pathString); @Override boolean equals(Object object); Path getPath(); @Override int hashCode(); @Override String toString();  }
@Test public void testToString() { Path path = Paths.get("testFile"); PathHolder pathHolder = new PathHolder(path); String toString = pathHolder.toString(); Assert.assertEquals(path.toString(), toString); Assert.assertEquals( ReflectionTestUtil.getFieldValue(pathHolder, "_pathString"), toString); Assert.assertSame(toString, path.toString()); }
@Test public void testToStringSwitchSeparator() { PathHolder pathHolder = new PathHolder(Paths.get("")); char foreignSeparatorChar = CharPool.SLASH; char separatorChar = File.separatorChar; if (separatorChar == CharPool.SLASH) { foreignSeparatorChar = CharPool.BACK_SLASH; } ReflectionTestUtil.setFieldValue( pathHolder, "_pathString", "testFolder" + foreignSeparatorChar + "testFile"); char originalSeparatorChar = ReflectionTestUtil.getAndSetFieldValue( pathHolder, "_SEPARATOR_CHAR", foreignSeparatorChar); try { Assert.assertEquals( "testFolder" + separatorChar + "testFile", pathHolder.toString()); } finally { ReflectionTestUtil.setFieldValue( pathHolder, "_SEPARATOR_CHAR", originalSeparatorChar); } }