2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.squid.bridges;
22 import org.apache.commons.io.IOUtils;
23 import org.junit.Test;
24 import org.sonar.api.design.Dependency;
25 import org.sonar.api.resources.JavaPackage;
26 import org.sonar.api.resources.Resource;
27 import org.sonar.graph.DirectedGraph;
28 import org.sonar.graph.Dsm;
29 import org.sonar.graph.DsmManualSorter;
30 import org.sonar.squid.api.SourceCode;
31 import org.sonar.squid.api.SourceCodeEdge;
32 import org.sonar.squid.api.SourceCodeEdgeUsage;
33 import org.sonar.squid.api.SourcePackage;
35 import java.io.IOException;
37 import static org.hamcrest.core.Is.is;
38 import static org.junit.Assert.assertThat;
40 public class DsmSerializerTest {
43 public void serializeEmptyDsm() {
44 Dsm dsm = new Dsm(new DirectedGraph());
45 assertThat(DsmSerializer.serialize(dsm, new DependencyIndex(), new ResourceIndex()), is("[]"));
49 public void serialize() throws IOException {
52 SourcePackage foo = new SourcePackage("org/foo");
53 SourcePackage bar = new SourcePackage("org/bar");
54 SourceCodeEdge edge = new SourceCodeEdge(foo, bar, SourceCodeEdgeUsage.USES) {
56 public int getWeight() {
61 DirectedGraph<SourceCode, SourceCodeEdge> graph = new DirectedGraph<SourceCode, SourceCodeEdge>();
67 Resource fooSonar = new JavaPackage("org.foo");
69 Resource barSonar = new JavaPackage("org.bar");
72 Dependency dep = new Dependency(fooSonar, barSonar).setId(30l);
73 DependencyIndex depIndex = new DependencyIndex();
74 depIndex.put(edge, dep);
75 ResourceIndex resourceIndex = new ResourceIndex();
76 resourceIndex.put(foo, fooSonar);
77 resourceIndex.put(bar, barSonar);
79 Dsm<SourceCode> dsm = new Dsm<SourceCode>(graph);
80 DsmManualSorter.sort(dsm, bar, foo); // for test reproductibility
81 String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/plugins/squid/bridges/DsmSerializerTest/dsm.json"));
82 assertThat(DsmSerializer.serialize(dsm, depIndex, resourceIndex), is(json));