Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 23061 Details for
Bug 99978
MalformedTreeException on Inline Method
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
The file that generates the error
SayTests.java (text/x-java), 6.32 KB, created by
David Saff
on 2005-06-14 11:17:06 EDT
(
hide
)
Description:
The file that generates the error
Filename:
MIME Type:
Creator:
David Saff
Created:
2005-06-14 11:17:06 EDT
Size:
6.32 KB
patch
obsolete
>import java.lang.reflect.InvocationTargetException; >import java.lang.reflect.Method; >import java.util.ArrayList; >import java.util.Arrays; >import java.util.List; > >import junit.framework.JUnit4TestAdapter; > >import org.junit.Test; > >public class SayTests extends org.junit.Assert { > public class ArithmeticContext { > > private final int _answer; > > public ArithmeticContext(int answer) { > _answer = answer; > } > > @Override > public boolean equals(Object obj) { > return _answer == ((ArithmeticContext) obj)._answer; > } > > public int getAnswer() { > return _answer; > } > > } > > public class IntegerToken extends Token { > private final int _value; > > public IntegerToken(int value) { > _value = value; > } > > protected void addArg(ArrayList<Integer> returnThis) { > returnThis.add(_value); > } > > protected boolean nextTokenShouldCapitalize() { > return false; > } > > protected String segmentForMethodName(Token lastToken) { > return "___"; > } > > } > > public class SayTestLine { > // DUP: stupid class > private final String _line; > > public SayTestLine(String line) { > _line = line; > } > > public String getString() { > return _line; > } > } > > public class StringToken extends Token { > private final String _string; > > public StringToken(String string) { > _string = string; > } > > private String initialCaps(String string) { > return string.substring(0, 1).toUpperCase() + string.substring(1); > } > > protected void addArg(ArrayList<Integer> returnThis) { > // do nothing > } > > protected boolean nextTokenShouldCapitalize() { > return true; > } > > protected String segmentForMethodName(Token lastToken) { > String lowerCase = _string.toLowerCase(); > if (lastToken.nextTokenShouldCapitalize()) > return initialCaps(lowerCase); > return lowerCase; > } > > } > > public static abstract class Token { > public static final Token BEGINNING = new Token() { > @Override > protected void addArg(ArrayList<Integer> returnThis) { > // do nothing > } > > @Override > protected boolean nextTokenShouldCapitalize() { > return false; > } > > @Override > protected String segmentForMethodName(Token lastToken) { > return null; > } > }; > > protected abstract void addArg(ArrayList<Integer> returnThis); > > protected abstract boolean nextTokenShouldCapitalize(); > > protected abstract String segmentForMethodName(Token lastToken); > } > > public static junit.framework.Test suite() { > return new JUnit4TestAdapter(SayTests.class); > } > > public ArithmeticContext add___and___(int a, int b) { > return new ArithmeticContext(a + b); > } > > @Test > public void addAdds() { > assertEquals(5, add___and___(2, 3).getAnswer()); > } > > @Test > public void camelCaseMethodName() throws Exception { > assertEquals("checkThatTheAnswerIs___", > methodNameFor("Check that the answer is 5")); > } > > public boolean checkThatTheAnswerIs___(ArithmeticContext context, > int expected) { > return expected == context.getAnswer(); > // WANTED: unmix the methods > } > > @Test > public void checkTrue() { > assertTrue(linesPass("Add 2 and 3", "Check that the answer is 5")); > } > > @Test > public void evaluateLineCheckAnswer() throws Exception { > // WANTED: better diagnostic on "wrong number of arguments" > assertTrue((Boolean) evaluateLine("Check that the answer is 5", > new ArithmeticContext(5))); > } > > @Test > public void evaluateLineFindsNothing() throws Exception { > assertNull(evaluateLine("Jump in the air", null)); > } > > @Test > public void evaluateLineWorks() throws Exception { > assertEquals(new ArithmeticContext(5), > evaluateLine("Add 2 and 3", null)); > } > > @Test > public void gatherArgs() { > List<Integer> gatherArgs = gatherArgs(new SayTestLine("Add 2 and 3")); > assertEquals(Arrays.asList(2, 3), gatherArgs); > } > > @Test > public void getMethodNameTwoArgs() { > // DUP: constructor > assertEquals("add___and___", methodNameFor("Add 2 and 3")); > } > > @Test > public void theAnswerIs() { > assertTrue(checkThatTheAnswerIs___(new ArithmeticContext(5), 5)); > } > > @Test > public void theAnswerIsnt() { > assertFalse(checkThatTheAnswerIs___(new ArithmeticContext(6), 5)); > } > > private Object[] argsArray(ArithmeticContext context, Method method, > SayTestLine sline) { > List<Integer> args = gatherArgs(sline); > ArrayList<Object> objArgs = new ArrayList<Object>(args); > if (method.getParameterTypes()[0].equals(ArithmeticContext.class)) { > objArgs.add(0, context); > } > Object[] argsArray = objArgs.toArray(); > return argsArray; > } > > private Object evaluateLine(String line, ArithmeticContext context) > throws IllegalArgumentException, IllegalAccessException, > InvocationTargetException { > // DUP: constructor > SayTestLine sline = new SayTestLine(line); > String methodName = methodNameFor(sline); > Method[] methods = getClass().getMethods(); > for (Method method : methods) { > if (method.getName().equals(methodName)) { > return method.invoke(this, argsArray(context, method, > sline)); > } > } > return null; > } > > private List<Integer> gatherArgs(SayTestLine sline) { > // REF: push SayTestLine as far as possible > ArrayList<Integer> returnThis = new ArrayList<Integer>(); > for (Token token : tokenize(sline)) { > token.addArg(returnThis); > } > return returnThis; > } > > private boolean linesPass(String... lines) { > // REF: file too big > // TODO Auto-generated method stub > return true; > } > > private String methodNameFor(SayTestLine sline) { > String returnThis = ""; > Token lastToken = Token.BEGINNING; > for (Token token : tokenize(sline.getString())) { > returnThis += token.segmentForMethodName(lastToken); > lastToken = token; > } > return returnThis; > } > > private String methodNameFor(String line) { > return methodNameFor(new SayTestLine(line)); > } > > private Token tokenForString(String string) { > try { > return new IntegerToken(Integer.parseInt(string)); > } catch (NumberFormatException e) { > return new StringToken(string); > } > } > > private List<Token> tokenize(SayTestLine sline) { > return tokenize(sline.getString()); > } > > private List<Token> tokenize(String line) { > ArrayList<Token> returnThis = new ArrayList<Token>(); > String[] strings = line.split(" "); > for (String string : strings) { > returnThis.add(tokenForString(string)); > } > return returnThis; > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 99978
: 23061 |
23211
|
23212