Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Refactor tests to run correctly on Windows os
Browse files Browse the repository at this point in the history
  • Loading branch information
DesMarshall01 committed Nov 17, 2017
1 parent 90a3c77 commit 8c0e4f8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;

Expand Down Expand Up @@ -33,7 +34,8 @@ public void shouldLoadObjectSchema() throws Exception {
@Test
public void shouldThrowExceptionIfFileDoesNotExist() throws Exception {
expectedException.expect(SchemaLoaderException.class);
expectedException.expectMessage("File failed to load: src/test/resources/schemas/unknown-file.json");
String message = "File failed to load: src/test/resources/schemas/unknown-file.json".replace('/', File.separatorChar);
expectedException.expectMessage(message);
expectedException.expectCause(isA(FileNotFoundException.class));

new SchemaLoader().loadFrom(get("src/test/resources/schemas/unknown-file.json").toFile());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package uk.gov.justice.generation.pojo.write;


import static java.util.Collections.singletonList;
import static org.apache.commons.io.FileUtils.cleanDirectory;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -15,6 +17,12 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;

import javax.lang.model.element.Modifier;

Expand All @@ -25,33 +33,35 @@

public class SourceWriterTest {

private final static String TEST_SOURCE_OUTPUT_DIR_NAME = "./target/test-generation";
private final static String TEST_PACKAGE_NAME = "org.bloggs.fred";

private final SourceWriter sourceWriter = new SourceWriter();

private File sourceOutputDirectory;

@Before
@SuppressWarnings("ResultOfMethodCallIgnored")
public void setup() throws Exception {
sourceOutputDirectory = new File("./target/test-generation");
sourceOutputDirectory = new File(TEST_SOURCE_OUTPUT_DIR_NAME);

sourceOutputDirectory.mkdirs();

if (sourceOutputDirectory.exists()) {
cleanDirectory(sourceOutputDirectory);
}

}

@Test
public void shouldWriteASingleSourceFile() throws Exception {
final String packageName = "org.bloggs.fred";

final TypeSpec helloWorld = simpleClassTypeSpec();

final ClassGeneratable classGenerator = mock(ClassGeneratable.class);
final GenerationContext generationContext = mock(GenerationContext.class);

when(classGenerator.generate()).thenReturn(helloWorld);
when(classGenerator.getPackageName()).thenReturn(packageName);
when(classGenerator.getPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(generationContext.getOutputDirectoryPath()).thenReturn(sourceOutputDirectory.toPath());

for (final ClassGeneratable classGeneratable : singletonList(classGenerator)) {
Expand All @@ -64,23 +74,37 @@ public void shouldWriteASingleSourceFile() throws Exception {
@Test
@SuppressWarnings("ResultOfMethodCallIgnored")
public void shouldThrowExceptionIfUnableToWriteJavaFile() throws Exception {
final String packageName = "org.bloggs.fred";

final TypeSpec helloWorld = simpleClassTypeSpec();

final ClassGeneratable classGenerator = mock(ClassGeneratable.class);
final GenerationContext generationContext = mock(GenerationContext.class);

when(classGenerator.generate()).thenReturn(helloWorld);
when(classGenerator.getPackageName()).thenReturn(packageName);
when(generationContext.getOutputDirectoryPath()).thenReturn(sourceOutputDirectory.toPath());
final Path destPathMock = mock(Path.class);
final FileSystem fileSystemMock = mock(FileSystem.class);
final FileSystemProvider providerMock = mock(FileSystemProvider.class);

sourceOutputDirectory.setWritable(false);
// Next few lines provide mocking to enable IOException to be thrown from
// FileSystemProvider.newOutputstream()
when(destPathMock.getFileSystem()).thenReturn(fileSystemMock);
when(destPathMock.toString()).thenReturn(TEST_SOURCE_OUTPUT_DIR_NAME);
when(destPathMock.resolve(anyString())).thenReturn(destPathMock);

when(fileSystemMock.provider()).thenReturn(providerMock);

final BasicFileAttributes basicFileAttributes = Files.readAttributes(sourceOutputDirectory.toPath(), BasicFileAttributes.class);
when(providerMock.readAttributes(anyObject(), (Class) anyObject(), new LinkOption[0])).thenReturn(basicFileAttributes);
when(providerMock.newOutputStream(anyObject(), anyVararg())).thenThrow(new IOException());

when(classGenerator.generate()).thenReturn(helloWorld);
when(classGenerator.getPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(generationContext.getOutputDirectoryPath()).thenReturn(destPathMock);

try {
sourceWriter.write(classGenerator, generationContext);

fail();

} catch (final SourceCodeWriteException ex) {
sourceOutputDirectory.setWritable(true);
assertThat(ex.getMessage(), is("Failed to write java file to './target/test-generation' for 'org.bloggs.fred.Address.java'"));
Expand Down

0 comments on commit 8c0e4f8

Please sign in to comment.