Package org.junithelper.plugin.io

Examples of org.junithelper.plugin.io.PropertiesLoader


        }
        return config;
    }

    protected PropertiesLoader getPropertiesLoader(String language) {
        return new PropertiesLoader(language);
    }
View Full Code Here


    public void run(IAction action) {

        store = getIPreferenceStore();
        Configuration config = getConfiguration(store, selection);
        PropertiesLoader props = getPropertiesLoader(store.getString(Preference.lang));

        StructuredSelection structuredSelection = null;

        try {
View Full Code Here

    }

    @Override
    protected void createFieldEditors() {

        PropertiesLoader props = new PropertiesLoader(getPreferenceStore().getString(Preference.lang));
        {
            // parent group
            Composite parent = getFieldEditorParent();
            {
                GridData gd = new GridData(768);
                parent.setLayoutData(gd);
                {
                    String en = Preference.Lang.English;
                    String ja = Preference.Lang.Japanese;
                    String[][] labelAndValues = new String[][] { { Preference.Lang.EnglishLabel, en },
                            { Preference.Lang.JapaneseLabel, ja }, };
                    editor_Language = new RadioGroupFieldEditor(Preference.lang, "Select your language.", 2,
                            labelAndValues, parent);
                    addField(editor_Language);

                    // output file encoding
                    addField(new StringFieldEditor(Preference.Common.outputFileEncoding, props
                            .get(Preference.Common.outputFileEncoding), parent));

                    // src/main/java
                    addField(new StringFieldEditor(Preference.Common.srcMainPath, props
                            .get(Preference.Common.srcMainPath), parent));
                    // src/test/java
                    addField(new StringFieldEditor(Preference.Common.srcTestPath, props
                            .get(Preference.Common.srcTestPath), parent));

                    // line break policy
                    {
                        String forceCRLF = Preference.LineBreakPolicy.forceCRLF;
                        String forceLF = Preference.LineBreakPolicy.forceLF;
                        String forceNewFileCRLF = Preference.LineBreakPolicy.forceNewFileCRLF;
                        String forceNewFileLF = Preference.LineBreakPolicy.forceNewFileLF;
                        String[][] lbpLabelAndValues = new String[][] { { props.get(forceCRLF), forceCRLF },
                                { props.get(forceLF), forceLF }, { props.get(forceNewFileCRLF), forceNewFileCRLF },
                                { props.get(forceNewFileLF), forceNewFileLF } };
                        editor_lineBreakPolicy = new RadioGroupFieldEditor(Preference.Common.lineBreakPolicy, props
                                .get(Preference.LineBreakPolicy.description), 5, lbpLabelAndValues, parent);
                        addField(editor_lineBreakPolicy);
                    }

                    // Soft tabs
                    {
                        editor_useSoftTabs = new BooleanFieldEditor(Preference.Common.useSoftTabs, props
                                .get(Preference.Common.useSoftTabs), parent);
                        addField(editor_useSoftTabs);
                        editor_softTabSize = new StringFieldEditor(Preference.Common.softTabSize, props
                                .get(Preference.Common.softTabSize), parent);
                        addField(editor_softTabSize);
                    }

                    // JUnit version(3 or 4)
                    String ver3 = Preference.TestClassGen.junitVersion3;
                    String ver4 = Preference.TestClassGen.junitVersion4;
                    String[][] junitVersionsLabelAndValues = new String[][] { { props.get(ver3), ver3 },
                            { props.get(ver4), ver4 }, };

                    editor_JUnitVersion = new RadioGroupFieldEditor(Preference.TestClassGen.junitVersion, props
                            .get(Preference.TestClassGen.description), 2, junitVersionsLabelAndValues, parent) {
                        @Override
                        protected void fireValueChanged(String p, Object o, Object n) {
                            super.fireValueChanged(p, o, n);
                            IPreferenceStore store = Activator.getDefault().getPreferenceStore();
                            store.setValue(Preference.TestClassGen.junitVersion, String.valueOf(n));
                        }
                    };
                    addField(editor_JUnitVersion);

                    // class to extend
                    editor_ClassToExtend = new StringFieldEditor(Preference.TestClassGen.classToExtend, props
                            .get(Preference.TestClassGen.classToExtend), parent);
                    addField(editor_ClassToExtend);

                    // common delimiter setting
                    editor_TestMethodNameBasicDelimiter = new StringFieldEditor(Preference.TestMethodGen.delimiter,
                            props.get(Preference.TestMethodGen.delimiter), 10, parent);
                    addField(editor_TestMethodNameBasicDelimiter);

                    editor_isTestMethodNameArgsRequired = new BooleanFieldEditor(Preference.TestMethodGen.enabledArgs,
                            props.get(Preference.TestMethodGen.enabledArgs), parent);
                    addField(editor_isTestMethodNameArgsRequired);
                    editor_TestMethodNameArgsPrefix = new StringFieldEditor(Preference.TestMethodGen.argsPrefix, props
                            .get(Preference.TestMethodGen.argsPrefix), parent);
                    addField(editor_TestMethodNameArgsPrefix);
                    editor_TestMethodNameArgsDelimiter = new StringFieldEditor(Preference.TestMethodGen.argsDelimiter,
                            props.get(Preference.TestMethodGen.argsDelimiter), parent);
                    addField(editor_TestMethodNameArgsDelimiter);

                    editor_isTestMethodNameReturnTypeRequired = new BooleanFieldEditor(
                            Preference.TestMethodGen.enabledReturn, props.get(Preference.TestMethodGen.enabledReturn),
                            parent);
                    addField(editor_isTestMethodNameReturnTypeRequired);
                    editor_TestMethodNameReturnPrefix = new StringFieldEditor(Preference.TestMethodGen.returnPrefix,
                            props.get(Preference.TestMethodGen.returnPrefix), parent);
                    addField(editor_TestMethodNameReturnPrefix);
                    editor_TestMethodNameRetrnDelimiter = new StringFieldEditor(
                            Preference.TestMethodGen.returnDelimiter, props
                                    .get(Preference.TestMethodGen.returnDelimiter), parent);
                    addField(editor_TestMethodNameRetrnDelimiter);

                    // enable exception thrown
                    editor_isExceptionThrownRequired = new BooleanFieldEditor(
                            Preference.TestMethodGen.enabledException, props
                                    .get(Preference.TestMethodGen.enabledException), parent);
                    addField(editor_isExceptionThrownRequired);
                    editor_TestMethodNameExceptionPrefix = new StringFieldEditor(
                            Preference.TestMethodGen.exceptionPrefix, props
                                    .get(Preference.TestMethodGen.exceptionPrefix), parent);
                    addField(editor_TestMethodNameExceptionPrefix);
                    editor_TestMethodNameExceptionDelimiter = new StringFieldEditor(
                            Preference.TestMethodGen.exceptionDelimiter, props
                                    .get(Preference.TestMethodGen.exceptionDelimiter), parent);
                    addField(editor_TestMethodNameExceptionDelimiter);

                    // public methods
                    editor_isPublicRequired = new BooleanFieldEditor(Preference.TestMethodGen.includePublic, props
                            .get(Preference.TestMethodGen.includePublic), parent);
                    addField(editor_isPublicRequired);
                    // protected methods
                    editor_isProtectedRequired = new BooleanFieldEditor(Preference.TestMethodGen.includeProtected,
                            props.get(Preference.TestMethodGen.includeProtected), parent);
                    addField(editor_isProtectedRequired);
                    // package local methods
                    editor_isPackageLocalRequired = new BooleanFieldEditor(
                            Preference.TestMethodGen.includePackageLocal, props
                                    .get(Preference.TestMethodGen.includePackageLocal), parent);
                    addField(editor_isPackageLocalRequired);

                    // enable excluding accessors
                    editor_isAccessorsExcluded = new BooleanFieldEditor(Preference.TestMethodGen.excludesAccessors,
                            props.get(Preference.TestMethodGen.excludesAccessors), parent);
                    addField(editor_isAccessorsExcluded);

                    {
                        String mock_none = Preference.TestMethodGen.usingMockNone;
                        String mock_easyMock = Preference.TestMethodGen.usingMockEasyMock;
                        String mock_jmock2 = Preference.TestMethodGen.usingMockJMock2;
                        String mock_mockito = Preference.TestMethodGen.usingMockMockito;
                        String mock_jmockit = Preference.TestMethodGen.usingMockJMockit;
                        String[][] mockLabelAndValues = new String[][] { { props.get(mock_none), mock_none },
                                { props.get(mock_easyMock), mock_easyMock }, { props.get(mock_jmock2), mock_jmock2 },
                                { props.get(mock_mockito), mock_mockito }, { props.get(mock_jmockit), mock_jmockit } };
                        editor_MockObjectFramework = new RadioGroupFieldEditor(Preference.TestMethodGen.usingMock,
                                props.get(Preference.TestMethodGen.descriptionForMock), 5, mockLabelAndValues, parent);
                        addField(editor_MockObjectFramework);
                    }

                    {
                        String comments_none = Preference.TestMethodGen.commentsNone;
                        String comments_aaa = Preference.TestMethodGen.commentsArrangeActAssert;
                        String comments_gwt = Preference.TestMethodGen.commentsGivenWhenThen;
                        String[][] commentsLabelAndValues = new String[][] {
                                { props.get(comments_none), comments_none }, { props.get(comments_aaa), comments_aaa },
                                { props.get(comments_gwt), comments_gwt } };
                        editor_TestingPatternComments = new RadioGroupFieldEditor(
                                Preference.TestMethodGen.usingTestingPatternComments, props
                                        .get(Preference.TestMethodGen.descriptionForTestingPatternComments), 3,
                                commentsLabelAndValues, parent);
                        addField(editor_TestingPatternComments);
                    }
View Full Code Here

    public void run(IAction action) {

        store = getIPreferenceStore();
        Configuration config = getConfiguration(store, selection);
        PropertiesLoader props = getPropertiesLoader(config.language);

        StructuredSelection structuredSelection = null;

        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        OutputStreamWriter writer = null;

        String projectName = null;
        String testCaseFileName = null;
        String testCaseCreateFilePath = null;
        String resourcePathForTargetClassFile = null;
        String resourcePathForTestClassFile = null;

        try {

            // -------------------------
            // get selection
            if (selection instanceof StructuredSelection) {
                // viewer
                structuredSelection = (StructuredSelection) selection;
            }
            if (isNotSelected(structuredSelection)) {
                openWarningForRequired(props);
                return;
            } else if (isSelectedSeveral(structuredSelection)) {
                openWarningForSelectOneOnly(props);
                return;
            }

            // -------------------------
            // get project path, resource path
            projectName = getProjectName(structuredSelection);
            resourcePathForTargetClassFile = getResourcePathForTargetClassFile(structuredSelection);
            String projectRootAbsolutePath = getWorkspaceRootAbsolutePath(getIWorkspaceRoot())
                    + StringValue.DirectorySeparator.General + projectName + StringValue.DirectorySeparator.General;

            resourcePathForTestClassFile = resourcePathForTargetClassFile.replaceFirst(
                    config.directoryPathOfProductSourceCode, config.directoryPathOfTestSourceCode).replace(
                    StringValue.FileExtension.JavaFile,
                    StringValue.JUnit.TestClassNameSuffix + StringValue.FileExtension.JavaFile);
            testCaseCreateFilePath = projectRootAbsolutePath
                    + getResourcePathForTargetClassFile(structuredSelection).replace(
                            config.directoryPathOfProductSourceCode, config.directoryPathOfTestSourceCode).replace(
                            StringValue.FileExtension.JavaFile,
                            StringValue.JUnit.TestClassNameSuffix + StringValue.FileExtension.JavaFile);
            testCaseFileName = (getClassNameFromResourcePathForTargetClassFile(resourcePathForTargetClassFile) + StringValue.FileExtension.JavaFile)
                    .replace(StringValue.FileExtension.JavaFile, StringValue.JUnit.TestClassNameSuffix
                            + StringValue.FileExtension.JavaFile);

            // -------------------------
            // mkdir -p /abc/def/
            String testCaseCreateDirPath = testCaseCreateFilePath.replaceFirst("/[^/\\.]+\\.java$", "/");
            File testDir = new File(testCaseCreateDirPath);
            String[] dirArr = testCaseCreateDirPath.split(StringValue.DirectorySeparator.General);
            String tmpDirPath = StringValue.Empty;
            String tmpResourceDirPath = StringValue.Empty;
            for (String each : dirArr) {
                tmpDirPath += StringValue.DirectorySeparator.General + each;
                File tmpDir = new File(tmpDirPath);
                // skip until project root dir
                if (tmpDir.getPath().length() <= projectRootAbsolutePath.length()) {
                    continue;
                }
                tmpResourceDirPath += StringValue.DirectorySeparator.General + each;
                if (!tmpDir.exists()) {
                    if (!tmpDir.mkdir()) {
                        System.err.println("create directory error : " + tmpDir.getPath());
                    }
                    String parentPathOfCreatedDir = projectName + StringValue.DirectorySeparator.General
                            + tmpResourceDirPath + "/..";
                    if (!ResourceRefreshUtil.refreshLocal(null, parentPathOfCreatedDir)) {
                        System.err.println("Resource refresh error!" + parentPathOfCreatedDir);
                    }
                }
            }
            if (!testDir.mkdirs()) {
                System.err.println("test directory already exist");
            }

            // -------------------------
            // resource sync
            String pathOfTestCaseDir = projectName + StringValue.DirectorySeparator.General
                    + resourcePathForTestClassFile + "/..";
            if (!ResourceRefreshUtil.refreshLocal(null, pathOfTestCaseDir)) {
                openWarningForResourceRefreshError(props);
                return;
            }

            try {
                File outputIOFile = new File(testCaseCreateDirPath + StringValue.DirectorySeparator.General
                        + testCaseFileName);
                // ---------------
                // confirm if test case file already exists
                String msg = props.get(Dialog.Common.alreadyExist) + " (" + testCaseFileName + ")"
                        + StringValue.LineFeed + props.get(Dialog.Common.confirmToProceed);
                if (outputIOFile.exists() && !openConfirm(props, msg)) {
                    return;
                }
                // ---------------
                // get target class file
                IResource targetClassResource = getIWorkspaceRoot().findMember(
                        "/" + projectName + "/" + resourcePathForTargetClassFile);
                IFile targetClassFile = (IFile) targetClassResource;

                // -------------------------
                // overwrite config
                // http://code.google.com/p/junithelper/issues/detail?id=72
                String absolutePath = projectRootAbsolutePath + StringValue.DirectorySeparator.General;
                config.directoryPathOfProductSourceCode = absolutePath + config.directoryPathOfProductSourceCode;
                config.directoryPathOfTestSourceCode = absolutePath + config.directoryPathOfTestSourceCode;

                // ---------------
                // generate test case source code string
                String encoding = UniversalDetectorUtil.getDetectedEncoding(EclipseIFileUtil
                        .getInputStreamFrom(targetClassFile));
                InputStream targetInputStream = EclipseIFileUtil.getInputStreamFrom(targetClassFile);
                String sourceCodeString = IOUtil.readAsString(targetInputStream, encoding);
                LineBreakProvider lineBreakProvider = new LineBreakProvider(config, null);
                TestCaseGenerator generator = TestCaseGeneratorFactory.create(config, lineBreakProvider);
                generator.initialize(new ClassMetaExtractor(config).extract(sourceCodeString));

                // ---------------
                // write test case
                outputStream = new FileOutputStream(testCaseCreateDirPath + StringValue.DirectorySeparator.General
                        + testCaseFileName);
                writer = new OutputStreamWriter(outputStream, getDetectedEncodingFrom(targetClassFile,
                        config.outputFileEncoding));
                writer.write(generator.getNewTestCaseSourceCode());

            } catch (InvalidPreferenceException ipe) {
                ipe.printStackTrace();
                openWarning(props, props.get(Dialog.Common.invalidPreference));
                return;
            } catch (FileNotFoundException fnfe) {
                fnfe.printStackTrace();
            } finally {
                IOUtil.close(writer);
View Full Code Here

    public void run(IAction action) {

        store = getIPreferenceStore();
        Configuration config = getConfiguration(store, selection);
        PropertiesLoader props = getPropertiesLoader(store.getString(Preference.lang));

        StructuredSelection structuredSelection = null;

        try {

            if (selection instanceof StructuredSelection) {
                // viewer
                structuredSelection = (StructuredSelection) selection;
            }
            if (isNotSelected(structuredSelection)) {
                // required selecttion
                openWarningForRequired(props);
                return;
            } else if (isSelectedSeveral(structuredSelection)) {
                // select only one
                openWarningForSelectOneOnly(props);
                return;
            }

            // ----------------------------------------
            // get project path, resource path
            String resourcePathForTargetClassFile = getResourcePathForTargetClassFile(structuredSelection)
                    .replaceFirst(config.directoryPathOfTestSourceCode, config.directoryPathOfProductSourceCode);
            String resourcePathForTestCaseFile = resourcePathForTargetClassFile.replaceFirst(
                    config.directoryPathOfProductSourceCode, config.directoryPathOfTestSourceCode).replaceFirst(
                    "[^(Test)]\\.java$", StringValue.JUnit.TestClassNameSuffix + StringValue.FileExtension.JavaFile);
            String projectName = getProjectName(structuredSelection);
            String projectRootAbsolutePath = getWorkspaceRootAbsolutePath(getIWorkspaceRoot())
                    + StringValue.DirectorySeparator.General + projectName + StringValue.DirectorySeparator.General;

            // ----------------------------------------
            // check selection
            if (!resourcePathForTestCaseFile.matches(".*" + RegExp.FileExtension.JavaFile)) {
                openWarningForSelectJavaFile(props);
                return;
            }

            // ----------------------------------------
            // confirm to execute
            String targetClassName = getClassNameFromResourcePathForTargetClassFile(resourcePathForTargetClassFile);
            String testCaseFilename = getTestClassNameFromClassName(targetClassName);
            String msg = props.get(Dialog.Common.confirmToChangeToJUnitVersion3) + " (" + testCaseFilename + ")";
            if (testCaseFilename == null || !openConfirm(props, msg)) {
                return;
            }

            // ----------------------------------------
View Full Code Here

    public void run(IAction action) {

        store = getIPreferenceStore();
        Configuration config = getConfiguration(store, selection);
        PropertiesLoader props = getPropertiesLoader(config.language);

        String resourcePathForTestCaseFile = null;
        StructuredSelection structuredSelection = null;

        try {

            if (selection instanceof StructuredSelection) {
                // select in view(package explorer, navigator)
                structuredSelection = (StructuredSelection) selection;
            }
            // dialog to confirm
            if (isNotSelected(structuredSelection)) {
                openWarningForRequired(props);
                return;
            } else if (isSelectedSeveral(structuredSelection)) {
                openWarningForSelectOneOnly(props);
                return;
            }

            String projectName = getProjectName(structuredSelection);
            String resourcePathForTargetClassFile = getResourcePathForTargetClassFile(structuredSelection);
            String targetClassName = getClassNameFromResourcePathForTargetClassFile(resourcePathForTargetClassFile);

            if (targetClassName == null) {
                // required to select java file
                openWarningForSelectJavaFile(props);
                return;
            }

            resourcePathForTestCaseFile = resourcePathForTargetClassFile.replace(
                    config.directoryPathOfProductSourceCode, config.directoryPathOfTestSourceCode).replace(
                    StringValue.FileExtension.JavaFile,
                    StringValue.JUnit.TestClassNameSuffix + StringValue.FileExtension.JavaFile);

            String projectRootAbsolutePath = getWorkspaceRootAbsolutePath(getIWorkspaceRoot())
                    + StringValue.DirectorySeparator.General + projectName + StringValue.DirectorySeparator.General;

            String testCaseFileAbsolutePath = projectRootAbsolutePath + resourcePathForTestCaseFile;

            File testCaseIOFile = new File(testCaseFileAbsolutePath);

            if (!testCaseIOFile.exists()) {
                // ----------------------------------------
                // create new file
                String testCaseFilename = getTestClassNameFromClassName(targetClassName);
                String msg = props.get(Dialog.Common.notExist) + " (" + testCaseFilename + ")" + StringValue.LineFeed
                        + props.get(Dialog.Common.confirmToCreateNewFile);
                if (testCaseFilename != null && openConfirm(props, msg)) {
                    new CreateNewTestCaseAction().run(action, selection);
                }
                return;
            }
View Full Code Here

    public void run(IAction action) {

        store = getIPreferenceStore();
        Configuration config = getConfiguration(store, selection);
        PropertiesLoader props = getPropertiesLoader(store.getString(Preference.lang));

        StructuredSelection structuredSelection = null;

        try {

            if (selection instanceof StructuredSelection) {
                // viewer
                structuredSelection = (StructuredSelection) selection;
            }
            if (isNotSelected(structuredSelection)) {
                // required selecttion
                openWarningForRequired(props);
                return;
            } else if (isSelectedSeveral(structuredSelection)) {
                // select only one
                openWarningForSelectOneOnly(props);
                return;
            }

            // ----------------------------------------
            // get project path, resource path
            String resourcePathForTargetClassFile = getResourcePathForTargetClassFile(structuredSelection)
                    .replaceFirst(config.directoryPathOfTestSourceCode, config.directoryPathOfProductSourceCode);
            String resourcePathForTestCaseFile = resourcePathForTargetClassFile.replaceFirst(
                    config.directoryPathOfProductSourceCode, config.directoryPathOfTestSourceCode).replaceFirst(
                    "[^(Test)]\\.java$", StringValue.JUnit.TestClassNameSuffix + StringValue.FileExtension.JavaFile);
            String projectName = getProjectName(structuredSelection);
            String projectRootAbsolutePath = getWorkspaceRootAbsolutePath(getIWorkspaceRoot())
                    + StringValue.DirectorySeparator.General + projectName + StringValue.DirectorySeparator.General;

            // ----------------------------------------
            // check selection
            if (!resourcePathForTestCaseFile.matches(".*" + RegExp.FileExtension.JavaFile)) {
                openWarningForSelectJavaFile(props);
                return;
            }

            // ----------------------------------------
            // confirm to execute
            String targetClassName = getClassNameFromResourcePathForTargetClassFile(resourcePathForTargetClassFile);
            String testCaseFilename = getTestClassNameFromClassName(targetClassName);
            String msg = props.get(Dialog.Common.confirmToChangeToJUnitVersion4) + " (" + testCaseFilename + ")";
            if (testCaseFilename == null || !openConfirm(props, msg)) {
                return;
            }

            // ----------------------------------------
View Full Code Here

public class PropertiesLoaderTest extends TestCase {

  public void test_get_A$String() throws Exception {
    String lang = "en";
    PropertiesLoader target = new PropertiesLoader(lang);
    // given
    String key = "labels.preference.Common.description";
    // e.g. : given(mocked.called()).willReturn(1);
    // when
    String actual = target.get(key);
    // then
    // e.g. : verify(mocked).called();
    String expected = "Settings for JUnit Helper plugin.";
    assertEquals(expected, actual);
  }
View Full Code Here

TOP

Related Classes of org.junithelper.plugin.io.PropertiesLoader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.