Skip to content

Commit

Permalink
added new unit tests, fixed some old unit tests, refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Dmytro Kulieshov <dkuliesh@redhat.com>
  • Loading branch information
Dmytro Kulieshov committed Oct 13, 2017
1 parent 7df0368 commit 776d3d9
Show file tree
Hide file tree
Showing 45 changed files with 1,010 additions and 4,941 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ public ResourcesPlugin(
@Named("che.jdt.workspace.index.dir") String indexPath,
@Named("che.user.workspaces.storage") String workspacePath,
Provider<ProjectManager> projectManager,
Provider<PathTransformer> pathResolverProvider,
Provider<PathTransformer> pathTransformerProvider,
Provider<FsManager> fsManagerProvider) {
ResourcesPlugin.indexPath = indexPath;
ResourcesPlugin.workspacePath = workspacePath;
pluginId = "cheWsPlugin";
EFS.setWsPath(workspacePath);
workspace =
new Workspace(workspacePath, projectManager, pathResolverProvider, fsManagerProvider);
new Workspace(workspacePath, projectManager, pathTransformerProvider, fsManagerProvider);
}

public static String getPathToWorkspace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
import static java.lang.String.valueOf;
import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.FileVisitResult.TERMINATE;
import static java.nio.file.Files.walkFileTree;
import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.ext.java.shared.Constants.CONTAINS_JAVA_FILES;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.che.api.core.model.workspace.config.ProjectConfig;
import org.eclipse.che.api.fs.server.PathTransformer;
import org.eclipse.che.api.project.server.type.ReadonlyValueProvider;
import org.eclipse.che.api.project.server.type.ValueProvider;
import org.eclipse.che.api.project.server.type.ValueProviderFactory;
Expand All @@ -39,69 +41,82 @@
* @author gazarenkov
* @author Florent Benoit
*/
@Singleton
public class JavaValueProviderFactory implements ValueProviderFactory {

private final PathTransformer pathTransformer;

@Inject
public JavaValueProviderFactory(PathTransformer pathTransformer) {
this.pathTransformer = pathTransformer;
}

@Override
public ValueProvider newInstance(ProjectConfig projectConfig) {
return new JavaValueProvider(projectConfig);
return new JavaValueProvider(pathTransformer.transform(projectConfig.getPath()));
}

static class JavaValueProvider extends ReadonlyValueProvider {
static private class JavaValueProvider extends ReadonlyValueProvider {

/** The root folder of this project. */
private final String projectWsPath;
/** If true, it means that there are some java files in this folder or in its children. */
/**
* The root folder of this project.
*/
private final Path projectFsPath;
/**
* If true, it means that there are some java files in this folder or in its children.
*/
private boolean containsJavaFiles;
/** Try to perform the check on java files only once with lazy check. */
/**
* Try to perform the check on java files only once with lazy check.
*/
private boolean initialized = false;

public JavaValueProvider(ProjectConfig projectConfig) {
this.projectWsPath = projectConfig.getPath();
JavaValueProvider(Path projectFsPath) {
this.projectFsPath = projectFsPath;
this.initialized = false;
}

/**
* Check recursively if the given folder contains java files or any of its children
*
* @param projectWsPath the initial folder to check
* @return true if the folder or a subfolder contains java files
*/
protected boolean hasJavaFilesInFolder(final String projectWsPath) {
private boolean hasJavaFilesInFolder() {
try {
Path start = Paths.get("/projects/" + projectWsPath);
AtomicBoolean hasJavaFilesInFolder = new AtomicBoolean();
Files.walkFileTree(
start,
AtomicBoolean detectedJavaFiles = new AtomicBoolean();
walkFileTree(
projectFsPath,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (file.getFileName().endsWith(".java")) {
hasJavaFilesInFolder.getAndSet(true);
if (file.toFile().getName().endsWith(".java")) {
detectedJavaFiles.getAndSet(true);
return TERMINATE;
} else {
return CONTINUE;
}
}
});
return hasJavaFilesInFolder.get();
return detectedJavaFiles.get();
} catch (IOException e) {
throw new IllegalStateException(
String.format("Unable to get files from ''%s''", projectWsPath), e);
throw new IllegalStateException("Unable to get files from from: " + projectFsPath, e);
}

}

/**
* Checks if java files are available in the root folder or in any children of the root folder
*
* @throws ValueStorageException if there is an error when checking
*/
protected void init() throws ValueStorageException {
private void init() throws ValueStorageException {
try {
this.containsJavaFiles = hasJavaFilesInFolder(projectWsPath);
this.containsJavaFiles = hasJavaFilesInFolder();
} catch (IllegalStateException e) {
throw new ValueStorageException(
String.format("Unable to get files from ''%s''", projectWsPath) + e.getMessage());
String.format("Unable to get files from ''%s'' because of %s", projectFsPath,
e.getMessage()));
}
this.initialized = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,27 @@
*/
package org.eclipse.che.plugin.java.server.projecttype;

import static java.nio.file.Files.createFile;
import static java.nio.file.Files.createTempDirectory;
import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.ext.java.shared.Constants.CONTAINS_JAVA_FILES;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.eclipse.che.api.core.model.workspace.config.ProjectConfig;
import org.eclipse.che.api.fs.server.PathTransformer;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

/**
* Test for the Project Type provider
Expand All @@ -20,112 +39,93 @@
*/
@Listeners(value = {MockitoTestNGListener.class})
public class JavaValueProviderFactoryTest {
//
// @Mock private FolderEntry rootProjectFolder;
//
// /** In this case we have a folder with a java file, so it should find a java file */
// @Test
// public void checkFoundJavaFilesInCurrentFolder() throws Throwable {
//
// // we return a file entry that is a java file
// FileEntry fileEntry = mock(FileEntry.class);
// when(fileEntry.getName()).thenReturn("helloworld.java");
// when(rootProjectFolder.getChildFiles()).thenReturn(Collections.singletonList(fileEntry));
// ValueProvider javaPropertiesValueProvider =
// new JavaValueProviderFactory().newInstance(rootProjectFolder);
// List<String> hasJavaFiles = javaPropertiesValueProvider.getValues(CONTAINS_JAVA_FILES);
// assertNotNull(hasJavaFiles);
// assertEquals(hasJavaFiles, Collections.singletonList("true"));
// }
//
// /** In this case we have a folder with a javascript file, so it shouldn't find any java files */
// @Test
// public void checkNotFoundJavaFilesInCurrentFolder() throws Throwable {
//
// // we return a file entry that is a javascript file
// FileEntry fileEntry = mock(FileEntry.class);
// when(fileEntry.getName()).thenReturn("helloworld.js");
// when(rootProjectFolder.getChildFiles()).thenReturn(Collections.singletonList(fileEntry));
// ValueProvider javaPropertiesValueProvider =
// new JavaValueProviderFactory().newInstance(rootProjectFolder);
// try {
// javaPropertiesValueProvider.getValues(CONTAINS_JAVA_FILES);
// } catch (ValueStorageException e) {
// assertEquals(e.getMessage(), "There are no Java files inside the project");
// }
// }
//
// /**
// * In this case we have a folder with a javascript file, but some sub folders contains java files
// */
// @Test
// public void checkFoundJavaButNotInRootFolder() throws Throwable {
//
// // we return a file entry that is a javascript file
// FileEntry fileEntry = mock(FileEntry.class);
// when(fileEntry.getName()).thenReturn("helloworld.js");
// when(rootProjectFolder.getChildFiles()).thenReturn(Collections.singletonList(fileEntry));
//
// FileEntry javaFileEntry = mock(FileEntry.class);
// when(javaFileEntry.getName()).thenReturn("helloworld.java");
//
// FolderEntry subFolder = mock(FolderEntry.class);
// when(subFolder.getChildFiles()).thenReturn(Collections.singletonList(javaFileEntry));
// when(rootProjectFolder.getChildFolders()).thenReturn(Collections.singletonList(subFolder));
//
// ValueProvider javaPropertiesValueProvider =
// new JavaValueProviderFactory().newInstance(rootProjectFolder);
// List<String> hasJavaFiles = javaPropertiesValueProvider.getValues(CONTAINS_JAVA_FILES);
// assertNotNull(hasJavaFiles);
// assertEquals(hasJavaFiles, Collections.singletonList("true"));
// }
//
// /** In this case we have java file in a very deep folder */
// @Test
// public void checkFoundJavaDeepFolder() throws Throwable {
//
// // we return a file entry that is a javascript file
// FileEntry fileEntry = mock(FileEntry.class);
// when(fileEntry.getName()).thenReturn("helloworld.js");
// when(rootProjectFolder.getChildFiles()).thenReturn(Collections.singletonList(fileEntry));
//
// FolderEntry subFolder = mock(FolderEntry.class);
// when(subFolder.getChildFiles()).thenReturn(Collections.emptyList());
// when(rootProjectFolder.getChildFolders()).thenReturn(Collections.singletonList(subFolder));
//
// FileEntry javaFileEntry = mock(FileEntry.class);
// when(javaFileEntry.getName()).thenReturn("helloworld.java");
// FolderEntry subSubFolder = mock(FolderEntry.class);
// when(subSubFolder.getChildFiles()).thenReturn(Collections.singletonList(javaFileEntry));
// when(subFolder.getChildFolders()).thenReturn(Collections.singletonList(subSubFolder));
//
// ValueProvider javaPropertiesValueProvider =
// new JavaValueProviderFactory().newInstance(rootProjectFolder);
// List<String> hasJavaFiles = javaPropertiesValueProvider.getValues(CONTAINS_JAVA_FILES);
// assertNotNull(hasJavaFiles);
// assertEquals(hasJavaFiles, Collections.singletonList("true"));
// }
//
// /** In this case we have an exception while trying to search in sub folders */
// @Test(expectedExceptions = ValueStorageException.class)
// public void checkWithErrorInSubfolder() throws Throwable {
//
// // we return a file entry that is a javascript file
// FileEntry fileEntry = mock(FileEntry.class);
// when(fileEntry.getName()).thenReturn("helloworld.js");
// when(rootProjectFolder.getChildFiles()).thenReturn(Collections.singletonList(fileEntry));
//
// FileEntry javaFileEntry = mock(FileEntry.class);
// when(javaFileEntry.getName())
// .thenThrow(new IllegalStateException("unable to get name of this file"));
//
// FolderEntry subFolder = mock(FolderEntry.class);
// when(subFolder.getChildFiles()).thenReturn(Collections.singletonList(javaFileEntry));
// when(rootProjectFolder.getChildFolders()).thenReturn(Collections.singletonList(subFolder));
//
// ValueProvider javaPropertiesValueProvider =
// new JavaValueProviderFactory().newInstance(rootProjectFolder);
// javaPropertiesValueProvider.getValues(CONTAINS_JAVA_FILES);
// org.testng.Assert.fail("We should have exception reported");
// }

private static final String SIMPLE_NAME = JavaValueProviderFactoryTest.class.getSimpleName();

private static final String HELLOWORLD_JAVA = "helloworld.java";
private static final String PROJECT_PATH = "/project/path";
private static final String HELLOWORLD_JS = "helloworld.js";
@Mock
private PathTransformer pathTransformer;
@InjectMocks
private JavaValueProviderFactory javaValueProviderFactory;

@Mock
private ProjectConfig projectConfig;


private File projectDirectory;
private File subDirectory;
private File file;

@BeforeMethod
public void createTemporaryDirectory() throws Exception {
projectDirectory = createTempDirectory(SIMPLE_NAME).toFile();
}

@BeforeMethod
public void setUp() throws Exception {
when(projectConfig.getPath()).thenReturn(PROJECT_PATH);
when(pathTransformer.transform(PROJECT_PATH)).thenReturn(projectDirectory.toPath());
}

@AfterMethod
public void cleanCreateFilesAndDirectories() throws Exception {
projectDirectory.deleteOnExit();

if (subDirectory != null) {
subDirectory.deleteOnExit();
}

if (file != null) {
file.deleteOnExit();
}
}

/**
* In this case we have a folder with a java file, so it should find a java file
*/
@Test
public void checkFoundJavaFilesInCurrentFolder() throws Exception {
file = createFile(projectDirectory.toPath().resolve(HELLOWORLD_JAVA)).toFile();

List<String> hasJavaFiles = javaValueProviderFactory.newInstance(projectConfig)
.getValues(CONTAINS_JAVA_FILES);

assertNotNull(hasJavaFiles);
assertEquals(hasJavaFiles, singletonList("true"));
}

/**
* In this case we have a folder with a javascript file, so it shouldn't find any java files
*/
@Test
public void checkNotFoundJavaFilesInCurrentFolder() throws Exception {
file = createFile(projectDirectory.toPath().resolve(HELLOWORLD_JS)).toFile();

List<String> hasJavaFiles = javaValueProviderFactory.newInstance(projectConfig)
.getValues(CONTAINS_JAVA_FILES);

assertNotNull(hasJavaFiles);
assertEquals(hasJavaFiles, singletonList("false"));
}

/**
* In this case we have a folder with a javascript file, but some sub folders contains java files
*/
@Test
public void checkFoundJavaButNotInRootFolder() throws Throwable {
Path projectDirectoryPath = projectDirectory.toPath();
Path subDirectoryPath = projectDirectoryPath.resolve("subDirectory");

subDirectory = Files.createDirectory(subDirectoryPath).toFile();

file = createFile(subDirectory.toPath().resolve(HELLOWORLD_JAVA)).toFile();

List<String> hasJavaFiles = javaValueProviderFactory.newInstance(projectConfig)
.getValues(CONTAINS_JAVA_FILES);

assertNotNull(hasJavaFiles);
assertEquals(hasJavaFiles, singletonList("true"));
}
}
Loading

0 comments on commit 776d3d9

Please sign in to comment.