1 17 package org.apache.ws.jaxme.js.junit; 18 19 import org.apache.ws.jaxme.js.JavaConstructor; 20 import org.apache.ws.jaxme.js.JavaField; 21 import org.apache.ws.jaxme.js.JavaMethod; 22 import org.apache.ws.jaxme.js.JavaQName; 23 import org.apache.ws.jaxme.js.JavaQNameImpl; 24 import org.apache.ws.jaxme.js.JavaSource; 25 import org.apache.ws.jaxme.js.JavaSourceFactory; 26 import org.apache.ws.jaxme.js.LocalJavaField; 27 import org.apache.ws.jaxme.js.PlaceHolder; 28 import junit.framework.Assert; 29 import junit.framework.TestCase; 30 31 35 public class BasicTest extends TestCase { 36 private static final String PACKAGE_NAME = "foo"; 37 private static final String CLASS_NAME = "Bar"; 38 39 public BasicTest(String pName) { 40 super(pName); 41 } 42 43 public JavaSource getSource(JavaSource.Protection pProtection) { 44 JavaSourceFactory factory = new JavaSourceFactory(); 45 JavaSource js; 46 JavaConstructor jcon; 47 JavaMethod jm; 48 JavaField jf; 49 LocalJavaField lfj; 50 if (pProtection == null) { 51 js = factory.newJavaSource(JavaQNameImpl.getInstance(PACKAGE_NAME, CLASS_NAME)); 52 js.newJavaInnerClass("Bof"); 53 jf = js.newJavaField("someField", int.class); 54 jcon = js.newJavaConstructor(); 55 jm = js.newJavaMethod("test", void.class); 56 lfj = jm.newJavaField(String .class, "localTest"); 57 } else { 58 js = factory.newJavaSource(JavaQNameImpl.getInstance(PACKAGE_NAME, CLASS_NAME), pProtection); 59 js.newJavaInnerClass("Bof", pProtection); 60 jf = js.newJavaField("someField", int.class, pProtection); 61 jcon = js.newJavaConstructor(pProtection); 62 jm = js.newJavaMethod("test", void.class, pProtection); 63 lfj = jm.newJavaField(String .class, "localTest"); 64 } 65 lfj.setFinal(true); 66 lfj.addLine(JavaSource.getQuoted("abc")); 67 68 jcon.addLine(jf, " = 0;"); 69 jm.addThrowNew(NullPointerException .class, JavaSource.getQuoted("Not implemented")); 70 return js; 71 } 72 73 public String getResult(JavaSource.Protection pProtection) { 74 String p = (pProtection == null || pProtection.equals(JavaSource.DEFAULT_PROTECTION)) ? 75 "" : (pProtection.toString() + " "); 76 return "package foo;\n" + 77 "\n" + 78 p + "class Bar {\n" + 79 " " + p + "class Bof {\n" + 80 " }\n" + 81 "\n" + 82 " " + p + "int someField;\n" + 83 "\n" + 84 "\n" + 85 " " + p + "Bar() {\n" + 86 " someField = 0;\n" + 87 " }\n" + 88 "\n" + 89 " " + p + "void test() {\n" + 90 " final String localTest = \"abc\";\n" + 91 " throw new NullPointerException(\"Not implemented\");\n" + 92 " }\n" + 93 "\n" + 94 "}\n"; 95 } 96 97 private String dump(String s, int offset) { 98 String dump = s.substring(offset); 99 if (dump.length() > 32) { 100 dump = dump.substring(0, 32); 101 } 102 return dump; 103 } 104 105 private String hexDump(String s, int offset) { 106 String dump = s.substring(offset); 107 if (dump.length() > 16) { 108 dump = dump.substring(0, 16); 109 } 110 StringBuffer sb = new StringBuffer (); 111 byte[] bytes = dump.getBytes(); 112 for (int i = 0; i < bytes.length; i++) { 113 String h = Integer.toHexString(bytes[i]); 114 while (h.length() < 2) { 115 h = "0" + h; 116 } 117 sb.append(h); 118 } 119 return sb.toString(); 120 } 121 122 public void assertStringEquals(String s1, String s2) { 123 int l1 = s1.length(); 124 int l2 = s2.length(); 125 if (l1 != l2) { 126 System.err.println("Lengths differ: " + l1 + " <=> " + l2); 127 } 128 int len = (l1 > l2) ? l2 : l1; 129 for (int i = 0; i < len; i++) { 130 if (s1.charAt(i) != s2.charAt(i)) { 131 String s = "Strings differ at offset " + len + ": "; 132 StringBuffer sb = new StringBuffer (); 133 for (int j = 0; j < s.length(); j++) { 134 sb.append(" "); 135 } 136 System.err.println(s + dump(s1, i) + " <=> " + dump(s2, i)); 137 System.err.println(sb + hexDump(s1, i) + " <=> " + hexDump(s2, i)); 138 break; 139 } 140 } 141 Assert.assertEquals(s1, s2); 142 } 143 144 public void testNull() { 145 JavaSource js = getSource(null); 146 assertStringEquals(getResult(null), js.toString()); 147 } 148 149 public void testDefaultProtection() { 150 JavaSource js = getSource(JavaSource.DEFAULT_PROTECTION); 151 assertStringEquals(getResult(JavaSource.DEFAULT_PROTECTION), js.toString()); 152 } 153 154 public void testPrivate() { 155 JavaSource js = getSource(JavaSource.PRIVATE); 156 assertStringEquals(getResult(JavaSource.PRIVATE), js.toString()); 157 } 158 159 public void testProtected() { 160 JavaSource js = getSource(JavaSource.PROTECTED); 161 assertStringEquals(getResult(JavaSource.PROTECTED), js.toString()); 162 } 163 164 public void testPublic() { 165 JavaSource js = getSource(JavaSource.PUBLIC); 166 assertStringEquals(getResult(JavaSource.PUBLIC), js.toString()); 167 } 168 169 public void testAbstract() { 170 JavaSourceFactory factory = new JavaSourceFactory(); 171 JavaSource js = factory.newJavaSource(JavaQNameImpl.getInstance("com.foo", "Bar"), JavaSource.PUBLIC); 172 js.setAbstract(true); 173 JavaMethod jm = js.newJavaMethod("test", JavaQNameImpl.VOID, JavaSource.PUBLIC); 174 jm.setAbstract(true); 175 assertStringEquals("public abstract void test();\n", jm.asString()); 176 assertStringEquals("package com.foo;\n" + 177 "\n" + 178 "public abstract class Bar {\n" + 179 " public abstract void test();\n" + 180 "\n" + 181 "}\n", js.asString()); 182 } 183 184 private JavaSource getPlaceHolderSource(boolean pAutoMode) { 185 JavaSourceFactory factory = new JavaSourceFactory(); 186 JavaSource js = factory.newJavaSource(JavaQNameImpl.getInstance("com.foo", "Bar"), JavaSource.PUBLIC); 187 JavaMethod main = js.newJavaMethod("main", JavaQNameImpl.VOID, JavaSource.PUBLIC); 188 main.addParam(String [].class, "pArgs"); 189 main.setStatic(true); 190 main.addThrows(Exception .class); 191 main.addFor(int.class, " i = 1; i < 10; i++"); 192 main.newPlaceHolder("test", pAutoMode); 193 main.addEndFor(); 194 js.setDynamicImports(true); 195 return js; 196 } 197 198 public void testPlaceHolders() { 199 boolean[] autoRemovable = new boolean[]{false, true}; 200 for (int i = 0; i < autoRemovable.length; i++) { 201 boolean autoMode = autoRemovable[i]; 202 JavaSource js = getPlaceHolderSource(autoMode); 203 204 if (autoMode) { 205 assertStringEquals("package com.foo;\n" + 206 "\n" + 207 "public class Bar {\n" + 208 " public static void main(java.lang.String[] pArgs) throws java.lang.Exception {\n" + 209 " for (int i = 1; i < 10; i++) {\n" + 210 " }\n" + 211 " }\n" + 212 "\n" + 213 "}\n", js.asString()); 214 } else { 215 boolean gotException = false; 216 try { 217 js.asString(); 218 } catch (IllegalStateException e) { 219 gotException = true; 220 } 221 assertTrue(gotException); 222 } 223 224 js = getPlaceHolderSource(autoMode); 225 JavaMethod main = js.getMethod("main", new JavaQName[]{JavaQNameImpl.getInstance(String [].class)}); 226 PlaceHolder test = main.getPlaceHolder("test"); 227 assertNotNull(test); 228 test.remove(); 229 main.addLine("// I am here"); 230 231 String expect = "package com.foo;\n" + 232 "\n" + 233 "public class Bar {\n" + 234 " public static void main(java.lang.String[] pArgs) throws java.lang.Exception {\n" + 235 " for (int i = 1; i < 10; i++) {\n" + 236 " // I am here\n" + 237 " }\n" + 238 " }\n" + 239 "\n" + 240 "}\n"; 241 String got = js.asString(); 242 assertStringEquals(expect, got); 243 } 244 } 245 } 246 | Popular Tags |