@Test public void registerFromFileInNestedDirectoryFeatureXmlFile() throws Exception { // Get the directory from dummyUri and create a subdir. File tmpFile = File.createTempFile("dummy", ".dat"); tmpFile.deleteOnExit(); File parentDir = tmpFile.getParentFile(); String childDirName = String.valueOf(Math.random()); File childDir = new File(parentDir, childDirName); childDir.mkdirs(); childDir.deleteOnExit(); File featureDir = new File(childDir, "thefeature"); featureDir.mkdirs(); featureDir.deleteOnExit(); File resFile = File.createTempFile("content", ".js", featureDir); resFile.deleteOnExit(); String content = "content-foo"; BufferedWriter out = new BufferedWriter(new FileWriter(resFile)); out.write(content); out.close(); File featureFile = File.createTempFile("feature", ".xml", featureDir); featureFile.deleteOnExit(); out = new BufferedWriter(new FileWriter(featureFile)); out.write(xml(NODEP_TPL, "gadget", resFile.toURI().toString(), null)); out.close(); registry = TestFeatureRegistry.newBuilder().build(childDir.toURI().toString()); // Verify single resource works all the way through. List<FeatureResource> resources = registry.getAllFeatures().getResources(); assertEquals(1, resources.size()); assertEquals(content, resources.get(0).getContent()); }
@Test public void returnOnlyContainerFilteredJs() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String feature = "thefeature"; String container = "foo"; String containerContent = "content1();"; String defaultContent = "content2();"; Uri featureUri = builder.expectResource( getContainerAndDefaultTpl(feature, container, containerContent, defaultContent)); registry = builder.build(featureUri.toString()); List<String> needed = Lists.newArrayList(feature); List<String> unsupported = Lists.newLinkedList(); List<FeatureResource> resources = registry.getFeatureResources( getCtx(RenderingContext.GADGET, container), needed, unsupported).getResources(); assertEquals(1, resources.size()); assertEquals(containerContent, resources.get(0).getContent()); }
@Test public void getFeaturesStringsNoTransitive() throws Exception { setupFullRegistry("gadget", null); List<String> needed = Lists.newArrayList("nodep", "bottom"); List<String> featureNames = registry.getFeatures(needed); assertEquals(2, featureNames.size()); assertEquals("nodep", featureNames.get(0)); assertEquals("bottom", featureNames.get(1)); }
@Test public void resourceGetsMergedAttribs() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); List<FeatureResource> resources = registry.getFeatureResources( getCtx(RenderingContext.GADGET, theContainer), needed, unsupported).getResources(); Map<String, String> lastAttribs = registry.getLastAttribs(); assertNotNull(lastAttribs); assertEquals(4, lastAttribs.size());
private void checkNoContentValidFeatureResources( String type, RenderingContext rctx) throws Exception { setupFullRegistry(type, null); GadgetContext ctx = getCtx(rctx, null); List<String> needed = Lists.newArrayList("nodep", "bottom"); List<String> unsupported = Lists.newLinkedList(); List<FeatureResource> resources = registry.getFeatureResources(ctx, needed, unsupported).getResources(); assertEquals(0, resources.size()); }
@Test public void unavailableFeatureCrashes() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Uri featUri = builder.expectResource(xml(BAD_DEP_TPL, "gadget", null, "content")); try { registry = builder.build(featUri.toString()); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_CONFIG, e.getCode()); } }
@Test public void getAllFeatures() throws Exception { setupFullRegistry("gadget", null); List<FeatureResource> resources = registry.getAllFeatures().getResources(); // No guaranteed order (top/mid/bottom bundle may be before nodep) // Just check that there are 5 resources around and let the above tests // handle transitivity checks. assertEquals(5, resources.size()); }
public TestFeatureRegistry build(String useFeature) throws GadgetException { return new TestFeatureRegistry( new TestFeatureResourceLoader(resourceMock), new TestCacheProvider(), useFeature); }
private void checkNoContentValidFeatureResources( String type, RenderingContext rctx) throws Exception { setupFullRegistry(type, null); GadgetContext ctx = getCtx(rctx, null); List<String> needed = Lists.newArrayList("nodep", "bottom"); List<String> unsupported = Lists.newLinkedList(); List<FeatureResource> resources = registry.getFeatureResources(ctx, needed, unsupported).getResources(); assertEquals(0, resources.size()); }
@Test public void unavailableFeatureCrashes() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Uri featUri = builder.expectResource(xml(BAD_DEP_TPL, "gadget", null, "content")); try { registry = builder.build(featUri.toString()); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_CONFIG, e.getCode()); } }
@Test public void resourceGetsMergedAttribs() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); List<FeatureResource> resources = registry.getFeatureResources( getCtx(RenderingContext.GADGET, theContainer), needed, unsupported).getResources(); Map<String, String> lastAttribs = registry.getLastAttribs(); assertNotNull(lastAttribs); assertEquals(4, lastAttribs.size());
@Test public void getAllFeatures() throws Exception { setupFullRegistry("gadget", null); List<FeatureResource> resources = registry.getAllFeatures().getResources(); // No guaranteed order (top/mid/bottom bundle may be before nodep) // Just check that there are 5 resources around and let the above tests // handle transitivity checks. assertEquals(5, resources.size()); }
@Test public void registerFromFileInNestedDirectoryFeatureXmlFile() throws Exception { // Get the directory from dummyUri and create a subdir. File tmpFile = File.createTempFile("dummy", ".dat"); tmpFile.deleteOnExit(); File parentDir = tmpFile.getParentFile(); String childDirName = String.valueOf(Math.random()); File childDir = new File(parentDir, childDirName); childDir.mkdirs(); childDir.deleteOnExit(); File featureDir = new File(childDir, "thefeature"); featureDir.mkdirs(); featureDir.deleteOnExit(); File resFile = File.createTempFile("content", ".js", featureDir); resFile.deleteOnExit(); String content = "content-foo"; BufferedWriter out = new BufferedWriter(new FileWriter(resFile)); out.write(content); out.close(); File featureFile = File.createTempFile("feature", ".xml", featureDir); featureFile.deleteOnExit(); out = new BufferedWriter(new FileWriter(featureFile)); out.write(xml(NODEP_TPL, "gadget", resFile.toURI().toString(), null)); out.close(); registry = TestFeatureRegistry.newBuilder().build(childDir.toURI().toString()); // Verify single resource works all the way through. List<FeatureResource> resources = registry.getAllFeatures().getResources(); assertEquals(1, resources.size()); assertEquals(content, resources.get(0).getContent()); }
@Test public void returnOnlyContainerFilteredJs() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String feature = "thefeature"; String container = "foo"; String containerContent = "content1();"; String defaultContent = "content2();"; Uri featureUri = builder.expectResource( getContainerAndDefaultTpl(feature, container, containerContent, defaultContent)); registry = builder.build(featureUri.toString()); List<String> needed = Lists.newArrayList(feature); List<String> unsupported = Lists.newLinkedList(); List<FeatureResource> resources = registry.getFeatureResources( getCtx(RenderingContext.GADGET, container), needed, unsupported).getResources(); assertEquals(1, resources.size()); assertEquals(containerContent, resources.get(0).getContent()); }
@Test public void filterFeaturesByContainerNoMatch() throws Exception { // Again test only for gadget case; above tests cover type <-> RenderingContext setupFullRegistry("gadget", "one, two, three"); GadgetContext ctx = getCtx(RenderingContext.GADGET, "four"); List<String> needed = Lists.newArrayList("nodep", "bottom"); List<String> unsupported = Lists.newLinkedList(); List<FeatureResource> resources = registry.getFeatureResources(ctx, needed, unsupported).getResources(); assertEquals(0, resources.size()); // no resource matches but all feature keys valid assertEquals(0, unsupported.size()); }
@Test public void missingIndexResultsInException() throws Exception { try { registry = TestFeatureRegistry.newBuilder().build(makeResourceUri(".txt").toString()); fail("Should have thrown an exception for missing .txt file"); } catch (GadgetException e) { // Expected. Verify code. assertEquals(GadgetException.Code.INVALID_PATH, e.getCode()); } }
@Test public void getFeaturesStringsNoTransitive() throws Exception { setupFullRegistry("gadget", null); List<String> needed = Lists.newArrayList("nodep", "bottom"); List<String> featureNames = registry.getFeatures(needed); assertEquals(2, featureNames.size()); assertEquals("nodep", featureNames.get(0)); assertEquals("bottom", featureNames.get(1)); }
@Test public void registerFromResourceFeatureXmlRelativeContent() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String content = "resource-content-relative()"; Uri contentUri = builder.expectResource(content); String relativePath = contentUri.getPath().substring(contentUri.getPath().lastIndexOf('/') + 1); Uri featureUri = builder.expectResource(xml(NODEP_TPL, "gadget", relativePath, null)); registry = builder.build(featureUri.toString()); // Verify single resource works all the way through. List<FeatureResource> resources = registry.getAllFeatures().getResources(); assertEquals(1, resources.size()); assertEquals(content, resources.get(0).getContent()); }
@Test public void returnDefaultMatchJs() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String feature = "thefeature"; String container = "foo"; String containerContent = "content1();"; String defaultContent = "content2();"; Uri featureUri = builder.expectResource( getContainerAndDefaultTpl(feature, container, containerContent, defaultContent)); registry = builder.build(featureUri.toString()); List<String> needed = Lists.newArrayList(feature); List<String> unsupported = Lists.newLinkedList(); List<FeatureResource> resources = registry.getFeatureResources( getCtx(RenderingContext.GADGET, "othercontainer"), needed, unsupported).getResources(); assertEquals(1, resources.size()); assertEquals(defaultContent, resources.get(0).getContent()); }
@Test public void filterFeaturesByContainerNoMatch() throws Exception { // Again test only for gadget case; above tests cover type <-> RenderingContext setupFullRegistry("gadget", "one, two, three"); GadgetContext ctx = getCtx(RenderingContext.GADGET, "four"); List<String> needed = Lists.newArrayList("nodep", "bottom"); List<String> unsupported = Lists.newLinkedList(); List<FeatureResource> resources = registry.getFeatureResources(ctx, needed, unsupported).getResources(); assertEquals(0, resources.size()); // no resource matches but all feature keys valid assertEquals(0, unsupported.size()); }