1   /*
2    * Created on 2005/08/26
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Templates
6    */
7   package test.tuigwaa;
8   
9   import java.io.StringWriter;
10  import java.io.UnsupportedEncodingException;
11  import java.net.URLDecoder;
12  import java.net.URLEncoder;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.Collections;
16  import java.util.Date;
17  import java.util.Iterator;
18  import java.util.List;
19  import java.util.Properties;
20  import java.util.Random;
21  import java.util.regex.Pattern;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.commons.io.HexDump;
26  import org.apache.commons.lang.ArrayUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.commons.mail.EmailException;
29  import org.apache.commons.mail.HtmlEmail;
30  import org.dom4j.Document;
31  import org.dom4j.Element;
32  import org.dom4j.QName;
33  import org.dom4j.dom.DOMDocument;
34  import org.dom4j.dom.DOMElement;
35  import org.dom4j.io.XMLWriter;
36  
37  import com.isenshi.util.CharUtil;
38  import com.isenshi.util.Diff;
39  import com.isenshi.util.PathUtils;
40  import com.isenshi.util.extlib.StrutsUtil;
41  
42  /***
43   * @author nishioka
44   */
45  public class TestUtils extends TestCase {
46  
47  	
48  	
49  	
50  	public void testParameter() throws UnsupportedEncodingException {
51  		System.out.println(CharUtil.urlDecode("%93%FA%95%F1%2F%8FW%8Cv%8C%8B%89%CA", "Windows-31J"));
52  		
53  	}
54  
55  	public String decode(String s, String enc) throws UnsupportedEncodingException {
56  
57  		boolean needToChange = false;
58  		StringBuffer sb = new StringBuffer();
59  		int numChars = s.length();
60  		int i = 0;
61  
62  		while (i < numChars) {
63  			char c = s.charAt(i);
64  			switch (c) {
65  			case '+':
66  				sb.append(' ');
67  				i++;
68  				needToChange = true;
69  				break;
70  			case '%':
71  				/*
72  				 * Starting with this instance of %, process all consecutive
73  				 * substrings of the form %xy. Each substring %xy will yield a
74  				 * byte. Convert all consecutive bytes obtained this way to
75  				 * whatever character(s) they represent in the provided
76  				 * encoding.
77  				 */
78  
79  				try {
80  					// (numChars-i)/3 is an upper bound for the number
81  					// of remaining bytes
82  					byte[] bytes = new byte[((numChars - i) / 3)*2];
83  					int pos = 0;
84  
85  					while (((i + 2) < numChars) && (c == '%')) {
86  						bytes[pos++] = (byte) Integer.parseInt(s.substring(
87  								i + 1, i + 3), 16);
88  						i += 3;
89  						if (i < numChars){
90  							c = s.charAt(i);
91  							if((pos%2 == 1)&& c != '%' && (i+1) <= numChars){ // && s.charAt(i+1) == '%') || i+1 == numChars){
92  								String str = s.substring(i,i+1);
93  								bytes[pos++] = str.getBytes()[0];
94  								i++;
95  								if(i == numChars){
96  									break;
97  								}
98  								c = s.charAt(i);
99  							}						
100 						}
101 					}
102 
103 					// A trailing, incomplete byte encoding such as
104 					// "%x" will cause an exception to be thrown
105 
106 					if ((i < numChars) && (c == '%'))
107 						throw new IllegalArgumentException(
108 								"URLDecoder: Incomplete trailing escape (%) pattern");
109 
110 					sb.append(new String(bytes, 0, pos, enc));
111 				} catch (NumberFormatException e) {
112 					throw new IllegalArgumentException(
113 							"URLDecoder: Illegal hex characters in escape (%) pattern - "
114 									+ e.getMessage());
115 				}
116 				needToChange = true;
117 				break;
118 			default:
119 				sb.append(c);
120 				i++;
121 				break;
122 			}
123 		}
124 		return (needToChange? sb.toString() : s);
125 	}
126 
127 	
128 	
129 	public void testCompile() {
130 		Pattern pattern = Pattern.compile("/[_]?search.*//.do");
131 		String hoge = "/searchxxxx.do";
132 		String test = "/_searchxxxx.do";
133 		String hage = "/sssss.do";
134 
135 		System.out.println(pattern.matcher(hoge).find());
136 		System.out.println(pattern.matcher(test).find());
137 		System.out.println(pattern.matcher(hage).find());
138 
139 	}
140 
141 	public void testChar() {
142 		int i = '。' / 256;
143 		int i2 = '。' % 256;
144 		System.out.println(i + "," + i2);
145 	}
146 
147 	public void testRandom() {
148 		Random random = new Random(new Date().getTime());
149 		for (int i = 0; i < 10; i++) {
150 			long l = random.nextLong();
151 			if (l < 0) {
152 
153 			}
154 		}
155 	}
156 
157 	public void testProperties() {
158 		Properties props = new Properties();
159 		props.setProperty("a", "b");
160 		props.setProperty("b", "d");
161 		props.setProperty("b", "e");
162 
163 		for (Iterator i = props.entrySet().iterator(); i.hasNext();) {
164 			System.out.println(i.next());
165 		}
166 	}
167 
168 	public void testJarInput() {
169 		// JarInputStream jinput = new JarInputStream(new FileInputStream(
170 		// ResourceUtils.getFile("hoge.jar")));
171 		// ResourceUtils.saveDecompressedJar(jinput, "tmp2");
172 	}
173 
174 	/*
175 	 * public void testJarOutput() throws IOException { OutputStream fos =
176 	 * ResourceUtils.getOutputStream("hoge.jar"); JarOutputStream jarout = new
177 	 * JarOutputStream(fos); ResourceUtils.writeJar(jarout, "hoge");
178 	 * jarout.close(); }
179 	 */
180 
181 	public void testSplit() {
182 		String a = "hoge.bbb";
183 		String[] aray1 = a.split("//.");
184 		// String[] aray2 = a.split("/");
185 		assertEquals(aray1[0], "hoge");
186 	}
187 
188 	public void testParentHoge() {
189 		assertEquals("aaa", "aaa/".substring(0, 3));
190 
191 		assertEquals("hoge", PathUtils.getParentPath("hoge/test"));
192 		assertEquals("hoge", PathUtils.getParentPath("hoge/test/"));
193 		assertEquals("hoge", PathUtils.getParentPath("hoge/test/"));
194 		assertEquals("hoge/test", PathUtils.getParentPath("hoge/test/mm"));
195 		assertNull(PathUtils.getParentPath("/"));
196 		assertNull(PathUtils.getParentPath("hoge/"));
197 	}
198 
199 	public void testCreateXML() throws Exception {
200 		Document doc = new DOMDocument();
201 		Element ele = new DOMElement("hoge");
202 		Element ele2 = new DOMElement("hoge2");
203 		QName qname = new QName("attr");
204 		// Attribute attr = new DOMAttribute(qname, "valuye");
205 		doc.add(ele);
206 		ele.add(ele2);
207 		ele2.addAttribute(qname, "hoge");
208 
209 		StringWriter writer = new StringWriter();
210 		XMLWriter xmlWriter = new XMLWriter(writer);
211 		xmlWriter.write(doc);
212 		xmlWriter.close();
213 
214 		System.out.println(writer.getBuffer().toString());
215 
216 	}
217 
218 	public void testPrefixAndSuffix() {
219 		String test = "prefix.hoge.suffix";
220 		test = CharUtil.removePrefixAndSuffix(test, "prefix.", ".suffix");
221 		assertEquals(test, "hoge");
222 	}
223 
224 	public void testReplace() {
225 		String template = "[${index}]";
226 		template = CharUtil.replace(template, "index", new Integer(2)
227 				.toString());
228 		assertEquals("[2]", template);
229 	}
230 
231 	public void testGetURLDecodedParameter() {
232 		String jLine = "??";
233 		try {
234 			String encodedLine = URLEncoder.encode(jLine, "UTF-8");
235 			String query = "null=&hoge=HHHHHH&hage=" + encodedLine;
236 			String jValue = StrutsUtil.getURLDecodedParameter(query, "hage");
237 			assertEquals(jLine, jValue);
238 		} catch (Exception e) {
239 			e.printStackTrace();
240 			fail();
241 		}
242 	}
243 
244 	public void testCol() {
245 		List list = new ArrayList();
246 		list.add("hoge");
247 		Collection col = Collections.unmodifiableCollection(list);
248 		assertEquals(true, col instanceof Collection);
249 	}
250 
251 	/*
252 	 * 
253 	 * public void testGetJDBCURL(){
254 	 * 
255 	 * try{ String url = HSQLDBUtil.getJDBCURL(); Pattern p =
256 	 * Pattern.compile(System.getProperty("file.separator")); String[] s =
257 	 * p.split(url); assertTrue((s[s.length-3].indexOf("classes")!= -1));
258 	 * 
259 	 * }catch(Exception e){ e.printStackTrace(); fail(); } }
260 	 */
261 	public void testDiff() {
262 		String SEP = System.getProperty("line.separator");
263 		String oldText = "I am Yuhei" + SEP + "Nishioka";
264 		String newText = "I am Yuhei" + SEP + "Nashioka";
265 
266 		Diff diff = new Diff(oldText, newText);
267 
268 		for (Iterator i = diff.getLines().iterator(); i.hasNext();) {
269 			Diff.Line line = (Diff.Line) i.next();
270 			if (line.isDeleteFlag()) {
271 				System.out.println("<del>" + line.getText() + "</del>");
272 			} else if (line.isInsertFlag()) {
273 				System.out.println("<insert>" + line.getText() + "</insernt>");
274 			} else {
275 				System.out.println(line.getText());
276 			}
277 
278 		}
279 
280 	}
281 
282 	public void testEmail() {
283 		HtmlEmail email = new HtmlEmail();
284 		email.setHostName("localhost");
285 		try {
286 			email.setCharset("ISO-2022-JP");
287 			email.addTo("nishioka@isenshi.com", "John Doe");
288 			email.setFrom("me@apache.org", "Me");
289 			email.setSubject("Test タイトルも日本語");
290 
291 			// email.setContent("西岡悠平 日本語", "text/plain; charset=ISO-2022-JP");
292 
293 			email.setHtmlMsg("<html><body><b>西岡</b></body></html>");
294 
295 			// email.setMsg("西岡悠平 日本語");
296 			// email.
297 			// email.send();
298 		} catch (EmailException e) {
299 			e.printStackTrace();
300 			fail();
301 		}
302 	}
303 
304 	public class Bean {
305 
306 		private String name1;
307 
308 		private String name2;
309 
310 		public Bean(String name1, String name2) {
311 			this.name1 = name1;
312 			this.name2 = name2;
313 		}
314 
315 		public void setName1(String name1) {
316 			this.name1 = name1;
317 		}
318 
319 		public void setName2(String name2) {
320 			this.name2 = name2;
321 		}
322 
323 		public String getName1() {
324 			return name1;
325 		}
326 
327 		public String getName2() {
328 			return name2;
329 		}
330 	}
331 
332 }