packagenameparsertests.java
来自「cqME :java framework for TCK test.」· Java 代码 · 共 175 行
JAVA
175 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.utils.javasourcemodel.tests;import java.io.ByteArrayInputStream;import java.io.IOException;import java.util.HashMap;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import junit.textui.TestRunner;import com.sun.tck.j2me.utils.javasourcemodel.Parser;import com.sun.tck.j2me.utils.javasourcemodel.ParserFactory;import com.sun.tck.j2me.utils.javasourcemodel.CompilationUnit;import com.sun.tck.j2me.utils.javasourcemodel.JavaLanguage;import com.sun.tck.j2me.utils.javasourcemodel.ParseException;/** * */public class PackageNameParserTests extends TestCase { public static Test suite() { return new TestSuite(PackageNameParserTests.class, "Package names tests for Parser"); } public static void main (String[] args) { TestRunner.run(suite()); } public void testPositive() { Parser parser = ParserFactory.getParser(JavaLanguage.SECOND_EDITION); String answer = null; for (int i = 0; i < validSamples.length; i++) { ByteArrayInputStream in = new ByteArrayInputStream(validSamples[i].getBytes()); try { answer = parser.parse(in).getPackageName(); } catch (IOException e) { fail("IOException thrown: " + e.getMessage()); } catch (ParseException e) { fail("ParseException thrown when parsing the following input: " + "\"" + validSamples[i] + "\". " + e.getMessage()); } finally { try { in.close(); } catch (IOException e) { fail("IOException thrown: " + e.getMessage()); } } assertEquals("Parser fails to read correct package name", validAnswers[i], answer); } } public void testNegative() { String answer = null; Parser parser = ParserFactory.getParser(JavaLanguage.SECOND_EDITION); for (int i = 0; i < invalidSamples.length; i++) { ByteArrayInputStream in = new ByteArrayInputStream(invalidSamples[i].getBytes()); try { answer = parser.parse(in).getPackageName(); fail("Expected ParseException was not thrown for following " + "input: \"" + invalidSamples[i] + "\".\n" + "Parsed package name: \"" + answer + "\""); } catch (IOException e) { fail("IOException thrown: " + e.getMessage()); } catch (ParseException e) { } finally { try { in.close(); } catch (IOException e) { fail("IOException thrown: " + e.getMessage()); } } } } static final String[] invalidSamples = new String[] { "package @a; class A {}", "package; class A {}", "package a/b/c; class A {}", "null", "abracadabra", "a package a; class A {}", "Package a; class A {}", "packAge a; class A {}", "PACKAGE a; class A {}" }; static final String[] validSamples = new String[] { "package a; class A {}", "package A; public class A {}", "package a_; interface A {}", "package _a; import b.c.D; class A {}", "package a3; abstract class A {}", "package a ; class A {}", "package /* bla-bla-bla */ a; class A {}", "package\n//bla-bla-bla\na; class A {}", "//bla-bla-bla\n//bla-bla-bla\npackage a; class A {}", "/* bla-bla-bla */package a; class A {}", "/* bla-bla-bla\n * bla-bla-bla */\npackage a; class A {}", "package a;", "package a.b.c; class A {}", "package a.b.C$_D; class A {}", "// package b; class A {}\npackage a; class A {}", "/* package b; class A {}\n*/\npackage a; class A{}", "package com.sun.tck.j2me.utils.javasourcemodel.tests; class A {}", "", "import b.C; class A {}", "interface A {}", "public class A {}", "abstract class A {}", "strictfp interface A {}", "//\n", "/**/", }; static final String[] validAnswers = new String[] { "a", "A", "a_", "_a", "a3", "a", "a", "a", "a", "a", "a", "a", "a.b.c", "a.b.C$_D", "a", "a", "com.sun.tck.j2me.utils.javasourcemodel.tests", "", "", "", "", "", "", "", "", };}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?