public static void main(String[] args) { /* The following test code is just an example of how you can write * a simple test program to check the basic functionality of your class * works as you expect. The primary aim is to test each constructor and * method - there are many combinations of how you could go about this. * For more complex classes and/or methods you may write several different * tests for each method to check boundary cases for example. */ System.out.println("Testing default constructor initialisation and get method..."); Counter c = new Counter(); System.out.println("Expected 0: Actual: " + c.getCount()); System.out.println("Testing increment method..."); c.increment(); c.increment(); System.out.println("Expected 2: Actual: " + c.getCount()); System.out.println("Testing set method..."); c.setCount(10); System.out.println("Expected 10: Actual: " + c.getCount()); System.out.println("Testing custom constructor initialisation..."); Counter c1 = new Counter( 5); // Note: creating a second object instance of the Counter class (which has its own // state) System.out.println("Expected 5: Actual: " + c1.getCount()); System.out.println("Testing reset method..."); c1.reset(); System.out.println("Expected 0: Actual: " + c1.getCount()); System.out.println("Testing toString method..."); System.out.println("Expected count=0: Actual: " + c1.toString()); System.out.println("Testing decrement method.."); c.decrement(); c.decrement(); System.out.println("Expected 1: Actual:" + c.getCount()); Counter launch = new Counter(); launch.incremendBy(10); while (!launch.isZero()) { System.out.print(launch.getCount() + " "); launch.decrement(); } System.out.print("Blast off!"); }
public static void main(String[] args) { System.out.println("This programs tests the features of the counter class."); Counter c1 = new Counter(), c2 = new Counter(); tester(c1, c2); c2.decrement(); tester(c1, c2); c1.setCounter(0); c2.increment(); tester(c1, c2); c2.setCounter(c1.getCounter()); tester(c1, c2); c1.decrement(); tester(c1, c2); System.out.println("Test complete."); }
public void testNumber() { Counter ct = CounterFactory.createCounter("testNumber", Type.NUMBER); assertEquals("Default value", 0.0, ct.nextValue(), delta); ct.setRawValue(10.0); assertEquals("setRawValue", 10.0, ct.nextValue(), delta); ct.increment(); assertEquals("increment", 11.0, ct.nextValue(), delta); ct.incrementBy(2.0); assertEquals("incrementBy", 13.0, ct.nextValue(), delta); ct.decrement(); assertEquals("decrement", 12.0, ct.nextValue(), delta); }
public static void main(String[] args) throws Exception { MBeanServer mserver = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("cz.jalasoft.learning.jmx:type=Monitoring"); // Monitoring mbean = new Monitoring(30); Counter counter = new Counter(40); MyDynamic dbean = new MyDynamic(counter); mserver.registerMBean(dbean, name); while (true) { Thread.sleep(1000); if (counter.value() <= 0) { break; } System.out.println("Counting down: " + counter.value()); counter.decrement(); } }
@Override public void run() { for (int i = 0; i < repeats; i++) counter.decrement(); }
protected void revisionToInstance() { synchronized (recursionCounter) { if (underConstruction) { // Return if revisionToInstance was called before to avoid doubled calls return; } underConstruction = true; InternalCDORevision revision = cdoRevision(); if (TRACER.isEnabled()) { TRACER.format( "Transfering revision to instance: {0} --> {1}", revision, instance); // $NON-NLS-1$ } boolean deliver = instance.eDeliver(); if (deliver) { instance.eSetDeliver(false); } Counter counter = recursionCounter.get(); if (counter == null) { counter = new Counter(); recursionCounter.set(counter); } InternalCDOResource resource = null; boolean bypassPermissionChecks = revision.bypassPermissionChecks(true); try { registerWrapper(this); counter.increment(); viewAndState.view.registerObject(this); revisionToInstanceResource(); revisionToInstanceContainer(); Resource eResource = instance.eResource(); if (eResource instanceof InternalCDOResource) { resource = (InternalCDOResource) eResource; resource.cdoInternalLoading(instance); } for (EStructuralFeature feature : classInfo.getAllPersistentFeatures()) { revisionToInstanceFeature(feature); } } catch (RuntimeException ex) { OM.LOG.error(ex); throw ex; } catch (Exception ex) { OM.LOG.error(ex); throw new CDOException(ex); } finally { try { revision.bypassPermissionChecks(bypassPermissionChecks); if (resource != null) { resource.cdoInternalLoadingDone(instance); } if (deliver) { instance.eSetDeliver(true); } } finally { if (counter.decrement() == 0) { recursionCounter.remove(); } unregisterWrapper(this); underConstruction = false; } } } }