Sampler { public static Sampler create(Double selectPercent) { if (selectPercent.equals(ALWAYS)) { return new AlwaysSampler(); } else if (selectPercent.equals(NEVER)) { return new NeverSampler(); } else { return new Sampler(selectPercent); } } protected  Sampler(); protected  Sampler(double selectPercent); static Sampler create(Double selectPercent); boolean select();  }
@Test public void always() { Sampler sampler = Sampler.create(0d); assertThat(sampler).isInstanceOf(NeverSampler.class); sampler = Sampler.create(100d); assertThat(sampler).isInstanceOf(AlwaysSampler.class); try { sampler = Sampler.create(101d); fail("shoud fail before"); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalArgumentException.class); } try { sampler = Sampler.create(-2.2); fail("shoud fail before"); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalArgumentException.class); } }