	@Test
	public void testCollectFutures() throws Exception {

		IAggregator mockAggregator = TestUtils.createMockAggregator();
		HttpServletRequest mockRequest = TestUtils.createMockRequest(mockAggregator);
		ICache mockCache = createMock(ICache.class);
		IModuleCache mockModuleCache = createMock(IModuleCache.class);
		expect(mockCache.getModules()).andReturn(mockModuleCache).anyTimes();
		expect(mockModuleCache.getBuild(eq(mockRequest), isA(IModule.class))).andAnswer(new IAnswer<Future<ModuleBuildReader>>() {
			@Override public Future<ModuleBuildReader> answer() throws Throwable {
				IModule module = (IModule)getCurrentArguments()[1];
				if (module.getModuleName().equals("notfound")) {
					throw new NotFoundException(module.getModuleName());
				}
				return new CompletedFuture<ModuleBuildReader>(
						new ModuleBuildReader(new StringReader(module.getModuleId() + " build"), true)
						);
			}
		}).anyTimes();
		replay(mockAggregator, mockRequest, mockCache, mockModuleCache);
		((TestCacheManager)mockAggregator.getCacheManager()).setCache(mockCache);
		ModuleList moduleList = new ModuleList();
		IModule module1 = new ModuleImpl("module1", new URI("file://module1.js")),
				module2 = new ModuleImpl("module2", new URI("file://module2.js")),
				notfound = new ModuleImpl("notfound", new URI("file://notfound.js"));
		moduleList.add(new ModuleListEntry(module1, ModuleSpecifier.MODULES, true));
		moduleList.add(new ModuleListEntry(module2, ModuleSpecifier.LAYER, true));
		moduleList.add(new ModuleListEntry(notfound, ModuleSpecifier.LAYER, true /* server expanded */));
		LayerBuilder builder = new LayerBuilder(mockRequest, null, moduleList);
		List<ModuleBuildFuture> futures = builder.collectFutures(moduleList, mockRequest);
		Assert.assertEquals(2, futures.size());
		ModuleBuildFuture future = futures.get(0);
		Assert.assertEquals("module1", future.getModule().getModuleId());
		Assert.assertEquals(ModuleSpecifier.MODULES, future.getModuleSpecifier());
		Assert.assertEquals("module1 build", toString(future.get()));
		future = futures.get(1);
		Assert.assertEquals("module2", future.getModule().getModuleId());
		Assert.assertEquals(ModuleSpecifier.LAYER, future.getModuleSpecifier());
		Assert.assertEquals("module2 build", toString(future.get()));

		// now make sure exception is thrown for not found module that isn't server expanded
		moduleList = new ModuleList();
		moduleList.add(new ModuleListEntry(module1, ModuleSpecifier.LAYER, true));
		moduleList.add(new ModuleListEntry(module2, ModuleSpecifier.LAYER, true));
		moduleList.add(new ModuleListEntry(notfound, ModuleSpecifier.MODULES, false /* not server expanded */));
		builder = new LayerBuilder(mockRequest, null, moduleList);
		boolean exceptionCaught = false;
		try {
			builder.collectFutures(moduleList, mockRequest);
		} catch (NotFoundException e) {
			exceptionCaught = true;
		}
		Assert.assertTrue(exceptionCaught);
	}