public TestFeatureRegistry build() throws GadgetException { return build(Joiner.on(",").join(featureFiles)); }
@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 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()); }
private void setupFullRegistry(String type, String containers) throws Exception { // Sets up a "full" gadget feature registry with several features registered: // nodep - has no deps on anything else // top - depends on mid_a and mid_b // mid_a and mid_b - both depend on bottom // bottom - depends on nothing else // The content registered for each is equal to the feature's name, for simplicity. // Also, all content is loaded as inline, also for simplicity. TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Map<String, String> attribs = Maps.newHashMap(); if (containers != null) { attribs.put("container", containers); } Uri nodepUri = builder.expectResource(xml(NODEP_TPL, type, null, "nodep", attribs)); Uri topUri = builder.expectResource(xml(TOP_TPL, type, null, "top", attribs)); Uri midAUri = builder.expectResource(xml(MID_A_TPL, type, null, "mid_a", attribs)); Uri midBUri = builder.expectResource(xml(MID_B_TPL, type, null, "mid_b", attribs)); Uri bottomUri = builder.expectResource(xml(BOTTOM_TPL, type, null, "bottom", attribs)); Uri txtFile = builder.expectResource(nodepUri.toString() + '\n' + topUri.toString() + '\n' + midAUri.toString() + '\n' + midBUri.toString() + '\n' + bottomUri.toString(), ".txt"); registry = builder.build(txtFile.toString()); }
private void setupFullRegistry(String type, String containers) throws Exception { // Sets up a "full" gadget feature registry with several features registered: // nodep - has no deps on anything else // top - depends on mid_a and mid_b // mid_a and mid_b - both depend on bottom // bottom - depends on nothing else // The content registered for each is equal to the feature's name, for simplicity. // Also, all content is loaded as inline, also for simplicity. TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Map<String, String> attribs = Maps.newHashMap(); if (containers != null) { attribs.put("container", containers); } Uri nodepUri = builder.expectResource(xml(NODEP_TPL, type, null, "nodep", attribs)); Uri topUri = builder.expectResource(xml(TOP_TPL, type, null, "top", attribs)); Uri midAUri = builder.expectResource(xml(MID_A_TPL, type, null, "mid_a", attribs)); Uri midBUri = builder.expectResource(xml(MID_B_TPL, type, null, "mid_b", attribs)); Uri bottomUri = builder.expectResource(xml(BOTTOM_TPL, type, null, "bottom", attribs)); Uri txtFile = builder.expectResource(nodepUri.toString() + '\n' + topUri.toString() + '\n' + midAUri.toString() + '\n' + midBUri.toString() + '\n' + bottomUri.toString(), ".txt"); registry = builder.build(txtFile.toString()); }
@Test public void registerFromResourceIndex() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); // One with extern resource loaded content... String content1 = "content1()"; Uri content1Uri = builder.expectResource(content1); Uri feature1Uri = builder.expectResource(xml(MID_A_TPL, "gadget", content1Uri.getPath(), null)); // One feature with inline content (that it depends on)... String content2 = "inline()"; Uri feature2Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", null, content2)); // .txt file to join the two Uri txtFile = builder.expectResource(feature1Uri.toString() + '\n' + feature2Uri.toString(), ".txt"); // Load resources from the text file and do basic validation they're good. registry = builder.build(txtFile.toString()); // Contents should be ordered based on the way they went in. List<FeatureResource> resources = registry.getAllFeatures().getResources(); assertEquals(2, resources.size()); assertEquals(content2, resources.get(0).getContent()); assertEquals(content1, resources.get(1).getContent()); }
@Test public void loopIsDetectedAndCrashes() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); // Set up a registry with features loop_a,b,c. C points back to A, which should // cause an exception to be thrown by the register method. String type = "gadget"; Uri loopAUri = builder.expectResource(xml(LOOP_A_TPL, type, null, "loop_a")); Uri loopBUri = builder.expectResource(xml(LOOP_B_TPL, type, null, "loop_b")); Uri loopCUri = builder.expectResource(xml(LOOP_C_TPL, type, null, "loop_c")); Uri txtFile = builder.expectResource(loopAUri.toString() + '\n' + loopBUri.toString() + '\n' + loopCUri.toString(), ".txt"); try { registry = builder.build(txtFile.toString()); fail("Should have thrown a loop-detected exception"); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_CONFIG, e.getCode()); } }
@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 loopIsDetectedAndCrashes() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); // Set up a registry with features loop_a,b,c. C points back to A, which should // cause an exception to be thrown by the register method. String type = "gadget"; Uri loopAUri = builder.expectResource(xml(LOOP_A_TPL, type, null, "loop_a")); Uri loopBUri = builder.expectResource(xml(LOOP_B_TPL, type, null, "loop_b")); Uri loopCUri = builder.expectResource(xml(LOOP_C_TPL, type, null, "loop_c")); Uri txtFile = builder.expectResource(loopAUri.toString() + '\n' + loopBUri.toString() + '\n' + loopCUri.toString(), ".txt"); try { registry = builder.build(txtFile.toString()); fail("Should have thrown a loop-detected exception"); } catch (GadgetException e) { assertEquals(GadgetException.Code.INVALID_CONFIG, e.getCode()); } }
@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 registerFromResourceFeatureXml() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String content = "resource-content()"; Uri contentUri = builder.expectResource(content); Uri featureUri = builder.expectResource(xml(NODEP_TPL, "gadget", contentUri.getPath(), null)); builder.addFeatureFile(featureUri.toString()); registry = builder.build(); // 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 missingFileResultsInException() throws Exception { try { registry = TestFeatureRegistry.newBuilder().build(new UriBuilder().setScheme("file") .setPath("/is/not/there.foo.xml").toUri().toString()); fail("Should have thrown missing .xml file exception"); } catch (GadgetException e) { // Expected. Verify code. assertEquals(GadgetException.Code.INVALID_CONFIG, e.getCode()); } }
@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 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()); } }
private void checkRegisterFromFileFeatureXml(boolean withScheme) throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String content = "content-" + (withScheme ? "withScheme" : "noScheme"); Uri resUri = makeFile(content); Uri featureFile = makeFile(xml(NODEP_TPL, "gadget", withScheme ? resUri.toString() : resUri.getPath(), null)); registry = builder.build(withScheme ? featureFile.toString() : featureFile.getPath()); // Verify single resource works all the way through. List<FeatureResource> resources = registry.getAllFeatures().getResources(); assertEquals(1, resources.size()); assertEquals(content, resources.get(0).getContent()); }
private void checkRegisterFromFileFeatureXml(boolean withScheme) throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String content = "content-" + (withScheme ? "withScheme" : "noScheme"); Uri resUri = makeFile(content); Uri featureFile = makeFile(xml(NODEP_TPL, "gadget", withScheme ? resUri.toString() : resUri.getPath(), null)); registry = builder.build(withScheme ? featureFile.toString() : featureFile.getPath()); // 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 registerFromResourceFeatureXml() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); String content = "resource-content()"; Uri contentUri = builder.expectResource(content); Uri featureUri = builder.expectResource(xml(NODEP_TPL, "gadget", contentUri.getPath(), null)); builder.addFeatureFile(featureUri.toString()); registry = builder.build(); // 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 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 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 missingFileResultsInException() throws Exception { try { registry = TestFeatureRegistry.newBuilder().build(new UriBuilder().setScheme("file") .setPath("/is/not/there.foo.xml").toUri().toString()); fail("Should have thrown missing .xml file exception"); } catch (GadgetException e) { // Expected. Verify code. assertEquals(GadgetException.Code.INVALID_CONFIG, e.getCode()); } }