/* * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.serverSide.priority; import java.io.File; import java.util.*; import jetbrains.buildServer.buildTriggers.scheduler.Time; import jetbrains.buildServer.serverSide.ProjectManager; import jetbrains.buildServer.serverSide.SBuildType; import jetbrains.buildServer.serverSide.ServerPaths; import jetbrains.buildServer.serverSide.cleanup.CleanupCannotBeStartedException; import jetbrains.buildServer.serverSide.cleanup.ServerCleanupManager; import jetbrains.buildServer.serverSide.impl.cleanup.DefaultCleanupSettings; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jmock.Expectations; import org.jmock.Mockery; /** * @author dmitry.neverov */ final class Util { private static int BUILD_TYPE_SEQUENCE = 0; static SBuildType createBuildType(Mockery context, final String buildTypeId) { final SBuildType bt = context.mock(SBuildType.class, "bt" + BUILD_TYPE_SEQUENCE++); context.checking(new Expectations() {{ allowing(bt).getBuildTypeId(); will(returnValue(buildTypeId)); }}); return bt; } static Map prepareBuildTypes(Mockery context, final ProjectManager projectManager, @NotNull String... btIds) { final Map id2buildType = new HashMap(); for (String btId : btIds) { SBuildType buildType = createBuildType(context, btId); id2buildType.put(btId, buildType); } final Set> combinations = allCombinationsOf(Arrays.asList(btIds)); context.checking(new Expectations() {{ for (Map.Entry btEntry : id2buildType.entrySet()) { allowing(projectManager).findBuildTypeById(btEntry.getKey()); will(returnValue(btEntry.getValue())); } allowing(projectManager).getAllBuildTypes(); will(returnValue(new ArrayList(id2buildType.values()))); for (Set comb : combinations) { allowing(projectManager).findBuildTypes(comb); will(returnValue(getValuesForKeys(id2buildType, comb))); } }}); return id2buildType; } static File getTestDataDir() { File f = new File("svnrepo/priority-queue/server/testData"); if (f.isDirectory()) { return f; } return new File("server/testData"); } private static List getValuesForKeys(Map map, Set keys) { List result = new ArrayList(); for (String key : keys) { result.add(map.get(key)); } return result; } private static Set> allCombinationsOf(List l) { if (l.isEmpty()) { Set> result = new HashSet>(); result.add(new HashSet()); return result; } else { String head = head(l); Set> result = allCombinationsOf(tail(l)); Set> resultCopy = new HashSet>(result); for (Set s : resultCopy) { Set copy = new HashSet(s); copy.add(head); result.add(copy); } return result; } } private static T head(List l) { return l.get(0); } private static List tail(List l) { return l.subList(1, l.size()); } static ServerPaths getServerPaths(File rootDir) { File systemDir = new File(rootDir, "system"); File backupDir = new File(rootDir, "backup"); return new ServerPaths(systemDir.getAbsolutePath(), getTestDataDir().getAbsolutePath(), backupDir.getAbsolutePath()); } static class MockServerCleanupManager implements ServerCleanupManager { @NotNull public DefaultCleanupSettings getDefaultCleanupSettings() {throw new UnsupportedOperationException();} public void setCleanupStartTime(@Nullable final Time time) {throw new UnsupportedOperationException();} public Time getCleanupStartTime() {throw new UnsupportedOperationException();} public long getLastCleanupElapsedTime() {throw new UnsupportedOperationException();} public void startCleanup() throws CleanupCannotBeStartedException {throw new UnsupportedOperationException();} public boolean isCleanupCanBeStarted() {throw new UnsupportedOperationException();} public boolean executeWithInactiveCleanup(@NotNull final Runnable runnable, final boolean waitTillCleanupFinished) { runnable.run(); return true; } } }