OrganizePom extends AbstractTransformer { @Override public void transform() { if (!enabled) return; String[] expectedElements = loadPrototype(); Element root = pom.getDom().getDocumentElement(); Map<String,Element> children = selectElements(root); Utils.reconstruct(root, children, expectedElements); } OrganizePom(PomWrapper pom, CommandLine args); @Override void transform();  }
@Test public void testBasicOperation() throws Exception { loadPom("cleaner/OrganizePom1.xml"); Element root = dom().getDocumentElement(); List<Element> initialChildren = DomUtil.getChildren(root); assertEquals("number of children before organize", 9, initialChildren.size()); CommandLine args = new CommandLine("--organizePom"); new OrganizePom(pom, args).transform(); List<Element> children = DomUtil.getChildren(root); assertEquals("number of children after organize", 9, children.size()); assertEquals("child 0 name", "modelVersion", children.get(0).getLocalName()); assertEquals("child 0 text", "4.0.0", DomUtil.getText(children.get(0))); assertEquals("child 1 name", "groupId", children.get(1).getLocalName()); assertEquals("child 1 text", "com.example.pom", DomUtil.getText(children.get(1))); assertEquals("child 2 name", "artifactId", children.get(2).getLocalName()); assertEquals("child 2 text", "OrganizePom1", DomUtil.getText(children.get(2))); assertEquals("child 3 name", "version", children.get(3).getLocalName()); assertEquals("child 3 text", "0.0.0-SNAPSHOT", DomUtil.getText(children.get(3))); assertEquals("child 4 name", "packaging", children.get(4).getLocalName()); assertEquals("child 4 text", "jar", DomUtil.getText(children.get(4))); assertEquals("child 5 name", "description", children.get(5).getLocalName()); assertEquals("child 6 name", "properties", children.get(6).getLocalName()); assertEquals("child 7 name", "dependencyManagement",children.get(7).getLocalName()); assertEquals("child 8 name", "dependencies", children.get(8).getLocalName()); }
@Test public void testDisabledByDefault() throws Exception { loadPom("cleaner/OrganizePom1.xml"); Element root = dom().getDocumentElement(); CommandLine args = new CommandLine(); new OrganizePom(pom, args).transform(); List<Element> children = DomUtil.getChildren(root); assertEquals("number of children", 9, children.size()); assertEquals("first child name", "description", children.get(0).getLocalName()); assertEquals("last child name", "modelVersion", children.get(8).getLocalName()); }
@Test public void testUnrecognizedChildrenAreRetained() throws Exception { loadPom("cleaner/OrganizePom2.xml"); Element root = dom().getDocumentElement(); List<Element> initialChildren = DomUtil.getChildren(root); assertEquals("number of children before organize", 7, initialChildren.size()); CommandLine args = new CommandLine("--organizePom"); new OrganizePom(pom, args).transform(); List<Element> children = DomUtil.getChildren(root); assertEquals("number of children after organize", 7, children.size()); assertEquals("child 0 name", "modelVersion", children.get(0).getLocalName()); assertEquals("child 1 name", "groupId", children.get(1).getLocalName()); assertEquals("child 2 name", "artifactId", children.get(2).getLocalName()); assertEquals("child 3 name", "version", children.get(3).getLocalName()); assertEquals("child 4 name", "packaging", children.get(4).getLocalName()); assertEquals("child 5 name", "description", children.get(5).getLocalName()); assertEquals("child 6 name", "argle", children.get(6).getLocalName()); }