BundleSet { void add(Bundle bundle) throws BundleException { Bundle existingBundle = symbolicName2bundle.put(bundle.getSymbolicName(), bundle); if (null != existingBundle) { throw new BundleException( String.format("Bundle '%s' (ID: %d) is already part of collection with ID %d.", bundle.getSymbolicName(), bundle.getBundleId(), existingBundle.getBundleId()), BundleException.DUPLICATE_BUNDLE_ERROR); } Bundle bundleWithSameID = id2bundle.put(bundle.getBundleId(), bundle); if (null != bundleWithSameID) { throw new BundleException( String.format("Bundle ID '%d' for '%s' is already used by '%s'.", bundle.getBundleId(), bundle.getSymbolicName(), bundleWithSameID.getSymbolicName()), BundleException.DUPLICATE_BUNDLE_ERROR); } } BundleSet();   }
@Test public void testSameSymbolicName() throws BundleException { final String symbolicName = "sym.a"; final long id1 = 12345; final long id2 = 23456; Bundle testBundle1 = new TestBundle(id1, symbolicName); Bundle testBundle2 = new TestBundle(id2, symbolicName); instance.add(testBundle1); boolean exceptionCaught = false; try { instance.add(testBundle2); } catch (BundleException e) { exceptionCaught = true; assertThat(e.getMessage()).as("BundleException does not contain symbolic name.").contains(symbolicName); assertThat(e.getMessage()).as("BundleException does not contain ID of exisiting bundle.").contains(Long.toString(id1)); assertThat(e.getMessage()).as("BundleException does not contain ID of new bundle.").contains(Long.toString(id2)); } assertThat(exceptionCaught).as("No BundleException thrown for duplicate symblic name.").isTrue(); }
@Test public void testSameID() throws BundleException { final String symbolicName1 = "sym.a"; final String symbolicName2 = "sym.b"; final long id = 12345; Bundle testBundle1 = new TestBundle(id, symbolicName1); Bundle testBundle2 = new TestBundle(id, symbolicName2); instance.add(testBundle1); boolean exceptionCaught = false; try { instance.add(testBundle2); } catch (BundleException e) { exceptionCaught = true; assertThat(e.getMessage()).as("BundleException does not contain ID.").contains(Long.toString(id)); assertThat(e.getMessage()).as("BundleException does not contain symbolic name of exisiting bundle.").contains(symbolicName1); assertThat(e.getMessage()).as("BundleException does not contain symbolic name of new bundle.").contains(symbolicName2); } assertThat(exceptionCaught).as("No BundleException thrown for duplicate symblic name.").isTrue(); }