Java Code Examples for org.junit.Test

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project accounted4, under directory /accounted4/accounted4-money/src/test/java/com/accounted4/money/loan/.

Source file: AmortizationCalculatorTest.java

  36 
vote

@Test public void testGetPaymentsInterestOnlyExtraPrincipal(){
  System.out.println("getPaymentsInterestOnlyExtraPrincipal");
  Money amount=new Money("100.00");
  double rate=12.0;
  AmortizationAttributes terms=new AmortizationAttributes();
  terms.setInterestOnly(true);
  terms.setLoanAmount(amount);
  terms.setTermInMonths(12);
  terms.setInterestRate(rate);
  double interestOnlyMonthlyPayment=AmortizationCalculator.getInterestOnlyMonthlyPayment(amount.getAmount().doubleValue(),rate);
  Money installment=new Money(BigDecimal.valueOf(interestOnlyMonthlyPayment));
  installment=installment.add(new Money("15.00"));
  terms.setRegularPayment(installment);
  LocalDate today=new LocalDate();
  LocalDate adjDate=new LocalDate();
  int dayOfMonth=today.getDayOfMonth();
  if (1 != dayOfMonth) {
    if (dayOfMonth > 15) {
      adjDate=new LocalDate(today.getYear(),today.getMonthOfYear(),1);
      adjDate=adjDate.plusMonths(1);
    }
 else     if (dayOfMonth < 15) {
      adjDate=new LocalDate(today.getYear(),today.getMonthOfYear(),15);
    }
  }
  terms.setStartDate(today);
  terms.setAdjustmentDate(adjDate);
  Iterator<ScheduledPayment> result=AmortizationCalculator.getPayments(terms);
  int resultCount=0;
  Money interestTotal=new Money("0.00");
  while (result.hasNext()) {
    resultCount++;
    ScheduledPayment payment=result.next();
    interestTotal=interestTotal.add(payment.getInterest());
  }
  assertEquals("Interest Only payment count -extra principal",7,resultCount);
  assertEquals("Interest Only interest total",new Money("7"),interestTotal);
}
 

Example 2

From project activejdbc, under directory /activejdbc/src/test/java/org/javalite/activejdbc/.

Source file: AssociationEqualityTest.java

  31 
vote

@Test public void testBelongsToEquals(){
  BelongsToAssociation one=new BelongsToAssociation("hello","world","hello_id");
  BelongsToAssociation two=new BelongsToAssociation("hello","world","hello_id");
  a(one).shouldBeEqual(two);
  BelongsToAssociation three=new BelongsToAssociation("hello","world","hi_id");
  a(one).shouldNotBeEqual(three);
}
 

Example 3

From project 4308Cirrus, under directory /tendril-android-lib/src/test/java/edu/colorado/cs/cirrus/android/.

Source file: DeserializationTest.java

  29 
vote

@Test public void canDeserializeMeterReading(){
  RestTemplate restTemplate=new RestTemplate();
  restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpUtils.getNewHttpClient()));
  HttpInputMessage him=new HttpInputMessage(){
    public HttpHeaders getHeaders(){
      return new HttpHeaders();
    }
    public InputStream getBody() throws IOException {
      return new FileInputStream(new File("src/test/resources/MeterReadings.xml"));
    }
  }
;
  for (  HttpMessageConverter<?> hmc : restTemplate.getMessageConverters()) {
    System.out.println("hmc: " + hmc);
    if (hmc.canRead(MeterReadings.class,MediaType.APPLICATION_XML)) {
      HttpMessageConverter<MeterReadings> typedHmc=(HttpMessageConverter<MeterReadings>)hmc;
      System.out.println("Can Read: " + hmc);
      hmc.canRead(MeterReadings.class,MediaType.APPLICATION_XML);
      try {
        MeterReadings mr=(MeterReadings)typedHmc.read(MeterReadings.class,him);
        System.out.println(mr);
      }
 catch (      HttpMessageNotReadableException e) {
        e.printStackTrace();
        fail();
      }
catch (      IOException e) {
        e.printStackTrace();
        fail();
      }
    }
  }
}
 

Example 4

From project 4308Cirrus, under directory /tendril-domain/src/test/java/edu/colorado/cs/cirrus/domain/model/.

Source file: CostAndConsumptionTest.java

  29 
vote

@Test public void canDeserializeCostAndConsumption(){
  Serializer serializer=new Persister();
  File source=new File("src/test/resources/CostAndConsumption.xml");
  try {
    CostAndConsumption exampleCostAndConsumption=serializer.read(CostAndConsumption.class,source);
    System.err.println(exampleCostAndConsumption);
    assertNotNull(exampleCostAndConsumption.getComponentList());
    assertNotNull(exampleCostAndConsumption.getConversionFactorList());
    assertNotNull(exampleCostAndConsumption.getDeviceCostAndConsumptionList());
  }
 catch (  Exception e) {
    e.printStackTrace();
    fail();
  }
}
 

Example 5

From project 4308Cirrus, under directory /tendril-domain/src/test/java/edu/colorado/cs/cirrus/domain/model/.

Source file: DeviceActionQueryTest.java

  29 
vote

@Test public void canDeserializeDeviceActionQuery1(){
  Serializer serializer=new Persister();
  File source=new File("src/test/resources/QueryDeviceAction1.xml");
  try {
    DeviceActionQuery exampleUser=serializer.read(DeviceActionQuery.class,source);
    System.err.println(exampleUser);
  }
 catch (  Exception e) {
    e.printStackTrace();
    fail();
  }
}
 

Example 6

From project 4308Cirrus, under directory /tendril-domain/src/test/java/edu/colorado/cs/cirrus/domain/model/.

Source file: DeviceActionQueryTest.java

  29 
vote

@Test public void canDeserializeDeviceActionQuery2(){
  Serializer serializer=new Persister();
  File source=new File("src/test/resources/QueryDeviceAction2.xml");
  try {
    DeviceActionQuery exampleUser=serializer.read(DeviceActionQuery.class,source);
    System.err.println(exampleUser);
  }
 catch (  Exception e) {
    e.printStackTrace();
    fail();
  }
}
 

Example 7

From project Aardvark, under directory /aardvark/src/main/test/gw/vark/it/.

Source file: AntTasksITCase.java

  29 
vote

@Test public void testHello(){
  TestOutputHandler stdOut=new TestOutputHandler("stdout");
  TestOutputHandler stdErr=new TestOutputHandler("stderr");
  runAnt("hello",stdOut,stdErr);
  assertThatOutput(stdErr).isEmpty();
  assertThatOutput(stdOut).containsSequence("","hello:","     [echo] hello","","BUILD SUCCESSFUL");
}
 

Example 8

From project Aardvark, under directory /aardvark/src/main/test/gw/vark/it/.

Source file: AntTasksITCase.java

  29 
vote

@Test public void testGosuHello(){
  TestOutputHandler stdOut=new TestOutputHandler("stdout");
  TestOutputHandler stdErr=new TestOutputHandler("stderr");
  runAnt("gosu-hello",stdOut,stdErr);
  assertThatOutput(stdErr).isEmpty();
  assertThatOutput(stdOut).containsSequence("","init-gosu:","","gosu-hello:","     [gosu] hello","","BUILD SUCCESSFUL");
}
 

Example 9

From project Aardvark, under directory /aardvark/src/main/test/gw/vark/it/.

Source file: AssemblyStructureITCase.java

  29 
vote

@Test public void licenseExists(){
  File assemblyDir=ITUtil.getAssemblyDir();
  File licenseFile=new File(assemblyDir,"LICENSE");
  assertThat(licenseFile).exists();
  assertThat(licenseFile).isFile();
}
 

Example 10

From project Aardvark, under directory /aardvark-core/src/test/java/gw/vark/.

Source file: AardvarkHelpModeTest.java

  29 
vote

@Test public void printHelp(){
  StringWriter writer=new StringWriter();
  PrintWriter out=new PrintWriter(writer);
  AardvarkHelpMode.printHelp(out);
  String eol=System.getProperty("line.separator");
  assertEquals("Usage:" + eol + "        vark [-f FILE] [options] [targets...]"+ eol+ ""+ eol+ "Options:"+ eol+ "        -f, -file FILE              load a file-based Gosu source"+ eol+ "            -url URL                load a url-based Gosu source"+ eol+ "            -classpath PATH         additional elements for the classpath, separated by commas"+ eol+ "        -p, -projecthelp            show project help (e.g. targets)"+ eol+ "            -logger LOGGERFQN       class name for a logger to use"+ eol+ "        -q, -quiet                  run with logging in quiet mode"+ eol+ "        -v, -verbose                run with logging in verbose mode"+ eol+ "        -d, -debug                  run with logging in debug mode"+ eol+ "        -g, -graphical              starts the graphical Aardvark editor"+ eol+ "            -verify                 verifies the Gosu source"+ eol+ "            -version                displays the version of Aardvark"+ eol+ "        -h, -help                   displays this command-line help"+ eol+ "",writer.toString());
}
 

Example 11

From project accent, under directory /src/test/java/net/lshift/accent/.

Source file: ConnectionBehaviourTest.java

  29 
vote

@Test public void shouldKeepAttemptingToReconnect() throws Exception {
  ConnectionFactory unconnectableFactory=createMock(ConnectionFactory.class);
  expect(unconnectableFactory.newConnection()).andStubThrow(new ConnectException("Boom"));
  replay(unconnectableFactory);
  final CountDownLatch attemptLatch=new CountDownLatch(4);
  ConnectionFailureListener eventingListener=new ConnectionFailureListener(){
    @Override public void connectionFailure(    Exception e){
      if (e instanceof ConnectException) {
        attemptLatch.countDown();
      }
 else {
        System.out.println("WARNING: Received unexpected exception - " + e);
      }
    }
  }
;
  AccentConnection conn=new AccentConnection(unconnectableFactory,eventingListener);
  assertTrue(attemptLatch.await(5000,TimeUnit.MILLISECONDS));
}
 

Example 12

From project accent, under directory /src/test/java/net/lshift/accent/.

Source file: ConnectionSmokeTest.java

  29 
vote

@Test public void shouldConnectPublishAndConsume() throws IOException, InterruptedException {
  AccentConnection conn=new AccentConnection(directFactory(),AccentTestUtils.printingFailureLogger());
  try {
    AccentChannel ch=conn.createChannel();
    ch.addChannelSetupListener(new ChannelListenerAdapter(){
      @Override public void channelCreated(      Channel c) throws IOException {
        c.queueDeclare("accent.testq",false,false,true,null);
      }
    }
);
    QueueingConsumer consumer=new QueueingConsumer(null);
    AccentConsumer aConsumer=new AccentConsumer(ch,"accent.testq",consumer);
    AccentConfirmPublisher publisher=new AccentConfirmPublisher(ch);
    publisher.reliablePublish("","accent.testq",new AMQP.BasicProperties(),new byte[0]);
    QueueingConsumer.Delivery d=consumer.nextDelivery(10000);
    assertNotNull(d);
    assertEquals(0,d.getBody().length);
    aConsumer.reliableAck(d.getEnvelope().getDeliveryTag(),false);
    ch.close();
  }
  finally {
    closeQuietly(conn);
  }
}
 

Example 13

From project accent, under directory /src/test/java/net/lshift/accent/.

Source file: ConnectionSmokeTest.java

  29 
vote

@Test public void shouldConsumeEvenIfConnectionIsntCreatedUntilLater() throws IOException, InterruptedException {
  AccentConnection directConn=new AccentConnection(directFactory(),AccentTestUtils.printingFailureLogger());
  AccentConnection managedConn=new AccentConnection(controlledFactory(5673),AccentTestUtils.smallPrintingFailureLogger());
  try {
    AccentChannel directCh=directConn.createChannel();
    AccentChannel managedCh=managedConn.createChannel();
    directCh.addChannelSetupListener(new ChannelListenerAdapter(){
      @Override public void channelCreated(      Channel c) throws IOException {
        c.queueDeclare("accent.testq",false,false,true,null);
      }
    }
);
    QueueingConsumer consumer=new QueueingConsumer(null);
    AccentConsumer aConsumer=new AccentConsumer(managedCh,"accent.testq",consumer);
    AccentConfirmPublisher publisher=new AccentConfirmPublisher(directCh);
    publisher.reliablePublish("","accent.testq",new AMQP.BasicProperties(),new byte[0]);
    QueueingConsumer.Delivery d=consumer.nextDelivery(2000);
    assertNull(d);
    ControlledConnectionProxy proxy=createProxy(5673);
    try {
      QueueingConsumer.Delivery d2=consumer.nextDelivery(2000);
      assertNotNull(d2);
      assertEquals(0,d2.getBody().length);
      aConsumer.reliableAck(d2.getEnvelope().getDeliveryTag(),false);
      closeQuietly(aConsumer);
    }
  finally {
      closeQuietly(proxy);
    }
  }
  finally {
    closeQuietly(managedConn);
    closeQuietly(directConn);
  }
}
 

Example 14

From project accent, under directory /src/test/java/net/lshift/accent/.

Source file: ConnectionSmokeTest.java

  29 
vote

@Test public void shouldRecoverConsumeIfConnectionIsLost() throws IOException, InterruptedException {
  AccentConnection conn=new AccentConnection(controlledFactory(5675),AccentTestUtils.printingFailureLogger());
  ControlledConnectionProxy proxy=createProxy(5675);
  try {
    AccentChannel ch=conn.createChannel();
    ch.addChannelSetupListener(new ChannelListenerAdapter(){
      @Override public void channelCreated(      Channel c) throws IOException {
        c.queueDeclare("accent.testq",false,false,true,null);
      }
    }
);
    QueueingConsumer consumer=new QueueingConsumer(null);
    AccentConsumer aConsumer=new AccentConsumer(ch,"accent.testq",consumer);
    AccentConfirmPublisher publisher=new AccentConfirmPublisher(ch);
    publisher.reliablePublish("","accent.testq",new AMQP.BasicProperties(),new byte[0]);
    QueueingConsumer.Delivery d=consumer.nextDelivery(10000);
    assertNotNull(d);
    aConsumer.reliableAck(d.getEnvelope().getDeliveryTag(),false);
    proxy.close();
    proxy.start();
    publisher.reliablePublish("","accent.testq",new AMQP.BasicProperties(),new byte[0]);
    QueueingConsumer.Delivery d2=consumer.nextDelivery(10000);
    assertNotNull(d);
    aConsumer.reliableAck(d2.getEnvelope().getDeliveryTag(),false);
    closeQuietly(aConsumer);
  }
  finally {
    closeQuietly(proxy);
    closeQuietly(conn);
  }
}
 

Example 15

From project accounted4, under directory /accounted4/accounted4-money/src/test/java/com/accounted4/money/loan/.

Source file: AmortizationCalculatorTest.java

  29 
vote

/** 
 * Test of getPayments method, of class AmortizationCalculator.
 */
@Test public void testGetPaymentsInterestOnly(){
  System.out.println("getPaymentsInterestOnly");
  Money amount=new Money("100.00");
  double rate=12.0;
  AmortizationAttributes terms=new AmortizationAttributes();
  terms.setInterestOnly(true);
  terms.setLoanAmount(amount);
  terms.setTermInMonths(12);
  terms.setInterestRate(rate);
  double interestOnlyMonthlyPayment=AmortizationCalculator.getInterestOnlyMonthlyPayment(amount.getAmount().doubleValue(),rate);
  terms.setRegularPayment(new Money(BigDecimal.valueOf(interestOnlyMonthlyPayment)));
  LocalDate today=new LocalDate();
  LocalDate adjDate=new LocalDate();
  int dayOfMonth=today.getDayOfMonth();
  if (1 != dayOfMonth) {
    if (dayOfMonth > 15) {
      adjDate=new LocalDate(today.getYear(),today.getMonthOfYear(),1);
      adjDate=adjDate.plusMonths(1);
    }
 else     if (dayOfMonth < 15) {
      adjDate=new LocalDate(today.getYear(),today.getMonthOfYear(),15);
    }
  }
  terms.setStartDate(today);
  terms.setAdjustmentDate(adjDate);
  Iterator<ScheduledPayment> result=AmortizationCalculator.getPayments(terms);
  int resultCount=0;
  Money interestTotal=new Money("0.00");
  while (result.hasNext()) {
    resultCount++;
    ScheduledPayment payment=result.next();
    interestTotal=interestTotal.add(payment.getInterest());
  }
  assertEquals("Interest Only payment count",12,resultCount);
  assertEquals("Interest Only interest total",new Money("12"),interestTotal);
}
 

Example 16

From project accounted4, under directory /accounted4/accounted4-money/src/test/java/com/accounted4/money/loan/.

Source file: AmortizationCalculatorTest.java

  29 
vote

/** 
 * Test of getInterestOnlyMonthlyPayment method, of class AmortizationCalculator.
 */
@Test public void testGetInterestOnlyMonthlyPayment(){
  System.out.println("getInterestOnlyMonthlyPayment");
  Money amount=new Money("100000.00");
  double rate=12.0;
  Money expResult=new Money("1000.00");
  double interestOnlyMonthlyPayment=AmortizationCalculator.getInterestOnlyMonthlyPayment(amount.getAmount().doubleValue(),rate);
  Money result=new Money(BigDecimal.valueOf(interestOnlyMonthlyPayment));
  assertEquals(expResult,result);
}
 

Example 17

From project accounted4, under directory /accounted4/accounted4-money/src/test/java/com/accounted4/money/loan/.

Source file: AmortizationCalculatorTest.java

  29 
vote

/** 
 * Test of getAmortizedMonthlyPayment method, of class AmortizationCalculator.
 */
@Test public void testGetAmortizedMonthlyPayment(){
  System.out.println("getAmortizedMonthlyPayment");
  Money loanAmount=new Money("100000.00",Currency.getInstance("CAD"),RoundingMode.CEILING);
  double i=12.0;
  int compoundPeriod=2;
  int amortizationPeriod=25 * 12;
  Money expResult=new Money("1031.90");
  double amortizedMonthlyPayment=AmortizationCalculator.getAmortizedMonthlyPayment(loanAmount,i,compoundPeriod,amortizationPeriod);
  Money result=new Money(BigDecimal.valueOf(amortizedMonthlyPayment),loanAmount.getCurrency(),loanAmount.getRoundingMode());
  assertEquals("Semi-annual compounding period",expResult,result);
  compoundPeriod=12;
  loanAmount=new Money("100000.00",Currency.getInstance("CAD"),RoundingMode.HALF_UP);
  expResult=new Money("1053.22");
  amortizedMonthlyPayment=AmortizationCalculator.getAmortizedMonthlyPayment(loanAmount,i,compoundPeriod,amortizationPeriod);
  result=new Money(BigDecimal.valueOf(amortizedMonthlyPayment),loanAmount.getCurrency(),loanAmount.getRoundingMode());
  assertEquals("Monthly compound period",expResult,result);
  compoundPeriod=12;
  loanAmount=new Money("100000.00",Currency.getInstance("CAD"),RoundingMode.CEILING);
  expResult=new Money("1053.23");
  amortizedMonthlyPayment=AmortizationCalculator.getAmortizedMonthlyPayment(loanAmount,i,compoundPeriod,amortizationPeriod);
  result=new Money(BigDecimal.valueOf(amortizedMonthlyPayment),loanAmount.getCurrency(),loanAmount.getRoundingMode());
  assertEquals("Monthly compound period, ceiling",expResult,result);
}
 

Example 18

From project ACLS-protocol-library, under directory /aclslib/src/test/java/au/edu/uq/cmm/aclslib/message/.

Source file: RequestReaderTest.java

  29 
vote

@Test public void testReadLogin() throws AclsException {
  RequestReader r=reader();
  Request req=r.read(source("1:steve|secret|"));
  assertEquals(RequestType.LOGIN,req.getType());
  LoginRequest login=(LoginRequest)req;
  assertEquals("steve",login.getUserName());
  assertEquals("secret",login.getPassword());
  assertEquals("here",req.getFacility().getFacilityName());
  assertNull(login.getLocalHostId());
}
 

Example 19

From project ACLS-protocol-library, under directory /aclslib/src/test/java/au/edu/uq/cmm/aclslib/message/.

Source file: RequestReaderTest.java

  29 
vote

@Test public void testReadLoginHostId() throws AclsException {
  RequestReader r=reader();
  Request req=r.read(source("1:steve|secret|:ID|"));
  assertEquals(RequestType.LOGIN,req.getType());
  LoginRequest login=(LoginRequest)req;
  assertEquals("steve",login.getUserName());
  assertEquals("secret",login.getPassword());
  assertEquals("there",login.getFacility().getFacilityName());
  assertEquals("ID",login.getLocalHostId());
}
 

Example 20

From project ACLS-protocol-library, under directory /aclslib/src/test/java/au/edu/uq/cmm/aclslib/message/.

Source file: RequestReaderTest.java

  29 
vote

@Test public void testReadStaffLogin() throws AclsException {
  RequestReader r=reader();
  Request req=r.read(source("21:steve|secret|"));
  assertEquals(RequestType.STAFF_LOGIN,req.getType());
  LoginRequest login=(LoginRequest)req;
  assertEquals("steve",login.getUserName());
  assertEquals("secret",login.getPassword());
  assertEquals("here",req.getFacility().getFacilityName());
  assertNull(login.getLocalHostId());
}
 

Example 21

From project ACLS-protocol-library, under directory /aclslib/src/test/java/au/edu/uq/cmm/aclslib/message/.

Source file: RequestReaderTest.java

  29 
vote

@Test public void testReadStaffLoginHostId() throws AclsException {
  RequestReader r=reader();
  Request req=r.read(source("21:steve|secret|:ID|"));
  assertEquals(RequestType.STAFF_LOGIN,req.getType());
  LoginRequest login=(LoginRequest)req;
  assertEquals("steve",login.getUserName());
  assertEquals("secret",login.getPassword());
  assertEquals("there",login.getFacility().getFacilityName());
  assertEquals("ID",login.getLocalHostId());
}
 

Example 22

From project activejdbc, under directory /activejdbc/src/test/java/org/javalite/activejdbc/.

Source file: AssociationEqualityTest.java

  29 
vote

@Test public void testOneToManyEquals(){
  OneToManyAssociation one=new OneToManyAssociation("hello","world","hello_id");
  OneToManyAssociation two=new OneToManyAssociation("hello","world","hello_id");
  a(one).shouldBeEqual(two);
  OneToManyAssociation three=new OneToManyAssociation("hello","world","hi_id");
  a(one).shouldNotBeEqual(three);
}
 

Example 23

From project activejdbc, under directory /activejdbc/src/test/java/org/javalite/activejdbc/.

Source file: AssociationEqualityTest.java

  29 
vote

@Test public void testMany2ManyEquals(){
  Many2ManyAssociation one=new Many2ManyAssociation("hello","world","join","hello_id","world_id");
  Many2ManyAssociation two=new Many2ManyAssociation("hello","world","join","hello_id","world_id");
  a(one).shouldBeEqual(two);
  Many2ManyAssociation three=new Many2ManyAssociation("hello","world","join","hi_id","world_id");
  a(one).shouldNotBeEqual(three);
}
 

Example 24

From project activejdbc, under directory /activejdbc/src/test/java/org/javalite/activejdbc/.

Source file: AttributeConverterTest.java

  29 
vote

@Test public void testDateConverter(){
  deleteAndPopulateTable("people");
  Person p=new Person();
  p.set("name","Marilyn");
  p.set("last_name","Monroe");
  p.set("dob","1935/6/12");
  p.validate();
  a(p.errors().size()).shouldBeEqual(1);
  p.set("dob","1935-12-06");
  p.validate();
  a(p.errors().size()).shouldBeEqual(0);
}