View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.buildcache;
20  
21  import java.io.File;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.Date;
30  import java.util.List;
31  
32  import org.apache.maven.artifact.DefaultArtifact;
33  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
34  import org.apache.maven.buildcache.hash.HashFactory;
35  import org.apache.maven.buildcache.xml.Build;
36  import org.apache.maven.buildcache.xml.XmlService;
37  import org.apache.maven.buildcache.xml.build.Artifact;
38  import org.apache.maven.buildcache.xml.build.CompletedExecution;
39  import org.apache.maven.buildcache.xml.build.DigestItem;
40  import org.apache.maven.buildcache.xml.build.ProjectsInputInfo;
41  import org.apache.maven.buildcache.xml.build.PropertyValue;
42  import org.junit.jupiter.api.Test;
43  
44  public class BuildInfoTest {
45  
46      @Test
47      public void name() throws Exception {
48  
49          XmlService xmlService = new XmlService();
50  
51          ProjectsInputInfo main = new ProjectsInputInfo();
52          main.setChecksum("dependencyChecksum");
53          main.addItem(createItem("pom", "<project><modelVersion>4.0.0</modelVersion></project>", "hash1"));
54          main.addItem(createItem("file", Paths.get(".").toString(), "hash2"));
55  
56          Artifact artifact = new Artifact();
57          artifact.setGroupId("g");
58          artifact.setArtifactId("a");
59          artifact.setType("t");
60          artifact.setClassifier("c");
61          artifact.setScope("s");
62          artifact.setFileName("f");
63          artifact.setFileSize(123456);
64          artifact.setFileHash("456L");
65  
66          org.apache.maven.buildcache.xml.build.Build buildInfo = new org.apache.maven.buildcache.xml.build.Build();
67          buildInfo.setCacheImplementationVersion("cacheImplementationVersion");
68          buildInfo.setBuildServer("server");
69          buildInfo.setBuildTime(new Date());
70          buildInfo.setArtifact(artifact);
71          buildInfo.setHashFunction("SHA-256");
72          buildInfo.setGoals(Collections.singletonList("install"));
73          final org.apache.maven.artifact.Artifact attachedArtifact =
74                  new DefaultArtifact("ag", "aa", "av", "as", "at", "ac", new DefaultArtifactHandler());
75          buildInfo.setAttachedArtifacts(Build.createAttachedArtifacts(
76                  Collections.singletonList(attachedArtifact), HashFactory.XX.createAlgorithm()));
77          buildInfo.setProjectsInputInfo(main);
78          buildInfo.setExecutions(createExecutions());
79  
80          byte[] bytes = xmlService.toBytes(buildInfo);
81          System.out.println(new String(bytes, StandardCharsets.UTF_8));
82          Path tempFilePath = Files.createTempFile("test", "test");
83          File file = tempFilePath.toFile();
84          file.deleteOnExit();
85          Files.write(tempFilePath, bytes);
86  
87          org.apache.maven.buildcache.xml.build.Build buildInfo1 = xmlService.loadBuild(file);
88          System.out.println(buildInfo1);
89      }
90  
91      private List<CompletedExecution> createExecutions() {
92          CompletedExecution execution = new CompletedExecution();
93          execution.setExecutionKey("execkey");
94          PropertyValue property = new PropertyValue();
95          property.setValue("value");
96          property.setName("key");
97          execution.addProperty(property);
98          return new ArrayList<>(Arrays.asList(execution));
99      }
100 
101     private DigestItem createItem(String pom, String s, String hash1) {
102         final DigestItem d1 = new DigestItem();
103         d1.setType(pom);
104         d1.setHash(s);
105         d1.setValue(hash1);
106         return d1;
107     }
108 }