Skip to content

Commit 8d6c5ec

Browse files
authored
Dev guide code style (#384)
Replacement for #271 (merged master into the branch).
1 parent 55d6d76 commit 8d6c5ec

8 files changed

+113
-112
lines changed

src/main/markdown/articles/testing_methodologies_using_gwt.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public class MeetingSummaryLabelTest extends GWTTestCase {
4343

4444
// Add tests here
4545
}
46-
4746
```
4847

4948
The only visible difference is that all GWTTestCases must override an abstract method called `getModuleName`, which returns a String containing the name of your GWT code module as defined in your application's module XML file.
@@ -111,7 +110,7 @@ public class PresenterTest extends TestCase {
111110
verify(scheduler);
112111
verify(view);
113112

114-
assertEquals("Should have updated the model's capacity", 225, meeting.getCapacity());
113+
assertEquals("Should have updated the model's capacity", 225, meeting.getCapacity());
115114
}
116115
}
117116

@@ -150,10 +149,10 @@ public class Presenter {
150149
}
151150

152151
/**
153-
* Callback when the view's capacity text box changes
154-
*
155-
* @param textField the capacity TextBox widget
156-
*/
152+
* Callback when the view's capacity text box changes
153+
*
154+
* @param textField the capacity TextBox widget
155+
*/
157156
public void requiredCapacityChanged(HasText textField) {
158157
meeting.setCapacity(Integer.parseInt(textField.getText()));
159158
if (!roomScheduler.canAcceptCapacityFor(meeting)) {
@@ -180,18 +179,17 @@ package com.google.gwt.user.client.ui;
180179
public interface HasText {
181180

182181
/**
183-
* Gets this object's text.
184-
*/
182+
* Gets this object's text.
183+
*/
185184
String getText();
186185

187186
/**
188-
* Sets this object's text.
189-
*
190-
* @param text the object's new text
191-
*/
187+
* Sets this object's text.
188+
*
189+
* @param text the object's new text
190+
*/
192191
void setText(String text);
193192
}
194-
195193
```
196194

197195
This simple interface is used by many GWT components and allows manipulation of a widget's text contents, including the TextBox in our example. This interface is extremely useful for testing because we don't need to pass in a real TextBox. Thus we avoid instantiating a text input in the DOM, requiring our test to extend GWTTestCase to run in a real browser. In this example, I've made a very simple fake implementation which wraps a String:
@@ -212,7 +210,6 @@ public class FakeTextContainer implements HasText {
212210
this.text = text;
213211
}
214212
}
215-
216213
```
217214

218215
Finally, let's take a look at our view implementation:
@@ -250,7 +247,6 @@ public class MeetingViewWidget extends Composite implements MeetingView {
250247
saveButton.setEnabled(false);
251248
}
252249
}
253-
254250
```
255251

256252
And lastly, the Meeting class code, for completeness:
@@ -267,7 +263,6 @@ public class Meeting {
267263
this.capacity = capacity;
268264
}
269265
}
270-
271266
```
272267

273268
As you can see, there's not much logic here. Most of the code is involved in setting up the event listeners and configuring the display widgets. So how do we test it in a GWTTestCase?

src/main/markdown/doc/latest/DevGuideCodingBasicsDelayed.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ To create a timer, create a new instance of the Timer class and then override th
2525

2626
```java
2727
Timer timer = new Timer() {
28-
public void run() {
29-
Window.alert ("Timer expired!");
30-
}
31-
};
28+
public void run() {
29+
Window.alert("Timer expired!");
30+
}
31+
};
3232

33-
// Execute the timer to expire 2 seconds in the future
34-
timer.schedule(2000);
33+
// Execute the timer to expire 2 seconds in the future
34+
timer.schedule(2000);
3535
```
3636

3737
Notice that the timer will not have a chance to execute the run() method until after control returns to the JavaScript event loop.
@@ -169,14 +169,14 @@ it to [Scheduler.scheduleDeferred](/javadoc/latest/com/google/gwt/core/client/Sc
169169
```java
170170
TextBox dataEntry;
171171

172-
// Set the focus on the widget after setup completes.
173-
Scheduler.get().scheduleDeferred(new Command() {
174-
public void execute () {
175-
dataEntry.setFocus();
176-
}
177-
});
172+
// Set the focus on the widget after setup completes.
173+
Scheduler.get().scheduleDeferred(new Command() {
174+
public void execute () {
175+
dataEntry.setFocus();
176+
}
177+
});
178178

179-
dataEntry = new TextBox();
179+
dataEntry = new TextBox();
180180
```
181181

182182
## Avoiding Slow Script Warnings: the IncrementalCommand class<a id="incremental"></a>

src/main/markdown/doc/latest/DevGuideCodingBasicsFormatting.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,36 @@ For most cases, you probably want to use the default decimal format:
2727

2828
```java
2929
NumberFormat fmt = NumberFormat.getDecimalFormat();
30-
double value = 12345.6789;
31-
String formatted = fmt.format(value);
32-
// Prints 1,2345.6789 in the default locale
33-
GWT.log("Formatted string is" + formatted, null);
30+
double value = 12345.6789;
31+
String formatted = fmt.format(value);
32+
// Prints 1,2345.6789 in the default locale
33+
GWT.log("Formatted string is" + formatted, null);
3434
```
3535

3636
The class can also be used to convert a numeric string back into a double:
3737

3838
```java
3939
double value = NumberFormat.getDecimalFormat().parse("12345.6789");
40-
GWT.log("Parsed value is" + value, null);
40+
GWT.log("Parsed value is" + value, null);
4141
```
4242

4343
The `NumberFormat` class also provides defaults for scientific notation:
4444

4545
```java
4646
double value = 12345.6789;
47-
String formatted = NumberFormat.getScientificFormat().format(value);
48-
// prints 1.2345E4 in the default locale
49-
GWT.log("Formatted string is" + formatted, null);
47+
String formatted = NumberFormat.getScientificFormat().format(value);
48+
// prints 1.2345E4 in the default locale
49+
GWT.log("Formatted string is" + formatted, null);
5050
```
5151

5252
Note that you can also specify your own pattern for formatting numbers. In the example below, we want to show 6 digits of precision on the right hand side of the decimal and
5353
format the left hand side with zeroes up to the hundred thousands place:
5454

5555
```java
5656
double value = 12345.6789;
57-
String formatted = NumberFormat.getFormat("000000.000000").format(value);
58-
// prints 012345.678900 in the default locale
59-
GWT.log("Formatted string is" + formatted, null);
57+
String formatted = NumberFormat.getFormat("000000.000000").format(value);
58+
// prints 012345.678900 in the default locale
59+
GWT.log("Formatted string is" + formatted, null);
6060
```
6161

6262
Here are the most commonly used pattern symbols for decimal formats:
@@ -86,23 +86,23 @@ For the `DateTimeFormat` class, there are a large number of default formats defi
8686
```java
8787
Date today = new Date();
8888

89-
// prints Tue Dec 18 12:01:26 GMT-500 2007 in the default locale.
90-
GWT.log(today.toString(), null);
89+
// prints Tue Dec 18 12:01:26 GMT-500 2007 in the default locale.
90+
GWT.log(today.toString(), null);
9191

92-
// prints 12/18/07 in the default locale
93-
GWT.log(DateTimeFormat.getShortDateFormat().format(today), null);
92+
// prints 12/18/07 in the default locale
93+
GWT.log(DateTimeFormat.getShortDateFormat().format(today), null);
9494

95-
// prints December 18, 2007 in the default locale
96-
GWT.log(DateTimeFormat.getLongDateFormat().format(today), null);
95+
// prints December 18, 2007 in the default locale
96+
GWT.log(DateTimeFormat.getLongDateFormat().format(today), null);
9797

98-
// prints 12:01 PM in the default locale
99-
GWT.log(DateTimeFormat.getShortTimeFormat().format(today), null);
98+
// prints 12:01 PM in the default locale
99+
GWT.log(DateTimeFormat.getShortTimeFormat().format(today), null);
100100

101-
// prints 12:01:26 PM GMT-05:00 in the default locale
102-
GWT.log(DateTimeFormat.getLongTimeFormat().format(today), null);
101+
// prints 12:01:26 PM GMT-05:00 in the default locale
102+
GWT.log(DateTimeFormat.getLongTimeFormat().format(today), null);
103103

104-
// prints Dec 18, 2007 12:01:26 PM in the default locale
105-
GWT.log(DateTimeFormat.getMediumDateTimeFormat().format(today), null);
104+
// prints Dec 18, 2007 12:01:26 PM in the default locale
105+
GWT.log(DateTimeFormat.getMediumDateTimeFormat().format(today), null);
106106
```
107107

108108
Like the `NumberFormat` class, you can also use this class to parse a date from a `String` into a `Date` representation. You also have the option of using

src/main/markdown/doc/latest/DevGuideCodingBasicsJSNI.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public static native int badExample() /*-{
6565
return "Not A Number";
6666
}-*/;
6767

68-
public void onClick () {
69-
try {
70-
int myValue = badExample();
71-
GWT.log("Got value " + myValue, null);
72-
} catch (Exception e) {
73-
GWT.log("JSNI method badExample() threw an exception:", e);
74-
}
75-
}
68+
public void onClick() {
69+
try {
70+
int myValue = badExample();
71+
GWT.log("Got value " + myValue, null);
72+
} catch (Exception e) {
73+
GWT.log("JSNI method badExample() threw an exception:", e);
74+
}
75+
}
7676
```
7777

7878
This example compiles as Java, and its syntax is verified by the GWT compiler
@@ -208,7 +208,7 @@ public class JSNIExample {
208208
209209
## Calling a Java Method from Handwritten JavaScript<a id="calling"></a>
210210

211-
Sometimes you need to access a method or constructor defined in GWT from outside JavaScript code. This code might be hand-written and included in another java script file, or
211+
Sometimes you need to access a method or constructor defined in GWT from outside JavaScript code. This code might be hand-written and included in another JavaScript file, or
212212
it could be a part of a third party library. In this case, the GWT compiler will not get a chance to build an interface between your JavaScript code and the GWT generated
213213
JavaScript directly.
214214

@@ -218,14 +218,13 @@ JavaScript code.
218218
```java
219219
package mypackage;
220220

221-
public MyUtilityClass
222-
{
223-
public static int computeLoanInterest(int amt, float interestRate,
224-
int term) { ... }
225-
public static native void exportStaticMethod() /*-{
226-
$wnd.computeLoanInterest =
227-
$entry(@mypackage.MyUtilityClass::computeLoanInterest(IFI));
228-
}-*/;
221+
public class MyUtilityClass {
222+
public static int computeLoanInterest(int amt, float interestRate,
223+
int term) { ... }
224+
public static native void exportStaticMethod() /*-{
225+
$wnd.computeLoanInterest =
226+
$entry(@mypackage.MyUtilityClass::computeLoanInterest(IFI));
227+
}-*/;
229228
}
230229
```
231230

src/main/markdown/doc/latest/DevGuideCodingBasicsJsInterop.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,25 @@ package com.gwt.example;
2424
@JsType
2525
public class MyClass {
2626

27-
public String name;
27+
public String name;
2828

29-
public MyClass(String name) {
30-
this.name = name;
31-
}
29+
public MyClass(String name) {
30+
this.name = name;
31+
}
3232

33-
public void sayHello() {
34-
return "Hello" + this.name;
35-
}
33+
public String sayHello() {
34+
return "Hello " + this.name;
35+
}
3636
}
3737
```
3838

3939
From the JS script, the object can be used as a JS object:
4040

4141
```javascript
42-
//the package name serves as a JS namespace
42+
// Note that exporting of Java Objects to JavaScript to be accessed by their
43+
// namespace (e.g. this sample) requires -generateJsInteropExports flag.
44+
45+
// the package name serves as a JS namespace
4346
var aClass = new com.gwt.example.MyClass('World');
4447

4548
console.log(aClass.sayHello());

src/main/markdown/doc/latest/DevGuideI18nMessages.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ The following code implements an alert dialog by substituting values into the me
2828

2929
```java
3030
public interface ErrorMessages extends Messages {
31-
String permissionDenied(int errorCode, String username);
32-
}
33-
ErrorMessages msgs = GWT.create(ErrorMessages.class)
31+
String permissionDenied(int errorCode, String username);
32+
}
33+
ErrorMessages msgs = GWT.create(ErrorMessages.class)
3434

35-
void permissionDenied(int errorVal, String loginId) {
36-
Window.alert(msgs.permissionDenied(errorVal, loginId));
37-
}
35+
void permissionDenied(int errorVal, String loginId) {
36+
Window.alert(msgs.permissionDenied(errorVal, loginId));
37+
}
3838
```
3939

4040
**Caution:** Be advised that the rules for using quotes may be a bit confusing. Refer to the [MessageFormat](http://java.sun.com/j2se/1.5.0/docs/api/java/text/MessageFormat.html) javadoc for more details.

src/main/markdown/doc/latest/DevGuideTesting.md

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ If you prefer not to use webAppCreator, you may create a test case suite by hand
106106
2. **If you do not have a GWT module yet, create a [module](DevGuideOrganizingProjects.html#DevGuideModules) that causes the source for your test case to be included.** If you are adding a test case to an existing GWT app, you can just use the existing module.
107107
3. **Implement the method [GWTTestCase.getModuleName()](/javadoc/latest/com/google/gwt/junit/client/GWTTestCase.html#getModuleName--) to return the fully-qualified name of the module.** This is the glue that tells the JUnit test case which module to instantiate.
108108
4. **Compile your test case class to bytecode.** You can use the Java compiler directly using [javac](http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html) or a Java IDE such as [Eclipse](http://eclipse.org).
109-
5. **Run your test case.** Use the class `junit.textui.TestRunner` as your main class and pass the full name of your test class as the command line argument, e.g. `com.example.foo.client.FooTest`. When running the test case, make sure your classpath includes:
110-
6. * Your project's `src` directory
109+
5. **Run your test case.** Use the class `junit.textui.TestRunner` as your main
110+
class and pass the full name of your test class as the command line argument,
111+
e.g. `com.example.foo.client.FooTest`. When running the test case, make sure your classpath includes:
112+
* Your project's `src` directory
111113
* Your project's `bin` directory
112114
* The `gwt-user.jar` library
113115
* The `gwt-dev.jar` library
@@ -211,9 +213,11 @@ there are [some differences between Java and JavaScript](DevGuideCodingBasics.ht
211213
that could cause your code to produce different results when deployed.
212214

213215
If you instead decide to run the JUnit TestRunner from the command line,
214-
you need to [pass arguments](#passingTestArguments) to `JUnitShell` to get your unit tests running in (legacy) development mode
216+
you need to [pass arguments](#passingTestArguments) to `JUnitShell` to get your unit tests running in (legacy) development mode:
215217

216-
`-Dgwt.args="-devMode"`
218+
```
219+
-Dgwt.args="-devMode"
220+
```
217221

218222
### Running your test in Manual Mode<a id="Manual_Mode"></a>
219223

@@ -353,32 +357,32 @@ The following example shows how to defensively cleanup the DOM before the next t
353357

354358
```java
355359
import com.google.gwt.junit.client.GWTTestCase;
356-
import com.google.gwt.user.client.DOM;
357-
import com.google.gwt.user.client.Element;
358-
359-
private static native String getNodeName(Element elem) /*-{
360-
return (elem.nodeName || "").toLowerCase();
361-
}-*/;
362-
363-
/**
364-
* Removes all elements in the body, except scripts and iframes.
365-
*/
366-
public void gwtSetUp () {
367-
Element bodyElem = RootPanel.getBodyElement();
368-
369-
List<Element> toRemove = new ArrayList<Element>();
370-
for (int i = 0, n = DOM.getChildCount(bodyElem); i < n; ++i) {
371-
Element elem = DOM.getChild(bodyElem, i);
372-
String nodeName = getNodeName(elem);
373-
if (!"script".equals(nodeName) &amp;&amp; !"iframe".equals(nodeName)) {
374-
toRemove.add(elem);
375-
}
360+
import com.google.gwt.user.client.DOM;
361+
import com.google.gwt.user.client.Element;
362+
363+
private static native String getNodeName(Element elem) /*-{
364+
return (elem.nodeName || "").toLowerCase();
365+
}-*/;
366+
367+
/**
368+
* Removes all elements in the body, except scripts and iframes.
369+
*/
370+
public void gwtSetUp () {
371+
Element bodyElem = RootPanel.getBodyElement();
372+
373+
List<Element> toRemove = new ArrayList<Element>();
374+
for (int i = 0, n = DOM.getChildCount(bodyElem); i < n; ++i) {
375+
Element elem = DOM.getChild(bodyElem, i);
376+
String nodeName = getNodeName(elem);
377+
if (!"script".equals(nodeName) && !"iframe".equals(nodeName)) {
378+
toRemove.add(elem);
376379
}
380+
}
377381

378-
for (int i = 0, n = toRemove.size(); i < n; ++i) {
379-
DOM.removeChild(bodyElem, toRemove.get(i));
380-
}
382+
for (int i = 0, n = toRemove.size(); i < n; ++i) {
383+
DOM.removeChild(bodyElem, toRemove.get(i));
381384
}
385+
}
382386
```
383387

384388
## Running Tests in Eclipse<a id="DevGuideRunningTestsInEclipse"></a>

0 commit comments

Comments
 (0)