c.s.tools.ide.analysis.modernize/test/unit/src/ide/analysis/modernize/YamlTest.java
author Ilia Gromov <ilia@netbeans.org>
Wed, 07 Jun 2017 20:23:29 +0300
branchrelease82
changeset 18423 b9d9af239a0c
parent 18403 ide.analysis.modernize/test/unit/src/ide/analysis/modernize/YamlTest.java@67d78b11d2e0
permissions -rw-r--r--
Fixing #270763 - Move clang-tidy integration to nb contrib
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - move wrapper
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - sign nbm
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - move tests
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - data for a new test
* * *
Fixed #270839 - [clang-tidy] Group checks in Editor hints
(transplanted from 35b6125ef00c470655dac6673075f5c12ec74593)
ilia@18403
     1
/*
ilia@18403
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
ilia@18403
     3
 *
ilia@18403
     4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
ilia@18403
     5
 *
ilia@18403
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
ilia@18403
     7
 * Other names may be trademarks of their respective owners.
ilia@18403
     8
 *
ilia@18403
     9
 * The contents of this file are subject to the terms of either the GNU
ilia@18403
    10
 * General Public License Version 2 only ("GPL") or the Common
ilia@18403
    11
 * Development and Distribution License("CDDL") (collectively, the
ilia@18403
    12
 * "License"). You may not use this file except in compliance with the
ilia@18403
    13
 * License. You can obtain a copy of the License at
ilia@18403
    14
 * http://www.netbeans.org/cddl-gplv2.html
ilia@18403
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
ilia@18403
    16
 * specific language governing permissions and limitations under the
ilia@18403
    17
 * License.  When distributing the software, include this License Header
ilia@18403
    18
 * Notice in each file and include the License file at
ilia@18403
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
ilia@18403
    20
 * particular file as subject to the "Classpath" exception as provided
ilia@18403
    21
 * by Oracle in the GPL Version 2 section of the License file that
ilia@18403
    22
 * accompanied this code. If applicable, add the following below the
ilia@18403
    23
 * License Header, with the fields enclosed by brackets [] replaced by
ilia@18403
    24
 * your own identifying information:
ilia@18403
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
ilia@18403
    26
 *
ilia@18403
    27
 * If you wish your version of this file to be governed by only the CDDL
ilia@18403
    28
 * or only the GPL Version 2, indicate your decision by adding
ilia@18403
    29
 * "[Contributor] elects to include this software in this distribution
ilia@18403
    30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
ilia@18403
    31
 * single choice of license, a recipient has the option to distribute
ilia@18403
    32
 * your version of this file under either the CDDL, the GPL Version 2 or
ilia@18403
    33
 * to extend the choice of license to its licensees as provided above.
ilia@18403
    34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
ilia@18403
    35
 * Version 2 license, then the option applies only if the new code is
ilia@18403
    36
 * made subject to such option by the copyright holder.
ilia@18403
    37
 *
ilia@18403
    38
 * Contributor(s):
ilia@18403
    39
 *
ilia@18403
    40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
ilia@18403
    41
 */
ilia@18403
    42
package ide.analysis.modernize;
ilia@18403
    43
ilia@18415
    44
import com.sun.tools.ide.analysis.modernize.impl.DiagnosticsTool;
ilia@18415
    45
import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl;
ilia@18415
    46
import com.sun.tools.ide.analysis.modernize.utils.AnalyticsTools;
ilia@18403
    47
import java.io.File;
ilia@18403
    48
import java.io.IOException;
ilia@18403
    49
import java.nio.file.Files;
ilia@18403
    50
import java.util.Arrays;
ilia@18403
    51
import java.util.Collections;
ilia@18403
    52
import org.junit.Test;
ilia@18403
    53
import org.netbeans.api.progress.ProgressHandle;
ilia@18403
    54
import org.netbeans.modules.analysis.SPIAccessor;
ilia@18403
    55
import org.netbeans.modules.analysis.spi.Analyzer;
ilia@18403
    56
import org.netbeans.modules.cnd.api.model.CsmFile;
ilia@18403
    57
import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
ilia@18403
    58
import org.netbeans.modules.cnd.modelutil.CsmUtilities;
ilia@18403
    59
import org.netbeans.modules.cnd.test.CndCoreTestUtils;
ilia@18403
    60
import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
ilia@18403
    61
import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
ilia@18403
    62
import org.netbeans.modules.nativeexecution.api.util.ConnectionManager;
ilia@18403
    63
import org.netbeans.modules.refactoring.api.Scope;
ilia@18403
    64
import org.openide.filesystems.FileObject;
ilia@18403
    65
ilia@18403
    66
/**
ilia@18403
    67
 *
ilia@18403
    68
 * @author Ilia Gromov
ilia@18403
    69
 */
ilia@18403
    70
public class YamlTest extends TidyTestCase {
ilia@18403
    71
ilia@18403
    72
    public YamlTest() {
ilia@18403
    73
        super("hello-world");
ilia@18403
    74
    }
ilia@18403
    75
ilia@18403
    76
    @Test
ilia@18403
    77
    public void testYaml() throws IOException, ConnectionManager.CancellationException {
ilia@18403
    78
        FileObject singleFileObject = getSourceFile("welcome.cc");
ilia@18403
    79
ilia@18403
    80
        File goldenFile = getGoldenFile("hello-world.json");
ilia@18403
    81
        String goldenYaml = new String(Files.readAllBytes(goldenFile.toPath()), "UTF-8").replaceAll("\\$path", getDataDir().getAbsolutePath());
ilia@18403
    82
ilia@18403
    83
        Scope scope = Scope.create(null, null, Arrays.asList(new FileObject[]{singleFileObject}));
ilia@18403
    84
        Analyzer.Context context = SPIAccessor.ACCESSOR.createContext(scope, null, null, ProgressHandle.createHandle("dummy"), 0, 0);
ilia@18403
    85
ilia@18403
    86
        Analyzer.AnalyzerFactory factory = new ModernizeAnalyzerImpl.AnalyzerFactoryImpl();
ilia@18403
    87
        ModernizeAnalyzerImpl analyzer = (ModernizeAnalyzerImpl) factory.createAnalyzer(context);
ilia@18403
    88
ilia@18403
    89
        ExecutionEnvironment execEnv = ExecutionEnvironmentFactory.getLocal();
ilia@18403
    90
ilia@18403
    91
        CsmFile csmFile = CsmUtilities.getCsmFile(singleFileObject, false, false);
ilia@18403
    92
        Item item = AnalyticsTools.findItem(csmFile, getProject());
ilia@18403
    93
ilia@18403
    94
        DiagnosticsTool diagnosticsTool = new DiagnosticsTool(execEnv, item, getProject(), getBinaryFile().getAbsolutePath());
ilia@18403
    95
ilia@18403
    96
        int exitCode = diagnosticsTool.process(Collections.singleton("*"), csmFile, true);
ilia@18403
    97
ilia@18403
    98
        if (exitCode != DiagnosticsTool.STATUS_OK) {
ilia@18403
    99
            fail();
ilia@18403
   100
        }
ilia@18403
   101
        String yamlAsString = diagnosticsTool.getYamlAsString();
ilia@18403
   102
ilia@18403
   103
        File real = File.createTempFile("test", null);
ilia@18403
   104
        Files.write(real.toPath(), yamlAsString.getBytes());
ilia@18403
   105
ilia@18403
   106
        File expected = File.createTempFile("test", null);
ilia@18403
   107
        Files.write(expected.toPath(), goldenYaml.getBytes());
ilia@18403
   108
ilia@18403
   109
        File diff = File.createTempFile("test", null);
ilia@18403
   110
        if (CndCoreTestUtils.diff(real, expected, diff)) {
ilia@18403
   111
            System.err.println("============DIFF============");
ilia@18403
   112
            System.err.println(new String(Files.readAllBytes(diff.toPath()), "UTF-8"));
ilia@18403
   113
            System.err.println("============DIFF============");
ilia@18403
   114
            fail();
ilia@18403
   115
        }
ilia@18403
   116
    }
ilia@18403
   117
}