Example:
Feature: Convert between different currencies Scenario: Convert SGD to CNY Given currency:"SGD", amount:10 Given fxRate is 4.60 When convert to "CNY" Then the result is 46 in CNY
JUnit Test: it can be generated by cucumber plugins, but not perfect for my IDE. (cannot generate all steps, but you still can copy the generated JUnit code from the output)
package com.liguoliang.bdd; import cucumber.annotation.Before; import cucumber.annotation.en.Given; import cucumber.annotation.en.Then; import cucumber.annotation.en.When; import org.junit.Assert; import java.math.BigDecimal; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** * Created by guoliang on 3/14/15. */ public class FxConverterTest { private FxConverter fxConverter; @Before public void setUp() { fxConverter = new FxConverter(); } @Given("^currency:\"([^\"]*)\", amount:(\\d+)$") public void currency_amount(String currency, BigDecimal amount) throws Throwable { fxConverter.setCurrencySource(currency); fxConverter.setAmountInput(amount); } @Given("^fxRate is (.+)$") public void fxRate_is_(BigDecimal rate) throws Throwable { fxConverter.setFxRate(rate); } @When("^convert to \"([^\"]*)\"$") public void convert_to(String currency) throws Throwable { fxConverter.convertTo(currency); } @Then("^the result is (.+) in CNY$") public void the_result_is_in_CNY(BigDecimal resultExpected) throws Throwable { assertTrue(resultExpected.compareTo(fxConverter.getAmountResult()) == 0); } }
Two’s complement is the way every computer I know of chooses to represent integers. To get the two’s complement negative notation of an integer, you write out the number in binary. You then invert the digits, and add one to the result.
Integer input = 50; String binString = Integer.toBinaryString(input); BitSet bitSet = BitSet.valueOf(new long[] {input}); for (int i = 0; i < binString.length(); i++) { bitSet.flip(i); } System.out.println(Arrays.toString(bitSet.toByteArray()));
There’s a quick tip from Google developers about switch, the suggestion is getting rid of throwing exception in switch default section if the default handler shouldn’t be reached.
Here’s an example in Java:
package com.liguoliang.java.core.lang.enum_; /** * Created by Guoliang, Li(liguoiang.com) on 3/8/2015. */ public class SwitchDefaultDemo { public static void main(String[] args) { System.out.println(getBookCategoryDesc(BookCategory.JAVA)); ; } private static String getBookCategoryDesc(BookCategory bookCategory) { String desc = null; switch (bookCategory) { case JAVA: desc = "It's a Java book"; break; case PYTHON: desc = "It's a PHP book"; break; default: throw new RuntimeException("Unknown book category"); } return desc; } public static enum BookCategory { JAVA, PYTHON, PHP; } }
The tip’s point is if developer added a new enum and forgot to update the switch statement accordingly, there’ll be a issue, it may fail the test or crash the application.
so to improve the switch statement, the video suggest to remove the default section. if some client calls getBookCategoryDesc(PHP), the method will be executed normally, instead of throwing an exception.
it seems debatable for me, I prefer to keep the exception, throwing an exception to exposure the risk instead of hiding it, because the exception tells the details, developers can fix the bug immediately. otherwise, developers will realize the issue and start to debug until user reporting it.
This is a Protocol Buffers hello world project following https://developers.google.com/protocol-buffers/docs/javatutorial
you may find all source code from github: https://github.com/guoliang-dev/google-protocol-buffers-java-helloworld
Code Generation
The message definition: addressbook.proto;
D:\dev\tools\protoc-2.5.0-win32>protoc.exe -I=D:\dev\..\src\main\resosurce\ --java_out=D:\dev..\src\main\java D:\dev\…\resosurce\addressbook.proto
AddressBookProtos.java is the generated Java class;
TestCase:
package com.liguoliang.java.protobuf;
public class ProtocolBuffersTest {
private static final String name = "google";@Test
public void testBuildInstance() throws Exception {
Person person = buildNewPerson();
assertEquals(name, person.getName());
}@Test
public void testWriteInstanceToFile() throws Exception {
String path = "./src/test/resources/address.txt";
AddressBook.Builder addressBookBuilder = AddressBook.newBuilder();
addressBookBuilder.addPerson(buildNewPerson());
AddressBook addressBook = addressBookBuilder.build();
FileOutputStream outputStream = new FileOutputStream(path);
addressBook.writeTo(outputStream);
outputStream.close();
System.out.println("AddressBook write to :\n " + path);
URL url = Resources.getResource("./address.txt");
String contentInFile = Resources.toString(url, Charsets.UTF_8);
assertTrue(contentInFile.contains(name));
}
@Test
public void testParseInstanceFromFile() throws FileNotFoundException, IOException {
AddressBook.Builder addressBookBuilderRead = AddressBook.newBuilder();
addressBookBuilderRead.mergeFrom(new FileInputStream("./src/test/resources/address-test.txt"));
AddressBook addressBookRead = addressBookBuilderRead.build();
System.out.println("AddressBook loaded: \n" + addressBookRead.toString());
assertEquals(1, addressBookRead.getPersonCount());
Person person = addressBookRead.getPerson(0);
assertEquals(name, person.getName());
}
private Person buildNewPerson() {
Person.Builder personBuilder = Person.newBuilder();
personBuilder.setId(1);
personBuilder.setName(name);
Person person = personBuilder.build();
return person;
}}
Set up Maven dependences
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.10.2.0</version>
</dependency>
Connect to the DB using JDBC
This method will print out all Tables; // before you do anything with the Database, there should be some System tables already.
package com.liguoliang; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCTest { private static final String DB_URL = "jdbc:derby:tempDBForTest;create=true"; public static void main(String[] args) { Connection conn = null ; try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ; conn = DriverManager.getConnection(DB_URL); String sql = "SELECT TABLENAME FROM SYS.SYSTABLES"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("Rs: " + rs.getString(1)); } } catch (SQLException se) { se.printStackTrace(); } catch(ClassNotFoundException e){ System.out.println("JDBC Driver not found in CLASSPATH") ; }finally { if(conn != null){ try{ conn.close() ; } catch(SQLException se){ se.printStackTrace(); } } } } }
Use Eclipse to manage your Derby
Switch to ‘Database Development’ perspective, you may create/manage Derby DB/table.
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.