ServerScope { public static ServerScope parse(String scope) throws IllegalArgumentException { if (scope.startsWith("unlimited")) { return Unlimited.getInstance(); } String[] chunks = scope.trim().split(":", 2); if (chunks.length != 2) throw new IllegalArgumentException("Invalid scope: " + scope); if ("node".equals(chunks[0])) { return new Node(chunks[1]); } else if ("run".equals(chunks[0])) { return new Build(chunks[1]); } else if ("time".equals(chunks[0])) { return new Time(chunks[1]); } else { throw new IllegalArgumentException("Invalid scope kind: " + chunks[0]); } } private  ServerScope(@Nonnull String scopeName, @Nonnull String specifier); static ServerScope parse(String scope); static @Nonnull ServerScope extract(Server server); @Override String toString(); String getValue(); @Override int hashCode(); @Override boolean equals(Object o); abstract boolean isOutOfScope(@Nonnull Server server); static final String METADATA_KEY; }
@Test @WithoutJenkins public void parse() { ServerScope.Node node = (ServerScope.Node) ServerScope.parse("node:asdf"); assertEquals("asdf", node.getName()); assertEquals("node:asdf", node.getValue()); assertEquals(node, ServerScope.parse(node.getValue())); ServerScope.Node node2 = (ServerScope.Node) ServerScope.parse("node:asdf:-4242"); assertEquals("asdf", node2.getName()); assertEquals("node:asdf:-4242", node2.getValue()); assertEquals(node2, ServerScope.parse(node2.getValue())); assertNotEquals(node, node2); ServerScope.Build run = (ServerScope.Build) ServerScope.parse("run:asdf:4"); assertEquals("asdf", run.getProject()); assertEquals(4, run.getRunNumber()); assertEquals("run:asdf:4", run.getValue()); assertEquals(run, ServerScope.parse(run.getValue())); ServerScope.Time time = (ServerScope.Time) ServerScope.parse("time:2017-01-11 14:09:25"); ServerScope.Time time2 = (ServerScope.Time) ServerScope.parse(time.getValue()); assertEquals("time:2017-01-11 14:09:25", time.getValue()); assertEquals(time, time2); ServerScope.Unlimited unlimited = (ServerScope.Unlimited) ServerScope.parse("unlimited:Custom reason specified here if needed"); assertEquals("unlimited:unlimited", unlimited.getValue()); assertEquals(unlimited, ServerScope.parse(unlimited.getValue())); }