001package org.conqat.lib.commons.uniformpath; 002 003import java.util.Objects; 004 005import org.assertj.core.api.AbstractAssert; 006import org.conqat.lib.commons.uniformpath.UniformPath.EType; 007 008/** Custom asserts on {@link UniformPath}. */ 009public class UniformPathAssert extends AbstractAssert<UniformPathAssert, UniformPath> { 010 011 private UniformPathAssert(UniformPath actual) { 012 super(actual, UniformPathAssert.class); 013 } 014 015 /** Creates a new {@link UniformPathAssert}. */ 016 public static UniformPathAssert assertThat(UniformPath actual) { 017 return new UniformPathAssert(actual); 018 } 019 020 /** Asserts that the uniform path has the given string representation. */ 021 public UniformPathAssert hasPath(String path) { 022 assertThat(actual).hasToString(path); 023 return this; 024 } 025 026 /** Asserts that the uniform path is a code path. */ 027 public UniformPathAssert isCodePath() { 028 return assertPathType(EType.CODE); 029 } 030 031 /** Asserts that the uniform path is an architecture path. */ 032 public UniformPathAssert isArchitecturePath() { 033 return assertPathType(EType.ARCHITECTURE); 034 } 035 036 /** Asserts that the uniform path is a non-code path. */ 037 public UniformPathAssert isNonCodePath() { 038 return assertPathType(EType.NON_CODE); 039 } 040 041 /** Asserts that the uniform path is an issue path. */ 042 public UniformPathAssert isIssuePath() { 043 return assertPathType(EType.ISSUES); 044 } 045 046 /** Asserts that the uniform path is a test path. */ 047 public UniformPathAssert isTestPath() { 048 return assertPathType(EType.TEST); 049 } 050 051 /** Asserts that the uniform path has the given type. */ 052 private UniformPathAssert assertPathType(EType pathType) { 053 isNotNull(); 054 if (!Objects.equals(actual.type, pathType)) { 055 failWithMessage("Expected path to have type <%s> but was <%s>", pathType, actual.type); 056 } 057 return this; 058 } 059 060 /** Asserts that the uniform path is a root path. */ 061 public UniformPathAssert isRootPath() { 062 isNotNull(); 063 if (!actual.isRoot()) { 064 failWithMessage("Expected path to be a root path"); 065 } 066 return this; 067 } 068 069 /** Asserts that the uniform path is not a root path. */ 070 public UniformPathAssert isNotRootPath() { 071 isNotNull(); 072 if (actual.isRoot()) { 073 failWithMessage("Expected path not to be a root path"); 074 } 075 return this; 076 } 077 078 /** Asserts that the uniform path has the given ancestor. */ 079 public UniformPathAssert hasAncestor(UniformPath potentialAncestor) { 080 isNotNull(); 081 if (!actual.hasAncestor(potentialAncestor)) { 082 failWithMessage("Expected <%s> to have ancestor <%s>", actual.toString(), potentialAncestor.toString()); 083 } 084 return this; 085 } 086 087 /** Asserts that the uniform path doesn't have the given ancestor. */ 088 public UniformPathAssert notHasAncestor(UniformPath potentialAncestor) { 089 isNotNull(); 090 if (actual.hasAncestor(potentialAncestor)) { 091 failWithMessage("Expected <%s> not to have ancestor <%s>", actual.toString(), potentialAncestor.toString()); 092 } 093 return this; 094 } 095 096 /** Asserts that the uniform path has the given descendant. */ 097 public UniformPathAssert hasDescendant(UniformPath potentialDescendant) { 098 isNotNull(); 099 if (!actual.hasDescendant(potentialDescendant)) { 100 failWithMessage("Expected <%s> to have descendant <%s>", actual.toString(), potentialDescendant.toString()); 101 } 102 return this; 103 } 104 105 /** Asserts that the uniform path doesn't have the given descendant. */ 106 public UniformPathAssert notHasDescendant(UniformPath potentialDescendant) { 107 isNotNull(); 108 if (actual.hasDescendant(potentialDescendant)) { 109 failWithMessage("Expected <%s> not to have descendant <%s>", actual.toString(), 110 potentialDescendant.toString()); 111 } 112 return this; 113 } 114 115 /** Asserts that the uniform path has the given parent. */ 116 public UniformPathAssert hasParent(UniformPath potentialParent) { 117 isNotNull(); 118 if (!actual.getParent().equals(potentialParent)) { 119 failWithMessage("Expected <%s> to be a parent of <%s>", potentialParent.toString(), actual.toString()); 120 } 121 return this; 122 } 123 124 /** Asserts that the uniform path doesn't have the given parent. */ 125 public UniformPathAssert notHasParent(UniformPath potentialParent) { 126 isNotNull(); 127 if (actual.getParent().equals(potentialParent)) { 128 failWithMessage("Expected <%s> not to be a parent of <%s>", potentialParent.toString(), actual.toString()); 129 } 130 return this; 131 } 132}