@org.junit.Test public void testAddsDefaultIncludeAndExcludePatternsWhenTestScanningIsOff() { configureTask(); test.setScanForTestClasses(false); ConfigurableFileTree files = (ConfigurableFileTree) test.getCandidateClassFiles(); assertThat(files.getDir(), equalTo(classesDir)); assertThat(files.getIncludes(), equalTo(toSet("**/*Tests.class", "**/*Test.class"))); assertThat(files.getExcludes(), equalTo(toSet("**/Abstract*.class"))); }
public ExecutionFailure assertThatCause(final Matcher<String> matcher) { if (failure instanceof LocationAwareException) { LocationAwareException exception = (LocationAwareException) failure; assertThat(exception.getReportableCauses(), hasItem(hasMessage(matcher))); } else { assertThat(failure.getCause(), notNullValue()); assertThat(failure.getCause().getMessage(), matcher); } return this; }
@org.junit.Test public void testInit() { assertThat(test.getTestFramework(), instanceOf(JUnitTestFramework.class)); assertNull(test.getTestClassesDir()); assertNull(test.getClasspath()); assertNull(test.getTestResultsDir()); assertNull(test.getTestReportDir()); assertThat(test.getIncludes(), isEmpty()); assertThat(test.getExcludes(), isEmpty()); assertFalse(test.isIgnoreFailures()); }
@org.junit.Test public void testScansForTestClassesInTheTestClassesDir() { configureTask(); test.include("include"); test.exclude("exclude"); FileTree classFiles = test.getCandidateClassFiles(); assertThat(classFiles, instanceOf(ConfigurableFileTree.class)); ConfigurableFileTree files = (ConfigurableFileTree) classFiles; assertThat(files.getDir(), equalTo(classesDir)); assertThat(files.getIncludes(), equalTo(toSet("include"))); assertThat(files.getExcludes(), equalTo(toSet("exclude"))); }
@Test public void copyActionCanChangeFileDestinationPath() { FileCopyDetails copyDetails = expectActionExecutedWhenFileVisited(); RelativePath newPath = new RelativePath(true, "new"); copyDetails.setRelativePath(newPath); assertThat(copyDetails.getRelativePath(), equalTo(newPath)); copyDetails.setPath("/a/b"); assertThat(copyDetails.getRelativePath(), equalTo(new RelativePath(true, "a", "b"))); copyDetails.setName("new name"); assertThat(copyDetails.getRelativePath(), equalTo(new RelativePath(true, "a", "new name"))); }
private void assertMatches(final String pattern, String matches, String... otherNames) { final Task task1 = task(matches); final Task task2 = task(matches); final Set<Task> tasks = new HashSet<Task>(); tasks.add(task2); for (String name : otherNames) { tasks.add(task(name)); } tasks.add(task(".")); tasks.add(task("other")); context.checking( new Expectations() { { one(project).getTasksByName(pattern, true); will(returnValue(toSet())); one(taskContainer).getAll(); will(returnValue(toSet(task1))); one(subProjectTaskContainer).getAll(); will(returnValue(tasks)); one(taskExecuter).addTasks(toSet(task1, task2)); } }); TaskNameResolvingBuildExecuter executer = new TaskNameResolvingBuildExecuter(toList(pattern)); executer.select(gradle); assertThat(executer.getDisplayName(), equalTo(String.format("primary task '%s'", matches))); }
@Test public void failsWhenUnknownTaskNameIsProvided() { final Task task1 = task("t1"); final Task task2 = task("t2"); context.checking( new Expectations() { { one(project).getTasksByName("b3", true); will(returnValue(toSet())); one(taskContainer).getAll(); will(returnValue(toSet(task1, task2))); one(subProjectTaskContainer).getAll(); will(returnValue(toSet())); } }); BuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("b3")); try { executer.select(gradle); fail(); } catch (TaskSelectionException e) { assertThat(e.getMessage(), equalTo("Task 'b3' not found in [project].")); } }
@Test public void treatsEachProvidedNameAsASeparateGroup() { final Task task1 = task("name1"); final Task task2 = task("name2"); context.checking( new Expectations() { { one(project).getTasksByName("name1", true); will(returnValue(toSet(task1))); one(project).getTasksByName("name2", true); will(returnValue(toSet(task2))); Sequence sequence = context.sequence("tasks"); one(taskExecuter).addTasks(toSet(task1)); inSequence(sequence); one(taskExecuter).addTasks(toSet(task2)); inSequence(sequence); one(taskExecuter).execute(); inSequence(sequence); } }); TaskNameResolvingBuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("name1", "name2")); executer.select(gradle); assertThat(executer.getDisplayName(), equalTo("primary tasks 'name1', 'name2'")); executer.execute(); }
@Test public void reportsTyposInTaskName() { final Task task1 = task("someTask"); final Task task2 = task("someTasks"); final Task task3 = task("sometask"); final Task task4 = task("other"); context.checking( new Expectations() { { one(project).getTasksByName("ssomeTask", true); will(returnValue(toSet())); one(taskContainer).getAll(); will(returnValue(toSet(task1, task2))); one(subProjectTaskContainer).getAll(); will(returnValue(toSet(task3, task4))); } }); TaskNameResolvingBuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("ssomeTask")); try { executer.select(gradle); fail(); } catch (TaskSelectionException e) { assertThat( e.getMessage(), equalTo( "Task 'ssomeTask' not found in [project]. Some candidates are: 'someTask', 'someTasks', 'sometask'.")); } }
@Test public void failsWhenProvidedNameIsAmbiguous() { final Task task1 = task("someTask"); final Task task2 = task("someTasks"); context.checking( new Expectations() { { one(project).getTasksByName("soTa", true); will(returnValue(toSet())); one(taskContainer).getAll(); will(returnValue(toSet(task1))); one(subProjectTaskContainer).getAll(); will(returnValue(toSet(task2))); } }); TaskNameResolvingBuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("soTa")); try { executer.select(gradle); fail(); } catch (TaskSelectionException e) { assertThat( e.getMessage(), equalTo( "Task 'soTa' is ambiguous in [project]. Candidates are: 'someTask', 'someTasks'.")); } }
@Test public void testGetModuleDependencies() throws IOException, ParseException { prepareResolveReport(); final ResolvedDependency root = context.mock(ResolvedDependency.class, "root"); final ResolvedDependency resolvedDependency1 = context.mock(ResolvedDependency.class, "resolved1"); final ResolvedDependency resolvedDependency2 = context.mock(ResolvedDependency.class, "resolved2"); final Set<ResolvedDependency> resolvedDependenciesSet = toSet(resolvedDependency1, resolvedDependency2); context.checking( new Expectations() { { allowing(conversionResultStub).getRoot(); will(returnValue(root)); allowing(root).getChildren(); will(returnValue(resolvedDependenciesSet)); } }); ModuleDescriptor moduleDescriptor = createAnonymousModuleDescriptor(); prepareTestsThatRetrieveDependencies(moduleDescriptor); Set<ResolvedDependency> actualFirstLevelModuleDependencies = ivyDependencyResolver.resolve(configurationStub).getFirstLevelModuleDependencies(); assertThat(actualFirstLevelModuleDependencies, equalTo(resolvedDependenciesSet)); }
@Test public void testResolveAndRethrowFailureWithMissingDependenciesShouldThrowResolveException() throws IOException, ParseException { ModuleDescriptor moduleDescriptor = createAnonymousModuleDescriptor(); prepareTestsThatRetrieveDependencies(moduleDescriptor); Exception failure = new Exception("broken"); prepareResolveReportWithError(failure); ResolvedConfiguration configuration = ivyDependencyResolver.resolve(configurationStub); assertTrue(configuration.hasError()); try { configuration.rethrowFailure(); fail(); } catch (ResolveException e) { assertThat( e.getMessage(), startsWith("Could not resolve all dependencies for <configuration>")); assertThat(e.getCause(), sameInstance((Throwable) failure)); } }
@org.junit.Test public void testExecuteWithTestFailuresAndStopAtFailures() { configureTask(); expectTestsFail(); try { test.executeTests(); fail(); } catch (GradleException e) { assertThat(e.getMessage(), startsWith("There were failing tests. See the report at")); } }
public void beforeExecute(Task task) { assertThat(current, nullValue()); assertTrue(planned.contains(task)); current = task; String taskPath = path(task); if (taskPath.startsWith(":buildSrc:")) { return; } executedTasks.add(taskPath); }
public void afterExecute(Task task, TaskState state) { assertThat(task, sameInstance(current)); current = null; String taskPath = path(task); if (taskPath.startsWith(":buildSrc:")) { return; } if (state.getSkipped()) { skippedTasks.add(taskPath); } }
@Test public void visitFileInvokesEachCopyAction() { final Action<FileCopyDetails> action1 = context.mock(Action.class, "action1"); final Action<FileCopyDetails> action2 = context.mock(Action.class, "action2"); final Collector<FileCopyDetails> collectDetails1 = collector(); final Collector<Object> collectDetails2 = collector(); final Collector<Object> collectDetails3 = collector(); context.checking( new Expectations() { { Sequence seq = context.sequence("seq"); one(delegate).visitSpec(spec); inSequence(seq); allowing(spec).getAllCopyActions(); will(returnValue(toList(action1, action2))); one(action1).execute(with(notNullValue(FileCopyDetails.class))); inSequence(seq); will(collectTo(collectDetails1)); one(action2).execute(with(notNullValue(FileCopyDetails.class))); inSequence(seq); will(collectTo(collectDetails2)); one(delegate).visitFile(with(not(sameInstance(details)))); inSequence(seq); will(collectTo(collectDetails3)); } }); visitor.visitSpec(spec); visitor.visitFile(details); assertThat(collectDetails1.get(), sameInstance(collectDetails2.get())); assertThat(collectDetails1.get(), sameInstance(collectDetails3.get())); }
@Test public void testResolveAndGetFilesWithMissingDependenciesShouldThrowResolveException() throws IOException, ParseException { ModuleDescriptor moduleDescriptor = createAnonymousModuleDescriptor(); prepareTestsThatRetrieveDependencies(moduleDescriptor); Exception failure = new Exception("broken"); prepareResolveReportWithError(failure); ResolvedConfiguration configuration = ivyDependencyResolver.resolve(configurationStub); context.checking( new Expectations() { { allowing(configurationStub).getAllDependencies(); } }); try { configuration.getFiles(Specs.SATISFIES_ALL); fail(); } catch (ResolveException e) { assertThat( e.getMessage(), startsWith("Could not resolve all dependencies for <configuration>")); assertThat(e.getCause(), sameInstance((Throwable) failure)); } }
@Test public void wrappedFileElementDelegatesToSourceForRemainingMethods() { final FileVisitDetails mappedDetails = expectSpecAndFileVisited(); final File file = new File("file"); context.checking( new Expectations() { { one(details).getFile(); will(returnValue(file)); } }); assertThat(mappedDetails.getFile(), sameInstance(file)); }
@Test public void getSizeReturnsSizeOfFilteredContent() { final FileCopyDetails mappedDetails = expectActionExecutedWhenFileVisited(); context.checking( new Expectations() { { one(details).open(); will(returnValue(new ByteArrayInputStream("content".getBytes()))); } }); mappedDetails.filter(HelperUtil.toClosure("{ 'PREFIX: ' + it } ")); assertThat(mappedDetails.getSize(), equalTo(15L)); }
@Test public void relativePathForDirIsSpecPathPlusFilePath() { FileVisitDetails visitDetails = expectSpecAndDirVisited(); context.checking( new Expectations() { { allowing(spec).getDestPath(); will(returnValue(new RelativePath(false, "spec"))); allowing(details).getRelativePath(); will(returnValue(new RelativePath(false, "dir"))); } }); assertThat(visitDetails.getRelativePath(), equalTo(new RelativePath(false, "spec", "dir"))); }
@Test public void initialRelativePathForFileIsSpecPathPlusFilePath() { FileCopyDetails copyDetails = expectActionExecutedWhenFileVisited(); context.checking( new Expectations() { { allowing(spec).getDestPath(); will(returnValue(new RelativePath(false, "spec"))); allowing(details).getRelativePath(); will(returnValue(new RelativePath(true, "file"))); } }); assertThat(copyDetails.getRelativePath(), equalTo(new RelativePath(true, "spec", "file"))); }
@Test public void testGetResolvedArtifacts() throws IOException, ParseException { prepareResolveReport(); final ResolvedArtifact resolvedArtifactDummy = context.mock(ResolvedArtifact.class); final Set<ResolvedArtifact> resolvedArtifacts = toSet(resolvedArtifactDummy); context.checking( new Expectations() { { allowing(conversionResultStub).getResolvedArtifacts(); will(returnValue(resolvedArtifacts)); } }); ModuleDescriptor moduleDescriptor = createAnonymousModuleDescriptor(); prepareTestsThatRetrieveDependencies(moduleDescriptor); assertThat( ivyDependencyResolver.resolve(configurationStub).getResolvedArtifacts(), equalTo(resolvedArtifacts)); }
@Test public void copyActionCanFilterContentWhenFileIsCopiedToStream() { final FileCopyDetails mappedDetails = expectActionExecutedWhenFileVisited(); context.checking( new Expectations() { { one(details).open(); will(returnValue(new ByteArrayInputStream("content".getBytes()))); } }); mappedDetails.filter(HelperUtil.toClosure("{ 'PREFIX: ' + it } ")); ByteArrayOutputStream outstr = new ByteArrayOutputStream(); mappedDetails.copyTo(outstr); assertThat(new String(outstr.toByteArray()), equalTo("PREFIX: content")); }
@Test public void failsWhenUnknownTaskPathIsProvided() { context.checking( new Expectations() { { one(project).findProject("a"); will(returnValue(null)); } }); BuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("a:b", "name2")); try { executer.select(gradle); fail(); } catch (TaskSelectionException e) { assertThat(e.getMessage(), equalTo("Project 'a' not found in [project].")); } }
@Test public void selectsAllTasksWithTheProvidedNameInCurrentProjectAndSubprojects() { final Task task1 = task("name"); final Task task2 = task("name"); context.checking( new Expectations() { { one(project).getTasksByName("name", true); will(returnValue(toSet(task1, task2))); one(taskExecuter).addTasks(toSet(task1, task2)); } }); TaskNameResolvingBuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("name")); executer.select(gradle); assertThat(executer.getDisplayName(), equalTo("primary task 'name'")); }
@Test public void selectsTaskWithMatchingPath() { final Task task1 = task("b"); context.checking( new Expectations() { { one(project).findProject("a"); will(returnValue(subProject)); one(subProjectTaskContainer).findByName("b"); will(returnValue(task1)); one(taskExecuter).addTasks(toSet(task1)); } }); TaskNameResolvingBuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("a:b")); executer.select(gradle); assertThat(executer.getDisplayName(), equalTo("primary task 'a:b'")); }
@Test public void usesCamelCaseAbbreviationToSelectTasksWhenNoExactMatchAndPathProvided() { final Task task1 = task("someTask"); final Task task2 = task("other"); context.checking( new Expectations() { { one(project).findProject("a:b:c"); will(returnValue(subProject)); one(subProjectTaskContainer).findByName("soTa"); will(returnValue(null)); one(subProjectTaskContainer).getAll(); will(returnValue(toSet(task1, task2))); one(taskExecuter).addTasks(toSet(task1)); } }); TaskNameResolvingBuildExecuter executer = new TaskNameResolvingBuildExecuter(toList("a:b:c:soTa")); executer.select(gradle); assertThat(executer.getDisplayName(), equalTo("primary task 'a:b:c:someTask'")); }
public ExecutionFailure assertThatDescription(Matcher<String> matcher) { assertThat(failure.getMessage(), containsLine(matcher)); return this; }
@Test public void testResolveAndGetFilesWithDependencySubset() throws IOException, ParseException { prepareResolveReport(); final ModuleDependency moduleDependencyDummy1 = context.mock(ModuleDependency.class, "dep1"); final ModuleDependency moduleDependencyDummy2 = context.mock(ModuleDependency.class, "dep2"); final SelfResolvingDependency selfResolvingDependencyDummy = context.mock(SelfResolvingDependency.class); final ResolvedDependency root = context.mock(ResolvedDependency.class, "root"); final ResolvedDependency resolvedDependency1 = context.mock(ResolvedDependency.class, "resolved1"); final ResolvedDependency resolvedDependency2 = context.mock(ResolvedDependency.class, "resolved2"); ResolvedDependency resolvedDependency3 = context.mock(ResolvedDependency.class, "resolved3"); final DependencySet dependencies = context.mock(DependencySet.class); final Map<Dependency, Set<ResolvedDependency>> firstLevelResolvedDependencies = new LinkedHashMap<Dependency, Set<ResolvedDependency>>(); firstLevelResolvedDependencies.put( moduleDependencyDummy1, toSet(resolvedDependency1, resolvedDependency2)); firstLevelResolvedDependencies.put(moduleDependencyDummy2, toSet(resolvedDependency3)); context.checking( new Expectations() { { allowing(resolvedDependency1).getParentArtifacts(root); will( returnValue( toSet( createResolvedArtifact( context, "dep1parent", "someType", "someExtension", new File("dep1parent"))))); allowing(resolvedDependency1).getChildren(); will(returnValue(emptySet())); allowing(resolvedDependency2).getParentArtifacts(root); will( returnValue( toSet( createResolvedArtifact( context, "dep2parent", "someType", "someExtension", new File("dep2parent"))))); allowing(resolvedDependency2).getChildren(); will(returnValue(emptySet())); allowing(configurationStub).getAllDependencies(); will(returnValue(dependencies)); allowing(dependencies).withType(ModuleDependency.class); will( returnValue( toDomainObjectSet( ModuleDependency.class, moduleDependencyDummy1, moduleDependencyDummy2))); allowing(conversionResultStub).getFirstLevelResolvedDependencies(); will(returnValue(firstLevelResolvedDependencies)); allowing(conversionResultStub).getRoot(); will(returnValue(root)); } }); ModuleDescriptor moduleDescriptor = createAnonymousModuleDescriptor(); prepareTestsThatRetrieveDependencies(moduleDescriptor); ResolvedConfiguration resolvedConfig = ivyDependencyResolver.resolve(configurationStub); Set<File> actualFiles = resolvedConfig.getFiles( new Spec<Dependency>() { public boolean isSatisfiedBy(Dependency element) { return element == moduleDependencyDummy1 || element == selfResolvingDependencyDummy; } }); assertThat(actualFiles, equalTo(toSet(new File("dep1parent"), new File("dep2parent")))); Set<ResolvedDependency> actualDeps = resolvedConfig.getFirstLevelModuleDependencies( new Spec<Dependency>() { public boolean isSatisfiedBy(Dependency element) { return element == moduleDependencyDummy1; } }); assertThat(actualDeps, equalTo(toSet(resolvedDependency1, resolvedDependency2))); }
public ExecutionFailure assertHasFileName(String filename) { assertThat(failure.getMessage(), startsWith(String.format("%s", filename))); return this; }