Skip to content

Commit

Permalink
[#18] Enhanced some unit tests for generators
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Apr 13, 2018
1 parent 0b6dd1b commit de12136
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.toolisticon.annotationprocessortoolkit.generators;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;

import javax.tools.FileObject;
import java.io.IOException;
import java.io.StringReader;

/**
* Unit test for {@link SimpleJavaWriter}.
*/
public class SimpleResourceReaderTest {

@Test
public void testReadln() throws IOException {

FileObject fileObject = Mockito.mock(FileObject.class);
StringReader stringReader = Mockito.spy(new StringReader("abc\ndef\nhij"));
Mockito.when(fileObject.openReader(Mockito.anyBoolean())).thenReturn(stringReader);


SimpleResourceReader unit = new SimpleResourceReader(fileObject);

MatcherAssert.assertThat(unit.readLine(), org.hamcrest.Matchers.equalTo("abc"));
MatcherAssert.assertThat(unit.readLine(), org.hamcrest.Matchers.equalTo("def"));
MatcherAssert.assertThat(unit.readLine(), org.hamcrest.Matchers.equalTo("hij"));
MatcherAssert.assertThat(unit.readLine(), org.hamcrest.Matchers.nullValue());

unit.close();

Mockito.verify(stringReader, Mockito.times(1)).close();

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.toolisticon.annotationprocessortoolkit.generators;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import javax.tools.FileObject;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
* Unit test for {@link SimpleResourceWriter}.
*/
public class SimpleResourceWriterTest {

private SimpleResourceWriter unit;

private FileObject fileObject;

private StringWriter stringWriter;

@Before
public void init() throws IOException {

fileObject = Mockito.mock(FileObject.class);
stringWriter = Mockito.spy(new StringWriter());
Mockito.when(fileObject.openWriter()).thenReturn(stringWriter);


unit = new SimpleResourceWriter(fileObject);
}

@Test
public void testAppend() throws IOException {

unit.append("ABC");
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABC"));
Mockito.verify(stringWriter, Mockito.times(1)).flush();

unit.append("DEF");
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABCDEF"));
Mockito.verify(stringWriter, Mockito.times(2)).flush();

}

@Test
public void testWrite_strings() throws IOException {

unit.write("ABC");
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABC"));
Mockito.verify(stringWriter, Mockito.times(1)).flush();

unit.write("DEF");
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABCDEF"));
Mockito.verify(stringWriter, Mockito.times(2)).flush();

}

@Test
public void testWrite_charArrays() throws IOException {

unit.write("ABC".toCharArray());
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABC"));
Mockito.verify(stringWriter, Mockito.times(1)).flush();

unit.write("DEF".toCharArray());
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABCDEF"));
Mockito.verify(stringWriter, Mockito.times(2)).flush();

}

@Test
public void testWrite_template() throws IOException {

final String TEMPLATE = "/testcases/generators/testTemplate.tpl";
Map<String,Object> model = new HashMap<String,Object>();
model.put("value","CD");

unit.writeTemplate(TEMPLATE,model);
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABCDEF"));
Mockito.verify(stringWriter, Mockito.times(1)).flush();


}

@Test
public void testClose() throws IOException {

unit.write("ABC");
MatcherAssert.assertThat(stringWriter.getBuffer().toString(), Matchers.is("ABC"));
Mockito.verify(stringWriter, Mockito.times(1)).flush();

unit.close();

Mockito.verify(stringWriter, Mockito.times(1)).close();


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AB${value}EF

0 comments on commit de12136

Please sign in to comment.