@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()); }
Uri content1Uri = builder.expectResource(content1); Map<String, String> attribs = Maps.newHashMap(); String theContainer = "the-container"; attribs.put("two", "attrib-two"); attribs.put("three", "attrib-three"); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null, attribs, resourceAttribs)); registry = builder.build(feature1Uri.toString());
Uri topUri = builder.expectResource( "<feature>" + "<name>top</name>" + "</feature>" ); Uri midAUri = builder.expectResource( "<feature>" + "<name>mid_a</name>" + "</feature>" ); Uri midBUri = builder.expectResource( "<feature>" + "<name>mid_b</name>" + "</feature>" ); Uri bottomUri = builder.expectResource( "<feature>" + "<name>bottom</name>" + "</feature>" ); Uri txtFile = builder.expectResource(topUri.toString() + '\n' + midAUri.toString() + '\n' + midBUri.toString() + '\n' + bottomUri.toString(), ".txt"); registry = builder.build(txtFile.toString());
Uri topUri = builder.expectResource( "<feature>" + "<name>top</name>" + "</feature>" ); Uri midAUri = builder.expectResource( "<feature>" + "<name>mid_a</name>" + "</feature>" ); Uri midBUri = builder.expectResource( "<feature>" + "<name>mid_b</name>" + "</feature>" ); Uri bottomUri = builder.expectResource( "<feature>" + "<name>bottom</name>" + "</feature>" ); Uri txtFile = builder.expectResource(topUri.toString() + '\n' + midAUri.toString() + '\n' + midBUri.toString() + '\n' + bottomUri.toString(), ".txt"); registry = builder.build(txtFile.toString());
Uri content1Uri = builder.expectResource(content1); Map<String, String> attribs = Maps.newHashMap(); String theContainer = "the-container"; attribs.put("container", theContainer); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null, attribs)); registry = builder.build(feature1Uri.toString());
Uri content1Uri = builder.expectResource(content1); Map<String, String> attribs = Maps.newHashMap(); String theContainer = "the-container"; attribs.put("container", theContainer); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null, attribs)); registry = builder.build(feature1Uri.toString());
Uri content1Uri = builder.expectResource(content1); Map<String, String> attribs = Maps.newHashMap(); String theContainer = "the-container"; attribs.put("container", theContainer); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null, attribs)); registry = builder.build(feature1Uri.toString());
Uri content1Uri = builder.expectResource(content1); Map<String, String> attribs = Maps.newHashMap(); String theContainer = "the-container"; attribs.put("container", theContainer); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null, attribs)); registry = builder.build(feature1Uri.toString());
featureA = builder.expectResource("<feature>" + "<name>featureA</name>" + "<dependency>featureB</dependency>" + "<gadget>" + " <script>top();gadget();</script>" + "</gadget>" + "<all>" + " <script>top();all();</script>" + "</all>" + "</feature>"); featureB = builder.expectResource("<feature>" + "<name>featureB</name>" + "<dependency>featureC</dependency>" + "<container>" + " <script>mid_a();container();</script>" + "</container>" + "<all>" + " <script>mid_a();all();</script>" + "</all>" + "</feature>"); featureC = builder.expectResource("<feature>" + "<name>featureC</name>" + "<dependency>featureD</dependency>" + "<container>" + " <script>mid_b();container();</script>" + "</container>" + "<gadget>" + " <script>mid_b();gadget();</script>" + "</gadget>" + "</feature>"); featureD = builder.expectResource("<feature>" + "<name>featureD</name>" + "<all>" + " <script>bottom();all();</script>" + "</all>" + "</feature>"); txtFile = builder.expectResource( featureA.toString() + '\n' + featureB.toString() + '\n' + featureC.toString() + '\n' + featureD.toString(), registry = builder.build(txtFile.toString()); } catch (GadgetException e) { fail("Shouldn't throw a GadgetException.");
@Test(expected = GadgetException.class) public void testCheckDependencyLoopWithPartialLoopDependencyError() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Uri featureA, featureB, featureC, featureD, txtFile; // featureC and featureD have dependency loop problem. featureA = builder.expectResource("<feature>" + "<name>featureA</name>" + "<dependency>featureB</dependency>" + "<gadget>" + " <script>top();gadget();</script>" + "</gadget>" + "<all>" + " <script>top();all();</script>" + "</all>" + "</feature>"); featureB = builder.expectResource("<feature>" + "<name>featureB</name>" + "<dependency>featureC</dependency>" + "<container>" + " <script>mid_a();container();</script>" + "</container>" + "<all>" + " <script>mid_a();all();</script>" + "</all>" + "</feature>"); featureC = builder.expectResource("<feature>" + "<name>featureC</name>" + "<dependency>featureD</dependency>" + "<container>" + " <script>mid_b();container();</script>" + "</container>" + "<gadget>" + " <script>mid_b();gadget();</script>" + "</gadget>" + "</feature>"); featureD = builder.expectResource("<feature>" + "<name>featureD</name>" + "<dependency>featureC</dependency>" + "<all>" + " <script>bottom();all();</script>" + "</all>" + "</feature>"); txtFile = builder.expectResource( featureA.toString() + '\n' + featureB.toString() + '\n' + featureC.toString() + '\n' + featureD.toString(), ".txt"); registry = builder.build(txtFile.toString()); }
featureA = builder.expectResource("<feature>" + "<name>featureA</name>" + "<dependency>featureB</dependency>" + "<gadget>" + " <script>top();gadget();</script>" + "</gadget>" + "<all>" + " <script>top();all();</script>" + "</all>" + "</feature>"); featureB = builder.expectResource("<feature>" + "<name>featureB</name>" + "<dependency>featureC</dependency>" + "<container>" + " <script>mid_a();container();</script>" + "</container>" + "<all>" + " <script>mid_a();all();</script>" + "</all>" + "</feature>"); featureC = builder.expectResource("<feature>" + "<name>featureC</name>" + "<dependency>featureD</dependency>" + "<container>" + " <script>mid_b();container();</script>" + "</container>" + "<gadget>" + " <script>mid_b();gadget();</script>" + "</gadget>" + "</feature>"); featureD = builder.expectResource("<feature>" + "<name>featureD</name>" + "<all>" + " <script>bottom();all();</script>" + "</all>" + "</feature>"); txtFile = builder.expectResource( featureA.toString() + '\n' + featureB.toString() + '\n' + featureC.toString() + '\n' + featureD.toString(), registry = builder.build(txtFile.toString()); } catch (GadgetException e) { fail("Shouldn't throw a GadgetException.");
@Test(expected = GadgetException.class) public void testCheckDependencyLoopWithLoopDependencyError() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Uri featureA, featureB, featureC, featureD, txtFile; // featureA, featureB, featureC and featureD have dependency loop // problem. featureA = builder.expectResource("<feature>" + "<name>featureA</name>" + "<dependency>featureB</dependency>" + "<gadget>" + " <script>top();gadget();</script>" + "</gadget>" + "<all>" + " <script>top();all();</script>" + "</all>" + "</feature>"); featureB = builder.expectResource("<feature>" + "<name>featureB</name>" + "<dependency>featureC</dependency>" + "<container>" + " <script>mid_a();container();</script>" + "</container>" + "<all>" + " <script>mid_a();all();</script>" + "</all>" + "</feature>"); featureC = builder.expectResource("<feature>" + "<name>featureC</name>" + "<dependency>featureD</dependency>" + "<container>" + " <script>mid_b();container();</script>" + "</container>" + "<gadget>" + " <script>mid_b();gadget();</script>" + "</gadget>" + "</feature>"); featureD = builder.expectResource("<feature>" + "<name>featureD</name>" + "<dependency>featureA</dependency>" + "<all>" + " <script>bottom();all();</script>" + "</all>" + "</feature>"); txtFile = builder.expectResource( featureA.toString() + '\n' + featureB.toString() + '\n' + featureC.toString() + '\n' + featureD.toString(), ".txt"); registry = builder.build(txtFile.toString()); }
Uri content1Uri = builder.expectResource(content1); Map<String, String> attribs = Maps.newHashMap(); String theContainer = "the-container"; attribs.put("two", "attrib-two"); attribs.put("three", "attrib-three"); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null, attribs, resourceAttribs)); registry = builder.build(feature1Uri.toString());
@Test(expected = GadgetException.class) public void testCheckDependencyLoopWithPartialLoopDependencyError() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Uri featureA, featureB, featureC, featureD, txtFile; // featureC and featureD have dependency loop problem. featureA = builder.expectResource("<feature>" + "<name>featureA</name>" + "<dependency>featureB</dependency>" + "<gadget>" + " <script>top();gadget();</script>" + "</gadget>" + "<all>" + " <script>top();all();</script>" + "</all>" + "</feature>"); featureB = builder.expectResource("<feature>" + "<name>featureB</name>" + "<dependency>featureC</dependency>" + "<container>" + " <script>mid_a();container();</script>" + "</container>" + "<all>" + " <script>mid_a();all();</script>" + "</all>" + "</feature>"); featureC = builder.expectResource("<feature>" + "<name>featureC</name>" + "<dependency>featureD</dependency>" + "<container>" + " <script>mid_b();container();</script>" + "</container>" + "<gadget>" + " <script>mid_b();gadget();</script>" + "</gadget>" + "</feature>"); featureD = builder.expectResource("<feature>" + "<name>featureD</name>" + "<dependency>featureC</dependency>" + "<all>" + " <script>bottom();all();</script>" + "</all>" + "</feature>"); txtFile = builder.expectResource( featureA.toString() + '\n' + featureB.toString() + '\n' + featureC.toString() + '\n' + featureD.toString(), ".txt"); registry = builder.build(txtFile.toString()); }
@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(expected = GadgetException.class) public void testCheckDependencyLoopWithLoopDependencyError() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); Uri featureA, featureB, featureC, featureD, txtFile; // featureA, featureB, featureC and featureD have dependency loop // problem. featureA = builder.expectResource("<feature>" + "<name>featureA</name>" + "<dependency>featureB</dependency>" + "<gadget>" + " <script>top();gadget();</script>" + "</gadget>" + "<all>" + " <script>top();all();</script>" + "</all>" + "</feature>"); featureB = builder.expectResource("<feature>" + "<name>featureB</name>" + "<dependency>featureC</dependency>" + "<container>" + " <script>mid_a();container();</script>" + "</container>" + "<all>" + " <script>mid_a();all();</script>" + "</all>" + "</feature>"); featureC = builder.expectResource("<feature>" + "<name>featureC</name>" + "<dependency>featureD</dependency>" + "<container>" + " <script>mid_b();container();</script>" + "</container>" + "<gadget>" + " <script>mid_b();gadget();</script>" + "</gadget>" + "</feature>"); featureD = builder.expectResource("<feature>" + "<name>featureD</name>" + "<dependency>featureA</dependency>" + "<all>" + " <script>bottom();all();</script>" + "</all>" + "</feature>"); txtFile = builder.expectResource( featureA.toString() + '\n' + featureB.toString() + '\n' + featureC.toString() + '\n' + featureD.toString(), ".txt"); registry = builder.build(txtFile.toString()); }
@Test public void registerOverrideFeature() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); // Feature 1 String content1 = "content1()"; Uri content1Uri = builder.expectResource(content1); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null)); String content2 = "content_two()"; Uri content2Uri = builder.expectResource(content2); Uri feature2Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content2Uri.getPath(), null)); registry = builder.build(feature1Uri.toString()); List<FeatureResource> resources1 = registry.getAllFeatures().getResources(); assertEquals(1, resources1.size()); assertEquals(content1, resources1.get(0).getContent()); // Register it again, different def. registry = builder.build(feature2Uri.toString()); List<FeatureResource> resources2 = registry.getAllFeatures().getResources(); assertEquals(1, resources2.size()); assertEquals(content2, resources2.get(0).getContent()); // Check cached resources too. List<FeatureResource> resourcesAgain = registry.getAllFeatures().getResources(); assertSame(resources2, resourcesAgain); }
@Test public void registerOverrideFeature() throws Exception { TestFeatureRegistry.Builder builder = TestFeatureRegistry.newBuilder(); // Feature 1 String content1 = "content1()"; Uri content1Uri = builder.expectResource(content1); Uri feature1Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content1Uri.getPath(), null)); String content2 = "content_two()"; Uri content2Uri = builder.expectResource(content2); Uri feature2Uri = builder.expectResource(xml(BOTTOM_TPL, "gadget", content2Uri.getPath(), null)); registry = builder.build(feature1Uri.toString()); List<FeatureResource> resources1 = registry.getAllFeatures().getResources(); assertEquals(1, resources1.size()); assertEquals(content1, resources1.get(0).getContent()); // Register it again, different def. registry = builder.build(feature2Uri.toString()); List<FeatureResource> resources2 = registry.getAllFeatures().getResources(); assertEquals(1, resources2.size()); assertEquals(content2, resources2.get(0).getContent()); // Check cached resources too. List<FeatureResource> resourcesAgain = registry.getAllFeatures().getResources(); assertSame(resources2, resourcesAgain); }
@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 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()); }