@Test public void testConstructor_shouldCreateEmptyMessage_whenNotPassedAMessage() throws Exception { //GIVEN //WHEN CyclicDependencyException exception = new CyclicDependencyException(); //THEN assertThat(exception.getMessage(), nullValue()); }
private static String format(List<Class> path, Class startClass) { if (path.size() == 0) { throw new IllegalArgumentException(); } int classPosition = Math.max(path.indexOf(startClass), 0); path = path.subList(classPosition, path.size()); int maxWordLength = findLongestClassNameLength(path); int middleWordPos = maxWordLength / 2 + MARGIN_SIZE; int loopLinePosition = maxWordLength + 2 * MARGIN_SIZE; StringBuilder builder = new StringBuilder(); addTopLines(builder, middleWordPos, loopLinePosition); for (Class clazz : path) { addLine(builder, clazz.getName(), middleWordPos, loopLinePosition); addLine(builder, "||", middleWordPos, loopLinePosition); } addHorizontalLine(builder, middleWordPos, loopLinePosition); return builder.toString(); }
public CyclicDependencyException(List<Class> path, Class startClass) { this(String.format("Class %s creates a cycle:%n%s", startClass.getName(), format(path, startClass))); }
private static void addTopLines(StringBuilder builder, int middleWordPos, int loopLinePosition) { builder.append(LINE_SEPARATOR); addHorizontalLine(builder, middleWordPos, loopLinePosition); addLine(builder, "||", middleWordPos, loopLinePosition); addLine(builder, "\\/", middleWordPos, loopLinePosition); }
@Test public void testConstructor_shouldCreateCause_whenPassedACause() throws Exception { //GIVEN Throwable cause = new Exception("Foo"); //WHEN CyclicDependencyException exception = new CyclicDependencyException(cause); //THEN assertThat(exception.getMessage(), CoreMatchers.is("java.lang.Exception: Foo")); assertThat(exception.getCause(), CoreMatchers.is(cause)); }
@Override public void checkCyclesStart(Class clazz, String name) { final Pair pair = new Pair(clazz, name); final LinkedHashSet<Pair> linkedHashSet = cycleDetectionStack.get(); if (linkedHashSet.contains(pair)) { throw new CyclicDependencyException(Pair.getClassList(linkedHashSet), clazz); } linkedHashSet.add(pair); }
@Test public void testConstructor_shouldCreateMessageAndCause_whenPassedAMessageAndCause() throws Exception { //GIVEN Throwable cause = new Exception(); //WHEN CyclicDependencyException exception = new CyclicDependencyException("Foo", cause); //THEN assertThat(exception.getMessage(), CoreMatchers.is("Foo")); assertThat(exception.getCause(), CoreMatchers.is(cause)); }
@Test(expected = IllegalArgumentException.class) public void newCyclicDependencyException_showThrowIllegalArgumentException_whenPathIsEmpty() throws Exception { //GIVEN, WHEN new CyclicDependencyException(new ArrayList<Class>(), CyclicFoo.class); //THEN fail("Should throw an exception as a wrong parameters are passed"); }
private static void addTopLines(StringBuilder builder, int middleWordPos, int loopLinePosition) { builder.append(LINE_SEPARATOR); addHorizontalLine(builder, middleWordPos, loopLinePosition); addLine(builder, "||", middleWordPos, loopLinePosition); addLine(builder, "\\/", middleWordPos, loopLinePosition); }
private static void addLine(StringBuilder builder, String content, int middleWordPos, int loopLinePosition) { int leftMarginSize = middleWordPos - content.length() / 2; int rightMarginSize = loopLinePosition - leftMarginSize - content.length(); builder.append(repeat(' ', leftMarginSize)); builder.append(content); builder.append(repeat(' ', rightMarginSize)); builder.append("||"); builder.append(LINE_SEPARATOR); }
private static String format(List<Class> path, Class startClass) { if (path.size() == 0) { throw new IllegalArgumentException(); } int classPosition = Math.max(path.indexOf(startClass), 0); path = path.subList(classPosition, path.size()); int maxWordLength = findLongestClassNameLength(path); int middleWordPos = maxWordLength / 2 + MARGIN_SIZE; int loopLinePosition = maxWordLength + 2 * MARGIN_SIZE; StringBuilder builder = new StringBuilder(); addTopLines(builder, middleWordPos, loopLinePosition); for (Class clazz : path) { addLine(builder, clazz.getName(), middleWordPos, loopLinePosition); addLine(builder, "||", middleWordPos, loopLinePosition); } addHorizontalLine(builder, middleWordPos, loopLinePosition); return builder.toString(); }
@Test public void testConstructor_shouldCreateMessage_whenPassedAMessage() throws Exception { //GIVEN //WHEN CyclicDependencyException exception = new CyclicDependencyException("Foo"); //THEN Assert.assertThat(exception.getMessage(), CoreMatchers.is("Foo")); }
@Test(expected = NullPointerException.class) public void newCyclicDependencyException_showThrowNullPointerException_whenStartClassIsNull() throws Exception { //GIVEN List<Class> path = new ArrayList<>(); path.add(CyclicFoo.class); //WHEN new CyclicDependencyException(path, null); //THEN fail("Should throw an exception as a wrong parameters are passed"); } }
public CyclicDependencyException(List<Class> path, Class startClass) { this(String.format("Class %s creates a cycle:%n%s", startClass.getName(), format(path, startClass))); }
@Test public void newCyclicDependencyException_showGenerateOneElementPath_whenCycleContainsOneElement() throws Exception { String expectedErrorMessage = "Class toothpick.data.CyclicFoo creates a cycle:\n" + "\n" + " ================\n" + " || ||\n" + " \\/ ||\n" + " toothpick.data.CyclicFoo ||\n" + " || ||\n" + " ================\n"; //GIVEN List<Class> path = new ArrayList<>(); path.add(CyclicFoo.class); //WHEN CyclicDependencyException exception = new CyclicDependencyException(path, CyclicFoo.class); //THEN assertThat(exception.getMessage(), is(expectedErrorMessage)); }
@Test(expected = NullPointerException.class) public void newCyclicDependencyException_showThrowNullPointerException_whenPathIsNull() throws Exception { //GIVEN, WHEN new CyclicDependencyException(null, CyclicFoo.class); //THEN fail("Should throw an exception as a wrong parameters are passed"); }
private static void addLine(StringBuilder builder, String content, int middleWordPos, int loopLinePosition) { int leftMarginSize = middleWordPos - content.length() / 2; int rightMarginSize = loopLinePosition - leftMarginSize - content.length(); builder.append(repeat(' ', leftMarginSize)); builder.append(content); builder.append(repeat(' ', rightMarginSize)); builder.append("||"); builder.append(LINE_SEPARATOR); }
@Test public void newCyclicDependencyException_showGenerateLastElementPath_whenCycleFinishesPath() throws Exception { String expectedErrorMessage = "Class toothpick.data.CyclicFoo creates a cycle:\n" + "\n" + " ================\n" + " || ||\n" + " \\/ ||\n" + " toothpick.data.CyclicFoo ||\n" + " || ||\n" + " ================\n"; //GIVEN List<Class> path = new ArrayList<>(); path.add(Bar.class); path.add(Foo.class); path.add(BarChild.class); path.add(CyclicFoo.class); //WHEN CyclicDependencyException exception = new CyclicDependencyException(path, CyclicFoo.class); //THEN assertThat(exception.getMessage(), is(expectedErrorMessage)); }