Skip to content

Commit

Permalink
Add missing @Override-Annotations, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zaplatynski committed May 2, 2016
1 parent 573b541 commit b4f027b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ConsoleOutputStream extends ByteArrayOutputStream {
private MessageConsole messageConsole;
private SimpleAttributeSet attributes;
private PrintStream printStream;
private boolean isFirstLine;
private boolean firstLine;

/**
* Specify the option text color and PrintStream.
Expand All @@ -42,7 +42,7 @@ public ConsoleOutputStream(final MessageConsole messageConsole, final Color text
this.printStream = printStream;

if (messageConsole.isAppend()) {
isFirstLine = true;
firstLine = true;
}
}

Expand All @@ -52,6 +52,7 @@ public ConsoleOutputStream(final MessageConsole messageConsole, final Color text
* string<br> <br> The message will be treated differently depending on whether the line will be
* appended or inserted into the Document
*/
@Override
public void flush() {
final String message = toString();

Expand Down Expand Up @@ -110,11 +111,11 @@ private void clearBuffer() {
// In case both the standard out and standard err are being redirected
// we need to insert a newline character for the first line only

if (isFirstLine && messageConsole.getDocument().getLength() != 0) {
if (firstLine && messageConsole.getDocument().getLength() != 0) {
buffer.insert(0, "\n");
}

isFirstLine = false;
firstLine = false;
final String line = buffer.toString();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public RemoveLinesJob(final LimitLinesDocumentListener documentListener,
this.event = event;
}

@Override
public void run() {
removeLines(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,37 @@
*/
public class LoggingViewToolbarItem implements ExecutableToolbarItem {

@Override
public void execute(@NotNull final ToolbarContext context) {
ConsoleWindow.getInstance().show();
}

@Override
public String getLabel(@NotNull final ToolbarContext context) {
return "The Second-Hand Log";
}

@Override
public boolean isEnabled(@NotNull final ToolbarContext context) {
return true;
}

@Override
public boolean isVisible(@NotNull final ToolbarContext context) {
return true;
}

@Override
public Icon getIcon(@NotNull final ToolbarContext context) {
return ConsoleWindow.getInstance().getIcon();
}

@Override
public Icon getPressedIcon(@NotNull final ToolbarContext context) {
return ConsoleWindow.getInstance().getImageIconPressed();
}

@Override
public Icon getRollOverIcon(@NotNull final ToolbarContext context) {
return ConsoleWindow.getInstance().getIcon();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ public LoggingViewToolbarItemsPlugin() {
this.items = new ArrayList<ExecutableToolbarItem>();
}

@Override
public Collection<? extends JavaClientToolbarItem> getItems() {
return Collections.unmodifiableCollection(items);
}

@Override
public void setUp(@NotNull final BaseContext context) {
items.add(new LoggingViewToolbarItem());
}

@Override
public void tearDown() {
// Do nothing.
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package de.marza.firstspirit.modules.logging.console;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.awt.Color;

import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ConsoleOutputStreamTest {

private ConsoleOutputStream testling;

private SimpleAttributeSet attributes;

@Mock
private MessageConsole console;

@Mock
private Document document;

@Mock
private JTextComponent textComponent;

@Before
public void setUp() throws Exception {
testling = new ConsoleOutputStream(console, Color.BLACK, null);

when(console.getDocument()).thenReturn(document);
when(console.getTextComponent()).thenReturn(textComponent);

attributes = new SimpleAttributeSet();
StyleConstants.setForeground(attributes, Color.BLACK);
}

@Test
public void flushAppend() throws Exception {
when(console.isAppend()).thenReturn(true);

testling.write("Test 123\n".getBytes());

testling.flush();

verify(document).insertString(0, "Test 123\n", attributes);
}

@Test
public void flushInsert() throws Exception {
when(console.isAppend()).thenReturn(false);

testling.write("Test 123".getBytes());

testling.flush();

testling.write("\n".getBytes());

testling.flush();

verify(document).insertString(0, "Test 123\n", attributes);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package de.marza.firstspirit.modules.logging.console;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.awt.Color;

import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class MessageConsoleTest {

private MessageConsole testling;

@Mock
private JTextComponent textComponent;

@Mock
private Document document;

@Before
public void setUp() throws Exception {

when(textComponent.getDocument()).thenReturn(document);

testling = new MessageConsole(textComponent);

verify(textComponent).setEditable(false);
verify(textComponent).setBackground(Color.WHITE);
}

@Test
public void getDocument() throws Exception {
assertThat(testling.getDocument(), is(sameInstance(document)));
}

@Test
public void isAppend() throws Exception {
assertThat(testling.isAppend(), is(true));
}

@Test
public void getTextComponent() throws Exception {
assertThat(testling.getTextComponent(), is(sameInstance(textComponent)));
}

}

0 comments on commit b4f027b

Please sign in to comment.