private RegionFilter createFilter(final String... packageNames) throws InvalidSyntaxException {
   Collection<String> filters = new ArrayList<String>(packageNames.length);
   for (String pkg : packageNames) {
     filters.add('(' + RegionFilter.VISIBLE_PACKAGE_NAMESPACE + '=' + pkg + ')');
   }
   RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
   for (String filter : filters) {
     builder.allow(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, filter);
   }
   return builder.build();
 }
 private static StartOptions[] getStartOptions(int options) {
   if (options == 0) {
     return new StartOptions[0];
   }
   Collection<StartOptions> result = new ArrayList<Module.StartOptions>(2);
   if ((options & Bundle.START_TRANSIENT) != 0) {
     result.add(StartOptions.TRANSIENT);
   }
   if ((options & Bundle.START_ACTIVATION_POLICY) != 0) {
     result.add(StartOptions.USE_ACTIVATION_POLICY);
   }
   return result.toArray(new StartOptions[result.size()]);
 }
  @Test
  public void testResolveTransitiveDups() throws BundleException, InvalidSyntaxException {
    region(REGION_A).connectRegion(region(REGION_B), createFilter(PACKAGE_B));
    region(REGION_A).connectRegion(region(REGION_C), createFilter(PACKAGE_DUP));
    region(REGION_A).connectRegion(region(REGION_D), createFilter(PACKAGE_DUP));
    region(REGION_B).connectRegion(region(REGION_C), createFilter(PACKAGE_DUP));
    region(REGION_C).connectRegion(region(REGION_D), createFilter(PACKAGE_DUP));
    region(REGION_D).connectRegion(region(REGION_A), createFilter(PACKAGE_DUP));

    this.candidates.add(packageCapability(BUNDLE_A, PACKAGE_DUP));
    this.candidates.add(packageCapability(BUNDLE_B, PACKAGE_DUP));
    this.candidates.add(packageCapability(BUNDLE_C, PACKAGE_DUP));
    this.candidates.add(packageCapability(BUNDLE_D, PACKAGE_DUP));

    Collection<BundleCapability> testCandidates = new ArrayList<BundleCapability>(this.candidates);
    this.resolverHook.filterMatches(bundleRequirement(BUNDLE_A), testCandidates);
    assertTrue(testCandidates.contains(packageCapability(BUNDLE_A, PACKAGE_DUP)));
    assertFalse(testCandidates.contains(packageCapability(BUNDLE_B, PACKAGE_DUP)));
    assertTrue(testCandidates.contains(packageCapability(BUNDLE_C, PACKAGE_DUP)));
    assertTrue(testCandidates.contains(packageCapability(BUNDLE_D, PACKAGE_DUP)));

    testCandidates = new ArrayList<BundleCapability>(this.candidates);
    this.resolverHook.filterMatches(bundleRequirement(BUNDLE_B), testCandidates);
    assertTrue(testCandidates.contains(packageCapability(BUNDLE_A, PACKAGE_DUP)));
    assertTrue(testCandidates.contains(packageCapability(BUNDLE_B, PACKAGE_DUP)));
    assertTrue(testCandidates.contains(packageCapability(BUNDLE_C, PACKAGE_DUP)));
    assertTrue(testCandidates.contains(packageCapability(BUNDLE_D, PACKAGE_DUP)));
  }
	public void testInvalidRegionName() {
		Collection<String> invalidNames = new ArrayList<String>();
		invalidNames.addAll(Arrays.asList(":", "bad:Name", ":bad::name:", ":badname", "badname:"));
		invalidNames.addAll(Arrays.asList("=", "bad=Name", "=bad==name=", "=badname", "badname="));
		invalidNames.addAll(Arrays.asList("\n", "bad\nName", "\nbad\n\nname\n", "\nbadname", "badname\n"));
		invalidNames.addAll(Arrays.asList("*", "bad*Name", "*bad**name*", "*badname", "badname*"));
		invalidNames.addAll(Arrays.asList("?", "bad?Name", "?bad??name?", "?badname", "badname?"));
		invalidNames.addAll(Arrays.asList(",", "bad,Name", ",bad,,name,", ",badname", "badname,"));
		invalidNames.addAll(Arrays.asList("\"", "bad\"Name", "\"bad\"\"name\"", "\"badname", "badname\""));
		invalidNames.addAll(Arrays.asList("\\", "bad\\Name", "\\bad\\\\name\\", "\\badname", "badname\\"));

		for (String invalidName : invalidNames) {
			try {
				digraph.createRegion(invalidName);
				fail("Expected failure to create region.");
			} catch (IllegalArgumentException e) {
				// expected
			} catch (BundleException e) {
				fail("Unexpected bundle exception: " + e.getMessage());
			}
		}

	}
	public void testBundleCollisionDisconnectedRegions() throws BundleException, InvalidSyntaxException {
		// get the system region
		Region systemRegion = digraph.getRegion(0);
		Collection<Bundle> bundles = new HashSet<Bundle>();
		// create 4 disconnected test regions and install each bundle into each region
		int numRegions = 4;
		String regionName = "IsolatedRegion_";
		for (int i = 0; i < numRegions; i++) {
			Region region = digraph.createRegion(regionName + i);
			// Import the system bundle from the systemRegion
			digraph.connect(region, digraph.createRegionFilterBuilder().allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, "(id=0)").build(), systemRegion);
			// must import Boolean services into systemRegion to test
			digraph.connect(systemRegion, digraph.createRegionFilterBuilder().allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, "(objectClass=java.lang.Boolean)").build(), region);
			for (String location : ALL) {
				Bundle b = bundleInstaller.installBundle(location, region);
				bundles.add(b);
			}
		}

		assertEquals("Wrong number of bundles installed", numRegions * ALL.size(), bundles.size());
		assertTrue("Could not resolve bundles.", bundleInstaller.resolveBundles(bundles.toArray(new Bundle[bundles.size()])));

		// test install of duplicates
		for (int i = 0; i < numRegions; i++) {
			Region region = digraph.getRegion(regionName + i);
			for (String name : ALL) {
				String location = bundleInstaller.getBundleLocation(name);
				try {
					Bundle b = region.installBundle(getName() + "_expectToFail", new URL(location).openStream());
					b.uninstall();
					fail("Expected a bundle exception on duplicate bundle installation: " + name);
				} catch (BundleException e) {
					// expected
					assertEquals("Wrong exception type.", BundleException.DUPLICATE_BUNDLE_ERROR, e.getType());
				} catch (IOException e) {
					fail("Failed to open bunldle location: " + e.getMessage());
				}
			}
		}

		// test update to a duplicate
		for (int i = 0; i < numRegions; i++) {
			Region region = digraph.getRegion(regionName + i);

			Bundle regionPP1 = region.getBundle(PP1, new Version(1, 0, 0));

			String locationSP1 = bundleInstaller.getBundleLocation(SP1);
			try {
				regionPP1.update(new URL(locationSP1).openStream());
				fail("Expected a bundle exception on duplicate bundle update: " + region);
			} catch (BundleException e) {
				// expected
				assertEquals("Wrong exception type.", BundleException.DUPLICATE_BUNDLE_ERROR, e.getType());
			} catch (IOException e) {
				fail("Failed to open bunldle location: " + e.getMessage());
			}

			// now uninstall SP1 and try to update PP1 to SP1 again
			Bundle regionSP1 = region.getBundle(SP1, new Version(1, 0, 0));
			regionSP1.uninstall();

			try {
				regionPP1.update(new URL(locationSP1).openStream());
			} catch (IOException e) {
				fail("Failed to open bunldle location: " + e.getMessage());
			}
		}
	}