Search in sources :

Example 51 with Library

use of com.intellij.openapi.roots.libraries.Library in project android by JetBrains.

the class AndroidModuleDependenciesSetupTest method createLibrary.

@NotNull
private Library createLibrary(@NotNull File binaryPath, @NotNull File sourcePath) {
    LibraryTable libraryTable = ProjectLibraryTable.getInstance(getProject());
    LibraryTable.ModifiableModel libraryTableModel = libraryTable.getModifiableModel();
    Library library = libraryTableModel.createLibrary(binaryPath.getName());
    Application application = ApplicationManager.getApplication();
    application.runWriteAction(libraryTableModel::commit);
    Library.ModifiableModel libraryModel = library.getModifiableModel();
    libraryModel.addRoot(pathToIdeaUrl(binaryPath), CLASSES);
    libraryModel.addRoot(pathToIdeaUrl(sourcePath), SOURCES);
    application.runWriteAction(libraryModel::commit);
    return library;
}
Also used : ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library) Application(com.intellij.openapi.application.Application) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with Library

use of com.intellij.openapi.roots.libraries.Library in project android by JetBrains.

the class AndroidModuleDependenciesSetupTest method testSetUpLibraryWithNewLibrary.

public void testSetUpLibraryWithNewLibrary() throws IOException {
    File binaryPath = createTempFile("fakeLibrary", "jar");
    File sourcePath = createTempFile("fakeLibrary-src", "jar");
    when(myLibraryFilePaths.findSourceJarPath(binaryPath)).thenReturn(sourcePath);
    String libraryName = binaryPath.getName();
    Module module = getModule();
    IdeModifiableModelsProvider modelsProvider = new IdeModifiableModelsProviderImpl(getProject());
    File[] binaryPaths = { binaryPath };
    myDependenciesSetup.setUpLibraryDependency(module, modelsProvider, libraryName, COMPILE, binaryPath, binaryPaths, EMPTY_FILE_ARRAY);
    // Apply changes before checking state.
    ApplicationManager.getApplication().runWriteAction(modelsProvider::commit);
    List<LibraryOrderEntry> libraryOrderEntries = getLibraryOrderEntries(module);
    // Only one library should be in the library table.
    assertThat(libraryOrderEntries).hasSize(1);
    Library library = libraryOrderEntries.get(0).getLibrary();
    assertNotNull(library);
    assertEquals(libraryName, library.getName());
    String[] binaryUrls = library.getUrls(CLASSES);
    assertThat(binaryUrls).hasLength(1);
    assertEquals(pathToIdeaUrl(binaryPath), binaryUrls[0]);
    String[] sourceUrls = library.getUrls(SOURCES);
    assertThat(sourceUrls).hasLength(1);
    assertEquals(pathToIdeaUrl(sourcePath), sourceUrls[0]);
    // Should not mark new libraries as "used"
    verify(myLibraryRegistry, never()).markAsUsed(library, binaryPaths);
    verify(myLibraryFilePaths).findSourceJarPath(binaryPath);
}
Also used : IdeModifiableModelsProviderImpl(com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) IdeModifiableModelsProvider(com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider) File(java.io.File)

Example 53 with Library

use of com.intellij.openapi.roots.libraries.Library in project android by JetBrains.

the class SyncLibraryRegistryTest method testMarkAsUsedWhenDisposed.

public void testMarkAsUsedWhenDisposed() {
    SyncLibraryRegistry libraryRegistry = SyncLibraryRegistry.getInstance(getProject());
    Disposer.dispose(libraryRegistry);
    Library library = mock(Library.class);
    try {
        libraryRegistry.markAsUsed(library, EMPTY_FILE_ARRAY);
        fail("Expecting error due to disposed instance");
    } catch (IllegalStateException ignored) {
    // expected
    }
}
Also used : Library(com.intellij.openapi.roots.libraries.Library)

Example 54 with Library

use of com.intellij.openapi.roots.libraries.Library in project android by JetBrains.

the class SyncLibraryRegistryTest method testGetLibrariesToUpdateWhenUrlsChanged.

public void testGetLibrariesToUpdateWhenUrlsChanged() throws IOException {
    File fakeJarPath = createTempFile("fake.jar", null);
    Library library = simulateProjectHasLibrary(fakeJarPath);
    File newJarPath = createTempFile("fake2.jar", null);
    SyncLibraryRegistry libraryRegistry = SyncLibraryRegistry.getInstance(getProject());
    libraryRegistry.markAsUsed(library, newJarPath);
    List<LibraryToUpdate> librariesToUpdate = libraryRegistry.getLibrariesToUpdate();
    assertThat(librariesToUpdate).hasSize(1);
    LibraryToUpdate libraryToUpdate = librariesToUpdate.get(0);
    assertSame(library, libraryToUpdate.getLibrary());
    assertThat(libraryToUpdate.getNewBinaryUrls()).containsExactly(pathToUrl(newJarPath.getPath()));
}
Also used : LibraryToUpdate(com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry.LibraryToUpdate) Library(com.intellij.openapi.roots.libraries.Library) File(java.io.File)

Example 55 with Library

use of com.intellij.openapi.roots.libraries.Library in project android by JetBrains.

the class SyncLibraryRegistryTest method testGetLibrariesToUpdateWhenUrlsWereAdded.

public void testGetLibrariesToUpdateWhenUrlsWereAdded() throws IOException {
    File fakeJarPath = createTempFile("fake.jar", null);
    Library library = simulateProjectHasLibrary(fakeJarPath);
    File newJarPath = createTempFile("fake2.jar", null);
    SyncLibraryRegistry libraryRegistry = SyncLibraryRegistry.getInstance(getProject());
    libraryRegistry.markAsUsed(library, fakeJarPath, newJarPath);
    List<LibraryToUpdate> librariesToUpdate = libraryRegistry.getLibrariesToUpdate();
    assertThat(librariesToUpdate).hasSize(1);
    LibraryToUpdate libraryToUpdate = librariesToUpdate.get(0);
    assertSame(library, libraryToUpdate.getLibrary());
    assertThat(libraryToUpdate.getNewBinaryUrls()).containsExactly(pathToIdeaUrl(fakeJarPath), pathToIdeaUrl(newJarPath));
}
Also used : LibraryToUpdate(com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry.LibraryToUpdate) Library(com.intellij.openapi.roots.libraries.Library) File(java.io.File)

Aggregations

Library (com.intellij.openapi.roots.libraries.Library)240 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)58 VirtualFile (com.intellij.openapi.vfs.VirtualFile)58 Module (com.intellij.openapi.module.Module)41 NotNull (org.jetbrains.annotations.NotNull)31 File (java.io.File)29 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)25 Nullable (org.jetbrains.annotations.Nullable)25 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)23 Project (com.intellij.openapi.project.Project)22 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)20 ArrayList (java.util.ArrayList)15 Sdk (com.intellij.openapi.projectRoots.Sdk)13 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)13 OrderEntry (com.intellij.openapi.roots.OrderEntry)10 THashSet (gnu.trove.THashSet)9 IOException (java.io.IOException)9 Element (org.jdom.Element)9 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)8 LibrariesContainer (com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer)7