### Question:
ProtocolUtils { public static void localizeRoleIntroductions(ProtocolModel pm) { final java.util.List<Role> unused=new java.util.ArrayList<Role>(); pm.visit(new DefaultVisitor() { public boolean start(Protocol p) { java.util.Iterator<Activity> iter=p.getBlock().getContents().iterator(); while (iter.hasNext()) { Activity act=iter.next(); if (act instanceof Introduces) { Introduces rl=(Introduces)act; for (int i=rl.getIntroducedRoles().size()-1; i >= 0; i--) { Role r=rl.getIntroducedRoles().get(i); Block b=RoleUtil.getEnclosingBlock(p, r, false); if (b == null) { unused.add(r); } else if (b != p.getBlock()) { Introduces innerrl=null; if (b.size() > 0 && b.get(0) instanceof Introduces && ((Introduces)b.get(0)).getIntroducer().equals(rl.getIntroducer())) { innerrl = (Introduces)b.get(0); } else { innerrl = new Introduces(); innerrl.setIntroducer(rl.getIntroducer()); b.getContents().add(0, innerrl); } rl.getIntroducedRoles().remove(r); innerrl.getIntroducedRoles().add(r); } } if (rl.getIntroducedRoles().size() == 0) { iter.remove(); } } else { break; } } return (true); } }); for (Role role : unused) { if (role.getParent() instanceof Introduces) { Introduces introduces=(Introduces)role.getParent(); Protocol protocol=introduces.getEnclosingProtocol(); locateRoleIntroductionWithinRun(protocol, role, introduces.getIntroducer()); introduces.getIntroducedRoles().remove(role); if (introduces.getIntroducedRoles().size() == 0) { ((Block)introduces.getParent()).remove(introduces); } } } }  static void setStartAndEndPosition(ModelObject obj, String contents, org.w3c.dom.Element elem); static String getNamespacePrefix(ProtocolModel model, String namespace); static void localizeRoleIntroductions(ProtocolModel pm);  }### Answer:
@Test public void test() { String filename="testmodels/protocol/LocalizeIntroduces.spr"; java.net.URL url= ClassLoader.getSystemResource(filename); if (url == null) { fail("Unable to locate resource: "+filename); } else { DefaultFeedbackHandler handler=new DefaultFeedbackHandler(); Journal journal=new JournalProxy(handler); org.scribble.protocol.model.ProtocolModel model=null; ANTLRProtocolParser parser=new ANTLRProtocolParser(); parser.setAnnotationProcessor(new org.savara.protocol.parser.AnnotationProcessor()); try { Content content=new ResourceContent(url.toURI()); model = parser.parse(null, content, journal); } catch(Exception e) { fail("Parsing protocol failed"); } if (model == null) { fail("Model is null"); } else { ProtocolUtils.localizeRoleIntroductions(model); org.scribble.protocol.export.text.TextProtocolExporter exporter= new org.scribble.protocol.export.text.TextProtocolExporter(); java.io.ByteArrayOutputStream os=new java.io.ByteArrayOutputStream(); exporter.export(model, journal, os); try { os.close(); } catch(Exception e) { fail("Failed to close stream"); } String str=os.toString(); filename="results/protocol/LocalizeIntroduces.spr"; java.io.InputStream is= ClassLoader.getSystemResourceAsStream(filename); if (is != null) { try { byte[] b=new byte[is.available()]; is.read(b); is.close(); String expected=new String(b); if (!expected.equals(str)) { System.err.println("Localized introduces not as expected:\r\n"+ expected+"\r\nbut got:\r\n"+str); fail("Localized introduces not as expected:\r\n"+ expected+"\r\nbut got:\r\n"+str); } } catch (Exception e) { fail("Failed to load expected protocol"); } } } } }