<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.aries.application.runtime.itests;

import static org.ops4j.pax.exam.CoreOptions.equinox;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

import org.apache.aries.application.management.AriesApplication;
import org.apache.aries.application.management.AriesApplicationManager;
import org.apache.aries.application.utils.filesystem.FileSystem;
import org.apache.aries.unittest.fixture.ArchiveFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.ZipFixture;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.osgi.service.obr.Capability;
import org.osgi.service.obr.Repository;
import org.osgi.service.obr.RepositoryAdmin;
import org.osgi.service.obr.Resource;

@RunWith(JUnit4TestRunner.class)
public class OBRAppManagerTest extends AbstractIntegrationTest {

  /* Use @Before not @BeforeClass so as to ensure that these resources
   * are created in the paxweb temp directory, and not in the svn tree
   */
  static boolean createdApplications = false;
  @Before
  public static void createApplications() throws Exception {
    if (createdApplications) {
      return;
    }
    ZipFixture testBundle = ArchiveFixture.newZip()
        .manifest().symbolicName("org.apache.aries.sample")
          .attribute("Bundle-Version", "1.0.0")
          .attribute("Export-Package", "org.apache.aries.sample.impl")
          .end()
        .binary("org/apache/aries/sample/impl/HelloWorldImpl.class",
            OBRAppManagerTest.class.getClassLoader().getResourceAsStream("org/apache/aries/sample/impl/HelloWorldImpl.class"))
        .end();

    FileOutputStream fout = new FileOutputStream("bundle.jar");
    testBundle.writeOut(fout);
    fout.close();

    ZipFixture testEba = ArchiveFixture.newZip()
      .jar("sample.jar")
        .manifest().symbolicName("org.apache.aries.sample")
          .attribute("Bundle-Version", "1.0.0")
          .attribute("Import-Package", "org.apache.aries.sample.impl")
          .end()
        .binary("OSGI-INF/blueprint/sample-blueprint.xml",
            OBRAppManagerTest.class.getClassLoader().getResourceAsStream("basic/sample-blueprint.xml"))
        .end()
         .binary("META-INF/APPLICATION.MF",
        OBRAppManagerTest.class.getClassLoader().getResourceAsStream("basic/APPLICATION.MF"))
        .end();
    fout = new FileOutputStream("test.eba");
    testEba.writeOut(fout);
    fout.close();
    
    StringBuilder repositoryXML = new StringBuilder();
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(OBRAppManagerTest.class.getResourceAsStream("/obr/repository.xml")));
    String line;
    
    while ((line = reader.readLine()) != null) {
      repositoryXML.append(line);
      repositoryXML.append("\r\n");
    }
    
    String repo = repositoryXML.toString().replaceAll("bundle_location", new File("bundle.jar").getAbsolutePath());
    
    System.out.println(repo);
    
    FileWriter writer = new FileWriter("repository.xml");
    writer.write(repo);
    writer.close();
    
    createdApplications = true;
  }

  @Test
  public void testAppWithApplicationManifest() throws Exception {
    RepositoryAdmin repositoryAdmin = getOsgiService(RepositoryAdmin.class);
    
    repositoryAdmin.addRepository(new File("repository.xml").toURI().toURL());

    Repository[] repos = repositoryAdmin.listRepositories();
    
    for (Repository repo : repos) {
      Resource[] resources = repo.getResources();
      
      for (Resource r : resources) {
        Capability[] cs = r.getCapabilities();
        
        for (Capability c : cs) {
          System.out.println(c.getName() + " : " + c.getProperties());
        }
      }
    }
    
    AriesApplicationManager manager = getOsgiService(AriesApplicationManager.class);
    AriesApplication app = manager.createApplication(FileSystem.getFSRoot(new File("test.eba")));<commit_msg>ARIES-174 Due to ARIES-182 the application must be explicitly resolved, this is not done in the OBR test, so it does not end up testing the OBR resolve function. Added call to resolve.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.aries.application.runtime.itests;

import static org.ops4j.pax.exam.CoreOptions.equinox;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

import org.apache.aries.application.management.AriesApplication;
import org.apache.aries.application.management.AriesApplicationManager;
import org.apache.aries.application.utils.filesystem.FileSystem;
import org.apache.aries.unittest.fixture.ArchiveFixture;
import org.apache.aries.unittest.fixture.ArchiveFixture.ZipFixture;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.osgi.service.obr.Capability;
import org.osgi.service.obr.Repository;
import org.osgi.service.obr.RepositoryAdmin;
import org.osgi.service.obr.Resource;

@RunWith(JUnit4TestRunner.class)
public class OBRAppManagerTest extends AbstractIntegrationTest {

  /* Use @Before not @BeforeClass so as to ensure that these resources
   * are created in the paxweb temp directory, and not in the svn tree
   */
  static boolean createdApplications = false;
  @Before
  public static void createApplications() throws Exception {
    if (createdApplications) {
      return;
    }
    ZipFixture testBundle = ArchiveFixture.newZip()
        .manifest().symbolicName("org.apache.aries.sample")
          .attribute("Bundle-Version", "1.0.0")
          .attribute("Export-Package", "org.apache.aries.sample.impl")
          .end()
        .binary("org/apache/aries/sample/impl/HelloWorldImpl.class",
            OBRAppManagerTest.class.getClassLoader().getResourceAsStream("org/apache/aries/sample/impl/HelloWorldImpl.class"))
        .end();

    FileOutputStream fout = new FileOutputStream("bundle.jar");
    testBundle.writeOut(fout);
    fout.close();

    ZipFixture testEba = ArchiveFixture.newZip()
      .jar("sample.jar")
        .manifest().symbolicName("org.apache.aries.sample")
          .attribute("Bundle-Version", "1.0.0")
          .attribute("Import-Package", "org.apache.aries.sample.impl")
          .end()
        .binary("OSGI-INF/blueprint/sample-blueprint.xml",
            OBRAppManagerTest.class.getClassLoader().getResourceAsStream("basic/sample-blueprint.xml"))
        .end()
         .binary("META-INF/APPLICATION.MF",
        OBRAppManagerTest.class.getClassLoader().getResourceAsStream("basic/APPLICATION.MF"))
        .end();
    fout = new FileOutputStream("test.eba");
    testEba.writeOut(fout);
    fout.close();
    
    StringBuilder repositoryXML = new StringBuilder();
    
    BufferedReader reader = new BufferedReader(new InputStreamReader(OBRAppManagerTest.class.getResourceAsStream("/obr/repository.xml")));
    String line;
    
    while ((line = reader.readLine()) != null) {
      repositoryXML.append(line);
      repositoryXML.append("\r\n");
    }
    
    String repo = repositoryXML.toString().replaceAll("bundle_location", new File("bundle.jar").getAbsolutePath());
    
    System.out.println(repo);
    
    FileWriter writer = new FileWriter("repository.xml");
    writer.write(repo);
    writer.close();
    
    createdApplications = true;
  }

  @Test
  public void testAppWithApplicationManifest() throws Exception {
    RepositoryAdmin repositoryAdmin = getOsgiService(RepositoryAdmin.class);
    
    repositoryAdmin.addRepository(new File("repository.xml").toURI().toURL());

    Repository[] repos = repositoryAdmin.listRepositories();
    
    for (Repository repo : repos) {
      Resource[] resources = repo.getResources();
      
      for (Resource r : resources) {
        Capability[] cs = r.getCapabilities();
        
        for (Capability c : cs) {
          System.out.println(c.getName() + " : " + c.getProperties());
        }
      }
    }
    
    AriesApplicationManager manager = getOsgiService(AriesApplicationManager.class);
    AriesApplication app = manager.createApplication(FileSystem.getFSRoot(new File("test.eba")));
    app = manager.resolve(app);
{'fraction_non_alphanumeric': 0.11230784294526584, 'fraction_numerical': 0.003916576911779105, 'mean_word_length': 3.7004141739530603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4447447', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2891, 'n_words': 667}