/* * Copyright 2000-2012 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 com.intellij.testFramework.fixtures.impl; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ContentEntry; import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.builders.ModuleFixtureBuilder; import com.intellij.testFramework.fixtures.IdeaProjectTestFixture; import com.intellij.testFramework.fixtures.ModuleFixture; import com.intellij.testFramework.fixtures.TestFixtureBuilder; import consulo.roots.impl.ProductionContentFolderTypeProvider; import java.io.File; import java.util.ArrayList; import java.util.List; /** * @author mike */ public abstract class ModuleFixtureBuilderImpl<T extends ModuleFixture> implements ModuleFixtureBuilder<T> { private static int ourIndex; protected final List<String> myContentRoots = new ArrayList<String>(); protected final List<String> mySourceRoots = new ArrayList<String>(); protected final TestFixtureBuilder<? extends IdeaProjectTestFixture> myFixtureBuilder; private T myModuleFixture; protected String myOutputPath; protected String myTestOutputPath; public ModuleFixtureBuilderImpl(TestFixtureBuilder<? extends IdeaProjectTestFixture> fixtureBuilder) { myFixtureBuilder = fixtureBuilder; } @Override public ModuleFixtureBuilder<T> addContentRoot(final String contentRootPath) { myContentRoots.add(contentRootPath); return this; } @Override public ModuleFixtureBuilder<T> addSourceRoot(final String sourceRootPath) { assert !myContentRoots.isEmpty() : "content root should be added first"; mySourceRoots.add(sourceRootPath); return this; } @Override public void setOutputPath(final String outputPath) { myOutputPath = outputPath; } @Override public void setTestOutputPath(String outputPath) { myTestOutputPath = outputPath; } protected Module createModule() { final Project project = myFixtureBuilder.getFixture().getProject(); assert project != null; String moduleName = String.valueOf(getNextIndex()); final String moduleFilePath = new File(project.getProjectFilePath()).getParent(); return ModuleManager.getInstance(project).newModule(moduleName, moduleFilePath); } private static int getNextIndex() { return ourIndex++; } @Override public synchronized T getFixture() { if (myModuleFixture == null) { myModuleFixture = instantiateFixture(); } return myModuleFixture; } @Override public void addSourceContentRoot(final String path) { addContentRoot(path); addSourceRoot(path); } protected abstract T instantiateFixture(); Module buildModule() { final Module[] module = {null}; ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { module[0] = createModule(); initModule(module[0]); } }); return module[0]; } protected void initModule(Module module) { final ModuleRootManager rootManager = ModuleRootManager.getInstance(module); final ModifiableRootModel rootModel = rootManager.getModifiableModel(); for (String contentRoot : myContentRoots) { final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(contentRoot); assert virtualFile != null : "cannot find content root: " + contentRoot; final ContentEntry contentEntry = rootModel.addContentEntry(virtualFile); for (String sourceRoot: mySourceRoots) { String s = contentRoot + "/" + sourceRoot; VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByPath(s); if (vf == null) { final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(sourceRoot); if (file != null && VfsUtil.isAncestor(virtualFile, file, false)) vf = file; } //assert vf != null : "cannot find source root: " + sourceRoot; if (vf != null) { contentEntry.addFolder(vf, ProductionContentFolderTypeProvider.getInstance()); } } } setupRootModel(rootModel); rootModel.commit(); } protected void setupRootModel(ModifiableRootModel rootModel) { } }