-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#18] Enhanced some unit tests for generators
- Loading branch information
1 parent
0b6dd1b
commit de12136
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...t/java/io/toolisticon/annotationprocessortoolkit/generators/SimpleResourceReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
|
||
|
||
} |
104 changes: 104 additions & 0 deletions
104
...t/java/io/toolisticon/annotationprocessortoolkit/generators/SimpleResourceWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
annotationprocessor/src/test/resources/testcases/generators/testTemplate.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
AB${value}EF |