@Test public void testEarliest() { Event event = BuildUtil.earliest(Event.Type.success, e0, e1, e2, e3, e4, e5, e6, e7); assertThat(event, is(e1)); event = BuildUtil.earliest(Event.Type.failure, e1, e2, e6); assertNull(event); event = BuildUtil.earliest(Event.Type.success); assertNull(event); }
@Test public void testEmptyAggregate() { Event event = BuildUtil.aggregate(Event.Type.failure); assertNull(event); event = BuildUtil.aggregate(Event.Type.failure, e1, e2); assertNull(event); event = BuildUtil.aggregate(Event.Type.success, e0, e3, e4, e5); assertNull(event); }
@Test public void testLatest() { Event event = BuildUtil.latest(Event.Type.failure, e0, e1, e2, e3, e4, e5, e6, e7); assertThat(event, is(e5)); event = BuildUtil.latest(Event.Type.success, e0, e3, e4); assertNull(event); event = BuildUtil.latest(Event.Type.failure); assertNull(event); }
@Test public void testBreakNames() { Builds builds = new Builds(); builds.build("b0").job("j0").events(e0, e1, e2, e3, e4, e5, e6, e7); builds.build("b0").job("j1").events(); builds.build("b1").job("j0").events(e0, e3, e4); builds = BuildUtil.summarize(builds); Collection<String> names = BuildUtil.summarizedBreakNames(builds); assertCollection(names, "c1", "c2", "c3", "d1"); }
@Test public void testCountEvents() { Builds builds = new Builds(); builds.build("b0").job("j0").events(e0, e1); assertThat(BuildUtil.countEvents(builds), is(2)); builds.build("b0").job("j1").events(e2); assertThat(BuildUtil.countEvents(builds), is(3)); builds.build("b1").job("j0").events(e0); assertThat(BuildUtil.countEvents(builds), is(4)); }
@Test public void testAggregate() { Event event = BuildUtil.aggregate(Event.Type.failure, e0, e1, e2, e3, e4, e5, e6, e7); assertThat(event.type, is(Event.Type.failure)); assertThat(event.timeStamp, is(0L)); assertCollection(event.names, "c1", "c2", "c3", "d1", "e1", "e2", "e3", "e4"); event = BuildUtil.aggregate(Event.Type.success, e0, e1, e2, e3, e4, e5, e6, e7); assertThat(event.type, is(Event.Type.success)); assertThat(event.timeStamp, is(1L)); assertCollection(event.names, "a1", "b1", "b2", "g1"); }
@Test public void testSummarize() { Builds builds = new Builds(); builds.build("b0").job("j0").events(e0, e1, e2, e3, e4, e5, e6, e7); builds.build("b0").job("j1").events(); builds.build("b1").job("j0").events(e0, e3, e4); builds = BuildUtil.summarize(builds); assertBuildNames(builds.builds, "b0", "b1"); assertJobNames(builds.build("b0").jobs, "j0", "j1"); assertJobNames(builds.build("b1").jobs, "j0"); assertIterable( builds.build("b0").job("j0").events, new Event(Event.Type.success, 6L), new Event(Event.Type.failure, 3L, "c1", "c2", "c3", "d1", "e1", "e2", "e3", "e4")); assertTrue(builds.build("b0").job("j1").events.isEmpty()); assertIterable( builds.build("b1").job("j0").events, new Event(Event.Type.failure, 0L, "c1", "c2", "c3", "d1")); }
/** * 根据传入参数创建CLASS文件 * * @param 类名 * @param 保存路径 * @param 属性描述 * @return Class */ public Class build(String clsname, String savepath, Collection<Property> properties) { Class cls = null; try { String classname = BuildUtil.transferClassName(clsname); ClassWriter cw = new ClassWriter(0); cw.visit( V1_1, ACC_PUBLIC, classname, null, "edu/zju/cims201/GOF/hibernate/pojo/MetaKnowledge", new String[] {"edu/zju/cims201/GOF/hibernate/pojo/Knowledge", "java/io/Serializable"}); // 建立构造函数 MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mw.visitVarInsn(ALOAD, 0); mw.visitMethodInsn( INVOKESPECIAL, "edu/zju/cims201/GOF/hibernate/pojo/MetaKnowledge", "<init>", "()V"); mw.visitInsn(RETURN); mw.visitMaxs(1, 1); mw.visitEnd(); BuildProperty property = null; String propertytype = null; String propertyname = null; ; // 建立属性 Iterator iterator = properties.iterator(); while (iterator.hasNext()) { // 建立属性对应的类变量 property = (BuildProperty) (iterator.next()); if (!property.getName().equals("knowledgetype")) { propertytype = BuildUtil.transferClassName(property.getPropertyType()); propertyname = WordUtils.capitalize(property.getName()); cw.visitField(ACC_PRIVATE, property.getName(), "L" + propertytype + ";", null, null) .visitEnd(); // 建立get方法 mw = cw.visitMethod( ACC_PUBLIC, "get" + propertyname, "()L" + propertytype + ";", null, null); mw.visitCode(); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, classname, property.getName(), "L" + propertytype + ";"); mw.visitInsn(ARETURN); mw.visitMaxs(1, 1); mw.visitEnd(); // 建立set方法 mw = cw.visitMethod( ACC_PUBLIC, "set" + propertyname, "(L" + propertytype + ";)V", null, null); mw.visitCode(); mw.visitVarInsn(ALOAD, 0); mw.visitVarInsn(ALOAD, 1); mw.visitFieldInsn(PUTFIELD, classname, property.getName(), "L" + propertytype + ";"); mw.visitMaxs(2, 2); mw.visitInsn(RETURN); mw.visitEnd(); } } cw.visitEnd(); byte[] code = cw.toByteArray(); if (savepath != null) { Assistant.createNewFileFromWeb(savepath); FileOutputStream fos = new FileOutputStream(savepath); fos.write(code); fos.close(); } // web方式下出错,先注释掉 // cls = this.defineClass(clsname, code, 0, code.length); return cls; } catch (Throwable e) { e.printStackTrace(); } return cls; }